def test_cppcheck_report_storage(self):
        """ In the stored report not the default zero hash should be used. """

        test_dir = os.path.dirname(os.path.realpath(__file__))

        report_dir = os.path.join(test_dir, 'test_proj')

        codechecker_cfg = self._test_cfg['codechecker_cfg']

        # Copy report files to a temporary directory not to modify the
        # files in the repository.
        # Report files will be overwritten during the tests.
        temp_workspace = os.path.join(codechecker_cfg['workspace'],
                                      'test_proj')
        shutil.copytree(report_dir, temp_workspace)

        report_file = os.path.join(temp_workspace, 'divide_zero.plist')
        # Convert file paths to absolute in the report.
        plist_test.prefix_file_path(report_file, temp_workspace)

        run_name = 'cppcheck'
        store_cmd = [
            env.codechecker_cmd(),
            'store',
            '--name',
            run_name,
            # Use the 'Default' product.
            '--url',
            env.parts_to_url(codechecker_cfg),
            temp_workspace
        ]

        out = subprocess.check_output(store_cmd,
                                      encoding="utf-8",
                                      errors="ignore")
        print(out)
        query_cmd = [
            env.codechecker_cmd(),
            'cmd',
            'results',
            run_name,
            # Use the 'Default' product.
            '--url',
            env.parts_to_url(codechecker_cfg),
            '-o',
            'json'
        ]

        out = subprocess.check_output(query_cmd,
                                      encoding="utf-8",
                                      errors="ignore")
        print(out)
        reports = json.loads(out)
        self.assertEqual(len(reports), 5)
        # The stored hash should not be "0".
        for report in reports:
            self.assertNotEqual(report['bugHash'], "0")
Beispiel #2
0
def setup_package():
    """Setup the environment for the tests then start the server."""

    global TEST_WORKSPACE
    TEST_WORKSPACE = env.get_workspace('store_test')

    os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

    # Configuration options.
    codechecker_cfg = {
        'check_env': env.test_env(TEST_WORKSPACE),
        'workspace': TEST_WORKSPACE,
        'checkers': [],
        'reportdir': os.path.join(TEST_WORKSPACE, 'reports'),
        'test_project': 'store_test'
    }

    # Start or connect to the running CodeChecker server and get connection
    # details.
    print("This test uses a CodeChecker server... connecting...")
    server_access = codechecker.start_or_get_server()
    server_access['viewer_product'] = 'store_test'
    codechecker.add_test_package_product(server_access, TEST_WORKSPACE)

    # Extend the checker configuration with the server access.
    codechecker_cfg.update(server_access)

    # Export the test configuration to the workspace.
    env.export_test_cfg(TEST_WORKSPACE, {'codechecker_cfg': codechecker_cfg})

    # Copy test files to a temporary directory not to modify the
    # files in the repository.
    # Report files will be overwritten during the tests.
    test_dir = os.path.dirname(os.path.realpath(__file__))
    dst_dir = os.path.join(TEST_WORKSPACE, "test_proj")
    shutil.copytree(os.path.join(test_dir, "test_proj"), dst_dir)

    prefix_file_paths = [
        os.path.join(dst_dir, "divide_zero", "divide_zero.plist"),
        os.path.join(dst_dir, "double_suppress", "double_suppress.plist")
    ]

    for file_name in prefix_file_paths:
        plist_test.prefix_file_path(file_name, os.path.dirname(file_name))
    def test_review_status_update_from_source_trim(self):
        """
        Test if the review status comments changes in the source code
        are updated at the server when trim path is used.

        The report is store twice and between the storage the
        review status as a source code comment is modified.
        The test checks is after the source code modification
        and storage the review status is updated correctly at
        the server too.
        """
        test_project_path = os.path.join(self.test_workspace,
                                         'review_status_files')
        test_project_name = 'review_status_update_proj'

        plist_file = os.path.join(test_project_path, 'divide_zero.plist')
        source_file = os.path.join(test_project_path, 'divide_zero.cpp')
        plist_test.prefix_file_path(plist_file, test_project_path)

        codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)
        codechecker_cfg['reportdir'] = test_project_path

        codechecker.store(codechecker_cfg, test_project_name)

        codechecker_cfg['trim_path_prefix'] = test_project_path

        # Run data for the run created by this test case.
        run_filter = RunFilter(names=[test_project_name], exactMatch=True)

        runs = self._cc_client.getRunData(run_filter, None, 0, None)
        run = runs[0]
        runid = run.runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        reports = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(reports)
        self.assertNotEqual(len(reports), 0)
        self.assertEqual(len(reports), 2)

        for report in reports:
            print(report)
            self.assertEqual(report.reviewData.status,
                             ReviewStatus.INTENTIONAL)

        # Modify review comments from intentional to confirmed for the
        # second store.
        with open(source_file, 'r+', encoding='utf-8', errors='ignore') as sf:
            content = sf.read()
            new_content = content.replace("codechecker_intentional",
                                          "codechecker_confirmed")
            sf.truncate(0)
            sf.write(new_content)

        # modify review comments and store the reports again
        with open(source_file, encoding='utf-8', errors='ignore') as sf:
            content = sf.read()

        # Update the plist file modification date to be newer than
        # the source file so it can be stored, because there was no
        # actual analysis.
        date = datetime.datetime.now() + datetime.timedelta(minutes=5)
        mod_time = time.mktime(date.timetuple())
        os.utime(plist_file, (mod_time, mod_time))

        codechecker.store(codechecker_cfg, test_project_name)

        # Check if all the review statuses were updated to the new at the
        # server.
        reports = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(reports)
        self.assertNotEqual(len(reports), 0)
        self.assertEqual(len(reports), 2)
        for report in reports:
            self.assertEqual(report.reviewData.status, ReviewStatus.CONFIRMED)
