diff --git a/singer/utils.py b/singer/utils.py index 4cd179d..3efdccf 100644 --- a/singer/utils.py +++ b/singer/utils.py @@ -185,6 +185,9 @@ def check_config(config, required_keys): if missing_keys: raise Exception("Config is missing required keys: {}".format(missing_keys)) +def update_config_file(config_path, new_config): + with open(config_path, 'w') as output: + output.write(json.dumps(new_config, indent=2)) def backoff(exceptions, giveup): """Decorates a function to retry up to 5 times using an exponential backoff diff --git a/tests/test_utils.py b/tests/test_utils.py index bb26da6..0783510 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,7 +3,7 @@ import pytz import logging import singer.utils as u - +import json class TestFormat(unittest.TestCase): def test_small_years(self): @@ -33,3 +33,22 @@ def test_exception_fn(self): def foo(): raise RuntimeError("foo") self.assertRaises(RuntimeError, foo) + +class TestUpdateConfig(unittest.TestCase): + def test_file_changed(self): + original_config = {'key_a': 'val_a'} + + with open('config.json', 'w') as file: + file.write(json.dumps(original_config)) + + changed_config = {'key_b': 'val_b'} + + u.update_config_file('config.json', changed_config) + + read_config = u.load_json('config.json') + + self.assertEqual(changed_config.get('key_b'), read_config.get('key_b')) + + def tearDown(self): + import os + os.remove('config.json')