-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_execute_doctests.py
184 lines (148 loc) · 6.25 KB
/
test_execute_doctests.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import contextlib
import functools
import logging
import pathlib
import sys
import unittest
from execute_doctests import (
get_module_name,
get_package_path,
import_module_from_package,
import_standalone_module,
parse_args,
)
logger = logging.getLogger(__name__)
class TestGetModuleName(unittest.TestCase):
def setUp(self):
self.cwd = pathlib.Path("/home/user/my_project")
self.sys_paths = ["", "/home/user/my_project/src"]
def test_module_path_extends_a_sys_path(self):
module_path = pathlib.Path(
"/home/user/my_project/src/my_package/my_subpackage/my_module.py"
)
package_path = pathlib.Path("my_package/my_subpackage/my_module.py")
path = get_module_name(self.cwd, self.sys_paths, module_path, package_path)
self.assertEqual(path, "my_package.my_subpackage.my_module")
def test_module_path_does_not_extend_a_sys_path(self):
module_path = pathlib.Path("/home/user/my_project/tests/test_my_module.py")
package_path = pathlib.Path("my_package/my_subpackage/my_module.py")
path = get_module_name(self.cwd, self.sys_paths, module_path, package_path)
self.assertIsNone(path)
def test_module_path_is_in_the_current_working_directory(self):
module_path = pathlib.Path("/home/user/my_project/my_module.py")
package_path = pathlib.Path("my_module.py")
path = get_module_name(self.cwd, self.sys_paths, module_path, package_path)
self.assertEqual(path, "my_module")
def stub_contains_init(
directories_with_init: list[pathlib.Path], directory: pathlib.Path
):
return directory in directories_with_init
class TestGetPackagePath(unittest.TestCase):
def test_with_trailing_package_directories(self):
contains_init = functools.partial(
stub_contains_init,
[
pathlib.Path("/home/user/my_project/src/my_package/my_subpackage"),
pathlib.Path("/home/user/my_project/src/my_package"),
],
)
module_path = pathlib.Path(
"/home/user/my_project/src/my_package/my_subpackage/my_module.py"
)
self.assertEqual(
pathlib.Path("my_package/my_subpackage/my_module.py"),
get_package_path(module_path, contains_init),
)
def test_with_standalone_package(self):
contains_init = functools.partial(stub_contains_init, [])
module_path = pathlib.Path(
"/home/user/my_project/my_example_scripts/my_module.py"
)
self.assertIsNone(get_package_path(module_path, contains_init))
class TestImportModuleFromPackage(unittest.TestCase):
def test_return_imported_module_when_module_is_in_importable_package(self):
current_dir = pathlib.Path(__file__).parent
module_to_import = (
current_dir / "test_data" / "package" / "subpackage" / "module.py"
)
module_path_in_package = pathlib.Path("package/subpackage/module.py")
with extra_sys_path(current_dir / "test_data"):
module = import_module_from_package(
module_to_import, module_path_in_package
)
self.assertIsNotNone(module)
self.assertEqual(module.__file__, str(module_to_import))
def test_return_none_when_module_is_in_unimportable_package(self):
current_dir = pathlib.Path(__file__).parent
module_to_import = (
current_dir / "test_data" / "package" / "subpackage" / "module.py"
)
module_path_in_package = pathlib.Path("package/subpackage/module.py")
module = import_module_from_package(module_to_import, module_path_in_package)
self.assertIsNone(module)
class TestImportStandaloneModule(unittest.TestCase):
def test_return_imported_module(self):
current_dir = pathlib.Path(__file__).parent
module_to_import = current_dir / "test_data" / "module.py"
module = import_standalone_module(module_to_import)
self.assertIsNotNone(module)
self.assertEqual(module.__file__, str(module_to_import))
@contextlib.contextmanager
def extra_sys_path(extra_path: pathlib.Path):
sys.path.insert(0, str(extra_path))
yield
sys.path = list(filter(lambda path: path != str(extra_path), sys.path))
class TestParseArgs(unittest.TestCase):
def test_with_additional_pythonpath_directories(self):
namespace = parse_args(
[
"execute_docstests.py",
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
"30",
"--pythonpath",
"/home/user/my_project/src",
"--pythonpath",
"/home/user/my_project",
]
)
self.assertEqual(
namespace.module_path,
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
)
self.assertEqual(namespace.lineno, 30)
self.assertEqual(len(namespace.pythonpath), 2)
self.assertEqual(namespace.pythonpath[0], "/home/user/my_project/src")
self.assertEqual(namespace.pythonpath[1], "/home/user/my_project")
def test_without_additional_pythonpath_directories(self):
namespace = parse_args(
[
"execute_docstests.py",
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
"30",
]
)
self.assertEqual(
namespace.module_path,
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
)
self.assertEqual(namespace.lineno, 30)
self.assertEqual(len(namespace.pythonpath), 0)
def test_default_verbosity_is_info(self):
namespace = parse_args(
[
"execute_docstests.py",
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
"30",
]
)
self.assertEqual(namespace.log_level, logging.INFO)
def test_increased_verbosity_is_debug(self):
namespace = parse_args(
[
"execute_docstests.py",
"/home/user/my_project/src/my_package/my_subpackage/my_module.py",
"30",
"--verbose",
]
)
self.assertEqual(namespace.log_level, logging.DEBUG)