ci / Compose (pull_request) Successful in 12s
ci / Workflows (pull_request) Successful in 9s
ci / Shell (pull_request) Successful in 21s
ci / Formatting (pull_request) Successful in 22s
ci / Python and tests (pull_request) Successful in 10s
ci / YAML (pull_request) Successful in 11s
ci / Dockerfiles (pull_request) Successful in 6s
ci / Kubernetes (pull_request) Successful in 8s
ci / image-plan (pull_request) Skipped
ci / Image (${{ matrix.name }}) (pull_request) Skipped
ci / build (pull_request) Skipped
61 lines
2.8 KiB
Python
61 lines
2.8 KiB
Python
"""Run the real unit tests with external CI files and detect leaked writes."""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from ci_test_case import CI_COMMAND_FILES, IsolatedCITestCase
|
|
|
|
|
|
class CIOutputIsolationTests(IsolatedCITestCase):
|
|
def test_all_command_files_are_private_and_environment_is_restored(self):
|
|
with tempfile.TemporaryDirectory() as scratch:
|
|
external = {variable: str(Path(scratch) / variable) for variable in CI_COMMAND_FILES}
|
|
for path in external.values():
|
|
Path(path).write_text('external CI file\n')
|
|
with patch.dict(os.environ, external):
|
|
probe = IsolatedCITestCase()
|
|
probe.setUp()
|
|
private = []
|
|
try:
|
|
for variable in CI_COMMAND_FILES:
|
|
self.assertNotEqual(os.environ[variable], external[variable])
|
|
path = Path(os.environ[variable])
|
|
private.append(path)
|
|
path.write_text('test-only command\n')
|
|
finally:
|
|
probe.doCleanups()
|
|
for variable in CI_COMMAND_FILES:
|
|
self.assertEqual(os.environ[variable], external[variable])
|
|
self.assertEqual(Path(external[variable]).read_text(), 'external CI file\n')
|
|
self.assertTrue(all(not path.exists() for path in private))
|
|
|
|
def test_unit_suite_preserves_external_ci_files(self):
|
|
tests = Path(__file__).resolve().parent
|
|
modules = sorted(p.stem for p in tests.glob('test_*.py') if p.name != Path(__file__).name)
|
|
with tempfile.TemporaryDirectory() as scratch:
|
|
environment = os.environ.copy()
|
|
environment['PYTHONPATH'] = str(tests) + os.pathsep + environment.get('PYTHONPATH', '')
|
|
expected = {}
|
|
for variable in CI_COMMAND_FILES:
|
|
path = Path(scratch) / variable
|
|
content = f'external {variable}\n'
|
|
path.write_text(content)
|
|
environment[variable] = str(path)
|
|
expected[path] = content
|
|
result = subprocess.run( # noqa: S603 -- Run local test modules with the current Python interpreter.
|
|
[sys.executable, '-m', 'unittest', *modules, '-q'],
|
|
cwd=tests.parent,
|
|
env=environment,
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
timeout=60,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
for path, content in expected.items():
|
|
self.assertEqual(path.read_text(), content, f'Unit tests wrote to external {path.name}')
|