def test_report_no_no_added_lines(): module = cover.SourceModule('path/foo.py', []) module.have_report = True assert module.report() == 'path/foo.py (No added/changed lines)\n' lines = [ cover.SourceLine(10, is_context=True), cover.SourceLine(12, is_context=True) ] module = cover.SourceModule('deleted_lines_file', lines) module.have_report = True assert module.report() == 'deleted_lines_file (No added/changed lines)\n'
def test_coverage_status_collection(): coverage_info = """ ... <title>Coverage for foo.py: 81%</title> ... <p id="n63" class="pln"><a href="#n63">63</a></p> <p id="n64" class="stm par run hide_run"><a href="#n64">64</a></p> <p id="n65" class="stm mis"><a href="#n65">65</a></p> <p id="n66" class="stm run hide_run"><a href="#n66">66</a></p> ... </td> <td class="text"> """.splitlines() non_executable_line = cover.SourceLine(63) partial_covered_line = cover.SourceLine(64, is_context=False) missing_line = cover.SourceLine(65, is_context=False) covered_line = cover.SourceLine(66, is_context=False) module = cover.SourceModule('foo.py', [ non_executable_line, partial_covered_line, missing_line, covered_line ]) cover.check_coverage_status(coverage_info, module) assert non_executable_line.status == ' ' assert partial_covered_line.status == 'par' assert missing_line.status == 'mis' assert covered_line.status == 'run' assert module.coverage == '81%'
def test_checking_coverage_file(monkeypatch): coverage_info = """ ... <title>Coverage for foo.py: 5%</title> ... <p id="n2" class="stm run"><a href="#n2">2</a></p> ... </td> <td class="text"> """.splitlines() line = cover.SourceLine(2, is_context=False) module = cover.SourceModule('foo.py', [line]) def is_a_file(filename): return True monkeypatch.setattr(os.path, 'isfile', is_a_file) with mock.patch.object(builtins, 'open', mock.mock_open(), create=True) as mock_open: mock_open.return_value.readlines.return_value = coverage_info cover.check_coverage_file('.', module) assert module.have_report assert line.status == 'run' assert module.coverage == '5%'
def test_check_coverage_no_lines(): coverage_info = """ <p id="n1" class="pln"><a href="#n1">1</a></p> <p id="n2" class="pln"><a href="#n2">2</a></p> <p id="n3" class="pln"><a href="#n3">3</a></p> """.splitlines() module = cover.SourceModule('foo.py', []) cover.check_coverage_status(coverage_info, module) assert module.lines == []
def test_report_one_line(): line = cover.SourceLine(10, is_context=False, code=' x = 1') module = cover.SourceModule('path/foo.py', [line]) module.have_report = True module.coverage = '100%' line.status = 'run' expected = """path/foo.py (run=1, mis=0, par=0, ign=0) 100% 10 run + x = 1 """ assert module.report() == expected
def test_update_status_line(): line = cover.SourceLine(10, is_context=False) module = cover.SourceModule('foo', [line]) module.update_line_status(10, 'pln') assert line.status == ' ' module.update_line_status(10, 'stm run') assert line.status == 'run' module.update_line_status(10, 'stm mis') assert line.status == 'mis' module.update_line_status(10, 'stm par') assert line.status == 'par'
def test_report_multiple_blocks(): lines = [ cover.SourceLine(10, is_context=False, code='x = 1'), cover.SourceLine(11, is_context=False, code='y = 2'), cover.SourceLine(20, is_context=False, code='z = 3'), cover.SourceLine(21, is_context=False, code='for i in [1, 2]') ] lines[0].status = 'run' lines[1].status = 'mis' lines[2].status = 'par' lines[3].status = ' ' module = cover.SourceModule('path/foo.py', lines) module.have_report = True module.coverage = '50%' expected = """path/foo.py (run=1, mis=1, par=1, ign=1) 50% 10 run + x = 1 11 mis + y = 2 20 par + z = 3 21 + for i in [1, 2] """ assert module.report() == expected
def test_report_non_coverage_files(): module = cover.SourceModule('path/foo.py', []) assert module.report() == 'path/foo.py (No coverage data)\n'
def test_missing_coverage_file(fake_cover_project): module = cover.SourceModule('foo.py', []) cover.check_coverage_file(fake_cover_project, module) assert not module.have_report
def test_fail_update_status_no_matching_line(): line = cover.SourceLine(10, is_context=False) module = cover.SourceModule('foo', [line]) module.update_line_status(12, 'stm run') assert line.status == '???'
def test_determine_coverage_file_name(): filename = "relative/path/to/file.py" module = cover.SourceModule(filename, lines=[]) assert module.cover_file == 'relative_path_to_file_py.html'