From 86d821c494800215007e7800a6553b171e897822 Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 12 Jul 2018 21:31:47 +0100 Subject: [PATCH 1/5] :bug: take template option from command line. #30 --- .moban.cd/changelog.yml | 1 + CHANGELOG.rst | 2 ++ moban/copier.py | 10 +++------- moban/mobanfile.py | 22 ++++++++++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index f6c7f274..1ec7d2bd 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -5,6 +5,7 @@ releases: - action: Added details: - "`#32`: option 1 copy a directory without its subdirectories." + - "`#30`: command line template option is ignore when a moban file is present" date: pending release version: 0.2.4 - changes: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7799ebda..047a965e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,8 @@ Added #. `#32 `_: option 1 copy a directory without its subdirectories. +#. `#30 `_: command line template + option is ignore when a moban file is present 0.2.3 - 10-07-2018 -------------------------------------------------------------------------------- diff --git a/moban/copier.py b/moban/copier.py index 15750177..e94945df 100644 --- a/moban/copier.py +++ b/moban/copier.py @@ -16,7 +16,7 @@ def copy_files(self, file_list): for dest, src in _iterate_list_of_dicts(file_list): src_path = self._get_src_file(src) if src_path is None: - if src.endswith('**'): + if src.endswith("**"): source_dir = src[:-3] src_path = self._get_src_file(source_dir) if src_path: @@ -60,9 +60,7 @@ def _copy_dir(self, source, actual_source_path, dest): self._increment_file_count() src_file_under_dir = os.path.join(source, file_name) dest_file_under_dir = os.path.join(dest, file_name) - new_file_pair.append( - {dest_file_under_dir: src_file_under_dir} - ) + new_file_pair.append({dest_file_under_dir: src_file_under_dir}) if len(new_file_pair) > 0: self.copy_files(new_file_pair) @@ -72,9 +70,7 @@ def _copy_dir_recursively(self, source, actual_source_path, dest): self._increment_file_count() src_file_under_dir = os.path.join(source, file_name) dest_file_under_dir = os.path.join(dest, file_name) - new_file_pair.append( - {dest_file_under_dir: src_file_under_dir} - ) + new_file_pair.append({dest_file_under_dir: src_file_under_dir}) self.copy_files(new_file_pair) def _copy(self, src_path, dest): diff --git a/moban/mobanfile.py b/moban/mobanfile.py index 24a8c24d..8782cd82 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -23,6 +23,7 @@ def find_default_moban_file(): def handle_moban_file_v1(moban_file_configurations, command_line_options): merged_options = None + target = extract_target(command_line_options) if constants.LABEL_CONFIG in moban_file_configurations: merged_options = merge( command_line_options, @@ -35,6 +36,8 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options): targets = moban_file_configurations.get(constants.LABEL_TARGETS) if targets: + if target: + targets += target number_of_templated_files = handle_targets(merged_options, targets) else: number_of_templated_files = 0 @@ -101,3 +104,22 @@ def handle_plugin_dirs(plugin_dirs): for plugin in plugins: plugin_module = os.path.basename(plugin_dir) + "." + plugin do_import(plugin_module) + + +def extract_target(options): + template = options.get(constants.LABEL_TEMPLATE) + config = options.get(constants.LABEL_CONFIG) + output = options.get(constants.LABEL_OUTPUT) + result = [] + if template: + if config: + result = [ + { + constants.LABEL_TEMPLATE: template, + constants.LABEL_CONFIG: config, + constants.LABEL_OUTPUT: output, + } + ] + else: + result = [{template: output}] + return result From 1c80d8f456074af4909e87b3b3343f39e86c7177 Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 12 Jul 2018 22:33:38 +0100 Subject: [PATCH 2/5] :microscope: test the feature --- .../test_command_line_options.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/integration_tests/test_command_line_options.py b/tests/integration_tests/test_command_line_options.py index 76ea5f71..804feebf 100644 --- a/tests/integration_tests/test_command_line_options.py +++ b/tests/integration_tests/test_command_line_options.py @@ -121,6 +121,21 @@ def test_single_command(self, fake_template_doer): ], ) + @patch("moban.engine.Engine.render_to_files") + def test_single_command_with_options(self, fake_template_doer): + test_args = ["moban", "-t", "abc.jj2", "-c", "new.yml", "-o", "xyz.output"] + with patch.object(sys, "argv", test_args): + main() + call_args = list(fake_template_doer.call_args[0][0]) + eq_( + call_args, + [ + ("README.rst.jj2", "new.yml", "README.rst"), + ("setup.py.jj2", "new.yml", "setup.py"), + ('abc.jj2', 'new.yml', 'xyz.output') + ], + ) + def tearDown(self): os.unlink(self.config_file) os.unlink(self.data_file) From 24afb554d114ff12d1812cb6a623f5653b749236 Mon Sep 17 00:00:00 2001 From: chfw Date: Fri, 13 Jul 2018 07:56:50 +0100 Subject: [PATCH 3/5] :microscope: more test coverage. #32 --- moban/mobanfile.py | 6 +++- .../test_command_line_options.py | 34 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/moban/mobanfile.py b/moban/mobanfile.py index 8782cd82..4922988f 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -36,6 +36,7 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options): targets = moban_file_configurations.get(constants.LABEL_TARGETS) if targets: + print(target) if target: targets += target number_of_templated_files = handle_targets(merged_options, targets) @@ -112,6 +113,9 @@ def extract_target(options): output = options.get(constants.LABEL_OUTPUT) result = [] if template: + if output is None: + raise Exception( + "Please specify a output file name for %s." % template) if config: result = [ { @@ -121,5 +125,5 @@ def extract_target(options): } ] else: - result = [{template: output}] + result = [{output: template}] return result diff --git a/tests/integration_tests/test_command_line_options.py b/tests/integration_tests/test_command_line_options.py index 804feebf..1ea18e7c 100644 --- a/tests/integration_tests/test_command_line_options.py +++ b/tests/integration_tests/test_command_line_options.py @@ -121,9 +121,32 @@ def test_single_command(self, fake_template_doer): ], ) + @patch("moban.engine.Engine.render_to_files") + def test_single_command_with_a_few_options(self, fake_template_doer): + test_args = ["moban", "-t", "abc.jj2", "-o", "xyz.output"] + with patch.object(sys, "argv", test_args): + main() + call_args = list(fake_template_doer.call_args[0][0]) + eq_( + call_args, + [ + ("README.rst.jj2", "data.yaml", "README.rst"), + ("setup.py.jj2", "data.yaml", "setup.py"), + ("abc.jj2", "data.yaml", "xyz.output"), + ], + ) + @patch("moban.engine.Engine.render_to_files") def test_single_command_with_options(self, fake_template_doer): - test_args = ["moban", "-t", "abc.jj2", "-c", "new.yml", "-o", "xyz.output"] + test_args = [ + "moban", + "-t", + "abc.jj2", + "-c", + "new.yml", + "-o", + "xyz.output", + ] with patch.object(sys, "argv", test_args): main() call_args = list(fake_template_doer.call_args[0][0]) @@ -132,10 +155,17 @@ def test_single_command_with_options(self, fake_template_doer): [ ("README.rst.jj2", "new.yml", "README.rst"), ("setup.py.jj2", "new.yml", "setup.py"), - ('abc.jj2', 'new.yml', 'xyz.output') + ("abc.jj2", "new.yml", "xyz.output"), ], ) + @raises(Exception) + @patch("moban.engine.Engine.render_to_files") + def test_single_command_without_output_option(self, fake_template_doer): + test_args = ["moban", "-t", "abc.jj2"] + with patch.object(sys, "argv", test_args): + main() + def tearDown(self): os.unlink(self.config_file) os.unlink(self.data_file) From 3afd7977312d0219d640b8f186d711d0dd8faec6 Mon Sep 17 00:00:00 2001 From: chfw Date: Fri, 13 Jul 2018 08:01:55 +0100 Subject: [PATCH 4/5] :art: update code --- moban/mobanfile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moban/mobanfile.py b/moban/mobanfile.py index 4922988f..fa252ac0 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -36,8 +36,9 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options): targets = moban_file_configurations.get(constants.LABEL_TARGETS) if targets: - print(target) if target: + # if command line option exists, append its template to targets + # issue 30 targets += target number_of_templated_files = handle_targets(merged_options, targets) else: From a72b7b9dbfa7f57fd426dc1916f7d0f9189da23b Mon Sep 17 00:00:00 2001 From: chfw Date: Fri, 13 Jul 2018 08:05:22 +0100 Subject: [PATCH 5/5] :microscope: extend the test coverage to 3.7-dev --- .moban.d/travis.yml | 1 + .travis.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.moban.d/travis.yml b/.moban.d/travis.yml index 21961193..cb4c651f 100644 --- a/.moban.d/travis.yml +++ b/.moban.d/travis.yml @@ -3,6 +3,7 @@ {%block custom_python_versions%} python: - pypy-5.3.1 + - 3.7-dev - 3.6 - 3.5 - 3.4 diff --git a/.travis.yml b/.travis.yml index ea5146f1..c6ac046e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ notifications: email: false python: - pypy-5.3.1 + - 3.7-dev - 3.6 - 3.5 - 3.4