|
3 | 3 | import pathlib
|
4 | 4 | import platform
|
5 | 5 | import re
|
| 6 | +import secrets |
6 | 7 | import stat
|
7 | 8 | import subprocess
|
8 | 9 | import tempfile
|
@@ -286,41 +287,86 @@ def test_run_file_with_args(
|
286 | 287 | assert ret == result.lisp_out
|
287 | 288 |
|
288 | 289 | @pytest.fixture
|
289 |
| - def namespace_file(self, monkeypatch, tmp_path: pathlib.Path) -> pathlib.Path: |
290 |
| - parent = tmp_path / "package" |
| 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 |
291 | 299 | parent.mkdir()
|
292 |
| - nsfile = parent / "core.lpy" |
| 300 | + nsfile = parent / f"{child_ns}.lpy" |
293 | 301 | nsfile.touch()
|
294 | 302 | monkeypatch.syspath_prepend(str(tmp_path))
|
295 | 303 | yield nsfile
|
296 | 304 |
|
297 | 305 | def test_cannot_run_namespace_with_in_ns_arg(
|
298 |
| - self, run_cli, namespace_file: pathlib.Path |
| 306 | + self, run_cli, namespace_name: str, namespace_file: pathlib.Path |
299 | 307 | ):
|
300 | 308 | namespace_file.write_text("(println (+ 1 2))")
|
301 | 309 | with pytest.raises(SystemExit):
|
302 |
| - run_cli(["run", "--in-ns", "otherpackage.core", "-n", "package.core"]) |
| 310 | + run_cli(["run", "--in-ns", "otherpackage.core", "-n", namespace_name]) |
303 | 311 |
|
304 |
| - def test_run_namespace(self, run_cli, namespace_file: pathlib.Path): |
305 |
| - namespace_file.write_text("(println (+ 1 2))") |
306 |
| - result = run_cli(["run", "-n", "package.core"]) |
| 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]) |
307 | 317 | assert f"3{os.linesep}" == result.lisp_out
|
308 | 318 |
|
309 |
| - def test_run_namespace_main_ns(self, run_cli, namespace_file: pathlib.Path): |
| 319 | + def test_run_namespace_main_ns( |
| 320 | + self, run_cli, namespace_name: str, namespace_file: pathlib.Path |
| 321 | + ): |
310 | 322 | namespace_file.write_text(
|
311 |
| - "(ns package.core) (println (name *ns*)) (println *main-ns*)" |
| 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 |
312 | 329 | )
|
313 |
| - result = run_cli(["run", "-n", "package.core"]) |
314 |
| - assert f"package.core{os.linesep}package.core{os.linesep}" == result.lisp_out |
315 | 330 |
|
316 | 331 | @pytest.mark.parametrize("args,ret", cli_args_params)
|
317 | 332 | def test_run_namespace_with_args(
|
318 |
| - self, run_cli, namespace_file: pathlib.Path, args: List[str], ret: str |
| 333 | + self, |
| 334 | + run_cli, |
| 335 | + namespace_name: str, |
| 336 | + namespace_file: pathlib.Path, |
| 337 | + args: List[str], |
| 338 | + ret: str, |
319 | 339 | ):
|
320 |
| - namespace_file.write_text(self.cli_args_code) |
321 |
| - result = run_cli(["run", "-n", "package.core", *args]) |
| 340 | + namespace_file.write_text(f"(ns {namespace_name}) {self.cli_args_code}") |
| 341 | + result = run_cli(["run", "-n", namespace_name, *args]) |
322 | 342 | assert ret == result.lisp_out
|
323 | 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 | + ) |
| 358 | + |
| 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 | + ) |
| 364 | + |
| 365 | + monkeypatch.syspath_prepend(str(tmp_path)) |
| 366 | + |
| 367 | + result = run_cli(["run", "-n", ns_name]) |
| 368 | + assert f"loading: {ns_name}.{child}\nloading: {ns_name}\n" == result.out |
| 369 | + |
324 | 370 | def test_run_stdin(self, run_cli):
|
325 | 371 | result = run_cli(["run", "-"], input="(println (+ 1 2))")
|
326 | 372 | assert f"3{os.linesep}" == result.lisp_out
|
|
0 commit comments