Coverage for src / ocarina / dsl / testing / filter_tests_by_ids.py: 6.06%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-06-04 16:22 +0200

1"""Filter a sequence of tests by their test_id. 

2 

3Designed to be fed from CLI flags --only / --exclude. The two are mutually 

4exclusive — passing both raises. 

5 

6Unknown IDs (not matching any test.test_id) are silently ignored, so a typo 

7in a CI script does not break the run. 

8""" 

9 

10from typing import TYPE_CHECKING 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Iterable, Sequence 

14 

15 from ocarina.dsl.testing.oc_test import Test 

16 from ocarina.ports.ilogger import ILogger 

17 

18 

19def filter_tests_by_ids[Driver]( 

20 tests: Sequence[Test[Driver]], 

21 *, 

22 only: Iterable[str] = (), 

23 exclude: Iterable[str] = (), 

24 logger: ILogger, 

25) -> Sequence[Test[Driver]]: 

26 """Return tests filtered by test_id. 

27 

28 Args: 

29 tests: The full list of tests to filter. 

30 only: If non-empty, keep only tests whose test_id is in this set. 

31 exclude: If non-empty, drop tests whose test_id is in this set. 

32 logger: Used to log matched IDs. 

33 

34 Raises: 

35 ValueError: If both ``only`` and ``exclude`` are non-empty. 

36 

37 """ 

38 only_set = set(only) 

39 exclude_set = set(exclude) 

40 

41 if only_set and exclude_set: 

42 msg = "--only and --exclude cannot be used together" 

43 raise ValueError(msg) 

44 

45 known_ids = {test.test_id for test in tests} 

46 

47 if only_set: 

48 matched = only_set & known_ids 

49 if matched: 

50 joined = ", ".join(sorted(matched)) 

51 msg = f"--only: matched test IDs: {joined}" 

52 logger.info(msg) 

53 return [test for test in tests if test.test_id in only_set] 

54 

55 if exclude_set: 

56 matched = exclude_set & known_ids 

57 if matched: 

58 joined = ", ".join(sorted(matched)) 

59 msg = f"--exclude: matched test IDs: {joined}" 

60 logger.info(msg) 

61 return [test for test in tests if test.test_id not in exclude_set] 

62 

63 return tests