Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading