Skip to content

Commit cd4316c

Browse files
committed
Add test cases for global exclusion pre-commit option.
1 parent 4c3b90f commit cd4316c

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

numpydoc/tests/hooks/example_module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ def do_something(self, *args, **kwargs):
2828
def process(self):
2929
"""Process stuff."""
3030
pass
31+
32+
33+
class NewClass:
34+
pass

numpydoc/tests/hooks/test_validate_hook.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def test_validate_hook(example_module, config, capsys):
6767
+-------------------------------------------+-------------------------------------+---------+----------------------------------------------------+
6868
| numpydoc/tests/hooks/example_module.py:28 | example_module.MyClass.process | EX01 | No examples section found |
6969
+-------------------------------------------+-------------------------------------+---------+----------------------------------------------------+
70+
| numpydoc/tests/hooks/example_module.py:33 | example_module.NewClass | GL08 | The object does not have a docstring |
71+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------------------+
7072
"""
7173
)
7274

@@ -102,6 +104,8 @@ def test_validate_hook_with_ignore(example_module, capsys):
102104
| | | | person (e.g. use "Generate" instead of |
103105
| | | | "Generates") |
104106
+-------------------------------------------+-------------------------------------+---------+----------------------------------------------------+
107+
| numpydoc/tests/hooks/example_module.py:33 | example_module.NewClass | GL08 | The object does not have a docstring |
108+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------------------+
105109
"""
106110
)
107111

@@ -144,6 +148,8 @@ def test_validate_hook_with_toml_config(example_module, tmp_path, capsys):
144148
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
145149
| numpydoc/tests/hooks/example_module.py:18 | example_module.MyClass.do_something | PR07 | Parameter "*args" has no description |
146150
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
151+
| numpydoc/tests/hooks/example_module.py:33 | example_module.NewClass | GL08 | The object does not have a docstring |
152+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
147153
"""
148154
)
149155

@@ -181,6 +187,8 @@ def test_validate_hook_with_setup_cfg(example_module, tmp_path, capsys):
181187
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
182188
| numpydoc/tests/hooks/example_module.py:18 | example_module.MyClass.do_something | PR07 | Parameter "*args" has no description |
183189
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
190+
| numpydoc/tests/hooks/example_module.py:33 | example_module.NewClass | GL08 | The object does not have a docstring |
191+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
184192
"""
185193
)
186194

@@ -199,3 +207,84 @@ def test_validate_hook_help(capsys):
199207
out = capsys.readouterr().out
200208
assert "--ignore" in out
201209
assert "--config" in out
210+
211+
212+
def test_validate_hook_exclude_option_pyproject(example_module, tmp_path, capsys):
213+
"""
214+
Test that a file is correctly processed with the config coming from
215+
a pyproject.toml file and exclusions provided.
216+
"""
217+
218+
with open(tmp_path / "pyproject.toml", "w") as config_file:
219+
config_file.write(
220+
inspect.cleandoc(
221+
r"""
222+
[tool.numpydoc_validation]
223+
checks = [
224+
"all",
225+
"EX01",
226+
"SA01",
227+
"ES01",
228+
]
229+
override_SS05 = '^((Process|Assess|Access) )'
230+
exclude = [
231+
'\.do_something$',
232+
'\.__init__$',
233+
]
234+
"""
235+
)
236+
)
237+
238+
expected = inspect.cleandoc(
239+
"""
240+
+-------------------------------------------+------------------------------+---------+--------------------------------------+
241+
| file | item | check | description |
242+
+===========================================+==============================+=========+======================================+
243+
| numpydoc/tests/hooks/example_module.py:4 | example_module.some_function | PR01 | Parameters {'name'} not documented |
244+
+-------------------------------------------+------------------------------+---------+--------------------------------------+
245+
| numpydoc/tests/hooks/example_module.py:33 | example_module.NewClass | GL08 | The object does not have a docstring |
246+
+-------------------------------------------+------------------------------+---------+--------------------------------------+
247+
"""
248+
)
249+
250+
return_code = main([example_module, "--config", str(tmp_path)])
251+
assert return_code == 1
252+
assert capsys.readouterr().err.rstrip() == expected
253+
254+
255+
def test_validate_hook_exclude_option_setup_cfg(example_module, tmp_path, capsys):
256+
"""
257+
Test that a file is correctly processed with the config coming from
258+
a setup.cfg file and exclusions provided.
259+
"""
260+
261+
with open(tmp_path / "setup.cfg", "w") as config_file:
262+
config_file.write(
263+
inspect.cleandoc(
264+
"""
265+
[tool:numpydoc_validation]
266+
checks = all,EX01,SA01,ES01
267+
override_SS05 = ^((Process|Assess|Access) )
268+
override_GL08 = ^(__init__)$
269+
exclude = \\.NewClass$,
270+
"""
271+
)
272+
)
273+
274+
expected = inspect.cleandoc(
275+
"""
276+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
277+
| file | item | check | description |
278+
+===========================================+=====================================+=========+========================================+
279+
| numpydoc/tests/hooks/example_module.py:4 | example_module.some_function | PR01 | Parameters {'name'} not documented |
280+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
281+
| numpydoc/tests/hooks/example_module.py:18 | example_module.MyClass.do_something | PR01 | Parameters {'**kwargs'} not documented |
282+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
283+
| numpydoc/tests/hooks/example_module.py:18 | example_module.MyClass.do_something | PR07 | Parameter "*args" has no description |
284+
+-------------------------------------------+-------------------------------------+---------+----------------------------------------+
285+
"""
286+
)
287+
288+
return_code = main([example_module, "--config", str(tmp_path)])
289+
assert return_code == 1
290+
assert capsys.readouterr().err.rstrip() == expected

0 commit comments

Comments
 (0)