def filter(self, work_db, _args): """Look for WorkItems in `work_db` that should not be mutated due to spor metadata. For each WorkItem, find anchors for the item's file/line/columns. If an anchor exists with metadata containing `{mutate: False}` then the WorkItem is marked as SKIPPED. Args: work_db: A WorkDB to filter. """ @lru_cache() def file_contents(file_path): "A simple cache of file contents." with file_path.open(mode="rt") as handle: return handle.readlines() for item in work_db.pending_work_items: try: repo = open_repository(item.module_path) except ValueError: log.info("No spor repository for %s", item.module_path) continue for _, anchor in repo.items(): if anchor.file_path != item.module_path.absolute(): continue metadata = anchor.metadata lines = file_contents(item.module_path) if _item_in_context( lines, item, anchor.context) and not metadata.get("mutate", True): log.info( "spor skipping %s %s %s %s %s %s", item.job_id, item.operator_name, item.occurrence, item.module_path, item.start_pos, item.end_pos, ) work_db.set_result( item.job_id, WorkResult( output="Filtered by spor", test_outcome=None, diff=None, worker_outcome=WorkerOutcome.SKIPPED, ), )
def intercept(work_db): """Look for WorkItems in `work_db` that should not be mutated due to spor metadata. For each WorkItem, find anchors for the item's file/line/columns. If an anchor exists with metadata containing `{mutate: False}` then the WorkItem is marked as SKIPPED. """ @lru_cache() def file_contents(file_path): "A simple cache of file contents." with file_path.open(mode="rt") as handle: return handle.readlines() for item in work_db.work_items: try: repo = open_repository(item.module_path) except ValueError: log.info("No spor repository for %s", item.module_path) continue for _, anchor in repo.items(): if anchor.file_path != item.module_path.absolute(): continue metadata = anchor.metadata lines = file_contents(item.module_path) if _item_in_context( lines, item, anchor.context) and not metadata.get("mutate", True): log.info( "spor skipping %s %s %s %s %s %s", item.job_id, item.operator_name, item.occurrence, item.module_path, item.start_pos, item.end_pos, ) work_db.set_result( item.job_id, WorkResult( output=None, test_outcome=None, diff=None, worker_outcome=WorkerOutcome.SKIPPED, ), )
def test_create_repo_from_nested_dir(repo): nested = repo.root / 'nested' nested.mkdir() assert open_repository(nested).root == repo.root
def test_create_repo_with_no_repo_raises_ValueError(tmpdir_path, excursion): with excursion(tmpdir_path): with pytest.raises(ValueError): open_repository(tmpdir_path)
def test_create_repo_from_root(tmpdir_path): initialize_repository(tmpdir_path) repo = open_repository(tmpdir_path) assert repo.root == tmpdir_path assert repo._spor_dir == tmpdir_path / '.spor'