diff options
author | Eric Lin <anselor@gmail.com> | 2018-05-19 15:54:08 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-05-19 15:54:08 -0400 |
commit | 1c1da0273faf3ab2316b4af9281cf15869b3da20 (patch) | |
tree | 100044bd28c56ab2b30f356e3ecfbd02af78d48b /tests/test_cmd2.py | |
parent | f44871b5d055651bb4991580b64b18b87568fd7a (diff) | |
download | cmd2-git-1c1da0273faf3ab2316b4af9281cf15869b3da20.tar.gz |
Changed some unit tests to use pytest-mock instead of mocker/monkeypatch because they were failing for me.
Added detection of ==SUPPRESS== in subcommand group names to avoid printing it in the help hint.
Added some examples to tab_autocompletion to demonstrate how to tie in to cmd2 path_complete
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 6e4a5a3e..89aa9d03 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -406,21 +406,26 @@ def test_history_output_file(base_app): content = normalize(f.read()) assert content == expected -def test_history_edit(base_app, monkeypatch): +def test_history_edit(base_app, mock): # Set a fake editor just to make sure we have one. We aren't really # going to call it due to the mock base_app.editor = 'fooedit' # Mock out the os.system call so we don't actually open an editor - m = mock.MagicMock(name='system') - monkeypatch.setattr("os.system", m) + count = [0] + + def fake_system(*args, **kwargs): + count[0] += 1 + + mock.patch.object(os, 'system', fake_system) # Run help command just so we have a command in history run_cmd(base_app, 'help') run_cmd(base_app, 'history -e 1') # We have an editor, so should expect a system call - m.assert_called_once() + assert count[0] == 1 + def test_history_run_all_commands(base_app): # make sure we refuse to run all commands as a default @@ -819,29 +824,36 @@ def test_edit_file_with_spaces(base_app, request, monkeypatch): # We think we have an editor, so should expect a system call m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename)) -def test_edit_blank(base_app, monkeypatch): +def test_edit_blank(base_app, mock): # Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock base_app.editor = 'fooedit' - # Mock out the os.system call so we don't actually open an editor - m = mock.MagicMock(name='system') - monkeypatch.setattr("os.system", m) + count = [0] + + def fake_system(*args, **kwargs): + count[0] += 1 + + mock.patch.object(os, 'system', fake_system) run_cmd(base_app, 'edit') # We have an editor, so should expect a system call - m.assert_called_once() + assert count[0] == 1 -def test_base_py_interactive(base_app): +def test_base_py_interactive(base_app, mock): # Mock out the InteractiveConsole.interact() call so we don't actually wait for a user's response on stdin - m = mock.MagicMock(name='interact') - InteractiveConsole.interact = m + count = [0] + + def fake_interact(*args, **kwargs): + count[0] += 1 + + mock.patch.object(InteractiveConsole, 'interact', fake_interact) run_cmd(base_app, "py") # Make sure our mock was called once and only once - m.assert_called_once() + assert count[0] == 1 def test_exclude_from_history(base_app, monkeypatch): |