Skip to content

Tests for report generation #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions report/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
126 changes: 126 additions & 0 deletions report/tests/aggregate_coverage_diff_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import io
import json
import sys
import pytest

import report.aggregate_coverage_diff as aggregate_coverage_diff

# --- Tests for compute_coverage_diff ---

def test_compute_coverage_diff_basic(monkeypatch):
class ExistingTextcov:
def __init__(self):
self.covered_lines = 2
monkeypatch.setattr(
aggregate_coverage_diff.evaluator,
'load_existing_textcov',
lambda project: ExistingTextcov()
)
monkeypatch.setattr(
aggregate_coverage_diff.evaluator,
'load_existing_coverage_summary',
lambda project: {'data': [{'totals': {'lines': {'count': 4}}}]}
)

class DummyTextcov:
def __init__(self):
self.covered_lines = 0
def merge(self, other):
self.covered_lines += other.covered_lines
def subtract_covered_lines(self, existing):
self.covered_lines -= existing.covered_lines
@classmethod
def from_file(cls, f):
inst = cls()
inst.covered_lines = int(f.read())
return inst
monkeypatch.setattr(
aggregate_coverage_diff.textcov,
'Textcov',
DummyTextcov
)

# Fake storage client with two blobs
class FakeBlob:
def __init__(self, name, content):
self.name = name
self._content = content
def open(self):
return io.StringIO(self._content)
class FakeClient:
def bucket(self, name):
return name # bucket identifier passed through
def list_blobs(self, bucket, prefix, delimiter):
# Return two blobs with covered lines 3 and 5
return [FakeBlob('a', '3'), FakeBlob('b', '5')]
monkeypatch.setattr(
aggregate_coverage_diff.storage,
'Client',
FakeClient
)

ratio = aggregate_coverage_diff.compute_coverage_diff('proj', ['gs://bucket/foo'])
assert ratio == pytest.approx(6/4)


def test_compute_coverage_diff_no_totals(monkeypatch):
class ExistingTextcov:
def __init__(self):
self.covered_lines = 0
monkeypatch.setattr(
aggregate_coverage_diff.evaluator,
'load_existing_textcov',
lambda project: ExistingTextcov()
)
monkeypatch.setattr(
aggregate_coverage_diff.evaluator,
'load_existing_coverage_summary',
lambda project: {}
)
class DummyTextcovEmpty:
def __init__(self):
self.covered_lines = 0
def merge(self, other):
pass
def subtract_covered_lines(self, existing):
pass
@classmethod
def from_file(cls, f):
return cls()
monkeypatch.setattr(
aggregate_coverage_diff.textcov,
'Textcov',
DummyTextcovEmpty
)
class FakeClientEmpty:
def bucket(self, name):
return name
def list_blobs(self, bucket, prefix, delimiter):
return []
monkeypatch.setattr(
aggregate_coverage_diff.storage,
'Client',
FakeClientEmpty
)
ratio = aggregate_coverage_diff.compute_coverage_diff('proj', ['gs://bucket/foo'])
assert ratio == 0

# --- Tests for main() ---

def test_main_prints_expected(monkeypatch, capsys):
monkeypatch.setattr(
aggregate_coverage_diff,
'compute_coverage_diff',
lambda project, links: 0.5
)
input_data = {'benchmarks': [
{'benchmark': 'x-proj', 'max_line_coverage_diff_report': 'link1'},
{'benchmark': 'y-proj2'}
]}
monkeypatch.setattr(
sys, 'stdin',
io.StringIO(json.dumps(input_data))
)
aggregate_coverage_diff.main()
out = capsys.readouterr().out.strip()
assert out == "{'proj': 0.5}"
Loading