|
1 | 1 | import importlib.util |
2 | 2 | import json |
| 3 | +import os |
3 | 4 | import pathlib |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
4 | 8 | import tempfile |
5 | 9 | import unittest |
6 | 10 |
|
@@ -115,5 +119,50 @@ def test_undeclared_failure_fails(self): |
115 | 119 | self.assertEqual('FAIL', self.report()['status']) |
116 | 120 |
|
117 | 121 |
|
| 122 | +class InstalledContractsTest(unittest.TestCase): |
| 123 | + def test_existing_reports_do_not_prove_an_invocation_that_executed_no_tests(self): |
| 124 | + with tempfile.TemporaryDirectory() as directory: |
| 125 | + root = pathlib.Path(directory) |
| 126 | + bin_dir = root / 'bin' |
| 127 | + bin_dir.mkdir() |
| 128 | + scripts = root / 'scripts' |
| 129 | + scripts.mkdir() |
| 130 | + runner = SCRIPT.with_name('run_installed_contracts.sh') |
| 131 | + for script in (SCRIPT, runner): |
| 132 | + shutil.copy(script, scripts) |
| 133 | + reports = root / 'target' / 'surefire-reports' |
| 134 | + reports.mkdir(parents=True) |
| 135 | + names = ['OpenCliCapabilityOverrideIT', 'OpenCliReadOnlyDiagnosticsIT'] |
| 136 | + for name in names: |
| 137 | + (reports / ('TEST-' + name + '.xml')).write_text( |
| 138 | + '<testsuite name="io.github.easy4j.opencli.contract.' + name + '" tests="1">' |
| 139 | + '<testcase name="previousRun"/></testsuite>', encoding='utf-8') |
| 140 | + |
| 141 | + commands = { |
| 142 | + 'npm': 'exit 0', |
| 143 | + 'node': 'case "$1" in --version) echo v24.18.0;; -p) echo /fixture/node;; *) echo 1.8.8;; esac', |
| 144 | + 'java': 'echo fixture-jdk', |
| 145 | + 'mvn': 'if [[ "$1" == -version ]]; then echo fixture-maven; fi', |
| 146 | + 'git': 'echo ' + HEAD, |
| 147 | + 'python': 'exec "$OPENCLI_TEST_PYTHON" "$@"', |
| 148 | + } |
| 149 | + for name, body in commands.items(): |
| 150 | + command = bin_dir / name |
| 151 | + command.write_text('#!/usr/bin/env bash\n' + body + '\n', encoding='utf-8') |
| 152 | + command.chmod(0o755) |
| 153 | + |
| 154 | + env = dict(os.environ, PATH=str(bin_dir) + os.pathsep + os.environ['PATH'], |
| 155 | + RUNNER_OS='Linux', RUNNER_TEMP=directory, GITHUB_REF_NAME='feature/2.0.x', |
| 156 | + OPENCLI_TEST_PYTHON=sys.executable) |
| 157 | + result = subprocess.run(['bash', 'scripts/run_installed_contracts.sh', 'mvn'], |
| 158 | + cwd=root, env=env, capture_output=True, text=True, timeout=15) |
| 159 | + report = json.loads((root / 'platform-evidence' / 'installed-report.json').read_text(encoding='utf-8')) |
| 160 | + self.assertEqual(1, result.returncode, result.stdout + result.stderr) |
| 161 | + self.assertEqual('FAIL', report['layers']['real-execution']['status']) |
| 162 | + self.assertEqual(0, report['layers']['real-execution']['executed']) |
| 163 | + archived = list((root / 'platform-evidence').glob('pre-install-surefire.*/surefire-reports/TEST-*.xml')) |
| 164 | + self.assertCountEqual(['TEST-' + name + '.xml' for name in names], [p.name for p in archived]) |
| 165 | + |
| 166 | + |
118 | 167 | if __name__ == '__main__': |
119 | 168 | unittest.main() |
0 commit comments