Files
cutdex/tool/tests/common_tools_test.py
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

59 lines
2.7 KiB
Python

"""Shared process/CLI contracts used by application and platform entrypoints."""
import contextlib
import hashlib
import io
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from common.files import file_sha256
from common.process import cli, run
class CommonToolsTests(unittest.TestCase):
def test_process_arguments_directory_and_child_environment_are_preserved(self):
with tempfile.TemporaryDirectory(prefix='cutdex common process ') as directory:
code = "import os,sys; print(os.getcwd()); print(os.environ['CUTDEX_TEST']); print(repr(sys.argv[1:]))"
arguments = ['A&B with spaces', 'with "quotes"', '', '%literal%']
result = run([sys.executable, '-c', code, *arguments], cwd=directory,
env={'CUTDEX_TEST': 'child'}, capture_output=True, text=True)
lines = result.stdout.splitlines()
self.assertEqual(Path(lines[0]).resolve(), Path(directory).resolve())
self.assertEqual(lines[1], 'child')
self.assertEqual(lines[2], repr(arguments))
def test_cli_preserves_child_failure_without_exposing_arguments(self):
def fail():
run([sys.executable, '-c', 'import sys; sys.exit(17)', 'secret-token'], capture_output=True)
with contextlib.redirect_stderr(io.StringIO()) as diagnostics:
self.assertEqual(cli(fail), 17)
self.assertIn('exit 17', diagnostics.getvalue())
self.assertNotIn('secret-token', diagnostics.getvalue())
def test_child_stdout_and_stderr_remain_separate(self):
result = run([sys.executable, '-c', "import sys; print('result'); print('diagnostic', file=sys.stderr)"],
capture_output=True, text=True)
self.assertEqual(result.stdout.strip(), 'result')
self.assertEqual(result.stderr.strip(), 'diagnostic')
def test_cli_reports_validation_failure_without_a_traceback(self):
def fail():
raise ValueError('Release files differ from the audited manifest')
with contextlib.redirect_stderr(io.StringIO()) as diagnostics:
self.assertEqual(cli(fail), 1)
self.assertEqual(diagnostics.getvalue(), 'Release files differ from the audited manifest\n')
def test_streamed_file_hash_matches_binary_contents(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'artifact'
payload = bytes(range(256)) * 32768
path.write_bytes(payload)
self.assertEqual(file_sha256(path), hashlib.sha256(payload).hexdigest())
if __name__ == '__main__':
unittest.main()