From e9d7351609026196823e75fcec8a907c5ed2e7c3 Mon Sep 17 00:00:00 2001 From: William Pietri Date: Mon, 4 Dec 2017 20:54:36 -0800 Subject: [PATCH] Adding tests for reading and writing config. --- sucks/cli.py | 3 ++- tests/test_cli.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sucks/cli.py b/sucks/cli.py index 4bc8240..a3d6407 100644 --- a/sucks/cli.py +++ b/sucks/cli.py @@ -58,9 +58,10 @@ def read_config(): def write_config(config): + os.makedirs(os.path.dirname(config_file()), exist_ok=True) with open(config_file(), 'w') as fp: for key in config: - fp.write(key + '=' + config[key] + "\n") + fp.write(key + '=' + str(config[key]) + "\n") def current_country(): diff --git a/tests/test_cli.py b/tests/test_cli.py index ef3add4..8d55c3c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,6 @@ +import tempfile +from unittest.mock import Mock, patch + import requests_mock from nose.tools import * @@ -12,11 +15,12 @@ def test_config_file_name(): assert_true(re.match(r'/.+/\w+/.config/sucks.conf', config_file())) -# def test_write_and_read_config(): -# config1 = {'a':1, 'b':2} -# write_config(config1) -# config2 = read_config() -# assert_equals(config1, config2) +def test_write_and_read_config(): + with patch('sucks.cli.config_file', Mock(return_value=os.path.join(tempfile.mkdtemp(), 'some_other_dir', 'sucks.conf'))): + write_config({'a': "ayyy", 'b': 2}) + config2 = read_config() + assert_equals(config2['a'], 'ayyy') + assert_equals(config2['b'], '2') def test_frequency_param_type(): t = FREQUENCY