Beispiel #1
0
def test_get_overall_coverage(mock_covdir_report):
    """
    Test covdir report overall coverage extraction
    """
    from code_coverage_backend import covdir

    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    out = covdir.get_overall_coverage(report, max_depth=1)
    assert out == {
        "": 85.11,
        "builtin": 84.4,
        "ctypes": 80.83,
        "frontend": 78.51,
        "perf": 65.45,
        "shell": 69.95,
        "threading": 90.54,
        "util": 73.29,
    }

    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    out = covdir.get_overall_coverage(report, max_depth=2)
    assert out == {
        "": 85.11,
        "builtin": 84.4,
        "builtin/intl": 78.62,
        "ctypes": 80.83,
        "ctypes/libffi": 49.59,
        "frontend": 78.51,
        "perf": 65.45,
        "shell": 69.95,
        "threading": 90.54,
        "util": 73.29,
    }
Beispiel #2
0
def test_open_report(tmpdir, mock_covdir_report):
    """
    Test opening reports
    """
    from code_coverage_backend import covdir

    empty = tmpdir.join("empty.json")
    assert covdir.open_report(empty.realpath()) is None

    bad = tmpdir.join("bad.json")
    bad.write("not json")
    assert covdir.open_report(bad.realpath()) is None

    invalid = tmpdir.join("invalid.json")
    invalid.write('"string"')
    assert covdir.open_report(invalid.realpath()) is None

    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    assert isinstance(report, dict)

    assert list(report.keys()) == [
        "children",
        "coveragePercent",
        "linesCovered",
        "linesMissed",
        "linesTotal",
        "name",
    ]
Beispiel #3
0
    def ingest_report(self, repository, push_id, changeset, date):
        """
        When a report exist for a changeset, download it and update redis data
        """
        assert isinstance(push_id, int)
        assert isinstance(date, int)

        # Download the report
        report_path = self.download_report(repository, changeset)
        if not report_path:
            return False

        # Read overall coverage for history
        key = KEY_OVERALL_COVERAGE.format(repository=repository,
                                          changeset=changeset)
        report = covdir.open_report(report_path)
        assert report is not None, "No report to ingest"
        overall_coverage = covdir.get_overall_coverage(report)
        assert len(overall_coverage) > 0, "No overall coverage"
        self.redis.hmset(key, overall_coverage)

        # Add the changeset to the sorted sets of known reports
        # The numeric push_id is used as a score to keep the ingested
        # changesets ordered
        self.redis.zadd(KEY_REPORTS.format(repository=repository),
                        {changeset: push_id})

        # Add the changeset to the sorted sets of known reports by date
        self.redis.zadd(KEY_HISTORY.format(repository=repository),
                        {changeset: date})

        logger.info("Ingested report", changeset=changeset)
        return True
Beispiel #4
0
    def get_coverage(self, report, path):
        """
        Load a report and its coverage for a specific path
        and build a serializable representation
        """
        assert isinstance(report, Report)
        data = covdir.open_report(report.path)
        if data is None:
            # Try to download the report if it's missing locally
            assert self.download_report(report), "Missing report {}".format(report)

            data = covdir.open_report(report.path)
            assert data

        out = covdir.get_path_coverage(data, path)
        out["changeset"] = report.changeset
        return out
Beispiel #5
0
    def get_coverage(self, report: Report, path: str) -> dict:
        """
        Load a report and its coverage for a specific path
        and build a serializable representation
        """
        data = covdir.open_report(report.path)
        if data is None:
            # Try to download the report if it's missing locally
            assert download_report(
                self.reports_dir, self.bucket,
                report.name), "Missing report {}".format(report)

            data = covdir.open_report(report.path)
            assert data

        out = covdir.get_path_coverage(data, path)
        out["changeset"] = report.changeset
        return out
Beispiel #6
0
    def get_coverage(self, repository, changeset, path):
        """
        Load a report and its coverage for a specific path
        and build a serializable representation
        """
        report_path = os.path.join(self.reports_dir,
                                   "{}/{}.json".format(repository, changeset))

        report = covdir.open_report(report_path)
        if report is None:
            # Try to download the report if it's missing locally
            report_path = self.download_report(repository, changeset)
            assert report_path is not False, "Missing report for {} at {}".format(
                repository, changeset)

            report = covdir.open_report(report_path)
            assert report

        out = covdir.get_path_coverage(report, path)
        out["changeset"] = changeset
        return out
