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..c7e4b6286 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 {su.quote(str(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")