Total Complexity | 9 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # pyre-strict |
||
2 | |||
3 | """ |
||
4 | We use Pyre Check to prevent type errors. However, for Pyre Check to fail if |
||
5 | type declarations aren't "perfect", all files need to start with a "pyre-strict" |
||
6 | comment. This test checks that all Python files start with such a comment. |
||
7 | """ |
||
8 | |||
9 | import unittest |
||
10 | import os |
||
11 | |||
12 | |||
13 | class PyreStrictTest(unittest.TestCase): |
||
14 | """ |
||
15 | Check that all relevant Python files have Pyre set to strict mode. |
||
16 | """ |
||
17 | |||
18 | def test_strict_all_python_files(self) -> None: |
||
19 | """ |
||
20 | Check that all relevant Python files have Pyre set to strict mode. |
||
21 | """ |
||
22 | for directory, _directories, files in os.walk('.'): |
||
23 | for file in files: |
||
24 | path = os.path.join(directory, file) |
||
25 | |||
26 | if self.path_can_safely_be_skipped(path): |
||
27 | continue |
||
28 | |||
29 | with open(path) as current_file: |
||
30 | first_line: str = current_file.readline() |
||
31 | |||
32 | self.assertEqual( |
||
33 | first_line, |
||
34 | '# pyre-strict\n', |
||
35 | path + ' is not set to Pyre strict mode.', |
||
36 | ) |
||
37 | |||
38 | @staticmethod |
||
39 | def path_can_safely_be_skipped(path: str) -> bool: |
||
40 | """ |
||
41 | Make sure that we only check Python files that are in the present |
||
42 | codebase. |
||
43 | |||
44 | :param path: |
||
45 | :return: |
||
46 | """ |
||
47 | if not path.endswith('.py'): |
||
48 | return True |
||
49 | |||
50 | if './lib/' in path: |
||
51 | # Prevent false positive in Scrutinizer. |
||
52 | return True |
||
53 | |||
54 | if './bin/activate_this.py' in path: |
||
55 | # Prevent false positive in Scrutinizer. |
||
56 | return True |
||
57 | |||
58 | return False |
||
59 |