Beispiel #7
0
    def ingest_report(self, report):
        """
        When a report exist for a changeset, download it and update redis data
        """
        assert isinstance(report, Report)

        # Download the report
        if not self.download_report(report):
            logger.info("Report not available", report=str(report))
            return False

        # Read overall coverage for history
        data = covdir.open_report(report.path)
        assert data is not None, "No report to ingest"
        overall_coverage = covdir.get_overall_coverage(data)
        assert len(overall_coverage) > 0, "No overall coverage"
        self.redis.hmset(report.key_overall, overall_coverage)

        # Apply expiry for overall report
        if report.ttl is not None:
            self.redis.expire(report.key_overall, report.ttl)

        # Add the changeset to the sorted sets of known reports
        # The numeric push_id is used as a score to keep the ingested
        # changesets ordered
        self.redis.zadd(
            KEY_REPORTS.format(
                repository=report.repository,
                platform=report.platform,
                suite=report.suite,
            ),
            {report.changeset: report.push_id},
        )

        # Add the changeset to the sorted sets of known reports by date
        self.redis.zadd(
            KEY_HISTORY.format(repository=report.repository),
            {report.changeset: report.date},
        )

        # Store the filters
        if report.platform != DEFAULT_FILTER:
            self.redis.sadd(KEY_PLATFORMS.format(repository=report.repository),
                            report.platform)
        if report.suite != DEFAULT_FILTER:
            self.redis.sadd(KEY_SUITES.format(repository=report.repository),
                            report.suite)

        logger.info("Ingested report", report=str(report))
        return True
Beispiel #8
0
def test_get_path_coverage(mock_covdir_report):
    """
    Test covdir report parsing to obtain coverage for a specific path
    """
    from code_coverage_backend import covdir

    # Full coverage
    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    out = covdir.get_path_coverage(report, "")
    assert isinstance(out, dict)
    assert out["coveragePercent"] == 85.11
    assert out["linesCovered"] == 267432
    assert out["linesMissed"] == 46779
    assert out["linesTotal"] == 314211
    assert out["name"] == "src"
    assert out["path"] == ""
    assert out["type"] == "directory"
    assert len(out["children"]) == 12
    assert [c["name"] for c in out["children"]] == [
        "builtin",
        "ctypes",
        "frontend",
        "jsapi.cpp",
        "jsdate.cpp",
        "jsexn.cpp",
        "jsexn.h",
        "jsmath.cpp",
        "perf",
        "shell",
        "threading",
        "util",
    ]

    # Subfolder
    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    out = covdir.get_path_coverage(report, "perf")
    assert isinstance(out, dict)
    assert out["coveragePercent"] == 65.45
    assert out["linesCovered"] == 125
    assert out["linesMissed"] == 66
    assert out["linesTotal"] == 191
    assert out["name"] == "perf"
    assert out["path"] == "perf"
    assert out["type"] == "directory"
    assert len(out["children"]) == 2
    assert [c["name"]
            for c in out["children"]] == ["pm_linux.cpp", "pm_stub.cpp"]

    # File
    report = covdir.open_report(mock_covdir_report)
    assert report is not None
    out = covdir.get_path_coverage(report, "perf/pm_linux.cpp")
    assert isinstance(out, dict)
    assert out == {
        "children": None,
        "coverage": [66, 138, 6, -1, -1],
        "coveragePercent": 81.69,
        "linesCovered": 58,
        "linesMissed": 13,
        "linesTotal": 71,
        "name": "pm_linux.cpp",
        "path": "perf/pm_linux.cpp",
        "type": "file",
    }

    # Missing file
    with pytest.raises(Exception) as e:
        report = covdir.open_report(mock_covdir_report)
        assert report is not None
        covdir.get_path_coverage(report, "nope.py")
    assert str(e.value) == "Path nope.py not found in report"