Repository navigation
Add targeted mutation gate for scientific invariants #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #!/usr/bin/env python3 | ||
| """Targeted mutation gate for the scientific invariants. | ||
|
|
||
| Each mutant is applied to a throwaway copy of the package, then the scientific | ||
| tests must fail. A surviving mutant means the invariant tests do not distinguish | ||
| a known-wrong semantic change from correct behaviour. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| TEST_FILES = ("test_scientific_invariants.py", "test_scientific_fuzz.py") | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Mutant: | ||
| name: str | ||
| relative_path: str | ||
| before: str | ||
| after: str | ||
|
|
||
|
|
||
| MUTANTS = ( | ||
| Mutant( | ||
| "radius-becomes-unique", | ||
| "core.py", | ||
| " MATCH_AMBIGUOUS,", | ||
| " MATCH_UNIQUE,", | ||
| ), | ||
| Mutant( | ||
| "fastq-validation-disabled", | ||
| "fastq_io.py", | ||
| " if not _valid_printable_ascii(seq):", | ||
| " if False:", | ||
| ), | ||
| Mutant( | ||
| "counts-parse-through-float", | ||
| "count_io.py", | ||
| " return int(text)", | ||
| " return int(float(text))", | ||
| ), | ||
| Mutant( | ||
| "ambiguous-features-counted", | ||
| "feature_matrix.py", | ||
| " if status == MATCH_UNIQUE and target_id:\n counts[(observation.cell_barcode, target_id)] += 1", | ||
| " if status in {MATCH_UNIQUE, MATCH_AMBIGUOUS}:\n counts[(observation.cell_barcode, target_id or f\"ambiguous_{result.target_index}\")] += 1", | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def _copy_source(destination: Path) -> None: | ||
| shutil.copytree(ROOT / "python" / "dotmatch", destination / "python" / "dotmatch") | ||
| shutil.copytree(ROOT / "python" / "quickdna", destination / "python" / "quickdna") | ||
| (destination / "tests").mkdir() | ||
| for name in TEST_FILES: | ||
| shutil.copy2(ROOT / "python" / "tests" / name, destination / "tests" / name) | ||
|
|
||
|
|
||
| def _library_path() -> Path: | ||
| for name in ("libdotmatch.dylib", "libdotmatch.so"): | ||
| candidate = ROOT / name | ||
| if candidate.is_file(): | ||
| return candidate | ||
| raise SystemExit("build the shared library first: make shared") | ||
|
|
||
|
|
||
| def _run_mutant(mutant: Mutant, library: Path) -> bool: | ||
| with tempfile.TemporaryDirectory(prefix="dotmatch-mutant-") as temporary: | ||
| root = Path(temporary) | ||
| _copy_source(root) | ||
| target = root / "python" / "dotmatch" / mutant.relative_path | ||
| source = target.read_text(encoding="utf-8") | ||
| if mutant.before not in source: | ||
| raise SystemExit(f"mutant {mutant.name}: source pattern not found in {mutant.relative_path}") | ||
| target.write_text(source.replace(mutant.before, mutant.after, 1), encoding="utf-8") | ||
| env = { | ||
| **os.environ, | ||
| "PYTHONPATH": str(root / "python"), | ||
| "DOTMATCH_LIB": str(library), | ||
| } | ||
| completed = subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "pytest", | ||
| str(root / "tests"), | ||
| "-q", | ||
| "-c", | ||
| os.devnull, | ||
| "-p", | ||
| "no:cacheprovider", | ||
| ], | ||
| cwd=root, | ||
| env=env, | ||
| check=False, | ||
| text=True, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| ) | ||
| if completed.returncode == 0: | ||
| print(f"survived: {mutant.name}", file=sys.stderr) | ||
| print(completed.stdout[-4000:], file=sys.stderr) | ||
| return False | ||
| print(f"killed: {mutant.name}") | ||
| return True | ||
|
|
||
|
|
||
| def main() -> int: | ||
| library = _library_path() | ||
| killed = [_run_mutant(mutant, library) for mutant in MUTANTS] | ||
| if not all(killed): | ||
| return 1 | ||
| print(f"all {len(MUTANTS)} targeted scientific mutants were killed") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.