diff --git a/mathics/main.py b/mathics/main.py index 9b2d30c3e4..4e003bd92a 100755 --- a/mathics/main.py +++ b/mathics/main.py @@ -340,25 +340,6 @@ def main() -> int: definitions.set_line_no(0) - if args.execute: - for expr in args.execute: - evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell)) - result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT) - shell.print_result( - result, no_out_prompt=True, strict_wl_output=args.strict_wl_output - ) - if evaluation.exc_result == Symbol("Null"): - exit_rc = 0 - elif evaluation.exc_result == Symbol("$Aborted"): - exit_rc = -1 - elif evaluation.exc_result == Symbol("Overflow"): - exit_rc = -2 - else: - exit_rc = -3 - - if not args.persist: - return exit_rc - if args.FILE is not None: feeder = MathicsFileLineFeeder(args.FILE) try: @@ -377,7 +358,26 @@ def main() -> int: if args.persist: definitions.set_line_no(0) - else: + elif not args.execute: + return exit_rc + + if args.execute: + for expr in args.execute: + evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell)) + result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT) + shell.print_result( + result, no_out_prompt=True, strict_wl_output=args.strict_wl_output + ) + if evaluation.exc_result == Symbol("Null"): + exit_rc = 0 + elif evaluation.exc_result == Symbol("$Aborted"): + exit_rc = -1 + elif evaluation.exc_result == Symbol("Overflow"): + exit_rc = -2 + else: + exit_rc = -3 + + if not args.persist: return exit_rc if not args.quiet: diff --git a/test/data/script.m b/test/data/script.m new file mode 100644 index 0000000000..75e233ed62 --- /dev/null +++ b/test/data/script.m @@ -0,0 +1 @@ +Print["Hello"]; diff --git a/test/test_cli.py b/test/test_cli.py new file mode 100644 index 0000000000..9254a7d0c0 --- /dev/null +++ b/test/test_cli.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +import subprocess + +import os.path as osp +import re +import pytest +import sys + + +def get_testdir(): + filename = osp.normcase(osp.dirname(osp.abspath(__file__))) + return osp.realpath(filename) + + +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires Python 3.7 or higher") +def test_cli(): + script_file = osp.join(get_testdir(), "data", "script.m") + + # asserts output contains 'Hello' and '2' + assert re.match( + r"Hello\s+2", + subprocess.run( + ["mathics", "-e", "Print[1+1];", "-script", script_file], + capture_output=True, + ).stdout.decode("utf-8"), + ) + + +if __name__ == "__main__": + test_cli()