Coverage for glotter/test.py: 41%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-09-13 19:09 +0000

1import re 

2import sys 

3 

4import pytest 

5 

6from glotter.settings import Settings 

7from glotter.source import filter_sources, get_sources 

8from glotter.test_generator import generate_tests 

9from glotter.utils import error_and_exit 

10 

11 

12def test(args): 

13 generate_tests() 

14 test_args = ["-n", "auto"] if args.parallel else [] 

15 if not (args.language or args.project or args.source): 

16 _run_pytest_and_exit(*test_args) 

17 

18 all_tests = _collect_tests() 

19 sources_by_type = filter_sources(args, get_sources(Settings().source_root)) 

20 for project_type, sources in sources_by_type.items(): 

21 for source in sources: 

22 test_args += _get_tests(project_type, all_tests, source) 

23 

24 if not test_args: 

25 error_and_exit("No tests were found") 

26 

27 _run_pytest_and_exit(*test_args) 

28 

29 

30def _get_tests(project_type, all_tests, src=None): 

31 test_functions = Settings().get_test_mapping_name(project_type) 

32 tests = [] 

33 for test_func in test_functions: 

34 if src is not None: 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true

35 test_id = f"{src.language}/{src.name}{src.extension}" 

36 pattern = rf"^(\w/?)*\.py::{test_func}\[{re.escape(test_id)}(-.*)?\]$" 

37 else: 

38 pattern = rf"^(\w/?)*\.py::{test_func}\[.+\]$" 

39 tests.extend([tst for tst in all_tests if re.fullmatch(pattern, tst) is not None]) 

40 return tests 

41 

42 

43def _run_pytest_and_exit(*args): 

44 args = ["-v"] + list(args) 

45 code = pytest.main(args=args) 

46 sys.exit(code) 

47 

48 

49class TestCollectionPlugin: 

50 def __init__(self): 

51 self.collected = [] 

52 

53 def pytest_collection_modifyitems(self, items): 

54 for item in items: 

55 self.collected.append(item.nodeid) 

56 

57 

58def _collect_tests(): 

59 print("============================= collect test totals ==============================") 

60 plugin = TestCollectionPlugin() 

61 pytest.main(["-qq", "--collect-only"], plugins=[plugin]) 

62 return plugin.collected