Esempio n. 1
0
    def parse_coverage_artifacts(self,
                                 gcov_dir,
                                 jsvm_dir,
                                 merge=False,
                                 output_format='lcov',
                                 filter_covered=False):
        jsvm_output_file = 'jsvm_lcov_output.info'
        grcov_output_file = 'grcov_lcov_output.info'

        dirs = self.query_abs_dirs()

        sys.path.append(dirs['abs_test_install_dir'])
        sys.path.append(os.path.join(dirs['abs_test_install_dir'], 'mozbuild/codecoverage'))

        from lcov_rewriter import LcovFileRewriter
        jsvm_files = [os.path.join(jsvm_dir, e) for e in os.listdir(jsvm_dir)]
        rewriter = LcovFileRewriter(os.path.join(self.grcov_dir, 'chrome-map.json'))
        rewriter.rewrite_files(jsvm_files, jsvm_output_file, '')

        # Run grcov on the zipped .gcno and .gcda files.
        grcov_command = [
            os.path.join(self.grcov_dir, 'grcov'),
            '-t', output_format,
            '-p', self.prefix,
            '--ignore-dir', 'gcc*',
            '--ignore-dir', 'vs2017_*',
            os.path.join(self.grcov_dir, 'target.code-coverage-gcno.zip'), gcov_dir
        ]

        if 'coveralls' in output_format:
            grcov_command += ['--token', 'UNUSED', '--commit-sha', 'UNUSED']

        if merge:
            grcov_command += [jsvm_output_file]

        if mozinfo.os == 'win' or mozinfo.os == 'mac':
            grcov_command += ['--llvm']

        if filter_covered:
            grcov_command += ['--filter', 'covered']

        # 'grcov_output' will be a tuple, the first variable is the path to the lcov output,
        # the other is the path to the standard error output.
        tmp_output_file, _ = self.get_output_from_command(
            grcov_command,
            silent=True,
            save_tmpfiles=True,
            return_type='files',
            throw_exception=True,
        )
        shutil.move(tmp_output_file, grcov_output_file)

        shutil.rmtree(gcov_dir)
        shutil.rmtree(jsvm_dir)

        if merge:
            os.remove(jsvm_output_file)
            return grcov_output_file
        else:
            return grcov_output_file, jsvm_output_file
    def parse_coverage_artifacts(self, gcov_dir, jsvm_dir):
        jsvm_output_file = 'jsvm_lcov_output.info'
        grcov_output_file = 'grcov_lcov_output.info'

        dirs = self.query_abs_dirs()

        # Zip gcda files (will be given in input to grcov).
        file_path_gcda = os.path.join(os.getcwd(), 'code-coverage-gcda.zip')
        self.run_command(['zip', '-q', '-0', '-r', file_path_gcda, '.'],
                         cwd=gcov_dir)

        sys.path.append(dirs['abs_test_install_dir'])
        sys.path.append(
            os.path.join(dirs['abs_test_install_dir'],
                         'mozbuild/codecoverage'))

        from lcov_rewriter import LcovFileRewriter
        jsvm_files = [os.path.join(jsvm_dir, e) for e in os.listdir(jsvm_dir)]
        rewriter = LcovFileRewriter(
            os.path.join(self.grcov_dir, 'chrome-map.json'))
        rewriter.rewrite_files(jsvm_files, jsvm_output_file, '')

        # Run grcov on the zipped .gcno and .gcda files.
        grcov_command = [
            os.path.join(self.grcov_dir, 'grcov'), '-t', 'lcov', '-p',
            self.prefix, '--ignore-dir', 'gcc*', '--ignore-dir', 'vs2017_*',
            os.path.join(self.grcov_dir,
                         'target.code-coverage-gcno.zip'), file_path_gcda
        ]

        if mozinfo.os == 'win':
            grcov_command += ['--llvm']

        # 'grcov_output' will be a tuple, the first variable is the path to the lcov output,
        # the other is the path to the standard error output.
        tmp_output_file, _ = self.get_output_from_command(
            grcov_command,
            silent=True,
            save_tmpfiles=True,
            return_type='files',
            throw_exception=True,
        )
        shutil.move(tmp_output_file, grcov_output_file)

        return grcov_output_file, jsvm_output_file
