Gitea CI / Check code and tests (push) Successful in 7m38s
Gitea CI / Build Android APK (push) Successful in 6m20s
Gitea CI / Audit and package Windows x64 (push) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (push) Canceled after 0s
Gitea CI / Gitea CI passed (push) Canceled after 0s
Gitea CI / Build Windows x64 app (push) Canceled after 14m42s
59 lines
2.6 KiB
Python
59 lines
2.6 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]), Path(directory))
|
|
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()
|