8000 test to show config priority behaves correctly · ARMmbed/snippet@81231a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81231a4

Browse files
committed
test to show config priority behaves correctly
1 parent 1295121 commit 81231a4

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

src/snippet/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
def get_cli_opts():
99
parser = argparse.ArgumentParser()
10-
parser.add_argument('--config', type=str,
11-
help='path to config file')
10+
parser.add_argument('--config', type=str, action='append',
11+
help='paths (or globs) to config files')
1212
parser.add_argument('dir', nargs='?', default=os.getcwd(),
13-
help='path to project root, used by relative paths in config [cwd]')
13+
help='path to project root, used by any relative paths in loaded configs [cwd]')
1414
parser.add_argument('-v', '--verbosity', action='count', default=0,
1515
help='increase output verbosity')
1616
return parser
@@ -21,7 +21,7 @@ def run_from_cli():
2121
log_level = logging.WARNING - 10 * args.verbosity
2222
logging.basicConfig(level=log_level)
2323
snippet.main(snippet.config.get_config(
24-
config_path=args.config,
24+
config_paths=args.config,
2525
project_root=args.dir,
2626
))
2727

src/snippet/config.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,32 @@ class Config:
3939
stop_on_first_failure = False # fail early
4040

4141

42-
def find_config(root):
43-
return glob.glob(os.path.join(root, '**', '*.toml'), recursive=True)
42+
def find_configs(glob_patterns):
43+
configs = []
44+
for glob_pattern in glob_patterns:
45+
configs.extend(glob.glob(glob_pattern, recursive=True))
46+
return configs
4447

4548

46-
def get_config(config_path=None, **options):
49+
def config_paths_from_env():
50+
env_var = os.environ.get('SNIPPET_CONFIG_PATH')
51+
return list(env_var) if env_var else []
52+
53+
54+
def get_config(config_paths=None, **options):
4755
config = Config()
4856
project_root = os.path.abspath(options.get('project_root', config.project_root))
4957

5058
new_options = {}
51-
config_path = config_path or os.environ.get('SNIPPET_CONFIG_PATH')
52-
toml_files = [config_path] if config_path else find_config(root=project_root)
53-
for toml_file in toml_files:
59+
60+
config_paths = config_paths or []
61+
config_paths.extend(config_paths_from_env())
62+
63+
# fallback option - search the project directory
64+
if not config_paths:
65+
config_paths.append(os.path.join(project_root, '**', '*.toml'))
66+
67+
for toml_file in find_configs(glob_patterns=config_paths):
5468
logging.debug('trying config from %s', toml_file)
5569
with open(toml_file) as f:
5670
try:

tests/samples/config_fixture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
```python
22
# example: this config is itself an example
3-
input_glob = '*'
3+
input_glob = 'does not match anything'
44

55
stop_on_first_failure = true
66
```

tests/test_config.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ def setUpClass(cls):
2323
fh.write(textwrap.dedent("""
2424
[snippet]
2525
# an example: this config is itself an example
26-
input_glob = '*'
26+
input_glob = 'does not match anything'
2727
2828
stop_on_first_failure = true
2929
end_flag = 'custom value'
3030
3131
foo = 'bar'
32+
fizz = 'buzz'
33+
""").lstrip())
34+
35+
cls.tmp_fp_2 = os.path.join(tmp_test_dir, 'config2.toml')
36+
with open(cls.tmp_fp_2, 'w') as fh:
37+
fh.write(textwrap.dedent("""
38+
[snippet]
39+
input_glob = 'config.toml'
40+
41+
foo = 'baz'
3242
""").lstrip())
3343

3444
@classmethod
@@ -37,14 +47,21 @@ def tearDownClass(cls):
3747

3848
def test_config_from_file(self):
3949
# explicitly load config from a file
40-
config = snippet.config.get_config(config_path=self.tmp_fp)
50+
config = snippet.config.get_config(config_paths=[self.tmp_fp])
4151
self.assertEqual(config.end_flag, 'custom value')
4252
self.assertEqual(config.foo, 'bar')
53+
self.assertEqual(config.fizz, 'buzz')
54+
55+
def test_config_from_multi_globs(self):
56+
# explicitly load from two files
57+
config = snippet.config.get_config(config_paths=[self.tmp_fp, self.tmp_fp_2])
58+
self.assertEqual(config.foo, 'baz')
59+
self.assertEqual(config.fizz, 'buzz')
4360

4461
def test_config_from_cli(self):
4562
# load config when run as a module
4663
subprocess.check_call(
47-
[sys.executable, '-m', 'snippet', tmp_test_dir, '--config', self.tmp_fp],
64+
[sys.executable, '-m', 'snippet', tmp_test_dir, '--config', self.tmp_fp, '--config', self.tmp_fp_2],
4865
stderr=subprocess.STDOUT
4966
)
5067

@@ -58,4 +75,4 @@ def test_auto_config(self):
5875
# load config, without explicitly setting the config path
5976
config = snippet.config.get_config()
6077
self.assertEqual(config.end_flag, 'custom value')
61-
self.assertEqual(config.foo, 'bar')
78+
self.assertEqual(config.fizz, 'buzz')

0 commit comments

Comments
 (0)
0