Skip to content

Commit fa09a2c

Browse files
authored
Fix acknowledge when run on multiple files (#244)
* write a failing test * make it pass * format
1 parent bf81954 commit fa09a2c

File tree

6 files changed

+170
-7
lines changed

6 files changed

+170
-7
lines changed

ni_python_styleguide/_acknowledge_existing_errors/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def acknowledge_lint_errors(
8989
exclude=exclude,
9090
app_import_names=app_import_names,
9191
extend_ignore=extend_ignore,
92-
file_or_dir=file_or_dir,
92+
file_or_dir=[bad_file],
9393
excluded_errors=EXCLUDED_ERRORS,
9494
)
9595

@@ -128,7 +128,7 @@ def _handle_emergent_violations(exclude, app_import_names, extend_ignore, file_o
128128
exclude=exclude,
129129
app_import_names=app_import_names,
130130
extend_ignore=extend_ignore,
131-
file_or_dir=file_or_dir,
131+
file_or_dir=[bad_file],
132132
excluded_errors=EXCLUDED_ERRORS,
133133
)
134134
)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ line-length = 100
6868

6969
[tool.pytest.ini_options]
7070
addopts = "--doctest-modules"
71-
norecursedirs = "*__snapshots"
71+
norecursedirs = "*__snapshots tests/*/input/*"
7272

7373

7474
[tool.ni-python-styleguide]
75-
extend_exclude = "*__snapshots/*/*input.py"
75+
extend_exclude = "*__snapshots/*/*input.py,tests/*/input/*.py"
7676

7777

7878
[build-system]

