Skip to content

Commit 1cabd6d

Browse files
authored
Refactor CLI run subcommand tests into nested classes for organization (#1041)
No functional changes. Just moving the tests that already exist into nested subclasses based on the particular "form" of `basilisp run` that each is testing (e.g. code string, file, stdin, or namespace).
1 parent 6948a1e commit 1cabd6d

File tree

1 file changed

+148
-143
lines changed

1 file changed

+148
-143
lines changed

tests/basilisp/cli_test.py

Lines changed: 148 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def isolated_filesystem():
3636
wd = os.getcwd()
3737
os.chdir(d)
3838
try:
39-
yield
39+
yield d
4040
finally:
4141
os.chdir(wd)
4242

@@ -228,160 +228,165 @@ def test_other_exception(self, run_cli):
228228
assert "Exception: CLI test" in result.err
229229

230230

231-
class TestRun:
232-
cli_args_params = [
233-
([], f"0{os.linesep}"),
234-
(["--"], f"0{os.linesep}"),
235-
(["1", "2", "3"], f"6{os.linesep}"),
236-
(["--", "1", "2", "3"], f"6{os.linesep}"),
237-
]
238-
cli_args_code = "(println (apply + (map int *command-line-args*)))"
231+
cli_run_args_params = [
232+
([], f"0{os.linesep}"),
233+
(["--"], f"0{os.linesep}"),
234+
(["1", "2", "3"], f"6{os.linesep}"),
235+
(["--", "1", "2", "3"], f"6{os.linesep}"),
236+
]
237+
cli_run_args_code = "(println (apply + (map int *command-line-args*)))"
239238

239+
240+
class TestRun:
240241
def test_run_ns_and_code_mutually_exclusive(self, run_cli):
241242
with pytest.raises(SystemExit):
242243
run_cli(["run", "-c", "-n"])
243244

244-
def test_run_code(self, run_cli):
245-
result = run_cli(["run", "-c", "(println (+ 1 2))"])
246-
assert f"3{os.linesep}" == result.lisp_out
247-
248-
def test_run_code_main_ns(self, run_cli):
249-
result = run_cli(["run", "-c", "(println *main-ns*)"])
250-
assert f"nil{os.linesep}" == result.lisp_out
251-
252-
@pytest.mark.parametrize("args,ret", cli_args_params)
253-
def test_run_code_with_args(self, run_cli, args: List[str], ret: str):
254-
result = run_cli(["run", "-c", self.cli_args_code, *args])
255-
assert ret == result.lisp_out
256-
257-
def test_run_file_rel(self, isolated_filesystem, run_cli):
258-
with open("test.lpy", mode="w") as f:
259-
f.write("(println (+ 1 2))")
260-
result = run_cli(["run", "test.lpy"])
261-
assert f"3{os.linesep}" == result.lisp_out
262-
263-
def test_run_file_abs(self, isolated_filesystem, run_cli):
264-
with open("test.lpy", mode="w") as f:
265-
f.write("(println (+ 1 3))")
266-
full_path = os.path.abspath("test.lpy")
267-
result = run_cli(["run", full_path])
268-
assert f"4{os.linesep}" == result.lisp_out
269-
270-
def test_run_file_not_found(self, isolated_filesystem, run_cli):
271-
with pytest.raises(FileNotFoundError):
272-
run_cli(["run", "xyz.lpy"])
273-
274-
def test_run_file_main_ns(self, isolated_filesystem, run_cli):
275-
with open("test.lpy", mode="w") as f:
276-
f.write("(println *main-ns*)")
277-
result = run_cli(["run", "test.lpy"])
278-
assert f"nil{os.linesep}" == result.lisp_out
279-
280-
@pytest.mark.parametrize("args,ret", cli_args_params)
281-
def test_run_file_with_args(
282-
self, isolated_filesystem, run_cli, args: List[str], ret: str
283-
):
284-
with open("test.lpy", mode="w") as f:
285-
f.write(self.cli_args_code)
286-
result = run_cli(["run", "test.lpy", *args])
287-
assert ret == result.lisp_out
288-
289-
@pytest.fixture
290-
def namespace_name(self) -> str:
291-
return f"package.core{secrets.token_hex(4)}"
292-
293-
@pytest.fixture
294-
def namespace_file(
295-
self, monkeypatch, tmp_path: pathlib.Path, namespace_name: str
296-
) -> pathlib.Path:
297-
parent_ns, child_ns = namespace_name.split(".", maxsplit=1)
298-
parent = tmp_path / parent_ns
299-
parent.mkdir()
300-
nsfile = parent / f"{child_ns}.lpy"
301-
nsfile.touch()
302-
monkeypatch.syspath_prepend(str(tmp_path))
303-
yield nsfile
304-
305-
def test_cannot_run_namespace_with_in_ns_arg(
306-
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
307-
):
308-
namespace_file.write_text("(println (+ 1 2))")
309-
with pytest.raises(SystemExit):
310-
run_cli(["run", "--in-ns", "otherpackage.core", "-n", namespace_name])
311-
312-
def test_run_namespace(
313-
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
314-
):
315-
namespace_file.write_text(f"(ns {namespace_name}) (println (+ 1 2))")
316-
result = run_cli(["run", "-n", namespace_name])
317-
assert f"3{os.linesep}" == result.lisp_out
318-
319-
def test_run_namespace_main_ns(
320-
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
321-
):
322-
namespace_file.write_text(
323-
f"(ns {namespace_name}) (println (name *ns*)) (println *main-ns*)"
324-
)
325-
result = run_cli(["run", "-n", namespace_name])
326-
assert (
327-
f"{namespace_name}{os.linesep}{namespace_name}{os.linesep}"
328-
== result.lisp_out
329-
)
245+
class TestRunCode:
246+
def test_run_code(self, run_cli):
247+
result = run_cli(["run", "-c", "(println (+ 1 2))"])
248+
assert f"3{os.linesep}" == result.lisp_out
249+
250+
def test_run_code_main_ns(self, run_cli):
251+
result = run_cli(["run", "-c", "(println *main-ns*)"])
252+
assert f"nil{os.linesep}" == result.lisp_out
253+
254+
@pytest.mark.parametrize("args,ret", cli_run_args_params)
255+
def test_run_code_with_args(self, run_cli, args: List[str], ret: str):
256+
result = run_cli(["run", "-c", cli_run_args_code, *args])
257+
assert ret == result.lisp_out
258+
259+
class TestRunFile:
260+
def test_run_file_rel(self, isolated_filesystem, run_cli):
261+
with open("test.lpy", mode="w") as f:
262+
f.write("(println (+ 1 2))")
263+
result = run_cli(["run", "test.lpy"])
264+
assert f"3{os.linesep}" == result.lisp_out
265+
266+
def test_run_file_abs(self, isolated_filesystem, run_cli):
267+
with open("test.lpy", mode="w") as f:
268+
f.write("(println (+ 1 3))")
269+
full_path = os.path.abspath("test.lpy")
270+
result = run_cli(["run", full_path])
271+
assert f"4{os.linesep}" == result.lisp_out
272+
273+
def test_run_file_not_found(self, isolated_filesystem, run_cli):
274+
with pytest.raises(FileNotFoundError):
275+
run_cli(["run", "xyz.lpy"])
276+
277+
def test_run_file_main_ns(self, isolated_filesystem, run_cli):
278+
with open("test.lpy", mode="w") as f:
279+
f.write("(println *main-ns*)")
280+
result = run_cli(["run", "test.lpy"])
281+
assert f"nil{os.linesep}" == result.lisp_out
282+
283+
@pytest.mark.parametrize("args,ret", cli_run_args_params)
284+
def test_run_file_with_args(
285+
self, isolated_filesystem, run_cli, args: List[str], ret: str
286+
):
287+
with open("test.lpy", mode="w") as f:
288+
f.write(cli_run_args_code)
289+
result = run_cli(["run", "test.lpy", *args])
290+
assert ret == result.lisp_out
291+
292+
class TestRunNamespace:
293+
@pytest.fixture
294+
def namespace_name(self) -> str:
295+
return f"package.core{secrets.token_hex(4)}"
296+
297+
@pytest.fixture
298+
def namespace_file(
299+
self, monkeypatch, tmp_path: pathlib.Path, namespace_name: str
300+
) -> pathlib.Path:
301+
parent_ns, child_ns = namespace_name.split(".", maxsplit=1)
302+
parent = tmp_path / parent_ns
303+
parent.mkdir()
304+
nsfile = parent / f"{child_ns}.lpy"
305+
nsfile.touch()
306+
monkeypatch.syspath_prepend(str(tmp_path))
307+
yield nsfile
308+
309+
def test_cannot_run_namespace_with_in_ns_arg(
310+
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
311+
):
312+
namespace_file.write_text("(println (+ 1 2))")
313+
with pytest.raises(SystemExit):
314+
run_cli(["run", "--in-ns", "otherpackage.core", "-n", namespace_name])
315+
316+
def test_run_namespace(
317+
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
318+
):
319+
namespace_file.write_text(f"(ns {namespace_name}) (println (+ 1 2))")
320+
result = run_cli(["run", "-n", namespace_name])
321+
assert f"3{os.linesep}" == result.lisp_out
322+
323+
def test_run_namespace_main_ns(
324+
self, run_cli, namespace_name: str, namespace_file: pathlib.Path
325+
):
326+
namespace_file.write_text(
327+
f"(ns {namespace_name}) (println (name *ns*)) (println *main-ns*)"
328+
)
329+
result = run_cli(["run", "-n", namespace_name])
330+
assert (
331+
f"{namespace_name}{os.linesep}{namespace_name}{os.linesep}"
332+
== result.lisp_out
333+
)
330334

331-
@pytest.mark.parametrize("args,ret", cli_args_params)
332-
def test_run_namespace_with_args(
333-
self,
334-
run_cli,
335-
namespace_name: str,
336-
namespace_file: pathlib.Path,
337-
args: List[str],
338-
ret: str,
339-
):
340-
namespace_file.write_text(f"(ns {namespace_name}) {self.cli_args_code}")
341-
result = run_cli(["run", "-n", namespace_name, *args])
342-
assert ret == result.lisp_out
343-
344-
def test_run_namespace_with_subnamespace(
345-
self, run_cli, monkeypatch, tmp_path: pathlib.Path
346-
):
347-
ns_name = f"parent{secrets.token_hex(4)}"
348-
child = "child"
349-
350-
parent_ns_dir = tmp_path / ns_name
351-
parent_ns_dir.mkdir()
352-
353-
parent_ns_file = tmp_path / f"{ns_name}.lpy"
354-
parent_ns_file.touch()
355-
parent_ns_file.write_text(
356-
f'(ns {ns_name} (:require [{ns_name}.{child}])) (python/print "loading:" *ns*)'
357-
)
335+
@pytest.mark.parametrize("args,ret", cli_run_args_params)
336+
def test_run_namespace_with_args(
337+
self,
338+
run_cli,
339+
namespace_name: str,
340+
namespace_file: pathlib.Path,
341+
args: List[str],
342+
ret: str,
343+
):
344+
namespace_file.write_text(f"(ns {namespace_name}) {cli_run_args_code}")
345+
result = run_cli(["run", "-n", namespace_name, *args])
346+
assert ret == result.lisp_out
347+
348+
def test_run_namespace_with_subnamespace(
349+
self, run_cli, monkeypatch, tmp_path: pathlib.Path
350+
):
351+
ns_name = f"parent{secrets.token_hex(4)}"
352+
child = "child"
353+
354+
parent_ns_dir = tmp_path / ns_name
355+
parent_ns_dir.mkdir()
356+
357+
parent_ns_file = tmp_path / f"{ns_name}.lpy"
358+
parent_ns_file.touch()
359+
parent_ns_file.write_text(
360+
f'(ns {ns_name} (:require [{ns_name}.{child}])) (python/print "loading:" *ns*)'
361+
)
358362

359-
child_ns_file = parent_ns_dir / f"{child}.lpy"
360-
child_ns_file.touch()
361-
child_ns_file.write_text(
362-
f'(ns {ns_name}.{child}) (python/print "loading:" *ns*)'
363-
)
363+
child_ns_file = parent_ns_dir / f"{child}.lpy"
364+
child_ns_file.touch()
365+
child_ns_file.write_text(
366+
f'(ns {ns_name}.{child}) (python/print "loading:" *ns*)'
367+
)
364368

365-
monkeypatch.syspath_prepend(str(tmp_path))
369+
monkeypatch.syspath_prepend(str(tmp_path))
366370

367-
result = run_cli(["run", "-n", ns_name])
368-
assert f"loading: {ns_name}.{child}\nloading: {ns_name}\n" == result.out
371+
result = run_cli(["run", "-n", ns_name])
372+
assert f"loading: {ns_name}.{child}\nloading: {ns_name}\n" == result.out
369373

370-
def test_run_stdin(self, run_cli):
371-
result = run_cli(["run", "-"], input="(println (+ 1 2))")
372-
assert f"3{os.linesep}" == result.lisp_out
374+
class TestRunStdin:
375+
def test_run_stdin(self, run_cli):
376+
result = run_cli(["run", "-"], input="(println (+ 1 2))")
377+
assert f"3{os.linesep}" == result.lisp_out
373378

374-
def test_run_stdin_main_ns(self, run_cli):
375-
result = run_cli(["run", "-"], input="(println *main-ns*)")
376-
assert f"nil{os.linesep}" == result.lisp_out
379+
def test_run_stdin_main_ns(self, run_cli):
380+
result = run_cli(["run", "-"], input="(println *main-ns*)")
381+
assert f"nil{os.linesep}" == result.lisp_out
377382

378-
@pytest.mark.parametrize("args,ret", cli_args_params)
379-
def test_run_stdin_with_args(self, run_cli, args: List[str], ret: str):
380-
result = run_cli(
381-
["run", "-", *args],
382-
input=self.cli_args_code,
383-
)
384-
assert ret == result.lisp_out
383+
@pytest.mark.parametrize("args,ret", cli_run_args_params)
384+
def test_run_stdin_with_args(self, run_cli, args: List[str], ret: str):
385+
result = run_cli(
386+
["run", "-", *args],
387+
input=cli_run_args_code,
388+
)
389+
assert ret == result.lisp_out
385390

386391

387392
def test_version(run_cli):

0 commit comments

Comments
 (0)