Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / tools / test_structure_check.py
test_structure_check.py108 lines · 4.6 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """Unit tests for the lines ratchet in structure_check.py (stdlib only). |
| 3 | |
| 4 | Run: python3 tools/test_structure_check.py |
| 5 | or: python3 -m unittest discover -s tools -p 'test_*.py' |
| 6 | """ |
| 7 | import json |
| 8 | import os |
| 9 | import sys |
| 10 | import tempfile |
| 11 | import unittest |
| 12 | |
| 13 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 14 | import structure_check as sc # noqa: E402 |
| 15 | |
| 16 | |
| 17 | class CountedSourceTest(unittest.TestCase): |
| 18 | def test_extensions_and_segment_exclusions(self): |
| 19 | exts = [".kt", ".java", ".rs", ".swift"] |
| 20 | segs = ["third_party", "build", "target"] |
| 21 | self.assertTrue(sc.is_counted_source("notes/src/test/A.kt", exts, segs)) |
| 22 | self.assertTrue(sc.is_counted_source("apple/App.swift", exts, segs)) |
| 23 | self.assertTrue(sc.is_counted_source("scripts/maps/src/main.rs", exts, segs)) |
| 24 | self.assertFalse(sc.is_counted_source("notes/A.py", exts, segs)) |
| 25 | self.assertFalse(sc.is_counted_source("third_party/x/A.kt", exts, segs)) |
| 26 | self.assertFalse(sc.is_counted_source("notes/build/generated/A.kt", exts, segs)) |
| 27 | self.assertFalse(sc.is_counted_source("maps/target/A.rs", exts, segs)) |
| 28 | |
| 29 | def test_count_source_lines(self): |
| 30 | with tempfile.TemporaryDirectory() as tmp: |
| 31 | path = os.path.join(tmp, "A.kt") |
| 32 | with open(path, "wb") as fh: |
| 33 | fh.write(b"one\ntwo\nthree") |
| 34 | self.assertEqual(sc.count_source_lines(path), 3) |
| 35 | with open(path, "wb") as fh: |
| 36 | fh.write(b"") |
| 37 | self.assertEqual(sc.count_source_lines(path), 0) |
| 38 | |
| 39 | |
| 40 | class ModuleMappingTest(unittest.TestCase): |
| 41 | SETTINGS = """ |
| 42 | include(":library") |
| 43 | include(":library:ml") |
| 44 | include(":clock") |
| 45 | include(":clock:wear") |
| 46 | project(":clock:wear").projectDir = file("clock-wear") |
| 47 | include(":communicate") |
| 48 | include(":madeveloper") |
| 49 | """ |
| 50 | |
| 51 | def test_parse_and_longest_match(self): |
| 52 | modules = sc.parse_module_dirs(self.SETTINGS) |
| 53 | self.assertEqual(modules["library/ml"], "library/ml") |
| 54 | self.assertEqual(modules["clock/wear"], "clock-wear") |
| 55 | self.assertEqual(sc.module_for_path("library/ml/src/A.kt", modules), "library/ml") |
| 56 | self.assertEqual(sc.module_for_path("library/src/A.kt", modules), "library") |
| 57 | self.assertEqual(sc.module_for_path("clock-wear/src/A.kt", modules), "clock/wear") |
| 58 | self.assertEqual(sc.module_for_path("scripts/maps/A.rs", modules), "scripts") |
| 59 | self.assertEqual(sc.module_for_path("MmTelFeature.java", modules), "(root)") |
| 60 | |
| 61 | def test_excluded_module_matching(self): |
| 62 | excluded = ["communicate-dev"] |
| 63 | self.assertTrue(sc.is_excluded_module("communicate-dev", excluded)) |
| 64 | self.assertFalse(sc.is_excluded_module("communicate", excluded)) |
| 65 | self.assertFalse(sc.is_excluded_module("scripts", excluded)) |
| 66 | self.assertFalse(sc.is_excluded_module("madeveloper", excluded)) |
| 67 | |
| 68 | |
| 69 | class TallyTest(unittest.TestCase): |
| 70 | def test_tally_excludes_only_communicate_dev(self): |
| 71 | with tempfile.TemporaryDirectory() as tmp: |
| 72 | def write(relpath, lines): |
| 73 | path = os.path.join(tmp, relpath) |
| 74 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 75 | with open(path, "w", encoding="utf-8") as fh: |
| 76 | fh.write("\n".join("x" for _ in range(lines)) + "\n") |
| 77 | |
| 78 | write("notes/src/A.kt", 3) |
| 79 | write("communicate-dev/src/B.kt", 5) |
| 80 | write("scripts/maps/src/C.rs", 2) |
| 81 | write("notes/build/D.kt", 7) |
| 82 | budget = sc.default_line_budget() |
| 83 | modules = {"notes": "notes", "communicate-dev": "communicate-dev"} |
| 84 | total, per_module, excluded_total, _ = sc.tally_lines( |
| 85 | tmp, |
| 86 | ["notes/src/A.kt", "communicate-dev/src/B.kt", |
| 87 | "scripts/maps/src/C.rs", "notes/build/D.kt"], |
| 88 | modules, budget) |
| 89 | self.assertEqual(total, 5) # notes (3) + scripts (2) |
| 90 | self.assertEqual(per_module["notes"], 3) |
| 91 | self.assertEqual(excluded_total, 5) |
| 92 | |
| 93 | def test_budget_roundtrip(self): |
| 94 | with tempfile.TemporaryDirectory() as tmp: |
| 95 | path = os.path.join(tmp, "line-budget.json") |
| 96 | budget = sc.default_line_budget() |
| 97 | sc.write_line_budget(budget, 42, {"notes": 42}, path=path) |
| 98 | loaded, error = sc.load_line_budget(path) |
| 99 | self.assertIsNone(error) |
| 100 | self.assertEqual(loaded["baseline_lines"], 42) |
| 101 | self.assertEqual(loaded["excluded_modules"], ["communicate-dev"]) |
| 102 | with open(path, encoding="utf-8") as fh: |
| 103 | raw = json.load(fh) |
| 104 | self.assertIn("communicate-dev", raw["excluded_modules_note"]) |
| 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | unittest.main() |