Coverage for test_code_checks.py: 74%

39 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-08 14:15 +0200

1import ast 

2import os 

3from glob import glob 

4 

5 

6def check_ast(node: ast.AST, path: str): 

7 ok = True 

8 if hasattr(node, "body"): 

9 for child in node.body: 

10 child_ok = check_ast(child, path) 

11 ok = ok and child_ok 

12 else: 

13 if isinstance(node, ast.Assert): 

14 print(f"assert detected in line {node.lineno} of {path}") 

15 ok = False 

16 if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call): 

17 value = node.value 

18 if ( 

19 hasattr(value, "func") 

20 and hasattr(value.func, "id") 

21 and value.func.id == "print" 

22 ): 

23 print(f"print detected in line {node.lineno} of {path}") 

24 ok = False 

25 return ok 

26 

27 

28def test_check_modules(): 

29 test_directory = os.path.realpath(os.path.dirname(os.path.abspath(__file__))) 

30 paths = glob(test_directory + "/../**/*.py") 

31 ok = True 

32 for path in paths: 

33 if ( 

34 test_directory in os.path.realpath(path) 

35 ): # if it's a test we don't care. this very file contains print statements itself. 

36 continue 

37 try: 

38 with open(path) as f: 

39 content = f.read() 

40 try: 

41 tree = ast.parse(content) 

42 module_ok = check_ast(tree, path) 

43 ok = ok and module_ok 

44 except Exception as e: 

45 print(f"parsing error in {path}, with error: {e}.") 

46 ok = False 

47 except Exception as e: 

48 print(f"error reading {path}, with error: {e}") 

49 ok = False 

50 assert ok