summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-07-12 09:43:51 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-07-12 09:43:51 -0400
commit87ab9c8a86d51966fee94f53a1b136d231d764e5 (patch)
tree2109d0d318ead59b2c2e658d0aa0b5e717902e82 /tests/test_cmd2.py
parent95e6dfa67edd6bfe7cd6c19e484318ccf5cd6ab5 (diff)
downloadcmd2-git-87ab9c8a86d51966fee94f53a1b136d231d764e5.tar.gz
Added a session-scoped test fixture to ensure teardown of the persistent history file
Also: - Switched to writing the file in the temp directory
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index b695afbc..25d1db3f 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1826,14 +1826,21 @@ def test_onecmd_raw_str_quit(base_app):
assert out == ''
-def test_existing_history_file(capsys, request):
+@pytest.fixture(scope="session")
+def hist_file():
+ fd, filename = tempfile.mkstemp(prefix='hist_file', suffix='.txt')
+ os.close(fd)
+ yield filename
+ # teardown code
+ try:
+ os.remove(filename)
+ except FileNotFoundError:
+ pass
+
+def test_existing_history_file(hist_file, capsys):
import atexit
import readline
- # Create path to a history file
- test_dir = os.path.dirname(request.module.__file__)
- hist_file = os.path.join(test_dir, 'hist_file')
-
# Create the history file before making cmd2 app
with open(hist_file, 'w'):
pass
@@ -1842,21 +1849,20 @@ def test_existing_history_file(capsys, request):
app = cmd2.Cmd(persistent_history_file=hist_file)
out, err = capsys.readouterr()
+ # Make sure there were no errors
+ assert err == ''
+
# Unregister the call to write_history_file that cmd2 did
atexit.unregister(readline.write_history_file)
- # Remove created history file and make sure there were no errors
+ # Remove created history file
os.remove(hist_file)
- assert err == ''
-def test_new_history_file(capsys, request):
+
+def test_new_history_file(hist_file, capsys):
import atexit
import readline
- # Create path to a history file
- test_dir = os.path.dirname(request.module.__file__)
- hist_file = os.path.join(test_dir, 'hist_file')
-
# Remove any existing history file
try:
os.remove(hist_file)
@@ -1867,12 +1873,14 @@ def test_new_history_file(capsys, request):
app = cmd2.Cmd(persistent_history_file=hist_file)
out, err = capsys.readouterr()
+ # Make sure there were no errors
+ assert err == ''
+
# Unregister the call to write_history_file that cmd2 did
atexit.unregister(readline.write_history_file)
- # Remove created history file and make sure there were no errors
+ # Remove created history file
os.remove(hist_file)
- assert err == ''
def test_bad_history_file_path(capsys, request):
# Use a directory path as the history file