| #!/usr/bin/python3 |
| |
| import unittest |
| import changed_lines |
| import lcov_parser |
| import incremental_coverage |
| from io import StringIO |
| import os |
| |
| |
| class IncrementalCoverageTest(unittest.TestCase): |
| |
| |
| def test_normal_output(self): |
| updated_lines = { |
| 'file1.txt': [2,3], |
| 'file2.txt': [4] |
| } |
| |
| covered_lines = { |
| '/dir/file1.txt': {2: True, 3: False}, |
| '/dir/file2.txt': {4: True} |
| } |
| |
| coverage = \ |
| incremental_coverage.coverage(updated_lines, covered_lines, '/dir') |
| |
| self.assertAlmostEqual(coverage, 2/3) |
| |
| def test_no_coverage(self): |
| updated_lines = { |
| 'file1.txt': [9], |
| 'file2.txt': [9] |
| } |
| |
| covered_lines = { |
| '/dir/file1.txt': {9: False}, |
| '/dir/file2.txt': {9: False} |
| } |
| |
| coverage = \ |
| incremental_coverage.coverage(updated_lines, covered_lines, '/dir') |
| |
| self.assertAlmostEqual(coverage, 0.0) |
| |
| def test_full_coverage(self): |
| updated_lines = { |
| 'file1.txt': [13, 23], |
| 'file2.txt': [12] |
| } |
| |
| covered_lines = { |
| '/dir/file1.txt': {13: True, 23: True}, |
| '/dir/file2.txt': {12: True} |
| } |
| |
| coverage = \ |
| incremental_coverage.coverage(updated_lines, covered_lines, '/dir') |
| |
| self.assertAlmostEqual(coverage, 1.0) |
| |
| def test_only_comments(self): |
| updated_lines = { |
| 'file1.txt': [4], |
| 'file2.txt': [3] |
| } |
| |
| covered_lines = { |
| '/dir/file1.txt': {2: True, 3: False}, |
| '/dir/file2.txt': {4: True} |
| } |
| |
| coverage = \ |
| incremental_coverage.coverage(updated_lines, covered_lines, '/dir') |
| |
| self.assertIsNone(coverage) |
| |
| def test_method_integration(self): |
| updated_lines = changed_lines.from_diff(_GIT_DIFF.strip()) |
| |
| with StringIO(_TEST_FILE_DATA.strip()) as test_file: |
| _, covered_lines = lcov_parser.parse(test_file) |
| |
| coverage = \ |
| incremental_coverage.coverage(updated_lines, covered_lines, '/dir') |
| |
| self.assertAlmostEqual(2/3, coverage) |
| |
| |
| _TEST_FILE_DATA = """ |
| TN:kunit_presubmit_tests |
| SF: /dir/file1.txt |
| DA:2,1 |
| DA:3,0 |
| DA:5,1 |
| end_of_record |
| SF: /dir/file2.txt |
| DA:1,1 |
| DA:2,0 |
| DA:4,1 |
| end_of_record |
| """ |
| |
| _GIT_DIFF = """ |
| diff --git a/file1.txt b/file1.txt |
| index 170f11f..041325e 100755 |
| --- a/file1.txt |
| +++ b/file1.txt |
| @@ -1,10 +1,10 @@ |
| line 1 |
| +line 2 |
| +line 3 |
| |
| diff --git a/file2.txt b/file2.txt |
| index 6675c3b..e69b66c 100644 |
| --- a/file2.txt |
| +++ b/file2.txt |
| @@ -1,25 +1,28 @@ |
| line 1 |
| line 2 |
| +# line 3 |
| +line 4 |
| """ |
| |
| |
| if __name__ == '__main__': |
| unittest.main() |