Ejemplo n.º 1
0
def test_output_formatter_get_table_formatter(table_type, mocker, monkeypatch):
    """The returned table formatter uses the correct table type."""
    mock_table_format = mocker.Mock()
    monkeypatch.setattr(utils.tabulate, "TableFormat", mock_table_format)
    mock_functools_partial = mocker.Mock()

    # functools.partial does not have an __eq__ method so any comparison
    # between two partial functions will use id() and not actual equality.
    # To work around this, we mock functools.partial. But it isn't as
    # easy as it is with other monkeypatching:
    # https://github.com/pytest-dev/pytest/pull/3382
    # However, this fails on py3.8
    with monkeypatch.context() as m:
        m.setattr(utils.functools, "partial", mock_functools_partial)

        conf = config.InterrogateConfig()
        formatter = utils.OutputFormatter(conf)
        formatter.get_table_formatter(table_type=table_type)

    mock_table_format.assert_called_once_with(
        lineabove=None,
        linebelowheader=None,
        linebetweenrows=None,
        linebelow=None,
        headerrow=mock_functools_partial.return_value,
        datarow=mock_functools_partial.return_value,
        padding=1,
        with_header_hide=None,
    )
    mock_functools_partial.assert_called_once_with(
        formatter._interrogate_line_formatter, table_type=table_type)
Ejemplo n.º 2
0
def test_output_formatter_set_summary_markup(has_markup, padded_cells,
                                             expected_cells, monkeypatch):
    """Summary info is marked up with expected esc codes."""
    conf = config.InterrogateConfig()
    formatter = utils.OutputFormatter(conf)
    monkeypatch.setattr(formatter, "should_markup", lambda: has_markup)

    actual_cells = formatter.set_summary_markup(padded_cells)

    assert expected_cells == actual_cells
Ejemplo n.º 3
0
def test_output_formatter_get_table_formatter_py38(table_type, mocker,
                                                   monkeypatch):
    """The returned table formatter uses the correct table type."""
    mock_table_format = mocker.Mock()
    monkeypatch.setattr(utils.tabulate, "TableFormat", mock_table_format)

    conf = config.InterrogateConfig()
    formatter = utils.OutputFormatter(conf)
    formatter.get_table_formatter(table_type=table_type)

    mock_table_format.assert_called_once()
Ejemplo n.º 4
0
def test_output_formatter_should_markup(color_conf, envvar, hasmarkup,
                                        expected, monkeypatch):
    """Expect markup unless configured (envvar or config) otherwise."""
    conf = config.InterrogateConfig(color=color_conf)
    formatter = utils.OutputFormatter(conf)
    if hasmarkup:
        monkeypatch.setattr(formatter.tw, "hasmarkup", hasmarkup)
    if envvar:
        monkeypatch.setenv("INTERROGATE_COLOR", envvar)

    assert expected == formatter.should_markup()
Ejemplo n.º 5
0
    def print_results(self, results, output, verbosity):
        """Print results to a given output stream.

        :param InterrogateResults results: results of docstring coverage
            interrogation.
        :param output: filename to output results. If ``None``, uses
            ``sys.stdout``.
        :type output: ``str`` or ``None``
        :param int verbosity: level of detail to print out (``0``-``2``).
        """
        with utils.smart_open(output, "w") as f:
            self.output_formatter = utils.OutputFormatter(
                file=f, config=self.config
            )
            results = self._sort_results(results)
            if verbosity > 0:
                base = self._get_header_base()
                self.output_formatter.tw.sep(
                    "=",
                    "Coverage for {}".format(base),
                    fullwidth=self.output_formatter.TERMINAL_WIDTH,
                )
            if verbosity > 1:
                self._print_detailed_table(results)
            if verbosity > 0:
                self._print_summary_table(results)

            status, color = "PASSED", {"green": True}
            if results.ret_code > 0:
                status, color = "FAILED", {"red": True}

            if self.output_formatter.should_markup() is False:
                color = {}

            status_line = "RESULT: {} (minimum: {}%, actual: {:.1f}%)".format(
                status, self.config.fail_under, results.perc_covered
            )
            if verbosity > 0:
                self.output_formatter.tw.sep(
                    "-",
                    title=status_line,
                    fullwidth=self.output_formatter.TERMINAL_WIDTH,
                    **color
                )
            else:
                self.output_formatter.tw.line(status_line)
Ejemplo n.º 6
0
def test_output_formatter_interrogate_line_formatter_windows(
    table_type,
    padded_cells,
    colwidths,
    colaligns,
    width,
    expected,
    monkeypatch,
):
    """Data is padded and aligned correctly to fit the terminal width."""
    conf = config.InterrogateConfig(color=False)
    formatter = utils.OutputFormatter(conf)
    monkeypatch.setattr(formatter, "TERMINAL_WIDTH", width)

    actual = formatter._interrogate_line_formatter(padded_cells,
                                                   colwidths,
                                                   colaligns,
                                                   table_type=table_type)

    assert width - 1 == len(actual)
    assert expected == actual
Ejemplo n.º 7
0
def test_output_formatter_get_table_formatter_raises():
    """Raise if received an table type other than 'detailed' or 'summary'."""
    conf = config.InterrogateConfig()
    formatter = utils.OutputFormatter(conf)
    with pytest.raises(AssertionError):
        formatter.get_table_formatter(table_type="not_a_type")