def _run_hook_from_repo_dir(repo_dir, hook_name, project_dir, context, delete_project_on_failure, custom_filters={}): """Run hook from repo directory, clean project directory if hook fails. :param repo_dir: Project template input directory. :param hook_name: The hook to execute. :param project_dir: The directory to execute the script from. :param context: Cookiecutter project context. :param delete_project_on_failure: Delete the project directory on hook failure? """ with work_in(repo_dir): try: run_hook(hook_name, project_dir, context, custom_filters=custom_filters) except FailedHookException: if delete_project_on_failure: rmtree(project_dir) logger.error( "Stopping generation because %s hook " "script didn't exit successfully", hook_name, ) raise
def test_public_run_hook(self): '''Execute hook from specified template in specified output directory''' tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with utils.work_in(self.repo_path): hooks.run_hook('pre_gen_project', tests_dir) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'python_pre.txt'))) hooks.run_hook('post_gen_project', tests_dir) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'shell_post.txt')))
def test_run_hook(self): """Execute hook from specified template in specified output \ directory.""" tests_dir = os.path.join(self.repo_path, "input{{hooks}}") with utils.work_in(self.repo_path): hooks.run_hook("pre_gen_project", tests_dir, {}) assert os.path.isfile(os.path.join(tests_dir, "python_pre.txt")) hooks.run_hook("post_gen_project", tests_dir, {}) assert os.path.isfile(os.path.join(tests_dir, "shell_post.txt"))
def test_run_hook(self): """Execute hook from specified template in specified output \ directory.""" tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with cookiecutter.utils.context_manager.work_in(self.repo_path): hooks.run_hook('pre_gen_project', tests_dir, {}) assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt')) assert os.path.isfile(os.path.join(tests_dir, 'shell_pre.txt')) hooks.run_hook('post_gen_project', tests_dir, {}) assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt'))
def test_run_failing_hook(self): hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with open(hook_path, 'w') as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") with utils.work_in(self.repo_path): with pytest.raises(exceptions.FailedHookException) as excinfo: hooks.run_hook('pre_gen_project', tests_dir, {}) assert 'Hook script failed' in str(excinfo.value)
def test_run_failing_hook(self): hook_path = os.path.join(self.hooks_path, "pre_gen_project.py") tests_dir = os.path.join(self.repo_path, "input{{hooks}}") with open(hook_path, "w") as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") with utils.work_in(self.repo_path): with pytest.raises(exceptions.FailedHookException) as excinfo: hooks.run_hook("pre_gen_project", tests_dir, {}) assert "Hook script failed" in str(excinfo.value)
def test_run_failing_hook(self): """Test correct exception raise if hook exit code is not zero.""" hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with open(hook_path, 'w') as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") with utils.work_in(self.repo_path): with pytest.raises(exceptions.FailedHookException) as excinfo: hooks.run_hook('pre_gen_project', tests_dir, {}) assert 'Hook script failed' in str(excinfo.value)
def test_public_run_hook(self): '''Execute hook from specified template in specified output directory''' tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with utils.work_in(self.repo_path): hooks.run_hook('pre_gen_project', tests_dir) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'python_pre.txt'))) config_file = os.path.join(self.repo_path, "../test-evaluate/cookiecutter.json") hooks.run_hook('post_gen_project', tests_dir, config_file) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'shell_post.txt'))) self.assertTrue(os.path.isfile(config_file)) self.assertIn("cookiecutter.json", open(os.path.join(tests_dir, 'config_file.txt')).read()) self.assertIn("fat", open(os.path.join(tests_dir, 'yo_mama_file.txt')).read())
def test_public_run_hook(self): '''Execute hook from specified template in specified output directory''' tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') with utils.work_in(self.repo_path): hooks.run_hook('pre_gen_project', {'cookiecutter': {'test': 'test'}}, tests_dir) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'python_pre.txt'))) f = open(os.path.join(tests_dir, 'python_pre.txt')) self.assertEqual(f.readline(), 'test') hooks.run_hook('post_gen_project', {'cookiecutter': {'test': 'test'}}, tests_dir) self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'shell_post.txt'))) f = open(os.path.join(tests_dir, 'shell_post.txt')) self.assertEqual(f.readline(), 'test\n')
def _patched_run_hook(hook_name, project_dir, context): """Used to patch cookiecutter's ``run_hook`` function. This patched version ensures that the temple.yaml file is created before any cookiecutter hooks are executed """ if hook_name == 'post_gen_project': with temple.utils.cd(project_dir): temple.utils.write_temple_config(context['cookiecutter'], context['template'], context['version']) return cc_hooks.run_hook(hook_name, project_dir, context)