Beispiel #4
0
    def test_tim_path_prefix_store(self):
        """Trim the path prefix from the sored reports.

        The source file paths are converted to absolute with the
        temporary test directory, the test trims that temporary
        test directory from the source file path during the storage.
        """

        test_dir = os.path.dirname(os.path.realpath(__file__))

        report_dir = os.path.join(test_dir, "test_proj")

        codechecker_cfg = self._test_cfg["codechecker_cfg"]

        # Copy report files to a temporary directory not to modify the
        # files in the repository.
        # Report files will be overwritten during the tests.
        temp_workspace = os.path.join(codechecker_cfg["workspace"],
                                      "test_proj")
        shutil.copytree(report_dir, temp_workspace)

        report_file = os.path.join(temp_workspace, "divide_zero.plist")

        # Convert file paths to absolute in the report.
        plist_test.prefix_file_path(report_file, temp_workspace)

        report_content = {}
        with open(report_file, mode="rb") as rf:
            report_content = plistlib.load(rf)

        trimmed_paths = [
            util.trim_path_prefixes(path, [temp_workspace])
            for path in report_content["files"]
        ]

        run_name = "store_test"
        store_cmd = [
            env.codechecker_cmd(),
            "store",
            temp_workspace,
            "--name",
            run_name,
            "--url",
            env.parts_to_url(codechecker_cfg),
            "--trim-path-prefix",
            temp_workspace,
            "--verbose",
            "debug",
        ]

        try:
            out = subprocess.check_output(store_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
            print(out)
        except subprocess.CalledProcessError as cerr:
            print(cerr.stdout)
            print(cerr.stderr)
            raise

        query_cmd = [
            env.codechecker_cmd(),
            "cmd",
            "results",
            run_name,
            # Use the 'Default' product.
            "--url",
            env.parts_to_url(codechecker_cfg),
            "-o",
            "json",
        ]

        out = subprocess.check_output(query_cmd,
                                      encoding="utf-8",
                                      errors="ignore")
        reports = json.loads(out)

        print(json.dumps(reports, indent=2))

        self.assertEqual(len(reports), 4)
        for report in reports:
            self.assertIn(report["checkedFile"], trimmed_paths)