Coverage for glotter/test.py: 41%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-12 02:25 +0000

1import re 

2import sys 

3 

4import pytest 

5 

6from glotter.source import get_sources, filter_sources 

7from glotter.settings import Settings 

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( 

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

41 ) 

42 return tests 

43 

44 

45def _run_pytest_and_exit(*args): 

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

47 code = pytest.main(args=args) 

48 sys.exit(code) 

49 

50 

51class TestCollectionPlugin: 

52 def __init__(self): 

53 self.collected = [] 

54 

55 def pytest_collection_modifyitems(self, items): 

56 for item in items: 

57 self.collected.append(item.nodeid) 

58 

59 

60def _collect_tests(): 

61 print( 

62 "============================= collect test totals ==============================" 

63 ) 

64 plugin = TestCollectionPlugin() 

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

66 return plugin.collected