Skip to content

Commit

Permalink
Merge pull request #65 from flomotlik/mandatory-fix
Browse files Browse the repository at this point in the history
Module and jinja function fixes
  • Loading branch information
flomotlik authored Sep 27, 2017
2 parents 45de0bf + 84bb652 commit 11840b0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ check-code:
integration-test:
py.test -s tests/integration

start-docker:
dev:
docker-compose build formica
docker-compose run formica bash

Expand Down
13 changes: 7 additions & 6 deletions formica/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def code_escape(source):

def mandatory(a):
from jinja2.runtime import Undefined

if isinstance(a, Undefined):
if isinstance(a, Undefined) or a is None:
raise FormicaArgumentException('Mandatory variable not set.')
return a


def resource(name):
name = ''.join(e for e in name.title() if e.isalnum())
return name
if name is None:
return ''
else:
return ''.join(e for e in name.title() if e.isalnum())


class Loader(object):
Expand Down Expand Up @@ -112,10 +113,10 @@ def merge(self, template, file):

def merge_variables(self, module_vars):
merged_vars = {}
for k, v in module_vars.items():
merged_vars[k] = v
for k, v in self.variables.items():
merged_vars[k] = v
for k, v in module_vars.items():
merged_vars[k] = v
return merged_vars

def load(self):
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def test_supports_extra_jinja_vars(tmpdir):
assert actual == {"Description": "Bar"}


def test_module_vars_have_precedence_over_global(tmpdir):
load = Loader(variables={'test': 'bar'})
example = '{"Description": "{{ test }}"}'
with Path(tmpdir):
os.mkdir('moduledir')
with open('moduledir/test.template.json', 'w') as f:
f.write(example)
with open('test.template.json', 'w') as f:
f.write('{"Modules": [{"path": "moduledir", "vars": {"test": "baz"}}]}')
load.load()
actual = json.loads(load.template())
assert actual == {"Description": "baz"}


def test_supports_resouce_command(load, tmpdir):
example = '{"Description": "{{ \'ABC%123.\' | resource }}"}'
with Path(tmpdir):
Expand All @@ -110,6 +124,16 @@ def test_supports_resouce_command(load, tmpdir):
assert actual == {"Description": "Abc123"}


def test_resouce_command_supports_none_value(load, tmpdir):
example = '{"Description": "{{ None | resource }}"}'
with Path(tmpdir):
with open('test.template.json', 'w') as f:
f.write(example)
load.load()
actual = json.loads(load.template())
assert actual == {"Description": ""}


def test_template_loads_submodules(load, tmpdir):
example = '{"Description": "{{ \'test\'}}"}'
with Path(tmpdir):
Expand Down Expand Up @@ -210,6 +234,18 @@ def test_mandatory_filter_throws_exception(load, tmpdir):
load.load()


def test_mandatory_filter_throws_exception_in_module(load, tmpdir):
example = '{"Description": "{{ test | mandatory }}"}'
with Path(tmpdir):
os.mkdir('moduledir')
with open('moduledir/test.template.json', 'w') as f:
f.write(example)
with open('test.template.json', 'w') as f:
f.write('{"Modules": [{"path": "moduledir", "vars": {"test": {{ test }} }}]}')
with pytest.raises(SystemExit):
load.load()


def test_wrong_key_throws_exception(load, tmpdir):
example = '{"SomeKey": "test"}'
with Path(tmpdir):
Expand Down

0 comments on commit 11840b0

Please sign in to comment.