Skip to content

Commit

Permalink
Merge pull request #153 from besbes/feature/load-case-insensitive
Browse files Browse the repository at this point in the history
Use regex to be case-insensitive regardless of file system (#151)
  • Loading branch information
flomotlik authored Jun 4, 2020
2 parents 8e6d502 + e31741f commit ba7480d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions formica/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import sys
import re

import logging
import yaml
Expand Down Expand Up @@ -137,7 +138,7 @@ def merge(self, template, file):
logger.info("File {} is empty".format(file))

def load_module(self, module_path, element_key, element_value):
module_path = self.path + "/" + "/".join(module_path.lower().split("::"))
module_path = self.path + "/" + "/".join(module_path.split("::"))
file_name = "*"

if not os.path.isdir(module_path):
Expand All @@ -164,7 +165,8 @@ def load(self):
files = []

for file_type in FILE_TYPES:
files.extend(glob.glob("{}/{}.template.{}".format(self.path, self.filename, file_type)))
pattern = re.compile(fnmatch.translate("{}.template.{}".format(self.filename, file_type)), re.IGNORECASE)
files.extend([filename for filename in os.listdir(self.path) if pattern.match(filename)])

if not files:
logger.info("Could not find any template files in {}".format(self.path))
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ def test_template_loads_submodules_with_specific_file(load, tmpdir):
assert actual == {"Description": "test"}


def test_template_loads_submodule_case_insensitive(load, tmpdir):
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(json.dumps({'Resources': {'TestResource': {'From': 'Moduledir'}}}))
load.load()
actual = json.loads(load.template())
assert actual == {"Description": "test"}


def test_template_loads_submodules_with_specific_file(load, tmpdir):
example = '{"Description": "{{ \'test\'}}"}'
with Path(tmpdir):
os.mkdir('moduledir')
with open('moduledir/TesT.template.json', 'w') as f:
f.write(example)
with open('moduledir/test2.template.yml', 'w') as f:
f.write('Sometestthing: does not fail')
with open('test.template.json', 'w') as f:
f.write(json.dumps({'Resources': {'TestResource': {'From': 'ModuleDir::tESt'}}}))
load.load()
actual = json.loads(load.template())
assert actual == {"Description": "test"}


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

0 comments on commit ba7480d

Please # to comment.