diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_history.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/tests/test_history.py b/tests/test_history.py index 2da34cfd..9752ed07 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -638,6 +638,36 @@ def test_history_file_is_directory(capsys): _, err = capsys.readouterr() assert 'is a directory' in err +def test_history_can_create_directory(mocker): + # Mock out atexit.register so the persistent file doesn't written when this function + # exists because we will be deleting the directory it needs to go to. + mock_register = mocker.patch('atexit.register') + + # Create a temp path for us to use and let it get deleted + with tempfile.TemporaryDirectory() as test_dir: + pass + assert not os.path.isdir(test_dir) + + # Add some subdirectories for the complete history file directory + hist_file_dir = os.path.join(test_dir, 'subdir1', 'subdir2') + hist_file = os.path.join(hist_file_dir, 'hist_file') + + # Make sure cmd2 creates the history file directory + cmd2.Cmd(persistent_history_file=hist_file) + assert os.path.isdir(hist_file_dir) + + # Cleanup + os.rmdir(hist_file_dir) + +def test_history_cannot_create_directory(mocker, capsys): + mock_open = mocker.patch('os.makedirs') + mock_open.side_effect = OSError + + hist_file_path = os.path.join('fake_dir', 'file') + cmd2.Cmd(persistent_history_file=hist_file_path) + _, err = capsys.readouterr() + assert 'Error creating persistent history file directory' in err + def test_history_file_permission_error(mocker, capsys): mock_open = mocker.patch('builtins.open') mock_open.side_effect = PermissionError @@ -645,7 +675,7 @@ def test_history_file_permission_error(mocker, capsys): cmd2.Cmd(persistent_history_file='/tmp/doesntmatter') out, err = capsys.readouterr() assert not out - assert 'can not read' in err + assert 'Can not read' in err def test_history_file_conversion_no_truncate_on_init(hist_file, capsys): # make sure we don't truncate the plain text history file on init @@ -720,4 +750,4 @@ def test_persist_history_permission_error(hist_file, mocker, capsys): app._persist_history() out, err = capsys.readouterr() assert not out - assert 'can not write' in err + assert 'Can not write' in err |