Skip to content

Commit 9d9e449

Browse files
committed
Added unit test
1 parent 96d006d commit 9d9e449

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/test_history.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,27 @@ def test_history_file_is_directory(capsys):
638638
_, err = capsys.readouterr()
639639
assert 'is a directory' in err
640640

641+
def test_history_can_create_directory(mocker):
642+
# Mock out atexit.register so the persistent file doesn't written when this function
643+
# exists because we will be deleting the directory it needs to go to.
644+
mock_register = mocker.patch('atexit.register')
645+
646+
# Create a temp path for us to use and let it get deleted
647+
with tempfile.TemporaryDirectory() as test_dir:
648+
pass
649+
assert not os.path.isdir(test_dir)
650+
651+
# Add some subdirectories for the complete history file directory
652+
hist_file_dir = os.path.join(test_dir, 'subdir1', 'subdir2')
653+
hist_file = os.path.join(hist_file_dir, 'hist_file')
654+
655+
# Make sure cmd2 creates the history file directory
656+
cmd2.Cmd(persistent_history_file=hist_file)
657+
assert os.path.isdir(hist_file_dir)
658+
659+
# Cleanup
660+
os.rmdir(hist_file_dir)
661+
641662
def test_history_cannot_create_directory(mocker, capsys):
642663
mock_open = mocker.patch('os.makedirs')
643664
mock_open.side_effect = OSError

0 commit comments

Comments
 (0)