Skip to content

Commit 87ab9c8

Browse files
committed
Added a session-scoped test fixture to ensure teardown of the persistent history file
Also: - Switched to writing the file in the temp directory
1 parent 95e6dfa commit 87ab9c8

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

tests/test_cmd2.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,14 +1826,21 @@ def test_onecmd_raw_str_quit(base_app):
18261826
assert out == ''
18271827

18281828

1829-
def test_existing_history_file(capsys, request):
1829+
@pytest.fixture(scope="session")
1830+
def hist_file():
1831+
fd, filename = tempfile.mkstemp(prefix='hist_file', suffix='.txt')
1832+
os.close(fd)
1833+
yield filename
1834+
# teardown code
1835+
try:
1836+
os.remove(filename)
1837+
except FileNotFoundError:
1838+
pass
1839+
1840+
def test_existing_history_file(hist_file, capsys):
18301841
import atexit
18311842
import readline
18321843

1833-
# Create path to a history file
1834-
test_dir = os.path.dirname(request.module.__file__)
1835-
hist_file = os.path.join(test_dir, 'hist_file')
1836-
18371844
# Create the history file before making cmd2 app
18381845
with open(hist_file, 'w'):
18391846
pass
@@ -1842,21 +1849,20 @@ def test_existing_history_file(capsys, request):
18421849
app = cmd2.Cmd(persistent_history_file=hist_file)
18431850
out, err = capsys.readouterr()
18441851

1852+
# Make sure there were no errors
1853+
assert err == ''
1854+
18451855
# Unregister the call to write_history_file that cmd2 did
18461856
atexit.unregister(readline.write_history_file)
18471857

1848-
# Remove created history file and make sure there were no errors
1858+
# Remove created history file
18491859
os.remove(hist_file)
1850-
assert err == ''
18511860

1852-
def test_new_history_file(capsys, request):
1861+
1862+
def test_new_history_file(hist_file, capsys):
18531863
import atexit
18541864
import readline
18551865

1856-
# Create path to a history file
1857-
test_dir = os.path.dirname(request.module.__file__)
1858-
hist_file = os.path.join(test_dir, 'hist_file')
1859-
18601866
# Remove any existing history file
18611867
try:
18621868
os.remove(hist_file)
@@ -1867,12 +1873,14 @@ def test_new_history_file(capsys, request):
18671873
app = cmd2.Cmd(persistent_history_file=hist_file)
18681874
out, err = capsys.readouterr()
18691875

1876+
# Make sure there were no errors
1877+
assert err == ''
1878+
18701879
# Unregister the call to write_history_file that cmd2 did
18711880
atexit.unregister(readline.write_history_file)
18721881

1873-
# Remove created history file and make sure there were no errors
1882+
# Remove created history file
18741883
os.remove(hist_file)
1875-
assert err == ''
18761884

18771885
def test_bad_history_file_path(capsys, request):
18781886
# Use a directory path as the history file

0 commit comments

Comments
 (0)