fix: harden typed metrics review findings

This commit is contained in:
2026-08-14 13:50:04 +03:00
parent 996a5461de
commit b7face0e94
30 changed files with 722 additions and 382 deletions
@@ -53,6 +53,46 @@ class MetricsBoundaryTests(unittest.TestCase):
result = self.run_checker(root, str(product.relative_to(root)), str(adapter.relative_to(root)))
self.assertEqual(result.returncode, 0, result.stderr)
def test_adapter_allowance_does_not_hide_later_declaration(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
path = root / "crates/crank-observability/src/instrumentation.rs"
path.parent.mkdir(parents=True)
path.write_text(
'metrics::describe_counter!("ok", "ok");\nmetrics::counter!("bad").increment(1);',
encoding="utf-8",
)
result = self.run_checker(root, str(path.relative_to(root)))
self.assertNotEqual(result.returncode, 0)
def test_ignores_comments_and_strings_but_not_src_tests_directory(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
harmless = root / "apps/demo/src/harmless.rs"
production = root / "apps/demo/src/tests/production.rs"
harmless.parent.mkdir(parents=True)
production.parent.mkdir(parents=True)
harmless.write_text(
'// metrics::counter!("comment")\nconst TEXT: &str = r#"metrics::gauge!(\"string\")"#;',
encoding="utf-8",
)
production.write_text('metrics::counter!("bad");', encoding="utf-8")
allowed = self.run_checker(root, str(harmless.relative_to(root)))
rejected = self.run_checker(root, str(production.relative_to(root)))
self.assertEqual(allowed.returncode, 0, allowed.stderr)
self.assertNotEqual(rejected.returncode, 0)
def test_rejects_raw_recorder_api_and_empty_explicit_handoff(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
path = root / "apps/demo/src/raw.rs"
path.parent.mkdir(parents=True)
path.write_text("Recorder::register_counter(key, metadata);", encoding="utf-8")
self.assertNotEqual(
self.run_checker(root, str(path.relative_to(root))).returncode, 0
)
self.assertNotEqual(self.run_checker(root).returncode, 0)
def test_explicit_missing_traversal_and_symlink_fail_closed(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)