nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit 37b690f5a1dcad859ad71eeba70587728916f873
parent bae28cc30623c8e1da63e7233afd13912ce76cb7
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Thu, 13 Sep 2018 05:21:43 -0400

Properly mock config file

Diffstat:
Mtests/test_config.py | 105++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 66 insertions(+), 39 deletions(-)

diff --git a/tests/test_config.py b/tests/test_config.py @@ -1,79 +1,106 @@ # -*- coding: utf-8 -*- import os +import subprocess import sys from nncli.config import Config from pytest import raises +def mock_config_file(mocker, file_contents): + """mock the file and configparser 'enumerate' iterator""" + mock_cfg = mocker.mock_open( + read_data='\n'.join(file_contents) + ) + mocker.patch('configparser.open', mock_cfg) + mocker.patch('configparser.enumerate', new=mocker.Mock( + return_value=enumerate(file_contents, start=1) + )) + return mock_cfg + def test_init(mocker): - mocker.patch('subprocess.check_output') + """test nominal initialization""" + mock_cfg = mock_config_file( + mocker, + [ + '[nncli]', + 'cfg_nn_username=user', + 'cfg_nn_password_eval=password_cmd', + 'cfg_nn_host=nextcloud.example.org' + ]) + mocker.patch('subprocess.check_output', + new=mocker.Mock(return_value='yes\n')) + config = Config() - if sys.platform == 'linux': - assert config.config_home == os.path.join(os.path.expanduser('~'), \ - '.config', 'nncli') - assert config.cache_home == os.path.join(os.path.expanduser('~'), \ - '.cache', 'nncli') - if sys.platform == 'darwin': - assert config.config_home == os.path.join(os.path.expanduser('~'), \ - 'Library', 'Preferences', 'nncli') - assert config.cache_home == os.path.join(os.path.expanduser('~'), \ - 'Library', 'Caches', 'nncli') - - -def test_custom_file(): - with open('test_cfg', 'w') as config_file: - config_file.write('[nncli]\n') - config_file.write('cfg_nn_username=user\n') - config_file.write('cfg_nn_password=password\n') - config_file.write('cfg_nn_host=nextcloud.example.org\n') + mock_cfg.assert_called_once() + subprocess.check_output.assert_called_once() + assert config.get_config('nn_password') == 'yes' - config = Config('test_cfg') - os.remove('test_cfg') +def test_custom_file(mocker): + """test with a supplied (custom) config file""" + mock_cfg = mock_config_file(mocker, + [ + '[nncli]', + 'cfg_nn_username=user', + 'cfg_nn_password=password', + 'cfg_nn_host=nextcloud.example.org' + ]) -def test_bad_password_eval(): - with open('test_cfg', 'w') as config_file: - config_file.write('[nncli]\n') - config_file.write('cfg_nn_username=user\n') - config_file.write('cfg_nn_password_eval=password\n') - config_file.write('cfg_nn_host=nextcloud.example.org\n') + config = Config('test_cfg') + mock_cfg.assert_called_once_with('test_cfg', encoding=None) + assert config.get_config('nn_username') == 'user' + assert config.get_config('nn_password') == 'password' + +def test_bad_password_eval(mocker): + """test failed call to password eval""" + mock_cfg = mock_config_file(mocker, + [ + '[nncli]', + 'cfg_nn_username=user', + 'cfg_nn_password_eval=password', + 'cfg_nn_host=nextcloud.example.org' + ]) with raises(SystemExit): config = Config('test_cfg') - os.remove('test_cfg') -def test_empty_config(): - with open('test_cfg', 'w') as config_file: - config_file.write('\n') +def test_empty_config(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') - os.remove('test_cfg') -def test_get_config(): +def test_get_config(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_config('sort_mode') == 'date' -def test_get_config_descr(): +def test_get_config_descr(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_config_descr('sort_mode') == 'Sort mode' -def test_get_keybind(): +def test_get_keybind(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_keybind('help') == 'h' -def test_get_keybind_use(): +def test_get_keybind_use(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_keybind_use('help') == [ 'common' ] -def test_get_keybind_descr(): +def test_get_keybind_descr(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_keybind_descr('help') == 'Help' -def test_get_color(): +def test_get_color(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_color('default_fg') == 'default' -def test_get_color_descr(): +def test_get_color_descr(mocker): + mock_cfg = mock_config_file(mocker, []) config = Config('test_cfg') assert config.get_color_descr('default_fg') == 'Default fg'