Exemplo n.º 1
0
    def test_report_to_gerrit_conversion_report_url(self):
        """Conversion report with absolute filepath and CC_REPORT_URL env"""

        main = {
            "location": {
                "file": 0,
                "line": 10,
                "col": 10,
            },
            "description": "some description",
            "check_name": "my_checker",
            "issue_hash_content_of_line_in_context": "dummy_hash",
            "notes": [],
            "macro_expansions": [],
        }
        bugpath = {}
        files = {0: "/home/src/lib/main.cpp"}
        metadata = {}

        report_to_convert = Report(main, bugpath, files, metadata)
        os.environ["CC_REPORT_URL"] = "localhost:8080/index.html"
        got = gerrit.convert([report_to_convert], self.severity_map)

        # Remove environment variable not to influence the other tests.
        os.environ.pop("CC_REPORT_URL")

        expected = {
            "tag": "jenkins",
            "message": "CodeChecker found 1 issue(s) in the code. "
            "See: 'localhost:8080/index.html'",
            "labels": {
                "Code-Review": -1,
                "Verified": -1
            },
            "comments": {
                "/home/src/lib/main.cpp": [{
                    "range": {
                        "start_line": 10,
                        "start_character": 10,
                        "end_line": 10,
                        "end_character": 10,
                    },
                    "message":
                    "[LOW] /home/src/lib/main.cpp:10:10: "
                    "some description [my_checker]\n10",
                }]
            },
        }
        self.assertEquals(got, expected)
Exemplo n.º 2
0
def parse_convert_reports(input_dirs: List[str],
                          out_format: str,
                          severity_map: Dict,
                          trim_path_prefixes: List[str]) \
        -> Tuple[Union[Dict, List], int]:
    """Parse and convert the reports from the input dirs to the out_format.

    Retuns a dictionary which can be converted to the out_format type of
    json to be printed out or saved on the disk.
    """

    assert (out_format in [fmt for fmt in EXPORT_TYPES if fmt != 'html'])

    input_files = set()
    for input_path in input_dirs:
        input_path = os.path.abspath(input_path)
        if os.path.isfile(input_path):
            input_files.add(input_path)
        elif os.path.isdir(input_path):
            _, _, file_names = next(os.walk(input_path), ([], [], []))
            input_paths = [
                os.path.join(input_path, file_name) for file_name in file_names
            ]
            input_files.update(input_paths)

    all_reports = []
    for input_file in input_files:
        if not input_file.endswith('.plist'):
            continue
        _, reports = plist_parser.parse_plist_file(input_file)
        all_reports.extend(reports)

    if trim_path_prefixes:
        for report in all_reports:
            report.trim_path_prefixes(trim_path_prefixes)

    number_of_reports = len(all_reports)
    if out_format == "codeclimate":
        return (codeclimate.convert(all_reports,
                                    severity_map), number_of_reports)

    if out_format == "gerrit":
        return gerrit.convert(all_reports, severity_map), number_of_reports

    if out_format == "json":
        return [out_json.convert_to_parse(r) for r in all_reports], \
            number_of_reports
Exemplo n.º 3
0
    def test_report_to_gerrit_conversion_abs_filepath(self):
        """Conversion report with absolute filepath"""

        main = {
            "location": {
                "file": 0,
                "line": 10,
                "col": 10,
            },
            "description": "some description",
            "check_name": "my_checker",
            "issue_hash_content_of_line_in_context": "dummy_hash",
            "notes": [],
            "macro_expansions": [],
        }
        bugpath = {}
        files = {0: "/home/src/lib/main.cpp"}
        metadata = {}

        report_to_convert = Report(main, bugpath, files, metadata)

        got = gerrit.convert([report_to_convert], self.severity_map)
        expected = {
            "tag": "jenkins",
            "message": "CodeChecker found 1 issue(s) in the code.",
            "labels": {
                "Code-Review": -1,
                "Verified": -1
            },
            "comments": {
                "/home/src/lib/main.cpp": [{
                    "range": {
                        "start_line": 10,
                        "start_character": 10,
                        "end_line": 10,
                        "end_character": 10,
                    },
                    "message":
                    "[LOW] /home/src/lib/main.cpp:10:10: "
                    "some description [my_checker]\n10",
                }]
            },
        }
        self.assertEquals(got, expected)
