Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit b2eb063

Browse files
committed
tests: util: testing: consoletest: Split unittests into own file
Signed-off-by: John Andersen <johnandersenpdx@gmail.com>
1 parent 289089d commit b2eb063

File tree

2 files changed

+308
-295
lines changed

2 files changed

+308
-295
lines changed

tests/docs/test_consoletest.py

Lines changed: 7 additions & 295 deletions
Original file line numberDiff line numberDiff line change
@@ -11,310 +11,22 @@
1111
from dffml.util.asynctestcase import AsyncTestCase
1212

1313
from dffml.util.testing.consoletest.commands import *
14-
from dffml.util.testing.consoletest.parser import parse_nodes, Node
1514

1615

17-
# Root of DFFML source tree
18-
ROOT_DIR = os.path.join(os.path.dirname(__file__), "..", "..")
16+
ROOT_PATH = pathlib.Path(__file__).parent.parent.parent
17+
DOCS_PATH = ROOT_PATH / "docs"
18+
1919

2020
# Load files by path. We have to import literalinclude_diff for diff-files
2121
for module_name in ["literalinclude_diff"]:
2222
spec = importlib.util.spec_from_file_location(
23-
module_name,
24-
os.path.join(ROOT_DIR, "docs", "_ext", f"{module_name}.py"),
23+
module_name, str(ROOT_PATH / "docs" / "_ext" / f"{module_name}.py"),
2524
)
2625
module = importlib.util.module_from_spec(spec)
2726
spec.loader.exec_module(module)
2827
setattr(sys.modules[__name__], module_name, module)
2928

3029

