summaryrefslogtreecommitdiff
path: root/tests/unit/test_filenameonly_formatter.py
blob: 7dda50b4843ae1a0fe05782562a8bd87a7b687ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Tests for the FilenameOnly formatter object."""
import argparse

from flake8 import style_guide
from flake8.formatting import default


def options(**kwargs):
    """Create an argparse.Namespace instance."""
    kwargs.setdefault("output_file", None)
    kwargs.setdefault("tee", False)
    return argparse.Namespace(**kwargs)


def test_caches_filenames_already_printed():
    """Verify we cache filenames when we format them."""
    formatter = default.FilenameOnly(options())
    assert formatter.filenames_already_printed == set()

    formatter.format(
        style_guide.Violation("code", "file.py", 1, 1, "text", "l")
    )
    assert formatter.filenames_already_printed == {"file.py"}


def test_only_returns_a_string_once_from_format():
    """Verify format ignores the second error with the same filename."""
    formatter = default.FilenameOnly(options())
    error = style_guide.Violation("code", "file.py", 1, 1, "text", "1")

    assert formatter.format(error) == "file.py"
    assert formatter.format(error) is None


def test_show_source_returns_nothing():
    """Verify show_source returns nothing."""
    formatter = default.FilenameOnly(options())
    error = style_guide.Violation("code", "file.py", 1, 1, "text", "1")

    assert formatter.show_source(error) is None