|
1 |
| -from numpydoc.__main__ import main |
| 1 | +from __future__ import print_function |
2 | 2 |
|
| 3 | +from contextlib import contextmanager |
| 4 | +import os |
3 | 5 | import sys
|
| 6 | +import tempfile |
4 | 7 | try:
|
5 | 8 | from StringIO import StringIO
|
6 | 9 | except ImportError:
|
7 | 10 | from io import StringIO
|
8 | 11 |
|
| 12 | +from numpydoc.__main__ import main |
9 | 13 |
|
10 |
| -def test_main(): |
| 14 | + |
| 15 | +PACKAGE_CODE = """ |
| 16 | +'''This package has test stuff''' |
| 17 | +""" |
| 18 | + |
| 19 | +MODULE_CODE = """ |
| 20 | +'''This module has test stuff''' |
| 21 | +
|
| 22 | +def foo(a, b=5): |
| 23 | + '''Hello world |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + something : foo |
| 28 | + bar |
| 29 | + something_else |
| 30 | + bar |
| 31 | + ''' |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 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 |
| 53 | + |
| 54 | + |
| 55 | +def _capture_main(*args): |
11 | 56 | f = StringIO()
|
12 | 57 | sys.stdout, old_stdout = f, sys.stdout
|
13 |
| - main(['numpydoc.__main__.main']) |
14 |
| - assert f.getvalue().strip() == main.__doc__ |
15 |
| - sys.stdout = old_stdout |
| 58 | + try: |
| 59 | + main(args) |
| 60 | + return f.getvalue().strip('\n\r') |
| 61 | + finally: |
| 62 | + sys.stdout = old_stdout |
| 63 | + |
| 64 | + |
| 65 | +def test_main(): |
| 66 | + # TODO: does not currently check that numpydoc transformations are applied |
| 67 | + |
| 68 | + assert (_capture_main('numpydoc.__main__.main') == |
| 69 | + main.__doc__.strip()) |
| 70 | + |
| 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') |
0 commit comments