31-
class TestFunctions(AsyncTestCase):
32-
def test_parse_commands_multi_line(self):
33-
self.assertListEqual(
34-
parse_commands(
35-
[
36-
"$ python3 -m \\",
37-
" venv \\",
38-
" .venv",
39-
"some shit",
40-
"",
41-
"",
42-
"$ . \\",
43-
" .venv/bin/activate",
44-
"more asdflkj",
45-
"",
46-
]
47-
),
48-
[["python3", "-m", "venv", ".venv"], [".", ".venv/bin/activate"],],
49-
)
50-
51-
def test_parse_commands_substitution(self):
52-
for cmd in [
53-
["$ python3 $(cat feedface)"],
54-
["$ python3 `cat feedface`"],
55-
['$ python3 "`cat feedface`"'],
56-
]:
57-
with self.subTest(cmd=cmd):
58-
with self.assertRaises(NotImplementedError):
59-
parse_commands(cmd)
60-
61-
cmd = ["$ python3 '`cat feedface`'"]
62-
with self.subTest(cmd=cmd):
63-
parse_commands(cmd)
64-
65-
def test_parse_commands_single_line_with_output(self):
66-
self.assertListEqual(
67-
parse_commands(
68-
[
69-
"$ docker logs maintained_db 2>&1 | grep 'ready for'",
70-
"2020-01-13 21:31:09 0 [Note] mysqld: ready for connections.",
71-
"2020-01-13 21:32:16 0 [Note] mysqld: ready for connections.",
72-
]
73-
),
74-
[
75-
[
76-
"docker",
77-
"logs",
78-
"maintained_db",
79-
"2>&1",
80-
"|",
81-
"grep",
82-
"ready for",
83-
],
84-
],
85-
)
86-
87-
def test_build_command_venv_linux(self):
88-
self.assertEqual(
89-
build_command([".", ".venv/bin/activate"],),
90-
ActivateVirtualEnvCommand(".venv"),
91-
)
92-
93-
def test_pipes(self):
94-
self.assertListEqual(
95-
pipes(
96-
[
97-
"python3",
98-
"-c",
99-
r"print('Hello\nWorld')",
100-
"|",
101-
"grep",
102-
"Hello",
103-
]
104-
),
105-
[["python3", "-c", r"print('Hello\nWorld')"], ["grep", "Hello"]],
106-
)
107-
108-
async def test_run_commands(self):
109-
with tempfile.TemporaryFile() as stdout:
110-
await run_commands(
111-
[
112-
["python3", "-c", r"print('Hello\nWorld')"],
113-
["grep", "Hello", "2>&1"],
114-
],
115-
{"cwd": os.getcwd()},
116-
stdout=stdout,
117-
)
118-
stdout.seek(0)
119-
stdout = stdout.read().decode().strip()
120-
self.assertEqual(stdout, "Hello")
121-
122-
123-
class TestPipInstallCommand(unittest.TestCase):
124-
def test_fix_dffml_packages(self):
125-
command = PipInstallCommand(
126-
[
127-
"python",
128-
"-m",
129-
"pip",
130-
"install",
131-
"--use-feature=2020-resolver",
132-
"-U",
133-
"dffml",
134-
"-e",
135-
"dffml-model-scikit",
136-
"shouldi",
137-
"aiohttp",
138-
]
139-
)
140-
command.fix_dffml_packages({"root": ROOT_DIR})
141-
self.assertListEqual(
142-
command.cmd,
143-
[
144-
"python",
145-
"-m",
146-
"pip",
147-
"install",
148-
"--use-feature=2020-resolver",
149-
"-U",
150-
"-e",
151-
os.path.abspath(ROOT_DIR),
152-
"-e",
153-
os.path.abspath(os.path.join(ROOT_DIR, "model", "scikit")),
154-
"-e",
155-
os.path.abspath(os.path.join(ROOT_DIR, "examples", "shouldi")),
156-
"aiohttp",
157-
],
158-
)
159-
160-
161-
class TestDockerRunCommand(unittest.TestCase):
162-
def test_find_name(self):
163-
self.assertEqual(
164-
DockerRunCommand.find_name(
165-
["docker", "run", "--rm", "-d", "--name", "maintained_db",]
166-
),
167-
(
168-
"maintained_db",
169-
False,
170-
["docker", "run", "--rm", "-d", "--name", "maintained_db",],
171-
),
172-
)
173-
174-
175-
class TestParser(unittest.TestCase):
176-
def test_parse_nodes(self):
177-
self.maxDiff = None
178-
self.assertListEqual(
179-
list(
180-
filter(
181-
lambda node: node.directive
182-
in {"code-block", "literalinclude"},
183-
parse_nodes(
184-
inspect.cleandoc(
185-
r"""
186-
.. code-block:: console
187-
:test:
188-
189-
$ echo -e 'Hello\n\n\nWorld'
190-
Hello
191-
192-
193-
World
194-
195-
.. literalinclude:: some/file.py
196-
:filepath: myfile.py
197-
:test:
198-
199-
.. note::
200-
201-
.. note::
202-
203-
.. code-block:: console
204-
:test:
205-
:daemon: 8080
206-
207-
$ echo -e 'Hello\n\n\n World\n\n\nTest'
208-
Hello
209-
210-
211-
World
212-
213-
214-
Test
215-
216-
.. code-block:: console
217-
218-
$ echo -e 'Hello\n\n\n World\n\n\n\n'
219-
Hello
220-
221-
222-
World
223-
224-
225-
226-
$ echo 'feedface'
227-
feedface
228-
229-
.. note::
230-
231-
.. code-block:: console
232-
:test:
233-
234-
$ echo feedface
235-
feedface
236-
237-
.. code-block:: console
238-
:test:
239-
240-
$ echo feedface
241-
feedface
242-
"""
243-
)
244-
),
245-
)
246-
),
247-
[
248-
Node(
249-
directive="code-block",
250-
content=[
251-
r"$ echo -e 'Hello\n\n\nWorld'",
252-
"Hello",
253-
"",
254-
"",
255-
"World",
256-
],
257-
options={"test": True},
258-
node={},
259-
),
260-
Node(
261-
directive="literalinclude",
262-
content="",
263-
options={"filepath": "myfile.py", "test": True},
264-
node={"source": "some/file.py"},
265-
),
266-
Node(
267-
directive="code-block",
268-
content=[
269-
r"$ echo -e 'Hello\n\n\n World\n\n\nTest'",
270-
"Hello",
271-
"",
272-
"",
273-
" World",
274-
"",
275-
"",
276-
"Test",
277-
],
278-
options={"test": True, "daemon": "8080"},
279-
node={},
280-
),
281-
Node(
282-
directive="code-block",
283-
content=[
284-
r"$ echo -e 'Hello\n\n\n World\n\n\n\n'",
285-
"Hello",
286-
"",
287-
"",
288-
" World",
289-
"",
290-
"",
291-
"",
292-
"$ echo 'feedface'",
293-
"feedface",
294-
],
295-
options={},
296-
node={},
297-
),
298-
Node(
299-
directive="code-block",
300-
content=["$ echo feedface", "feedface",],
301-
options={"test": True},
302-
node={},
303-
),
304-
Node(
305-
directive="code-block",
306-
content=["$ echo feedface", "feedface",],
307-
options={"test": True},
308-
node={},
309-
),
310-
],
311-
)
312-
313-
314-
ROOT_PATH = pathlib.Path(__file__).parent.parent.parent
315-
DOCS_PATH = ROOT_PATH / "docs"
316-
317-
31830
class TestDocs(unittest.TestCase):
31931
"""
32032
A testcase for each doc will be added to this class
@@ -368,7 +80,7 @@ def testcase(self):
36880
)
36981
from sphinx.environment import BuildEnvironment
37082

371-
os.chdir(ROOT_DIR)
83+
os.chdir(str(ROOT_PATH))
37284

37385
filenames = [str(relative)]
37486

@@ -389,7 +101,7 @@ def _init_env(self, freshenv: bool) -> None:
389101
self.env.setup(self)
390102
self.env.find_files(self.config, self.builder)
391103

392-
confdir = os.path.join(ROOT_DIR, "docs")
104+
confdir = str(ROOT_PATH / "docs")
393105

394106
pickled_objs = {}
395107

@@ -407,7 +119,7 @@ def pickle_load(fileobj):
407119
"pickle.load", new=pickle_load
408120
), tempfile.TemporaryDirectory() as tempdir:
409121
app = SubSetSphinx(
410-
os.path.join(ROOT_DIR, "docs"),
122+
str(ROOT_PATH / "docs"),
411123
confdir,
412124
os.path.join(tempdir, "consoletest"),
413125
os.path.join(tempdir, "consoletest", ".doctrees"),

0 commit comments

Comments
 (0)