返回 CodeWhale
check-swallowed-errors-budget.test.py
根目录 / scripts / check-swallowed-errors-budget.test.py
1 #!/usr/bin/env python3
2 import importlib.util
3 import unittest
4 from pathlib import Path
5
6 spec = importlib.util.spec_from_file_location(
7 "swallowed", Path(__file__).with_name("check-swallowed-errors-budget.py")
8 )
9 swallowed = importlib.util.module_from_spec(spec)
10 spec.loader.exec_module(swallowed)
11
12
13 class SwallowedErrorsTest(unittest.TestCase):
14 def test_counts_each_target(self):
15 src = """
16 fn a() {
17 let _ = file.sync_all();
18 let _ = write_atomic(&p, b"x");
19 let _ = store.save_session(&s);
20 let _ = tracker.record_failure(1);
21 let _ = Command::new("x").spawn();
22 let _unused = file.sync_all();
23 file.sync_all().ok();
24 }
25 """
26 self.assertEqual(
27 swallowed.count_text(src),
28 {"sync_all": 1, "write_atomic": 1, "save_session": 1, "record_failure": 1, "spawn": 1},
29 )
30
31 def test_ignores_tests_comments_and_strings(self):
32 src = """
33 // let _ = file.sync_all();
34 fn a() { let _ = other(); let s = "let _ = x.sync_all();"; }
35 #[cfg(test)]
36 mod tests { fn t() { let _ = file.sync_all(); } }
37 """
38 self.assertEqual(swallowed.count_text(src), {})
39
40 def test_compare_flags_growth_and_reports_savings(self):
41 failures, savings = swallowed.compare(
42 {"a.rs": {"spawn": 2}}, {"a.rs": {"spawn": 1}, "b.rs": {"sync_all": 1}}
43 )
44 self.assertEqual(len(failures), 1)
45 self.assertEqual(len(savings), 1)
46 self.assertEqual(swallowed.compare({"a.rs": {"spawn": 1}}, {"a.rs": {"spawn": 1}}), ([], []))
47
48
49 if __name__ == "__main__":
50 unittest.main()
51
51 lines PYTHON