field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

test_maintainability_ratchet.py (10777B)


      1 from __future__ import annotations
      2 
      3 import json
      4 import sys
      5 import tempfile
      6 import unittest
      7 from pathlib import Path
      8 
      9 SCRIPTS = Path(__file__).resolve().parent
     10 if str(SCRIPTS) not in sys.path:
     11     sys.path.insert(0, str(SCRIPTS))
     12 
     13 import maintainability_ratchet as ratchet  # noqa: E402
     14 
     15 
     16 class MaintainabilityRatchetTests(unittest.TestCase):
     17     def _repository(self) -> tuple[tempfile.TemporaryDirectory[str], Path]:
     18         temporary = tempfile.TemporaryDirectory()
     19         root = Path(temporary.name)
     20         (root / "Tera").mkdir()
     21         (root / "TeraTests").mkdir()
     22         (root / "TeraUITests").mkdir()
     23         (root / "TeraPublicAPITests").mkdir()
     24         (root / "scripts").mkdir()
     25         (root / "scripts/legacy_fixture_writers").mkdir()
     26         (root / "test-fixtures").mkdir()
     27         (root / "Package.swift").write_text("// package\n", encoding="utf-8")
     28         for relative in ratchet.ORIGINAL_BOUNDED_MODULES:
     29             path = root / relative
     30             path.parent.mkdir(parents=True, exist_ok=True)
     31             path.write_text(
     32                 "// bounded\n" if path.suffix == ".swift" else "# bounded\n"
     33             )
     34         (root / "Tera/App.swift").write_text("struct App {}\n", encoding="utf-8")
     35         (root / "scripts/check.py").write_text(
     36             "def check():\n    return True\n", encoding="utf-8"
     37         )
     38         self._write_baseline(root)
     39         return temporary, root
     40 
     41     def _write_baseline(self, root: Path, **overrides: object) -> None:
     42         baseline: dict[str, object] = {
     43             "schema": "radroots.ios.maintainability-baseline.v1",
     44             "schema_version": 1,
     45             "source_revision": "c63002bcc4d3f6656e93aabe4fca6bd771376629",
     46             "thresholds": {
     47                 "swift_file_lines": 600,
     48                 "python_file_lines": 800,
     49                 "python_function_complexity": 10,
     50             },
     51             "swift_file_exception": [],
     52             "python_file_exception": [],
     53             "python_complexity_exception": [],
     54             "bounded_module": sorted(
     55                 {
     56                     *ratchet.ORIGINAL_BOUNDED_MODULES,
     57                     "Tera/App.swift",
     58                     "scripts/check.py",
     59                 }
     60             ),
     61         }
     62         baseline.update(overrides)
     63         (root / ratchet.BASELINE_PATH).write_text(
     64             json.dumps(baseline, sort_keys=True), encoding="utf-8"
     65         )
     66 
     67     def test_current_repository_satisfies_ratchet(self) -> None:
     68         ratchet.verify(SCRIPTS.parent)
     69 
     70     def test_new_oversized_swift_file_is_rejected(self) -> None:
     71         temporary, root = self._repository()
     72         with temporary:
     73             (root / "Tera/Large.swift").write_text("x\n" * 601, encoding="utf-8")
     74             with self.assertRaisesRegex(ratchet.MaintainabilityError, "inventory"):
     75                 ratchet.verify(root)
     76 
     77     def test_exception_cannot_grow_or_become_stale(self) -> None:
     78         temporary, root = self._repository()
     79         with temporary:
     80             path = root / "Tera/Runtime/TeraRuntimeClient.swift"
     81             path.write_text("x\n" * 602, encoding="utf-8")
     82             exception = [
     83                 {"path": "Tera/Runtime/TeraRuntimeClient.swift", "maximum_lines": 601}
     84             ]
     85             self._write_baseline(root, swift_file_exception=exception)
     86             with self.assertRaisesRegex(ratchet.MaintainabilityError, "regressed"):
     87                 ratchet.verify(root)
     88             path.write_text("struct App {}\n", encoding="utf-8")
     89             with self.assertRaisesRegex(ratchet.MaintainabilityError, "inventory"):
     90                 ratchet.verify(root)
     91 
     92     def test_new_oversized_python_file_is_rejected(self) -> None:
     93         temporary, root = self._repository()
     94         with temporary:
     95             (root / "scripts/large.py").write_text("x = 1\n" * 801, encoding="utf-8")
     96             with self.assertRaisesRegex(ratchet.MaintainabilityError, "inventory"):
     97                 ratchet.verify(root)
     98 
     99     def test_new_complex_python_function_is_rejected(self) -> None:
    100         temporary, root = self._repository()
    101         with temporary:
    102             branches = "".join(
    103                 f"    if value == {index}:\n        return {index}\n"
    104                 for index in range(10)
    105             )
    106             (root / "scripts/complex.py").write_text(
    107                 "def complex(value):\n" + branches + "    return -1\n",
    108                 encoding="utf-8",
    109             )
    110             with self.assertRaisesRegex(ratchet.MaintainabilityError, "inventory"):
    111                 ratchet.verify(root)
    112 
    113     def test_baseline_identity_threshold_and_order_are_closed(self) -> None:
    114         temporary, root = self._repository()
    115         with temporary:
    116             self._write_baseline(root, source_revision="f" * 40)
    117             with self.assertRaisesRegex(ratchet.MaintainabilityError, "identity"):
    118                 ratchet.verify(root)
    119             self._write_baseline(
    120                 root,
    121                 thresholds={
    122                     "swift_file_lines": 601,
    123                     "python_file_lines": 800,
    124                     "python_function_complexity": 10,
    125                 },
    126             )
    127             with self.assertRaisesRegex(ratchet.MaintainabilityError, "threshold"):
    128                 ratchet.verify(root)
    129             self._write_baseline(
    130                 root,
    131                 bounded_module=["scripts/check.py", "Tera/App.swift"],
    132             )
    133             with self.assertRaisesRegex(ratchet.MaintainabilityError, "inventory"):
    134                 ratchet.verify(root)
    135 
    136     def test_bounded_module_must_exist_and_remain_small(self) -> None:
    137         temporary, root = self._repository()
    138         with temporary:
    139             self._write_baseline(
    140                 root,
    141                 bounded_module=sorted(
    142                     {*ratchet.ORIGINAL_BOUNDED_MODULES, "Tera/Missing.swift"}
    143                 ),
    144             )
    145             with self.assertRaisesRegex(ratchet.MaintainabilityError, "absent"):
    146                 ratchet.verify(root)
    147             path = "Tera/Runtime/TeraRuntimeClient.swift"
    148             (root / path).write_text("x\n" * 601, encoding="utf-8")
    149             self._write_baseline(
    150                 root,
    151                 swift_file_exception=[{"path": path, "maximum_lines": 601}],
    152                 bounded_module=sorted({*ratchet.ORIGINAL_BOUNDED_MODULES, path}),
    153             )
    154             with self.assertRaisesRegex(ratchet.MaintainabilityError, "bounded"):
    155                 ratchet.verify(root)
    156 
    157     def test_exception_rows_are_exact_unique_and_ordered(self) -> None:
    158         invalid = [
    159             [
    160                 {"path": "b", "maximum_lines": 601},
    161                 {"path": "a", "maximum_lines": 601},
    162             ],
    163             [
    164                 {"path": "a", "maximum_lines": 601},
    165                 {"path": "a", "maximum_lines": 602},
    166             ],
    167             [{"path": "a", "maximum_lines": 601, "extra": True}],
    168         ]
    169         for rows in invalid:
    170             with self.subTest(rows=rows):
    171                 with self.assertRaises(ratchet.MaintainabilityError):
    172                     ratchet._closed_exception_map(
    173                         rows, key_name="path", ceiling_name="maximum_lines"
    174                     )
    175 
    176     def test_joint_source_and_baseline_cannot_add_an_exception(self) -> None:
    177         temporary, root = self._repository()
    178         with temporary:
    179             (root / "Tera/New.swift").write_text("x\n" * 601)
    180             self._write_baseline(
    181                 root,
    182                 swift_file_exception=[{"path": "Tera/New.swift", "maximum_lines": 601}],
    183             )
    184             with self.assertRaisesRegex(
    185                 ratchet.MaintainabilityError, "approved exception"
    186             ):
    187                 ratchet.verify(root)
    188 
    189     def test_joint_source_and_baseline_cannot_increase_original_ceilings(self) -> None:
    190         cases = [
    191             (
    192                 "swift_file_exception",
    193                 "Tera/Runtime/TeraRuntimeClient.swift",
    194                 821,
    195                 "x\n",
    196             ),
    197             (
    198                 "python_file_exception",
    199                 "scripts/local-social-fixture.py",
    200                 2627,
    201                 "x = 1\n",
    202             ),
    203         ]
    204         for category, path, ceiling, line in cases:
    205             with self.subTest(category=category):
    206                 temporary, root = self._repository()
    207                 with temporary:
    208                     (root / path).write_text(line * ceiling)
    209                     self._write_baseline(
    210                         root, **{category: [{"path": path, "maximum_lines": ceiling}]}
    211                     )
    212                     with self.assertRaisesRegex(
    213                         ratchet.MaintainabilityError, "approved exception"
    214                     ):
    215                         ratchet.verify(root)
    216 
    217     def test_joint_complexity_and_baseline_increase_is_rejected(self) -> None:
    218         temporary, root = self._repository()
    219         with temporary:
    220             branches = "".join(
    221                 f"    if value == {index}:\n        return {index}\n"
    222                 for index in range(16)
    223             )
    224             (root / "scripts/local-social-fixture.py").write_text(
    225                 "def valid_nostr_event(value):\n" + branches + "    return -1\n"
    226             )
    227             self._write_baseline(
    228                 root,
    229                 python_complexity_exception=[
    230                     {
    231                         "function": "scripts/local-social-fixture.py:valid_nostr_event",
    232                         "maximum_complexity": 17,
    233                     }
    234                 ],
    235             )
    236             with self.assertRaisesRegex(
    237                 ratchet.MaintainabilityError, "approved exception"
    238             ):
    239                 ratchet.verify(root)
    240 
    241     def test_original_bounded_entry_cannot_be_removed(self) -> None:
    242         temporary, root = self._repository()
    243         with temporary:
    244             original = set(ratchet.ORIGINAL_BOUNDED_MODULES)
    245             original.remove("scripts/package_privacy.py")
    246             self._write_baseline(root, bounded_module=sorted(original))
    247             with self.assertRaisesRegex(
    248                 ratchet.MaintainabilityError, "original bounded"
    249             ):
    250                 ratchet.verify(root)
    251 
    252     def test_original_exception_can_decrease_and_disappear(self) -> None:
    253         temporary, root = self._repository()
    254         with temporary:
    255             path = "Tera/Runtime/TeraRuntimeClient.swift"
    256             (root / path).write_text("x\n" * 810)
    257             self._write_baseline(
    258                 root, swift_file_exception=[{"path": path, "maximum_lines": 810}]
    259             )
    260             ratchet.verify(root)
    261             (root / path).write_text("x\n" * 600)
    262             self._write_baseline(root)
    263             ratchet.verify(root)
    264 
    265 
    266 if __name__ == "__main__":
    267     unittest.main()