diff options
-rw-r--r-- | gd_diff.py | 14 | ||||
-rw-r--r-- | tests/test_gd_diff.py | 23 |
2 files changed, 37 insertions, 0 deletions
@@ -6,6 +6,7 @@ # gns-deb-diff is under the Public Domain. See # <https://creativecommons.org/publicdomain/zero/1.0> +import json import os import re import shlex @@ -229,6 +230,19 @@ def config_file(): return os.path.join(config_dir(), 'config') +def read_config_file(): + """Return config as a Python Object; False when config does not \ + exist. + + """ + cf = config_file() + + if not os.path.isfile(cf): + return False + + return json.load(open(cf, 'r')) + + def pkgs_dir(): """Return the `pkgs` directory. diff --git a/tests/test_gd_diff.py b/tests/test_gd_diff.py index cc7fc4a..67d9b8d 100644 --- a/tests/test_gd_diff.py +++ b/tests/test_gd_diff.py @@ -6,6 +6,7 @@ # gns-deb-diff is under the Public Domain. See # <https://creativecommons.org/publicdomain/zero/1.0> +import json import os import subprocess import sys @@ -247,6 +248,28 @@ class TestGdDiff(object): 'gns-deb-diff', 'config')) + def test_read_config_file_fail(self): + config = read_config_file() + assert_equal(config, False) + + + def test_read_config_file_success(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + c_file = config_file() + + # first write sample config file. + json.dump({'user': 'usrnm', 'pass': 'weasaspeciesrfckd'}, + open(c_file, 'w')) + + # now on to the test. + config = read_config_file() + assert_equal(config['user'], 'usrnm') + assert_equal(config['pass'], 'weasaspeciesrfckd') + + def test_pkgs_dir(self): def env(e): return self.test_home |