Esempio n. 3
0
def run(websites):
    # Environmental vars set to overwrite default location of .gcda files
    if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
        prefix = '/builds/worker/workspace/build/src/'
        strip_count = prefix.count('/')
    elif sys.platform.startswith('cygwin') or sys.platform.startswith('win32'):
        prefix = 'z:/build/build/src/'
        strip_count = prefix.count('/') + 1

    # Remove a prefix from the path where .gcda files are stored
    os.environ['GCOV_PREFIX_STRIP'] = str(strip_count)
    os.environ['PATH'] += os.pathsep + os.path.abspath('tools')
    os.environ['MOZ_HEADLESS'] = '1'
    already_clicked_elems.clear()
    # Create temporary directories with context manager
    with tempfile.TemporaryDirectory() as gcov_dir, tempfile.TemporaryDirectory() as jsvm_dir:
        os.environ['GCOV_PREFIX'] = gcov_dir
        os.environ['JS_CODE_COVERAGE_OUTPUT_DIR'] = jsvm_dir

        # Webdriver uses Firefox Binaries from downloaded cov build
        driver = webdriver.Firefox(firefox_binary='tools/firefox/firefox-bin')

        set_timeouts(driver)

        for website in websites:
            # All steps are stored in new folder
            data_folder = str(uuid.uuid4())
            os.makedirs(data_folder, exist_ok=True)
            try:
                sequence = run_in_driver(website, driver)
                with open('{}/steps.txt'.format(data_folder), 'w') as f:
                    f.write('Website name: ' + website + '\n')
                    for element in sequence:
                        f.write(json.dumps(element) + '\n')
            except:  # noqa: E722
                traceback.print_exc(file=sys.stderr)
                close_all_windows_except_first(driver)

        # Add paths to Mozilla-central modules
        sys.path.insert(0, 'tools/mozbuild/codecoverage')
        sys.path.insert(0, 'tools')

        from lcov_rewriter import LcovFileRewriter
        jsvm_files = [os.path.join(jsvm_dir, e) for e in os.listdir(jsvm_dir)]
        rewriter = LcovFileRewriter(os.path.join('tools', 'chrome-map.json'))
        jsvm_output_dir = os.path.join(jsvm_dir, 'jsvm_output')
        os.makedirs(jsvm_output_dir, exist_ok=True)
        jsvm_output_file = os.path.join(jsvm_output_dir, 'jsvm_lcov_output.info')
        rewriter.rewrite_files(jsvm_files, jsvm_output_file, '')

        grcov_command = [
            os.path.join('tools', 'grcov'),
            '-t', 'coveralls+',
            '-p', prefix,
            'tools', gcov_dir,
            jsvm_output_dir,
            '--filter', 'covered',
            '--token', 'UNUSED',
            '--commit-sha', 'UNUSED'
        ]

        with open('output.json', 'w+') as outfile:
            subprocess.check_call(grcov_command, stdout=outfile)

        with open('tests_report.json') as baseline_rep, open('output.json') as rep:
            baseline_report = json.load(baseline_rep)
            report = json.load(rep)

        filterpaths.ignore_third_party_filter(report)

        # Create diff report
        diff_report = diff.compare_reports(baseline_report, report, True)

        with open('{}/diff.json'.format(data_folder), 'w') as outfile:
            json.dump(diff_report, outfile)

        generatehtml.generate_html(data_folder)

        driver.quit()

        return os.path.abspath(os.path.join(os.getcwd(), '{}/report'.format(data_folder)))
    def parse_coverage_artifacts(self,
                                 gcov_dir,
                                 jsvm_dir,
                                 merge=False,
                                 output_format='lcov'):
        jsvm_output_file = 'jsvm_lcov_output.info'
        grcov_output_file = 'grcov_lcov_output.info'

        dirs = self.query_abs_dirs()

        sys.path.append(dirs['abs_test_install_dir'])
        sys.path.append(
            os.path.join(dirs['abs_test_install_dir'],
                         'mozbuild/codecoverage'))

        from lcov_rewriter import LcovFileRewriter
        jsvm_files = [os.path.join(jsvm_dir, e) for e in os.listdir(jsvm_dir)]
        rewriter = LcovFileRewriter(
            os.path.join(self.grcov_dir, 'chrome-map.json'))
        rewriter.rewrite_files(jsvm_files, jsvm_output_file, '')

        # Zip gcda files (will be given in input to grcov).
        file_path_gcda = os.path.join(os.getcwd(), 'code-coverage-gcda.zip')
        with zipfile.ZipFile(file_path_gcda, 'w', zipfile.ZIP_STORED) as z:
            for topdir, _, files in os.walk(gcov_dir):
                for f in files:
                    full_path = os.path.join(topdir, f)
                    rel_path = os.path.relpath(full_path, gcov_dir)
                    z.write(full_path, rel_path)

        # Run grcov on the zipped .gcno and .gcda files.
        grcov_command = [
            os.path.join(self.grcov_dir, 'grcov'), '-t', output_format, '-p',
            self.prefix, '--ignore-dir', 'gcc*', '--ignore-dir', 'vs2017_*',
            os.path.join(self.grcov_dir,
                         'target.code-coverage-gcno.zip'), file_path_gcda
        ]

        if 'coveralls' in output_format:
            grcov_command += ['--token', 'UNUSED', '--commit-sha', 'UNUSED']

        if merge:
            grcov_command += [jsvm_output_file]

        if mozinfo.os == 'win':
            grcov_command += ['--llvm']

        # 'grcov_output' will be a tuple, the first variable is the path to the lcov output,
        # the other is the path to the standard error output.
        tmp_output_file, _ = self.get_output_from_command(
            grcov_command,
            silent=True,
            save_tmpfiles=True,
            return_type='files',
            throw_exception=True,
        )
        shutil.move(tmp_output_file, grcov_output_file)

        shutil.rmtree(gcov_dir)
        shutil.rmtree(jsvm_dir)

        if merge:
            os.remove(jsvm_output_file)
            return grcov_output_file
        else:
            return grcov_output_file, jsvm_output_file
