示例#1
0
    def test_no_code(self, utils_mock):
        utils_mock.return_value = ""

        finding = Finding({"file": "-file-"})

        with assert_raises(SnippetUnavailableException):
            finding.get_snippets(["/base"])
示例#2
0
    def test_loads_snippet_absolute_path(self, utils_mock):
        utils_mock.side_effect = lambda tool, args: \
            "42:T:-code-" if tool == "MethodExtractor" and args == '"/-absolute-file-" "-method-"' else ""

        finding = Finding({"file": "/-absolute-file-", "method": "-method-"})

        assert_equals(1, len(finding.get_snippets(["/base"])))
    def test_loads_snippet(self, utils_mock):
        utils_mock.side_effect = lambda tool, args:\
                "42:T:-code-" if tool == "MethodExtractor" and args == '"/base/-file-" "-method-"' else ""

        finding = Finding({"file": "-file-", "method": "-method-"})

        assert_equals([Snippet("class T {\n-code-\n}", 41)], finding.get_snippets("/base"))
示例#4
0
    def test_extraction_error(self, utils_mock):
        utils_mock.side_effect = CommandFailedError("cmd", "output")

        finding = Finding({"file": "-file-", "method": "-method-"})

        with assert_raises(SnippetUnavailableException):
            finding.get_snippets(["/base"])
示例#5
0
    def test_limits_number_of_findings(self):
        all = [Finding({"rank": "1"}), Finding({"rank": "2"})]
        self.uut.limit = 1

        actual = self.uut.get_potential_hits(all)

        assert_equals(1, len(actual))
示例#6
0
    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)
示例#7
0
    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"])
示例#8
0
    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"])
示例#9
0
    def test_extraction_error(self, utils_mock):
        utils_mock.side_effect = CommandFailedError("cmd", "output")

        finding = Finding({"file": "-file-", "method": "-method-"})

        with assert_raises(SnippetUnavailableException) as context:
            finding.get_snippets("/base")

        assert_equals('/base/-file-', context.exception.file)
        assert_equals(utils_mock.side_effect, context.exception.exception)
    def test_adds_rank(self, read_yamls_mock):
        read_yamls_mock.return_value.__enter__.return_value = [{"name": "f1"}, {"name": "f2"}]
        execution = DetectorExecution(DetectorMode.mine_and_detect, StubDetector(), create_version("-v-"),
                                      "-findings-base-path-", FindingsFilter())
        execution._get_findings_path = lambda: ""

        findings = execution._load_findings()

        assert_equals(findings, [
            Finding({"name": "f1", "rank": 0}),
            Finding({"name": "f2", "rank": 1})
        ])
示例#11
0
    def test_filters_by_startline(self, utils_mock):
        utils_mock.return_value = "42:T:t-code\n===\n32:A:a\nb\nc\n===\n22:N:n-code"

        finding = Finding({
            "file": "-file-",
            "method": "-method-",
            "startline": 33
        })

        snippets = finding.get_snippets(["/base"])

        assert_equals([Snippet("class A {\na\nb\nc\n}", 31)], snippets)
示例#12
0
    def test_returns_all_findings(self):
        expected = [
            Finding({
                "rank": "1",
                "file": ""
            }),
            Finding({
                "rank": "2",
                "file": ""
            })
        ]

        actual = self.uut.get_potential_hits(expected)

        assert_equals(expected, actual)
示例#13
0
    def _create_finding(self, data: Dict, convert_mock=None, file_paths=None, snippets=None):
        if snippets is None:
            snippets = []
        if file_paths is None:
            file_paths = []

        finding = Finding(data)
        finding.get_snippets = lambda source_paths: \
            snippets if source_paths == ["/sources/-p-/-v-/build/"] else {}["illegal source paths: %s" % source_paths]

        if convert_mock is not None:
            self.created_files_per_finding[str(finding)] = file_paths
            convert_mock.side_effect = lambda f, p: self.created_files_per_finding[str(f)]

        return finding
示例#14
0
    def create_finding(self, finding_data: Dict[str, str]):
        if "file" not in finding_data:
            finding_data["file"] = self.misuse.location.file
        if "method" not in finding_data:
            finding_data["method"] = self.misuse.location.method

        return Finding(finding_data)
示例#15
0
    def test_adds_rank(self, read_yamls_mock, _):
        read_yamls_mock.return_value.__enter__.return_value = [{
            "name": "f1"
        }, {
            "name": "f2"
        }]
        execution = DetectorRun(StubDetector(), create_version("-v-"),
                                "-findings-path-")

        findings = execution._load_findings()

        assert_equals(findings, [
            Finding({
                "name": "f1",
                "rank": 0
            }),
            Finding({
                "name": "f2",
                "rank": 1
            })
        ])
示例#16
0
    def test_no_code(self, utils_mock):
        utils_mock.return_value = ""

        finding = Finding({"file": "-file-"})

        assert_equals(finding.get_snippets("/base"), [])
示例#17
0
 def __create_finding(rank: int, finding_data: Dict):
     finding_data["rank"] = rank
     return Finding(finding_data)
示例#18
0
    def test_loads_multiple_snippets(self, utils_mock):
        utils_mock.return_value = "42:T:t-code\n===\n32:A:a-code"

        finding = Finding({"file": "-file-", "method": "-method-"})

        assert_equals(2, len(finding.get_snippets(["/base"])))
 def __create_finding(data: Dict):
     data["rank"] = data.pop("id")
     return Finding(data)
示例#20
0
 def _to_markdown_dict(finding: Finding) -> Dict[str, str]:
     markdown_dict = dict()
     for key, value in finding.items():
         if key != _SNIPPETS_KEY:
             markdown_dict[key] = as_markdown(value)
     return markdown_dict
示例#21
0
    def test_extraction_error(self, utils_mock):
        utils_mock.side_effect = CommandFailedError("cmd", "output")

        finding = Finding({"file": "-file-", "method": "-method-"})

        assert_equals([Snippet("Failed to execute 'cmd': output", 1)], finding.get_snippets("/base"))
示例#22
0
    def test_strips_additional_output(self, utils_mock):
        utils_mock.return_value = "Arbitrary additional output\n1:C:code"

        finding = Finding({"file": "-file-", "method": "-method-"})

        assert_equals(1, len(finding.get_snippets(["/base"])))