From c978190330672aa6334a5e3e45b23b717413f014 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:05:06 -0500 Subject: [PATCH 1/2] Report an error when run_script is given a directory of no size run_script takes a file of size zero for an empty script, which is not an error, and found a directory to be one only when opening it failed. A directory can report a size of zero too. Then run_script printed nothing and set last_result to True. This showed in CI as test_run_script_with_invalid_file failing on Windows with Python 3.12 and 3.14 and passing in the other four Windows jobs, and in the same two when they were run again: Windows reported that size for the tests directory in some jobs and not in others. Why it did is not known. run_script now looks for a directory first, so its answer no longer depends on the size. The message for a directory says that it is one, where it used to give the operating system's error for opening it. --- CHANGELOG.md | 3 +++ cmd2/cmd2.py | 6 ++++++ tests/test_cmd2.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a62248ecf..212bcb8a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ - `read_input()` and `read_secret()` now remove trailing newlines from what they return when reading from a terminal, as their documentation says. Before, a pasted newline could be left on the end if the prompt had no key binding of cmd2's for a paste. + - `run_script` now reports an error when it is given a directory which reports a size of zero, + as Windows does for some directories. Before, it took such a directory for an empty script, + printed nothing and set `last_result` to `True`. ## 4.4.4 (2026-11-30) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2e9bf85af..653942444 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -5962,6 +5962,12 @@ def do_run_script(self, args: argparse.Namespace) -> bool | None: self.last_result = False expanded_path = os.path.abspath(os.path.expanduser(args.script_path)) + # A directory is not a script. This is checked by itself because a directory can report a size of zero, as + # Windows does for some, and it would then be taken for an empty file below. + if os.path.isdir(expanded_path): + self.perror(f"Problem accessing script from '{expanded_path}': it is a directory") + return None + # Add some protection against accidentally running a Python file. The happens when users # mix up run_script and run_pyscript. if expanded_path.endswith(".py"): diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 70ce76655..1d8f7213b 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -595,6 +595,20 @@ def test_run_script_with_invalid_file(base_app, request) -> None: assert base_app.last_result is False +def test_run_script_with_directory_of_no_size(base_app, tmp_path, monkeypatch) -> None: + """A directory which reports a size of zero is still not an empty script. + + Windows reported that size in CI for the directory these tests are in, and run_script said nothing. + """ + monkeypatch.setattr(os.path, "getsize", lambda path: 0) + + _out, err = run_cmd(base_app, f"run_script {tmp_path}") + + assert "Problem accessing script from " in err[0] + assert "is a directory" in err[0] + assert base_app.last_result is False + + def test_run_script_with_empty_file(base_app, request) -> None: test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, "scripts", "empty.txt") From 2c6d58a48e8883be8fa075f2b398d2abc8de27d3 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:16:04 -0500 Subject: [PATCH 2/2] Quote the directory in the run_script test of a directory of no size Found in review. Where the temporary directory's path has a space, the command was parsed as several arguments, and argparse printed its usage before run_script looked at the directory. The path is now quoted, so the test passes or fails for what it tests wherever it runs. --- tests/test_cmd2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 1d8f7213b..c7e4b6286 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -602,7 +602,7 @@ def test_run_script_with_directory_of_no_size(base_app, tmp_path, monkeypatch) - """ monkeypatch.setattr(os.path, "getsize", lambda path: 0) - _out, err = run_cmd(base_app, f"run_script {tmp_path}") + _out, err = run_cmd(base_app, f"run_script {su.quote(str(tmp_path))}") assert "Problem accessing script from " in err[0] assert "is a directory" in err[0]