tests/test_cli/acknowledge_existing_errors_multiple_files/input/acknowledge_blank_file.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def method1():
2+
return 7
3+
4+
5+
def method2():
6+
"""Provide an examples of doc strings that are too long.
7+
8+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
9+
"""
10+
return 7 # Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
11+
12+
13+
class Foo:
14+
def __init__(self):
15+
pass
16+
17+
def add(self, o):
18+
"""Provide an examples of doc strings that are too long.
19+
20+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
21+
"""
22+
return 7 # Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
23+
24+
25+
class _PrivateFoo:
26+
def __init__(self):
27+
pass
28+
29+
def add(self, o):
30+
"""Provide an examples of doc strings that are too long.
31+
32+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
33+
"""
34+
return 7 # Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""example of a python file with linter errors.
2+
"""
3+
4+
import os
5+
6+
os.listdir()
7+
8+
9+
def method_with_shadow_builtin(input):
10+
"""Shadow a builtin."""
11+
return input
12+
13+
14+
def method_with_shadow_import(os):
15+
"""Shadow an import."""
16+
return os
17+
18+
19+
def method_with_shadow_import_on_multiple_lines(
20+
x,
21+
y,
22+
os,
23+
):
24+
"""Shadow an import."""
25+
return os
26+
27+
28+
def method_with_unused_param(unused_input):
29+
"""Provide and unused param."""
30+
return 5
31+
32+
33+
def method_with_parameters_on_multiple_lines(x, y):
34+
"""Provide parameters on multiple lines test case."""
35+
return x + y
36+
37+
38+
def method_with_bad_names_on_single_line(myBadlyNamedParam, my_other_Bad_name):
39+
"""Provide parameters with bad names on single line."""
40+
return myBadlyNamedParam + my_other_Bad_name
41+
42+
43+
def method_with_bad_names_on_multiple_lines_1(
44+
myBadlyNamedParam,
45+
):
46+
"""Provide parameters with bad names on multiple lines."""
47+
return myBadlyNamedParam + 5
48+
49+
50+
def method_with_bad_names_on_multiple_lines_2(
51+
myBadlyNamedParam,
52+
my_other_Bad_name,
53+
):
54+
"""Provide parameters with bad names on multiple lines."""
55+
return myBadlyNamedParam + my_other_Bad_name
56+
57+
58+
def method_withBadName_with_shadow(input):
59+
"""Shadow a builtin."""
60+
return input
61+
62+
63+
def method_withBadName_with_unused_param(unused_input):
64+
"""Provide and unused param."""
65+
return 5
66+
67+
68+
def method_withBadName_with_parameters_on_multiple_lines(x, y):
69+
"""Provide parameters on multiple lines test case."""
70+
return x + y
71+
72+
73+
def method_withBadName_with_bad_params_on_single_line(myBadlyNamedParam, my_other_Bad_name):
74+
"""Provide parameters with bad names on single line."""
75+
return myBadlyNamedParam + my_other_Bad_name
76+
77+
78+
def method_withBadName_with_bad_params_on_multiple_lines_1(
79+
myBadlyNamedParam,
80+
):
81+
"""Provide parameters with bad names on multiple lines."""
82+
return myBadlyNamedParam + 5
83+
84+
85+
def method_withBadName_with_bad_params_on_multiple_lines_2(
86+
myBadlyNamedParam,
87+
my_other_Bad_name,
88+
):
89+
"""Provide parameters with bad names on multiple lines."""
90+
return myBadlyNamedParam + my_other_Bad_name
91+
92+
93+
def method_withBadName_andParams(my_normal_param, myBadlyNamedParam, my_other_Bad_param):
94+
"""Provide example where black will want to split out result."""
95+
return 5 + 7
96+
97+
98+
def method_withBadName_and_bad_param_with_long_name(
99+
my_normal_param, myBadlyNamedParam, my_other_Bad_param
100+
):
101+
"""Provide example where black will want to split out result even more"""
102+
return 5 + 7

tests/test_cli/test_acknowledge_existing_errors.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
import pytest
77

8-
TEST_CASE_DIR = (
9-
pathlib.Path(__file__).parent.absolute() / "acknowledge_existing_errors_test_cases__snapshots"
10-
)
8+
MODULE_DIR = pathlib.Path(__file__).parent.absolute()
9+
TEST_CASE_DIR = MODULE_DIR / "acknowledge_existing_errors_test_cases__snapshots"
1110

1211

1312
@pytest.mark.parametrize(
@@ -73,3 +72,31 @@ def test_given_suppressed_file_linter_does_not_error(
7372
output = styleguide_command(command="lint", command_args=[test_file, *additional_args])
7473

7574
assert output.exit_code in (True, 0), f"Error in running:\n{output.output}\n\n"
75+
76+
77+
@pytest.mark.parametrize("cmd_args", [[], ["--aggressive"]], ids=["normal", "aggressive"])
78+
def test_given_folder_with_multiple_files_linter_does_not_error(
79+
cmd_args, tmp_path, styleguide_command, chdir
80+
):
81+
in_dir = MODULE_DIR / "acknowledge_existing_errors_multiple_files" / "input"
82+
test_dir = tmp_path / "input"
83+
shutil.copytree(in_dir, test_dir)
84+
chdir(tmp_path)
85+
86+
output = styleguide_command(command="acknowledge-existing-violations", command_args=cmd_args)
87+
88+
assert output.exit_code in (True, 0), f"Error in running:\n{output}"
89+
90+
91+
def test_given_folder_with_multiple_files_acknowledged__does_not_error(
92+
tmp_path, styleguide_command, chdir
93+
):
94+
in_dir = MODULE_DIR / "acknowledge_existing_errors_multiple_files" / "input"
95+
test_dir = tmp_path / "input"
96+
shutil.copytree(in_dir, test_dir)
97+
chdir(tmp_path)
98+
styleguide_command(command="acknowledge-existing-violations", command_args=["--aggressive"])
99+
100+
output = styleguide_command(command="lint", command_args=[])
101+
102+
assert output.exit_code in (True, 0), f"Error in running:\n{output.output}\n\n"

0 commit comments

Comments
 (0)