Beispiel #1
0
    def test_can_write_summary(self):
        source_analyses = (
            analysis.SourceAnalysis("script.sh", "Bash", "some", 200, 25, 1, 2,
                                    analysis.SourceState.analyzed.name, None),
            analysis.SourceAnalysis("some.py", "Python", "some", 300, 45, 3, 4,
                                    analysis.SourceState.analyzed.name, None),
            analysis.SourceAnalysis("other.py", "Python", "some", 500, 30, 5,
                                    6, analysis.SourceState.analyzed.name,
                                    None),
        )
        lines = self._summary_lines_for(source_analyses)
        assert len(lines) == 6, "lines={}".format(lines)

        python_data = _line_data_from(lines[2])
        assert python_data.language == "Python"
        assert python_data.code_count == 800
        assert python_data.documentation_count == 75
        assert python_data.code_percent == pytest.approx(80.0)
        assert python_data.documentation_percent == pytest.approx(75.0)

        bash_data = _line_data_from(lines[3])
        assert bash_data.language == "Bash"
        assert bash_data.code_count == 200
        assert bash_data.documentation_count == 25
        assert bash_data.code_percent == pytest.approx(20.0)
        assert bash_data.documentation_percent == pytest.approx(25.0)

        sum_total_data = lines[-1].split()
        assert len(sum_total_data) >= 3
        assert sum_total_data[-2:] == ["1000", "100"]
Beispiel #2
0
 def test_can_collect_totals(self):
     source_analyses = (
         analysis.SourceAnalysis('some.py', 'Python', 'some', 1, 2, 3, 4),
         analysis.SourceAnalysis('other.py', 'Python', 'some', 10, 20, 30, 40),
     )
     with tempfile.NamedTemporaryFile('w', encoding='utf-8', prefix='pygount_', suffix='.tmp') as target_stream:
         with write.BaseWriter(target_stream) as writer:
             for source_analysis in source_analyses:
                 writer.add(source_analysis)
     self.assertEqual(writer.total_source_count, 2)
     self.assertEqual(writer.total_line_count, 110)
Beispiel #3
0
def test_can_repr_empty_source_analysis_from_file():
    source_analysis = analysis.SourceAnalysis("some.py", "__empty__", "some",
                                              0, 0, 0, 0,
                                              analysis.SourceState.empty)
    expected_source_analysis_repr = "SourceAnalysis(path='some.py', language='__empty__', group='some', state=empty)"
    assert repr(source_analysis) == expected_source_analysis_repr
    assert repr(source_analysis) == str(source_analysis)
Beispiel #4
0
def test_can_collect_totals():
    source_analyses = (
        analysis.SourceAnalysis("some.py", "Python", "some", 1, 2, 3, 4,
                                analysis.SourceState.analyzed.name, None),
        analysis.SourceAnalysis("other.py", "Python", "some", 10, 20, 30, 40,
                                analysis.SourceState.analyzed.name, None),
    )
    with tempfile.NamedTemporaryFile("w",
                                     encoding="utf-8",
                                     prefix="pygount_",
                                     suffix=".tmp") as target_stream:
        with write.BaseWriter(target_stream) as writer:
            for source_analysis in source_analyses:
                writer.add(source_analysis)
    assert writer.total_source_count == 2
    assert writer.total_line_count == 110
Beispiel #5
0
 def test_can_write_cloc_xml(self):
     source_analyses = (
         analysis.SourceAnalysis('some.py', 'Python', 'some', 1, 2, 3, 4),
         analysis.SourceAnalysis('other.py', 'Python', 'some', 10, 20, 30, 40),
     )
     with io.StringIO() as target_stream:
         with write.ClocXmlWriter(target_stream) as writer:
             for source_analysis in source_analyses:
                 writer.add(source_analysis)
         xml_data = target_stream.getvalue()
         self.assertNotEqual(len(xml_data), 0)
     with io.StringIO(xml_data) as cloc_xml_stream:
         cloc_results_root = ElementTree.parse(cloc_xml_stream)
     file_elements = cloc_results_root.findall('files/file')
     self.assertIsNotNone(file_elements)
     self.assertEqual(len(file_elements), len(source_analyses))
Beispiel #6
0
def test_can_use_deprecated_counts():
    source_analysis = analysis.SourceAnalysis("some.py", "Python", "some", 1,
                                              2, 3, 4,
                                              analysis.SourceState.analyzed)
    assert source_analysis.code == source_analysis.code_count
    assert source_analysis.documentation == source_analysis.documentation_count
    assert source_analysis.empty == source_analysis.empty_count
    assert source_analysis.string == source_analysis.string_count
Beispiel #7
0
def test_can_write_cloc_xml():
    source_analyses = (
        analysis.SourceAnalysis("some.py", "Python", "some", 1, 2, 3, 4,
                                analysis.SourceState.analyzed.name, None),
        analysis.SourceAnalysis("other.py", "Python", "some", 10, 20, 30, 40,
                                analysis.SourceState.analyzed.name, None),
    )
    with io.StringIO() as target_stream:
        with write.ClocXmlWriter(target_stream) as writer:
            for source_analysis in source_analyses:
                writer.add(source_analysis)
        xml_data = target_stream.getvalue()
        assert len(xml_data) >= 1
    with io.StringIO(xml_data) as cloc_xml_stream:
        cloc_results_root = ElementTree.parse(cloc_xml_stream)
    file_elements = cloc_results_root.findall("files/file")
    assert file_elements is not None
    assert len(file_elements) == len(source_analyses)
Beispiel #8
0
def test_can_repr_error_source_analysis_from_file():
    source_analysis = analysis.SourceAnalysis("some.py", "__error__", "some",
                                              0, 0, 0, 0,
                                              analysis.SourceState.error,
                                              "error details")
    expected_source_analysis_repr = (
        "SourceAnalysis(path='some.py', language='__error__', group='some', state=error, state_info='error details')"
    )
    assert repr(source_analysis) == expected_source_analysis_repr
    assert repr(source_analysis) == str(source_analysis)
Beispiel #9
0
def test_can_repr_source_analysis_from_file():
    source_analysis = analysis.SourceAnalysis("some.py", "Python", "some", 1,
                                              2, 3, 4,
                                              analysis.SourceState.analyzed)
    expected_source_analysis_repr = (
        "SourceAnalysis(path='some.py', language='Python', group='some', "
        "state=analyzed, code_count=1, documentation_count=2, empty_count=3, string_count=4)"
    )
    assert repr(source_analysis) == expected_source_analysis_repr
    assert repr(source_analysis) == str(source_analysis)
Beispiel #10
0
    def test_can_format_data_longer_than_headings(self):
        huge_number = int("1234567890" * 10)
        long_language_name = "x" * 100
        min_line_length = len(
            long_language_name) + 2 * write.digit_width(huge_number)

        source_analyses = (analysis.SourceAnalysis(
            "x",
            long_language_name,
            "some",
            huge_number,
            huge_number,
            0,
            0,
            analysis.SourceState.analyzed.name,
            None,
        ), )
        for line in self._summary_lines_for(source_analyses):
            assert len(line) > min_line_length, "line={0}".format(line)