Skip to content

Commit 78fd8b9

Browse files
add more tests for template render and assets
increases coverage to 94%
1 parent 90bfca4 commit 78fd8b9

File tree

7 files changed

+214
-72
lines changed

7 files changed

+214
-72
lines changed

configme/assets.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Asset management.
55
"""
66

7+
from io import open as io_open
78

89
from os import makedirs
910
from os.path import basename
@@ -59,7 +60,7 @@ def location(self, location, location_subject,
5960
return location
6061

6162
# ....................................................................... #
62-
def join_path(self, path_parts, _os_path_join=join):
63+
def path_join(self, path_parts, _os_path_join=join):
6364
"""
6465
Join and return the folder path from segments.
6566
"""
@@ -157,3 +158,13 @@ def create_folder(self, folder_path, _os_makedirs=makedirs):
157158
raise LocationCreationError(msg)
158159

159160
return folder_path
161+
162+
# ....................................................................... #
163+
def write_to_file(self, file_path, content, _io_open=io_open):
164+
165+
# TODO: add error handling
166+
file_handler = _io_open(file_path, 'w')
167+
file_handler.write(content)
168+
file_handler.close()
169+
170+
return file_path

configme/cli/cli_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def run(self):
136136

137137
# and write config files out
138138
output_list = role.write_configs()
139-
139+
#try:
140+
# pass
140141
except ConfigMeException as err:
141142
# handle any recognizable erros in a CLI friendly fashion
142143
self.logger.error("Error: %s" % err.message)

configme/role.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Role(object):
3333
must be at least marginally human readable.
3434
3535
As such due to difficulty maintaining OS specific forbidden characters
36-
set, complying with ini file specifications, and keeping readability
37-
a set of forbidden characters have choosen.
36+
set, complying with INI file specifications, and keeping readability
37+
a set of forbidden characters have chosen.
3838
3939
The naming rules are:
4040
@@ -138,8 +138,8 @@ def output_folder_path(self):
138138
139139
:rtype: str/unicode
140140
"""
141-
join_path = self.config._asset_manager.join_path
142-
return join_path((self.config.output_path, self.suffixed_name))
141+
path_join = self.config._asset_manager.path_join
142+
return path_join((self.config.output_path, self.suffixed_name))
143143

144144
# ....................................................................... #
145145
@property
@@ -153,9 +153,9 @@ def settings_file_path(self):
153153
154154
:rtype: str/unicode
155155
"""
156-
join_path = self.config._asset_manager.join_path
156+
path_join = self.config._asset_manager.path_join
157157
file_name = "%s.%s" % (self.name, self.config.settings_file_extension)
158-
return join_path((self.config.settings_path, file_name))
158+
return path_join((self.config.settings_path, file_name))
159159

160160
# ....................................................................... #
161161
def __init__(self, config, name, suffix='', variables=None):

configme/template_renderer.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11

22
"""
3-
File Generator module.
3+
Template Renderers
44
"""
55

66
from abc import ABCMeta
77
from abc import abstractmethod
88

9-
from io import open as io_open
10-
119
from .exceptions import InvalidName
1210

1311

@@ -34,13 +32,14 @@ class BaseTemplateRenderer(object):
3432
must be at least marginally human readable.
3533
3634
As such due to difficulty maintaining OS specific forbidden characters
37-
set, complying with ini file specifications, and keeping readability
38-
a set of forbidden characters have choosen.
35+
set, complying with INI file specifications, and keeping readability
36+
a set of forbidden characters have chosen.
3937
4038
The naming rules are:
4139
4240
- name cannot start with a: `space` `/` `../` `./`
43-
- name cannot contain: `/../` `/./` `<` `>` `:` `"` `|` `?` `*`
41+
- name cannot contain:
42+
`/../` `/./` `<` `>` `:` `'` `"` `|` `?` `*` `\``
4443
4544
:type path: str
4645
@@ -58,7 +57,8 @@ class BaseTemplateRenderer(object):
5857
"""
5958

6059
_FORBIDDEN_START_CHARS = (" ", "/", "../", "./")
61-
_FORBIDDEN_CHARS = ("/../", "/./", "<", ">", ":", '"', "|", "?", "*")
60+
_FORBIDDEN_CHARS = \
61+
("/../", "/./", "<", ">", ":", "'", '"', "|", "?", "*", "`")
6262

6363
__metaclass__ = ABCMeta
6464

@@ -102,11 +102,11 @@ def output_file_path(self):
102102
:rtype: str
103103
"""
104104
asset_manager = self.config._asset_manager
105-
return asset_manager.join_path(
105+
return asset_manager.path_join(
106106
(self.role_output_folder_path, self.path))
107107

108108
# ....................................................................... #
109-
def create_output_folder(self):
109+
def write(self):
110110
"""
111111
Create the output folder specified in `output_folder_path`.
112112
If the parents for the folder do not exist they will be created as
@@ -126,10 +126,6 @@ def create_output_folder(self):
126126

127127
asset_manager = self.config._asset_manager
128128

129-
# if there is no folder for this config file just return the role path
130-
if asset_manager.path_dirname(self.path) == '':
131-
return self.role_output_folder_path
132-
133129
# otherwise get the folder path for the output file
134130
output_file_folder = asset_manager.path_dirname(self.output_file_path)
135131

@@ -140,7 +136,11 @@ def create_output_folder(self):
140136
# create folder
141137
asset_manager.create_folder(output_file_folder)
142138

143-
return output_file_folder
139+
# get the rendered config
140+
content = self.get_rendered_config()
141+
142+
# write to file
143+
return asset_manager.write_to_file(self.output_file_path, content)
144144

145145
# ....................................................................... #
146146
@abstractmethod
@@ -155,26 +155,7 @@ def get_rendered_config(self):
155155
:return: rendered template
156156
:rtype: str/unicode
157157
"""
158-
159-
pass
160-
161-
# ....................................................................... #
162-
def write(self, _io_open=io_open):
163-
"""
164-
Render and output config to file at `output_file_path`.
165-
166-
:return: path to the the generated config file.
167-
:rype: str
168-
"""
169-
170-
self.create_output_folder()
171-
172-
# TODO: exception handling
173-
file_handler = _io_open(self.output_file_path, 'w')
174-
file_handler.write(self.get_rendered_config())
175-
file_handler.close()
176-
177-
return self.output_file_path
158+
pass # pragma: no cover
178159

179160
# ....................................................................... #
180161
@classmethod

configme/tests/test_assets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def dummy_os_path_isdir(path):
7272
_os_path_isdir=dummy_os_path_isdir)
7373

7474
# ....................................................................... #
75-
def test_join_path(self):
75+
def test_path_join(self):
7676

7777
test_path_parts = ('test', 'path', 'parts')
7878

@@ -84,7 +84,7 @@ def dummy_os_path_join(*path_parts):
8484
asset_manager = self._makeOne()
8585

8686
self.assertEqual(
87-
asset_manager.join_path(test_path_parts,
87+
asset_manager.path_join(test_path_parts,
8888
_os_path_join=dummy_os_path_join),
8989
desired_result)
9090

configme/tests/test_role.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def dummy_asset_manager_maker():
104104
class DummyAssetManager(object):
105105

106106
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
107-
def join_path(self, path_parts):
107+
def path_join(self, path_parts):
108108
return '/'.join(path_parts)
109109

110110
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

0 commit comments

Comments
 (0)