Skip to content

Commit 331c9bc

Browse files
committed
--pyra cli
1 parent c83ae08 commit 331c9bc

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

psll/__main__.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ def _(subparsers: argparse._SubParsersAction) -> None:
236236
type=str,
237237
)
238238

239+
run_parser.add_argument(
240+
"--pyra",
241+
help="Path to the pyramid scheme interpreter",
242+
type=str,
243+
)
244+
239245
run_parser.add_argument("input", help="Input pyramid scheme file.")
240246

241247

@@ -251,6 +257,7 @@ def _(args: argparse.Namespace, extra: list[str]) -> tuple[argparse.Namespace, l
251257
raise ArgumentError("Input file does not have .pyra extension")
252258

253259
args.ruby = check_ruby(args.ruby)
260+
args.pyra = check_pyra(args.pyra, args.verbose)
254261

255262
return args, extra
256263

@@ -280,6 +287,12 @@ def _(subparsers: argparse._SubParsersAction) -> None:
280287
type=str,
281288
)
282289

290+
compile_and_run_parser.add_argument(
291+
"--pyra",
292+
help="Path to the pyramid scheme interpreter",
293+
type=str,
294+
)
295+
283296
compile_and_run_parser.add_argument(
284297
"input",
285298
help=("Input file written in the pyramid scheme (lisp (like)) syntax, with the .psll extension."),
@@ -303,6 +316,26 @@ def check_ruby(ruby: str) -> str:
303316
return ruby
304317

305318

319+
def check_pyra(pyra: str, verbose: int = 0) -> str:
320+
if pyra:
321+
# Check if the specified pyra.rb file exists
322+
if not op.exists(pyra):
323+
raise ArgumentError(f"Pyra.rb file '{pyra}' does not exist.")
324+
if not op.isfile(pyra):
325+
raise ArgumentError(f"'{pyra}' is not a file.")
326+
else:
327+
# Try to find pyra.rb
328+
found_pyra = find_pyra_rb(verbose)
329+
if found_pyra is None:
330+
raise ArgumentError(
331+
"Pyra.rb file not found. Please specify the path to the pyra.rb file."
332+
"Or use the `download-pyra` command to download it."
333+
)
334+
pyra = found_pyra
335+
336+
return pyra
337+
338+
306339
@register_validate_options(Subcommand.COMPILE_AND_RUN)
307340
def _(args: argparse.Namespace, extra: list[str]) -> tuple[argparse.Namespace, list[str]]:
308341
"""Validate options for the compile-and-run subcommand"""
@@ -318,6 +351,7 @@ def _(args: argparse.Namespace, extra: list[str]) -> tuple[argparse.Namespace, l
318351
raise ArgumentError("Input file does not have .psll extension")
319352

320353
args.ruby = check_ruby(args.ruby)
354+
args.pyra = check_pyra(args.pyra, args.verbose)
321355

322356
return args, extra
323357

@@ -563,19 +597,14 @@ def _(args: argparse.Namespace, extra: list[str]) -> None:
563597
if args.verbose > 1:
564598
print("Ruby version:", ruby_version)
565599

566-
pyra_rb = find_pyra_rb(args.verbose)
567-
568-
if pyra_rb is None:
569-
raise RuntimeError("Could not find pyra.rb. Make sure pyramid scheme is installed.")
570-
571600
if args.verbose > 1:
572-
print("pyra.rb:", pyra_rb)
601+
print("pyra.rb:", args.pyra)
573602

574603
# run pyramid scheme
575604
if args.verbose:
576605
print("Running pyramid scheme:")
577606

578-
subprocess.run([args.ruby, pyra_rb, args.input, *extra])
607+
subprocess.run([args.ruby, args.pyra, args.input, *extra])
579608

580609

581610
@register_subcommand(Subcommand.COMPILE_AND_RUN)

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ def pytest_addoption(parser: pytest.Parser) -> None:
99
action="store",
1010
help="specify the ruby executable to use for tests",
1111
)
12+
parser.addoption(
13+
"--pyra",
14+
action="store",
15+
help="specify the pyra.rb to use for tests",
16+
)
1217

1318

1419
def pytest_configure(config: pytest.Config) -> None:
1520
config.addinivalue_line("markers", "ruby: specify the ruby executable to use for tests")
21+
config.addinivalue_line("markers", "pyra: specify the pyra.rb to use for tests")
1622

1723

1824
@pytest.fixture(scope="session")
@@ -22,6 +28,13 @@ def ruby(request: pytest.FixtureRequest) -> Optional[str]:
2228
assert isinstance(ruby, (str, type(None))), f"Invalid ruby option: {ruby}"
2329
return ruby
2430

31+
@pytest.fixture(scope="session")
32+
def pyra(request: pytest.FixtureRequest) -> Optional[str]:
33+
"""Fixture to get the pyra.rb from the command line options"""
34+
pyra = request.config.getoption("--pyra", None)
35+
assert isinstance(pyra, (str, type(None))), f"Invalid pyra option: {pyra}"
36+
return pyra
37+
2538

2639
##========================================================================================================
2740
##

tests/test_examples.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@
4040
psll_examples.append((expected_filename, expected_output))
4141

4242

43-
def compile_and_run(filename: str, ruby: Optional[str] = None) -> str:
43+
def compile_and_run(
44+
filename: str,
45+
ruby: Optional[str] = None,
46+
pyra: Optional[str] = None,
47+
) -> str:
4448
"""Compile and run the given file, returning the output"""
4549
args = ["psll", "compile-and-run"]
4650
if ruby:
4751
args += ["--ruby", ruby]
52+
if pyra:
53+
args += ["--pyra", pyra]
4854
args += [filename]
4955
return subprocess.check_output(
5056
args,
@@ -73,10 +79,10 @@ def run(filename: str) -> str:
7379

7480

7581
@pytest.mark.parametrize("filename, expected_output", psll_examples)
76-
def test_examples(ruby: Optional[str], filename: str, expected_output: str) -> None:
82+
def test_examples(ruby: Optional[str], pyra: Optional[str], filename: str, expected_output: str) -> None:
7783
"""Test that the examples compile and run correctly"""
7884
# get the 'ruby' keyword from the pytest config
79-
assert compile_and_run(filename, ruby=ruby) == expected_output, f"Example {filename} output mismatch"
85+
assert compile_and_run(filename, ruby=ruby, pyra=pyra) == expected_output, f"Example {filename} output mismatch"
8086

8187

8288
@pytest.mark.parametrize("filename, expected_output", psll_examples)

0 commit comments

Comments
 (0)