Esempio n. 5
0
    def _package_coverage_data(self, action, success=None):
        if self.jsd_code_coverage_enabled:
            # Setup the command for compression
            dirs = self.query_abs_dirs()
            jsdcov_dir = dirs['abs_blob_upload_dir']
            zipFile = os.path.join(jsdcov_dir, "jsdcov_artifacts.zip")
            command = ["zip", "-r", "-q", zipFile, ".", "-i", "jscov*.json"]

            self.info("Beginning compression of JSDCov artifacts...")
            self.run_command(command, cwd=jsdcov_dir)

            # Delete already compressed JSCov artifacts.
            for filename in os.listdir(jsdcov_dir):
                if filename.startswith("jscov") and filename.endswith(".json"):
                    os.remove(os.path.join(jsdcov_dir, filename))

            self.info("Completed compression of JSDCov artifacts!")
            self.info("Path to JSDCov compressed artifacts: " + zipFile)

        if not self.code_coverage_enabled:
            return

        del os.environ['GCOV_PREFIX_STRIP']
        del os.environ['GCOV_PREFIX']
        del os.environ['JS_CODE_COVERAGE_OUTPUT_DIR']

        if not self.ccov_upload_disabled:
            dirs = self.query_abs_dirs()

            # Zip gcda files (will be given in input to grcov).
            file_path_gcda = os.path.join(os.getcwd(), 'code-coverage-gcda.zip')
            self.run_command(['zip', '-q', '-0', '-r', file_path_gcda, '.'], cwd=self.gcov_dir)

            sys.path.append(dirs['abs_test_install_dir'])
            sys.path.append(os.path.join(dirs['abs_test_install_dir'], 'mozbuild/codecoverage'))

            # Download the chrome-map.json file from the build machine.
            self.download_file(self.url_to_chrome_map)

            from lcov_rewriter import LcovFileRewriter
            jsvm_files = [os.path.join(self.jsvm_dir, e) for e in os.listdir(self.jsvm_dir)]
            rewriter = LcovFileRewriter('chrome-map.json')
            rewriter.rewrite_files(jsvm_files, 'jsvm_lcov_output.info', '')

            # Package JSVM coverage data.
            file_path_jsvm = os.path.join(dirs['abs_blob_upload_dir'], 'code-coverage-jsvm.zip')
            self.run_command(['zip', '-q', file_path_jsvm, 'jsvm_lcov_output.info'])

            # GRCOV post-processing
            # Download the gcno from the build machine.
            self.download_file(self.url_to_gcno, parent_dir=self.grcov_dir)

            # Run grcov on the zipped .gcno and .gcda files.
            grcov_command = [
                os.path.join(self.grcov_dir, 'grcov'),
                '-t', 'lcov',
                '-p', self.prefix,
                '--ignore-dir', 'gcc',
                os.path.join(self.grcov_dir, 'target.code-coverage-gcno.zip'), file_path_gcda
            ]

            if mozinfo.os == 'win':
                grcov_command += ['--llvm']

            # 'grcov_output' will be a tuple, the first variable is the path to the lcov output,
            # the other is the path to the standard error output.
            grcov_output, _ = self.get_output_from_command(
                grcov_command,
                silent=True,
                save_tmpfiles=True,
                return_type='files',
                throw_exception=True,
            )
            output_file_name = 'grcov_lcov_output.info'
            shutil.move(grcov_output, os.path.join(self.grcov_dir, output_file_name))

            # Zip the grcov output and upload it.
            self.run_command(
                ['zip', '-q', os.path.join(dirs['abs_blob_upload_dir'], 'code-coverage-grcov.zip'), output_file_name],
                cwd=self.grcov_dir
            )

        shutil.rmtree(self.gcov_dir)
        shutil.rmtree(self.jsvm_dir)
        shutil.rmtree(self.grcov_dir)