def setup(self): self.detector = StubDetector() self.misuse = create_misuse("-m1-") self.misuses = [self.misuse, create_misuse("-m2-")] self.uut = AllFindings()
def test_potential_hit_for_second_misuse(self): self.misuses.extend([create_misuse("-1st-"), create_misuse("-2nd-")]) finding = Finding({"rank": ":some potential hit for second misuse:"}) finding.is_potential_hit = lambda misuse, y: misuse == self.misuses[1] potential_hits = self.uut.get_potential_hits([finding]) assert_equals(self.misuses[1].id, potential_hits[0]["misuse"])
def test_filters_non_whitelisted(self): project = create_project("-project-") version = create_version("-version-", misuses=[], project=project) create_misuse("-id-", version=version) uut = CollectMisusesTask() actual = uut.run( version, DataEntityLists(["-project-.-version-.-other-id-"], [])) assert_equals([], actual)
def test_finds_only_own_misuses(self): project = create_project(self.project_id, base_path=self.temp_dir) misuse1 = create_misuse("1", project=project) create_file(misuse1.misuse_file) misuse2 = create_misuse("2", project=project) create_file(misuse2.misuse_file) self.uut._YAML = {"misuses": ["2"]} misuses = self.uut.misuses assert_equals([misuse2], misuses)
def test_finds_all_misuses(self): m1 = create_misuse("-m1-") m2 = create_misuse("-m2-") project = create_project("-project-") version = create_version("-version-", misuses=[m1, m2], project=project) uut = CollectMisusesTask() actual = uut.run(version, DataEntityLists([], [])) assert_equals([m1, m2], actual)
def test_white_list_misuse(self): project = create_project("-p-") misuse1 = create_misuse("-m1-", project=project) misuse2 = create_misuse("-m2-", project=project) version1 = create_version("-v1-", misuses=[misuse1], project=project) create_version("-v2-", misuses=[misuse2], project=project) self.uut.white_list = [misuse1.id] self.uut.process_project(project) assert_equals([call(project, version1)], self.uut.process_project_version.call_args_list)
def test_white_list_misuse(self): uut = ProjectMisuseTask() uut.process_project_misuse = MagicMock() project = create_project("-p-") misuse1 = create_misuse("-m1-", project=project) misuse2 = create_misuse("-m2-", project=project) create_version("-v-", misuses=[misuse1, misuse2], project=project) uut.white_list = [misuse1.id] uut.process_project(project) assert_equals([call(project, misuse1)], uut.process_project_misuse.call_args_list)
def test_reads_location(self): uut = create_misuse( '', meta={"location": { "file": "file.name", "method": "foo()" }}) assert_equals(Location("file.name", "foo()"), uut.location)
def setup(self): self.misuse = create_misuse( 'misuse', meta={"location": { "file": "a", "method": "m()" }}) self.snippets = [] self.misuse.get_snippets = lambda *_: self.snippets
def test_derives_compile_base_path(self): self.uut._MISUSES = [create_misuse("m") ] # prevent version from loading misuses project_compile = self.uut.get_compile("/base/path") assert_equals(join("/base/path", self.project_id, self.version_id), project_compile.base_path)
def test_copies_misuse_sources(self): self.mock_with_fake_compile() create_file(join(self.source_path, "mu.file")) self.version.misuses.append(create_misuse("1", meta={"location": {"file": "mu.file"}}, project=self.project)) self.uut.process_project_version(self.project, self.version) assert exists(join(self.misuse_source_path, "mu.file"))
def test_no_hit(self): self.misuses.append(create_misuse("-m1-")) finding = Finding({"rank": "no potential hit"}) finding.is_potential_hit = lambda misuse, y: False potential_hits = self.uut.get_potential_hits([finding]) assert_equals([], potential_hits)
def test_potential_hit(self): self.misuses.append(create_misuse("-m1-")) finding = Finding({"rank": ":potential hit for m1:"}) finding.is_potential_hit = lambda misuse, y: misuse == self.misuses[0] potential_hits = self.uut.get_potential_hits([finding]) assert_equals(self.misuses[0].id, potential_hits[0]["misuse"])
def test_process_misuse(self): uut = ProjectMisuseTask() uut.process_project_misuse = MagicMock() project = create_project("p1") m1 = create_misuse("m1") m2 = create_misuse("m2") m3 = create_misuse("m3") create_version("v1", misuses=[m1, m2], project=project) create_version("v2", misuses=[m3], project=project) uut.process_project(project) actual_call = uut.process_project_misuse.call_args_list assert_equals(3, len(actual_call)) assert_in(call(project, m1), actual_call) assert_in(call(project, m2), actual_call) assert_in(call(project, m3), actual_call)
def test_process_misuse(self): uut = ProjectVersionMisuseTask() uut.process_project_version_misuse = MagicMock() project = create_project("p1") m1 = create_misuse("m1") m2 = create_misuse("m2") m3 = create_misuse("m3") v1 = create_version("v1", misuses=[m1, m2], project=project) v2 = create_version("v2", misuses=[m3], project=project) uut.process_project(project) assert_equals([ call(project, v1, m1), call(project, v1, m2), call(project, v2, m3) ], uut.process_project_version_misuse.call_args_list)
def test_publish_url(self, post_mock, snippets_mock): misuse = create_misuse("-m-", project=self.project) create_version("-v-", project=self.project, misuses=[misuse]) task = PublishMetadataTask("-compiles-path-", "http://test.url") task.process_project_misuse(self.project, misuse) task.end() assert_equals(post_mock.call_args[0][0], "http://test.url/api/upload/metadata")
def test_finds_misuses(self): misuse = create_misuse("1", project=create_project(self.project_id, base_path=self.temp_dir)) create_file(misuse.misuse_file) self.uut._YAML = {"misuses": ["1"]} actual_misuses = self.uut.misuses assert_equals([misuse], actual_misuses)
def setup(self): self.misuse = create_misuse('-misuse-', meta={"location": {"file": "a", "method": "m()"}}) self.version = create_version("-version-", misuses=[self.misuse], project=create_project("-project-")) self.detector = StubDetector() self.findings_base_path = "-findings-" self.logger = logging.getLogger("test") self.uut = DetectOnlyExecution(self.detector, self.version, self.misuse, self.findings_base_path, PotentialHits(self.misuse))
def test_publish_url(self, post_mock, snippets_mock): misuse = create_misuse("-m-", project=self.project, version=self.version) task = PublishMetadataTask("-checkouts-path-", "http://test.url") task.run(self.project, misuse) task.end() assert_equals(post_mock.call_args[0][0], "http://test.url/metadata")
def test_skips_compile_patterns_if_no_pattern(self): self.mock_with_fake_compile() self.create_misuse_with_pattern("mwp", "a.java") makedirs(join(self.pattern_classes_path, "mwp")) self.version._MISUSES.append( create_misuse("mwop", project=self.project)) self.uut.process_project_version(self.project, self.version) assert not exists(join(self.pattern_classes_path, "mwp", "a.class"))
def test_no_warning_on_known_project_version_misuse( self, report_unknown_dataset_entry_mock, _, __): project = create_project("-project-", meta={}) version = create_version("-version-", project=project) misuse = create_misuse("-misuse-", project=project, version=version) uut = MisuseCheckTask({"-dataset-": [misuse.id]}, '', '') uut.run(project, version, misuse) uut.end() report_unknown_dataset_entry_mock.assert_not_called()
def setup(self): version_meta = {"build": {"src": "-source_dir-"}} self.project = create_project("-project-") self.version = create_version("-version-", meta=version_meta, project=self.project) self.misuse = create_misuse("-misuse-", version=self.version) checkout = MagicMock() checkout.exists = MagicMock(return_value=True) checkout.base_path = "-checkout_dir-" self.version.get_checkout = MagicMock(return_value=checkout)
def test_whitelisted_version(self): misuse = create_misuse("-id-") project = create_project("-project-") version = create_version("-version-", misuses=[misuse], project=project) uut = CollectMisusesTask() actual = uut.run(version, DataEntityLists(["-project-.-version-"], [])) assert_equals([misuse], actual)
def test_reads_fix(self): misuse = create_misuse("", meta={ "fix": { "commit": "http://link.to/commit", "description": "blub", "revision": 42 } }) assert_equals("http://link.to/commit", misuse.fix.commit) assert_equals("blub", misuse.fix.description) assert_equals("42", misuse.fix.revision)
def test_missing_source(self): project = create_project("-project-", meta=EMPTY_META) misuse = create_misuse("-id-", project=project, meta=EMPTY_META) version = create_version("-version-", meta=EMPTY_META, project=project, misuses=[misuse]) self.uut.process_project_version_misuse(project, version, misuse) self.uut._report_missing_key.assert_any_call( "source", "-project-/misuses/-id-/misuse.yml")
def setup(self): self.misuse = create_misuse('-misuse-', meta={"location": {"file": "a", "method": "m()"}}) self.version = create_version("-version-", misuses=[self.misuse], project=create_project("-project-")) self.detector = StubDetector() self.temp_dir = mkdtemp(prefix='mubench-run-test_') self.findings_path = join(self.temp_dir, "-findings-") self.logger = logging.getLogger("test") self.__orig_shell_exec = Shell.exec Shell.exec = MagicMock()
def test_version_and_misuse_with_same_name(self): self.uut._report_id_conflict = MagicMock() project = create_project("-project-", meta=EMPTY_META) misuse = create_misuse("-conflict-", project=project, meta=EMPTY_META) version = create_version("-conflict-", project=project, misuses=[misuse], meta=EMPTY_META) self.uut.process_project(project) self.uut._report_id_conflict.assert_any_call("-project-.-conflict-")
def test_missing_violations(self, _, __): project = create_project("-project-", meta={}) misuse = create_misuse("-id-", project=project, meta={}) version = create_version("-version-", meta={}, project=project, misuses=[misuse]) self.uut.run(project, version, misuse) self.uut._report_missing_key.assert_any_call( "violations", "-project-/misuses/-id-/misuse.yml")
def test_provided_patterns_run_no_patterns(self): self.version._MISUSES = [ create_misuse("-1-", project=self.project, patterns=[]) ] experiment = Experiment(Experiment.PROVIDED_PATTERNS, self.detector, "-findings_path-") run = experiment.get_run(self.version) assert_is_instance(run, Run) assert_equals(0, len(run.executions))
def test_missing_location_method(self, _, __): meta = {"location": {}} project = create_project("-project-", meta={}) misuse = create_misuse("-id-", project=project, meta=meta) version = create_version("-version-", meta={}, project=project, misuses=[misuse]) self.uut.run(project, version, misuse) self.uut._report_missing_key.assert_any_call( "location.method", "-project-/misuses/-id-/misuse.yml")