Skip to content

Commit

Permalink
fix: handle exclude z3 in collection stage
Browse files Browse the repository at this point in the history
Fix regex for matching z3-related modules
Exclude z3 tests in test_examples
  • Loading branch information
Raine-Yang-UofT committed Jan 1, 2025
1 parent 6e0f566 commit e682d69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
34 changes: 12 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import python_ta.contracts

Z3_RELATED_TESTS = {
r"tests/test_z3_constraints\.py",
r"tests/test_edge_feasibility\.py",
r"tests/test_impossible_condition_checker\.py",
r"tests/test_redundant_condition_checker\.py",
r"tests/test_z3_parser\.py",
r".*TestInconsistentReturnCheckerZ3Option.*",
r".*TestMissingReturnCheckerZ3Option.*",
r".*TestOneIterationCheckerZ3Option.*",
r".*TestPossiblyUndefinedCheckerZ3Option.*",
r".*TestRedundantAssignmentCheckerZ3Option.*",
r".*test_z3_constraints.*",
r".*test_edge_feasibility.*",
r".*test_impossible_condition_checker.*",
r".*test_redundant_condition_checker.*",
r".*test_z3_parser.*",
r".*test_z3_visitor.*",
r".*test_inconsistent_returns.*",
r".*test_missing_return_statements.*",
r".*test_one_iteration_checker.*",
r".*test_possibly_undefined_checker.*",
r".*test_redundant_assignment_checker.*",
}


Expand Down Expand Up @@ -60,16 +61,5 @@ def pytest_ignore_collect(path, config):

# Convert path to string for pattern matching
path_str = str(path)
print(path_str)
return any(re.search(pattern, path_str) for pattern in Z3_RELATED_TESTS)


def pytest_collection_modifyitems(config, items):
"""Modify collected test items to exclude certain tests based on configuration."""
if not config.getoption("--exclude-z3"):
return

skip_marker = pytest.mark.skip(reason="Only run when z3-solver is installed")
for item in items:
# Check if the test's nodeid matches any Z3-related patterns
if any(re.search(pattern, item.nodeid) for pattern in Z3_RELATED_TESTS):
item.add_marker(skip_marker)
16 changes: 15 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"e9950_forbidden_python_syntax.py",
]

# The following tests require z3-solver as dependency
Z3_RELATED_TESTS = ["r9900_redundant_condition.py", "r9901_impossible_condition.py"]


def get_file_paths(paths: Union[str, list[str]]) -> list[str]:
"""
Expand All @@ -54,10 +57,21 @@ def get_file_paths(paths: Union[str, list[str]]) -> list[str]:
if isinstance(paths, str):
paths = [paths]

# check if z3 dependency is available
z3_dependency_available = True
try:
import z3
except ImportError:
z3_dependency_available = False

for path in paths:
for root, _, files in os.walk(path, topdown=True):
for filename in files:
if filename not in IGNORED_TESTS and filename.endswith(".py"):
if (
filename not in IGNORED_TESTS
and filename.endswith(".py")
and (z3_dependency_available or filename not in Z3_RELATED_TESTS)
):
full_path = os.path.join(root, filename)
rel_path = os.path.relpath(full_path, path)
test_files.append(os.path.join(path, rel_path))
Expand Down

0 comments on commit e682d69

Please sign in to comment.