Exemplo n.º 4
0
def parse_convert_reports(input_dirs: List[str], out_format: str,
                          severity_map: Dict,
                          trim_path_prefixes: List[str]) -> Dict:
    """Parse and convert the reports from the input dirs to the out_format.

    Retuns a dictionary which can be converted to the out_format type of
    json to be printed out or saved on the disk.
    """

    input_files = set()
    for input_path in input_dirs:
        input_path = os.path.abspath(input_path)
        if os.path.isfile(input_path):
            input_files.add(input_path)
        elif os.path.isdir(input_path):
            _, _, file_names = next(os.walk(input_path), ([], [], []))
            input_paths = [
                os.path.join(input_path, file_name) for file_name in file_names
            ]
            input_files.update(input_paths)

    all_reports = []
    for input_file in input_files:
        if not input_file.endswith('.plist'):
            continue
        _, reports = plist_parser.parse_plist_file(input_file)
        all_reports.extend(reports)

    if trim_path_prefixes:
        for report in all_reports:
            report.trim_path_prefixes(trim_path_prefixes)

    if out_format == "codeclimate":
        return codeclimate.convert(all_reports)

    if out_format == "gerrit":
        return gerrit.convert(all_reports, severity_map)

    if out_format == "json":
        return [out_json.convert_to_parse(r) for r in all_reports]

    LOG.error("Unknown export format: %s", out_format)
    return {}
Exemplo n.º 5
0
    def test_report_to_gerrit_conversion_filter_changed_files(self):
        """Conversion report with changed files filter.

        Reports from the other.cpp file should be not in the converted list.
        """

        reports_to_convert = []

        # Empty for all reports.
        bugpath = {}
        metadata = {}

        main = {
            "location": {
                "file": 0,
                "line": 10,
                "col": 10,
            },
            "description": "some description",
            "check_name": "my_checker",
            "issue_hash_content_of_line_in_context": "dummy_hash",
            "notes": [],
            "macro_expansions": [],
        }
        files = {0: "/home/src/lib/main.cpp"}

        main_report = Report(main, bugpath, files, metadata)
        reports_to_convert.append(main_report)
        reports_to_convert.append(main_report)

        main = {
            "location": {
                "file": 0,
                "line": 10,
                "col": 10,
            },
            "description": "some description",
            "check_name": "my_checker",
            "issue_hash_content_of_line_in_context": "dummy_hash",
            "notes": [],
            "macro_expansions": [],
        }
        files = {0: "/home/src/lib/lib.cpp"}

        lib_report = Report(main, bugpath, files, metadata)
        reports_to_convert.append(lib_report)

        main = {
            "location": {
                "file": 0,
                "line": 10,
                "col": 10,
            },
            "description": "some description",
            "check_name": "my_checker",
            "issue_hash_content_of_line_in_context": "dummy_hash",
            "notes": [],
            "macro_expansions": [],
        }
        files = {0: "/home/src/lib/other.cpp"}

        other_report = Report(main, bugpath, files, metadata)
        reports_to_convert.append(other_report)

        dummy_changed_files_content = {
            "/COMMIT_MSG": {
                "status": "A",
                "lines_inserted": 1,
                "size_delta": 1,
                "size": 100,
            },
            "main.cpp": {
                "lines_inserted": 1,
                "lines_deleted": 1,
                "size_delta": 1,
                "size": 100,
            },
            "lib.cpp": {
                "lines_inserted": 1,
                "size_delta": 1,
                "size": 100
            },
        }
        fd, changed_files_file = tempfile.mkstemp()
        os.write(fd, json.dumps(dummy_changed_files_content).encode("utf-8"))
        os.close(fd)

        os.environ["CC_CHANGED_FILES"] = changed_files_file

        got = gerrit.convert(reports_to_convert, self.severity_map)
        os.remove(os.environ["CC_CHANGED_FILES"])

        # Remove environment variable not to influence the other tests.
        os.environ.pop("CC_CHANGED_FILES")

        review_comments = got["comments"]

        # Reports were found in two source files.
        self.assertEquals(len(review_comments), 2)

        # Two reports in the main.cpp file.
        self.assertEquals(len(review_comments["/home/src/lib/main.cpp"]), 2)

        self.assertEquals("CodeChecker found 3 issue(s) in the code.",
                          got["message"])
        self.assertNotIn("/home/src/lib/other.cpp", review_comments.keys())