|
1 |
| -from __future__ import print_function |
2 |
| - |
3 |
| -from contextlib import contextmanager |
4 |
| -import os |
5 | 1 | import sys
|
6 |
| -import tempfile |
7 |
| -try: |
8 |
| - from StringIO import StringIO |
9 |
| -except ImportError: |
10 |
| - from io import StringIO |
| 2 | +import io |
| 3 | +import pytest |
| 4 | +import numpydoc |
| 5 | +import numpydoc.__main__ |
11 | 6 |
|
12 |
| -from numpydoc.__main__ import main |
13 | 7 |
|
| 8 | +def _capture_stdout(func_name, *args, **kwargs): |
| 9 | + """ |
| 10 | + Return stdout of calling `func_name`. |
14 | 11 |
|
15 |
| -PACKAGE_CODE = """ |
16 |
| -'''This package has test stuff''' |
17 |
| -""" |
| 12 | + This docstring should be perfect, as it is used to test the |
| 13 | + validation with a docstring without errors. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + func_name : callable |
| 18 | + Function to be called. |
| 19 | + *args, **kwargs |
| 20 | + Will be passed to `func_name`. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + str |
| 25 | + The content that the function printed. |
| 26 | +
|
| 27 | + See Also |
| 28 | + -------- |
| 29 | + sys.stdout : Python's file handler for stdout. |
| 30 | +
|
| 31 | + Examples |
| 32 | + -------- |
| 33 | + >>> _capture_stdout(print, 'hello world') |
| 34 | + 'hello world' |
| 35 | + """ |
| 36 | + f = io.StringIO() |
| 37 | + sys.stdout, old_stdout = f, sys.stdout |
| 38 | + try: |
| 39 | + func_name(*args, **kwargs) |
| 40 | + return f.getvalue().strip('\n\r') |
| 41 | + finally: |
| 42 | + sys.stdout = old_stdout |
18 | 43 |
|
19 |
| -MODULE_CODE = """ |
20 |
| -'''This module has test stuff''' |
21 | 44 |
|
22 |
| -def foo(a, b=5): |
23 |
| - '''Hello world |
| 45 | +def _docstring_with_errors(): |
| 46 | + """ |
| 47 | + this docstring should report some errors |
24 | 48 |
|
25 | 49 | Parameters
|
26 | 50 | ----------
|
27 |
| - something : foo |
28 |
| - bar |
29 |
| - something_else |
30 |
| - bar |
31 |
| - ''' |
32 |
| -""" |
| 51 | + made_up_param : str |
| 52 | + """ |
| 53 | + pass |
33 | 54 |
|
34 | 55 |
|
35 |
| -@contextmanager |
36 |
| -def _mock_module(pkg_name): |
37 |
| - try: |
38 |
| - tempdir = tempfile.mkdtemp() |
39 |
| - os.mkdir(os.path.join(tempdir, pkg_name)) |
40 |
| - with open(os.path.join(tempdir, pkg_name, '__init__.py'), 'w') as f: |
41 |
| - print(PACKAGE_CODE, file=f) |
42 |
| - with open(os.path.join(tempdir, pkg_name, 'module.py'), 'w') as f: |
43 |
| - print(MODULE_CODE, file=f) |
44 |
| - |
45 |
| - sys.path.insert(0, tempdir) |
46 |
| - yield tempdir |
47 |
| - finally: |
48 |
| - try: |
49 |
| - os.path.rmdir(tempdir) |
50 |
| - sys.path.remove(tempdir) |
51 |
| - except: |
52 |
| - pass |
| 56 | +def _invalid_docstring(): |
| 57 | + """ |
| 58 | + This docstring should break the parsing. |
53 | 59 |
|
| 60 | + See Also |
| 61 | + -------- |
| 62 | + : this is invalid |
| 63 | + """ |
| 64 | + pass |
54 | 65 |
|
55 |
| -def _capture_main(*args): |
56 |
| - f = StringIO() |
57 |
| - sys.stdout, old_stdout = f, sys.stdout |
58 |
| - try: |
59 |
| - main(args) |
60 |
| - return f.getvalue().strip('\n\r') |
61 |
| - finally: |
62 |
| - sys.stdout = old_stdout |
63 | 66 |
|
| 67 | +def test_renders_package_docstring(): |
| 68 | + out = _capture_stdout(numpydoc.__main__.render_object, |
| 69 | + 'numpydoc') |
| 70 | + assert out.startswith('This package provides the numpydoc Sphinx') |
| 71 | + |
| 72 | + |
| 73 | +def test_renders_module_docstring(): |
| 74 | + out = _capture_stdout(numpydoc.__main__.render_object, |
| 75 | + 'numpydoc.__main__') |
| 76 | + assert out.startswith('Implementing `python -m numpydoc` functionality.') |
| 77 | + |
| 78 | + |
| 79 | +def test_renders_function_docstring(): |
| 80 | + out = _capture_stdout(numpydoc.__main__.render_object, |
| 81 | + 'numpydoc.tests.test_main._capture_stdout') |
| 82 | + assert out.startswith('Return stdout of calling') |
| 83 | + |
| 84 | + |
| 85 | +def test_render_object_returns_correct_exit_status(): |
| 86 | + exit_status = numpydoc.__main__.render_object( |
| 87 | + 'numpydoc.tests.test_main._capture_stdout') |
| 88 | + assert exit_status == 0 |
| 89 | + |
| 90 | + with pytest.raises(numpydoc.docscrape.ParseError): |
| 91 | + numpydoc.__main__.render_object( |
| 92 | + 'numpydoc.tests.test_main._invalid_docstring') |
| 93 | + |
| 94 | + |
| 95 | +def test_validate_detects_errors(): |
| 96 | + out = _capture_stdout(numpydoc.__main__.validate_object, |
| 97 | + 'numpydoc.tests.test_main._docstring_with_errors') |
| 98 | + assert 'SS02' in out |
| 99 | + assert 'Summary does not start with a capital letter' in out |
| 100 | + |
| 101 | + exit_status = numpydoc.__main__.validate_object( |
| 102 | + 'numpydoc.tests.test_main._docstring_with_errors') |
| 103 | + assert exit_status > 0 |
64 | 104 |
|
65 |
| -def test_main(): |
66 |
| - # TODO: does not currently check that numpydoc transformations are applied |
67 | 105 |
|
68 |
| - assert (_capture_main('numpydoc.__main__.main') == |
69 |
| - main.__doc__.strip()) |
| 106 | +def test_validate_perfect_docstring(): |
| 107 | + out = _capture_stdout(numpydoc.__main__.validate_object, |
| 108 | + 'numpydoc.tests.test_main._capture_stdout') |
| 109 | + assert out == '' |
70 | 110 |
|
71 |
| - # check it works with modules not imported from __init__ |
72 |
| - with _mock_module('somepackage1'): |
73 |
| - out = _capture_main('somepackage1.module.foo') |
74 |
| - assert out.startswith('Hello world\n') |
75 |
| - with _mock_module('somepackage2'): |
76 |
| - out = _capture_main('somepackage2.module') |
77 |
| - assert out.startswith('This module has test') |
78 |
| - with _mock_module('somepackage3'): |
79 |
| - out = _capture_main('somepackage3') |
80 |
| - assert out.startswith('This package has test') |
| 111 | + exit_status = numpydoc.__main__.validate_object( |
| 112 | + 'numpydoc.tests.test_main._capture_stdout') |
| 113 | + assert exit_status == 0 |
0 commit comments