Files
cutdex/tool/tests/cli_test.py
T

98 lines
5.1 KiB
Python

"""CLI contracts: help/invalid arguments must not start builds or publish files."""
import json
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
class CliTests(unittest.TestCase):
def test_python_help_and_unknown_option(self):
scripts = [
'check.py', 'macos/check_macos_release.py', 'macos/macos_updates.py',
'macos/test_macos_release.py', 'macos/test_macos_updates.py',
'macos/test_check_macos_release.py', 'windows/check_windows_release.py',
'windows/package_windows_release.py', 'windows/prepare_windows_sandbox.py',
'windows/windows_updates.py', 'windows/test_windows_updates.py',
'gitea/package-app.py',
'diagnostics/frame_budget.py',
]
for name in scripts:
for flag, code in [('--help', 0), ('--invalid-option', 2)]:
with self.subTest(script=name, flag=flag):
result = subprocess.run([sys.executable, str(ROOT / 'tool' / name), flag],
capture_output=True, text=True, timeout=30)
self.assertEqual(result.returncode, code, result.stderr)
self.assertIn('usage:', (result.stdout + result.stderr).lower())
@unittest.skipUnless(shutil.which('dart'), 'Dart SDK unavailable')
def test_context_diagnostic_arguments_and_json_output(self):
command = [shutil.which('dart'), 'run',
str(ROOT / 'tool/diagnostics/benchmark_agent_context.dart')]
for arguments, code in [(['--help'], 0), ([], 2)]:
result = subprocess.run(command + arguments, cwd=ROOT, capture_output=True,
text=True, timeout=60)
self.assertEqual(result.returncode, code, result.stderr)
self.assertIn('Usage:', result.stdout + result.stderr)
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'history.json'
path.write_text('[]')
result = subprocess.run(command + [str(path)], cwd=ROOT, capture_output=True,
text=True, timeout=60)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(json.loads(result.stdout)['messages'], 0)
@unittest.skipUnless(shutil.which('bash'), 'bash unavailable')
def test_shell_help_and_argument_count(self):
for script in (ROOT / 'tool/macos').glob('*.sh'):
with self.subTest(script=script.name):
result = subprocess.run(['bash', str(script), '--help'],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn('Usage:', result.stdout)
result = subprocess.run(['bash', str(script), 'one', 'two', 'three'],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 2, result.stderr)
@unittest.skipUnless(sys.platform == 'darwin' and shutil.which('swiftc'), 'macOS Swift required')
class NativeCliTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory(prefix='cutdex-cli-')
cls.addClassCleanup(cls.temp.cleanup)
cls.root = Path(cls.temp.name)
for name in ['set_macos_file_icon', 'sparkle_test_key']:
subprocess.run(['swiftc', str(ROOT / f'tool/macos/{name}.swift'),
'-o', str(cls.root / name)], check=True, capture_output=True, timeout=120)
def test_help_and_missing_arguments(self):
for name in ['set_macos_file_icon', 'sparkle_test_key']:
for arguments, code in [(['--help'], 0), ([], 2)]:
with self.subTest(script=name, arguments=arguments):
result = subprocess.run([str(self.root / name), *arguments], capture_output=True)
self.assertEqual(result.returncode, code, result.stderr)
def test_test_key_reuse_and_invalid_file(self):
path = self.root / 'keys with spaces'
command = [str(self.root / 'sparkle_test_key'), str(path)]
subprocess.run(command, check=True, capture_output=True)
original = (path / 'private.key').read_bytes()
subprocess.run(command, check=True, capture_output=True)
self.assertEqual((path / 'private.key').read_bytes(), original)
self.assertEqual((path / 'private.key').stat().st_mode & 0o777, 0o600)
(path / 'private.key').write_text('invalid key')
result = subprocess.run(command, capture_output=True)
self.assertEqual(result.returncode, 1)
self.assertNotIn(b'invalid key', result.stderr)
def test_missing_icon_returns_failure(self):
result = subprocess.run([str(self.root / 'set_macos_file_icon'),
str(self.root / 'missing.png'), str(self.root / 'missing.dmg')],
capture_output=True)
self.assertEqual(result.returncode, 1)