|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +ROOT = Path(__file__).resolve().parents[2] |
| 10 | +MODULE_PATH = ROOT / "scripts" / "check_qsl_compat.py" |
| 11 | +MODULE_SPEC = importlib.util.spec_from_file_location("check_qsl_compat", MODULE_PATH) |
| 12 | +check_qsl_compat = importlib.util.module_from_spec(MODULE_SPEC) |
| 13 | +assert MODULE_SPEC.loader is not None |
| 14 | +sys.modules[MODULE_SPEC.name] = check_qsl_compat |
| 15 | +MODULE_SPEC.loader.exec_module(check_qsl_compat) |
| 16 | + |
| 17 | + |
| 18 | +class QSLCompatCheckerTest(unittest.TestCase): |
| 19 | + def _make_repo_root(self, qsl_toml: str, pyproject: str) -> Path: |
| 20 | + root = Path(tempfile.mkdtemp()) |
| 21 | + (root / "qsl.toml").write_text(qsl_toml, encoding="utf-8") |
| 22 | + (root / "pyproject.toml").write_text(pyproject, encoding="utf-8") |
| 23 | + return root |
| 24 | + |
| 25 | + def _write_bundle(self, compat_root: Path, bundle_name: str, repos: dict[str, str]) -> None: |
| 26 | + bundle_path = compat_root / "compat" / "bundles" / f"{bundle_name}.toml" |
| 27 | + bundle_path.parent.mkdir(parents=True, exist_ok=True) |
| 28 | + block = [f'name = "{bundle_name}"', "[repos]"] |
| 29 | + for repo, ref in repos.items(): |
| 30 | + block.append(f'{repo} = "{ref}"') |
| 31 | + bundle_path.write_text("\n".join(block) + "\n", encoding="utf-8") |
| 32 | + |
| 33 | + def test_root_compat_bundle_and_ring_schema(self): |
| 34 | + with tempfile.TemporaryDirectory() as workspace: |
| 35 | + compat_root = Path(workspace) |
| 36 | + self._write_bundle( |
| 37 | + compat_root, |
| 38 | + "2026.07.1", |
| 39 | + {"QuantPlatformKit": "7032cde4547e7ec59af15df8935d142461a77051"}, |
| 40 | + ) |
| 41 | + repo_root = self._make_repo_root( |
| 42 | + qsl_toml=( |
| 43 | + "tier = \"ops/tooling\"\n" |
| 44 | + "ring = \"ring_e\"\n" |
| 45 | + "[compat]\n" |
| 46 | + 'bundle = "2026.07.1"\n' |
| 47 | + ), |
| 48 | + pyproject=( |
| 49 | + "dependencies = [\n" |
| 50 | + ' "quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@7032cde4547e7ec59af15df8935d142461a77051"\n' |
| 51 | + "]\n" |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + ok, issues, warnings, notes = check_qsl_compat._check(repo_root=repo_root, compat_root=compat_root) |
| 56 | + |
| 57 | + self.assertTrue(ok) |
| 58 | + self.assertEqual(issues, []) |
| 59 | + self.assertEqual(warnings, []) |
| 60 | + self.assertIn("bundle=2026.07.1", notes) |
| 61 | + self.assertIn("upgrade_ring=ring_e", notes) |
| 62 | + |
| 63 | + def test_not_enforced_bundle_reports_warning_for_short_sha_and_mismatch_but_main_stays_issue(self): |
| 64 | + with tempfile.TemporaryDirectory() as workspace: |
| 65 | + compat_root = Path(workspace) |
| 66 | + self._write_bundle( |
| 67 | + compat_root, |
| 68 | + "2026.07.1", |
| 69 | + { |
| 70 | + "QuantPlatformKit": "7032cde4547e7ec59af15df8935d142461a77051", |
| 71 | + "UsEquityStrategies": "9f0e5e2deca8a9c16d711eb4772f08a7901da101", |
| 72 | + }, |
| 73 | + ) |
| 74 | + repo_root = self._make_repo_root( |
| 75 | + qsl_toml=( |
| 76 | + "tier = \"ops/tooling\"\n" |
| 77 | + "ring = \"ring_e\"\n" |
| 78 | + "[compat]\n" |
| 79 | + 'bundle = "2026.07.1"\n' |
| 80 | + "enforce_bundle = false\n" |
| 81 | + ), |
| 82 | + pyproject=( |
| 83 | + "dependencies = [\n" |
| 84 | + ' "quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@abc123"\n' |
| 85 | + ' "quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@' |
| 86 | + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\n' |
| 87 | + ' "us-equity-strategies @ https://github.com/QuantStrategyLab/UsEquityStrategies.git?rev=main"\n' |
| 88 | + "]\n" |
| 89 | + ), |
| 90 | + ) |
| 91 | + |
| 92 | + ok, issues, warnings, notes = check_qsl_compat._check(repo_root=repo_root, compat_root=compat_root) |
| 93 | + |
| 94 | + self.assertFalse(ok) |
| 95 | + self.assertEqual(len(issues), 1) |
| 96 | + self.assertIn("forbidden ref 'main'", issues[0]) |
| 97 | + self.assertEqual(len(warnings), 2) |
| 98 | + self.assertTrue(any("forbidden short/invalid ref 'abc123'" in warning for warning in warnings)) |
| 99 | + self.assertTrue(any("bundle pin mismatch for QuantPlatformKit" in warning for warning in warnings)) |
| 100 | + self.assertEqual(notes[0], "qsl=" + str(repo_root / "qsl.toml")) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + unittest.main() |
0 commit comments