Skip to content

Commit bec09a8

Browse files
committed
add tests for config
1 parent 1251708 commit bec09a8

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/snippet/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def get_cli_opts():
77
parser = argparse.ArgumentParser()
8-
parser.add_argument('config', help='path to config file')
8+
parser.add_argument('--config', type=str, help='path to config file')
99
return parser
1010

1111

src/snippet/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ def get_config(config_path=None, **options):
3434
new_options = {}
3535
if config_path or os.environ.get('SNIPPET_CONFIG_PATH'):
3636
with open(config_path) as f:
37-
new_options.update(toml.load(f))
37+
config_file_contents = toml.load(f)
38+
snippet_config = config_file_contents['snippet']
39+
new_options.update(snippet_config)
3840
new_options.update(options)
3941
config = Config()
40-
for k, v in new_options:
42+
for k, v in new_options.items():
4143
setattr(config, k, v)
4244
return config

tests/test_config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import sys
5+
import textwrap
6+
import unittest
7+
8+
import snippet
9+
10+
11+
class Test(unittest.TestCase):
12+
tmpdir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'tmp_test_dir')
13+
14+
@classmethod
15+
def setUpClass(cls):
16+
# use a plain directory not-really-in-tmp to avoid cross-process perms issues in windows
17+
os.makedirs(cls.tmpdir)
18+
cls.tmp_fp = os.path.join(cls.tmpdir, 'config.toml')
19+
with open(cls.tmp_fp, 'w') as fh:
20+
fh.write(textwrap.dedent("""
21+
[snippet]
22+
input_glob = '*'
23+
end_flag = 'custom value'
24+
stop_on_first_failure = true
25+
foo = 'bar'
26+
""").lstrip())
27+
28+
@classmethod
29+
def tearDownClass(cls):
30+
shutil.rmtree(cls.tmpdir)
31+
32+
def test_config_from_file(self):
33+
config = snippet.config.get_config(config_path=self.tmp_fp)
34+
self.assertEqual(config.end_flag, 'custom value')
35+
self.assertEqual(config.foo, 'bar')
36+
37+
def test_config_from_cli(self):
38+
with self.assertRaises(subprocess.CalledProcessError) as ctx:
39+
subprocess.check_output(
40+
[sys.executable, '-m', 'snippet', '--config', self.tmp_fp],
41+
cwd=self.tmpdir,
42+
stderr=subprocess.STDOUT
43+
)
44+
self.assertIn('snippet.exceptions', str(ctx.exception.output))

tmp_test_dirs/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[snippet]
2+
input_glob = '*.py'
3+
end_flag = 'custom value'
4+
stop_on_first_failure = true
5+
foo = 'bar'

0 commit comments

Comments
 (0)