Skip to content

Commit 2bd0eea

Browse files
committed
tests for helpers in compile command
1 parent eab9b4a commit 2bd0eea

2 files changed

Lines changed: 77 additions & 9 deletions

File tree

‎paths_cli/commands/compile.py‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,45 @@
22

33
from paths_cli.parsing.root_parser import parse
44
from paths_cli.parameters import OUTPUT_FILE
5+
from paths_cli.errors import MissingIntegrationError
6+
import importlib
57

6-
def import_module(module_name):
8+
def import_module(module_name, format_type=None, install=None):
79
try:
8-
mod = __import__(module_name)
10+
mod = importlib.import_module(module_name)
911
except ImportError:
10-
# TODO: better error handling
11-
raise
12+
if format_type is None:
13+
format_type = module_name
14+
15+
msg = "Unable to find a parser for f{format_type} on your system."
16+
if install is not None:
17+
msg += " Please install f{install} to use this format."
18+
19+
raise MissingIntegrationError(msg)
1220
return mod
1321

1422
def load_yaml(f):
15-
yaml = import_module('yaml')
23+
yaml = import_module('yaml', format_type="YAML", install="PyYAML")
1624
return yaml.load(f.read(), Loader=yaml.FullLoader)
1725

1826
def load_json(f):
1927
json = import_module('json') # this should never fail... std lib!
2028
return json.loads(f.read())
2129

22-
def load_toml(f):
23-
toml = import_module('toml')
24-
return toml.loads(f.read())
30+
# TODO: It looks like TOML was working with my test dict -- I'll have to see
31+
# if that's an issue with the TOML format or just something weird. I thought
32+
# TOML was equally as flexible (in theory) as JSON.
33+
# def load_toml(f):
34+
# toml = import_module('toml', format_type="TOML", install="toml")
35+
# return toml.loads(f.read())
36+
2537

2638
EXTENSIONS = {
2739
'yaml': load_yaml,
2840
'yml': load_yaml,
2941
'json': load_json,
3042
'jsn': load_json,
31-
'toml': load_toml,
43+
# 'toml': load_toml,
3244
}
3345

3446
def select_loader(filename):
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
import json
5+
6+
from paths_cli.commands.compile import *
7+
8+
def test_import_module():
9+
new_json = import_module('json')
10+
assert new_json is json
11+
12+
def test_import_module_error():
13+
with pytest.raises(MissingIntegrationError, match="Unable to find"):
14+
import_module('foobarbazqux')
15+
16+
_BASIC_DICT = {'foo': ['bar', {'baz': 10}], 'qux': 'quux', 'froob': 0.5}
17+
18+
def _std_dump(module):
19+
return module.dump
20+
21+
@pytest.mark.parametrize('mod_name', ['json', 'yaml'])
22+
def test_loaders(mod_name, tmpdir):
23+
mod = pytest.importorskip(mod_name)
24+
dump_getter = {
25+
'json': _std_dump,
26+
'yaml': _std_dump,
27+
'toml': _std_dump,
28+
}[mod_name]
29+
loader = {
30+
'json': load_json,
31+
'yaml': load_yaml,
32+
# 'toml': load_toml,
33+
}[mod_name]
34+
dump = dump_getter(mod)
35+
filename = tmpdir.join("temp." + mod_name)
36+
with open(filename, 'a+') as fp:
37+
dump(_BASIC_DICT, fp)
38+
fp.seek(0)
39+
assert loader(fp) == _BASIC_DICT
40+
41+
@pytest.mark.parametrize('ext', ['jsn', 'json', 'yaml', 'yml'])
42+
def test_select_loader(ext):
43+
if ext in ['jsn', 'json']:
44+
expected = load_json
45+
elif ext in ['yml', 'yaml']:
46+
expected = load_yaml
47+
48+
assert select_loader("filename." + ext) is expected
49+
pass
50+
51+
def test_select_loader_error():
52+
with pytest.raises(RuntimeError, match="Unknown file extension"):
53+
select_loader('foo.bar')
54+
55+
def test_compile():
56+
pass

0 commit comments

Comments
 (0)