Skip to content

Commit 100a98f

Browse files
committed
Fixed tests in test_run_pyscript for prompt-toolkit migration
Also: - Fixed make clean so it cleans up code coverage file artifacts
1 parent 894368a commit 100a98f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ publish: validate-tag build ## Publish a release to PyPI, uses token from ~/.pyp
7777
BUILD_DIRS = build dist *.egg-info
7878
DOC_DIRS = build
7979
MYPY_DIRS = .mypy_cache dmypy.json dmypy.sock
80-
TEST_DIRS = .cache .coverage .pytest_cache htmlcov
80+
TEST_DIRS = .cache .pytest_cache htmlcov
81+
TEST_FILES = .coverage coverage.xml
8182

8283
.PHONY: clean-build
8384
clean-build: ## Clean build artifacts
@@ -108,6 +109,7 @@ clean-ruff: ## Clean ruff artifacts
108109
clean-test: ## Clean test artifacts
109110
@echo "🚀 Removing test artifacts"
110111
@uv run python -c "import shutil; import os; [shutil.rmtree(d, ignore_errors=True) for d in '$(TEST_DIRS)'.split() if os.path.isdir(d)]"
112+
@uv run python -c "from pathlib import Path; [Path(f).unlink(missing_ok=True) for f in '$(TEST_FILES)'.split()]"
111113

112114
.PHONY: clean
113115
clean: clean-build clean-docs clean-mypy clean-pycache clean-ruff clean-test ## Clean all artifacts

tests/test_run_pyscript.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Unit/functional testing for run_pytest in cmd2"""
22

3-
import builtins
43
import os
54
from unittest import (
65
mock,
@@ -43,9 +42,10 @@ def test_run_pyscript_with_nonexist_file(base_app) -> None:
4342
assert base_app.last_result is False
4443

4544

46-
def test_run_pyscript_with_non_python_file(base_app, request) -> None:
47-
m = mock.MagicMock(name='input', return_value='2')
48-
builtins.input = m
45+
def test_run_pyscript_with_non_python_file(base_app, request, monkeypatch) -> None:
46+
# Mock out the read_input call so we don't actually wait for a user's response on stdin
47+
read_input_mock = mock.MagicMock(name='read_input', return_value='2')
48+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
4949

5050
test_dir = os.path.dirname(request.module.__file__)
5151
filename = os.path.join(test_dir, 'scripts', 'help.txt')
@@ -55,13 +55,13 @@ def test_run_pyscript_with_non_python_file(base_app, request) -> None:
5555

5656

5757
@pytest.mark.parametrize('python_script', odd_file_names)
58-
def test_run_pyscript_with_odd_file_names(base_app, python_script) -> None:
58+
def test_run_pyscript_with_odd_file_names(base_app, python_script, monkeypatch) -> None:
5959
"""Pass in file names with various patterns. Since these files don't exist, we will rely
6060
on the error text to make sure the file names were processed correctly.
6161
"""
62-
# Mock input to get us passed the warning about not ending in .py
63-
input_mock = mock.MagicMock(name='input', return_value='1')
64-
builtins.input = input_mock
62+
# Mock read_input to get us passed the warning about not ending in .py
63+
read_input_mock = mock.MagicMock(name='read_input', return_value='1')
64+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
6565

6666
_out, err = run_cmd(base_app, f"run_pyscript {quote(python_script)}")
6767
err = ''.join(err)

0 commit comments

Comments
 (0)