Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_expand_vars_for_directories_in_config(monkeypatch):
monkeypatch.setenv('COOKIES', 'Users/bob/cookies')
config_file = 'tests/test-config/config-expand-vars.yaml'
user_config = config.get_user_config(config_file)
assert user_config['replay_dir'] == 'Users/bob/cookies/replay-files'
assert user_config['cookiecutters_dir'] == 'Users/bob/cookies/templates'
def test_get_user_config_valid(user_config_path, custom_config):
"""
Get config from a valid ~/.cookiecutterrc file
"""
shutil.copy('tests/test-config/valid-config.yaml', user_config_path)
conf = config.get_user_config()
assert conf == custom_config
def test_default_config_from_env_variable(
monkeypatch, custom_config_path, custom_config):
monkeypatch.setenv('COOKIECUTTER_CONFIG', custom_config_path)
user_config = config.get_user_config()
assert user_config == custom_config
def test_get_user_config_invalid(self):
""" Get config from an invalid ~/.cookiecutterrc file """
shutil.copy('tests/test-config/invalid-config.yaml', self.user_config_path)
self.assertRaises(InvalidConfiguration, config.get_user_config)
def test_get_user_config_valid(self):
""" Get config from a valid ~/.cookiecutterrc file """
shutil.copy('tests/test-config/valid-config.yaml', self.user_config_path)
conf = config.get_user_config()
expected_conf = {
'cookiecutters_dir': '/home/example/some-path-to-templates',
'default_context': {
"full_name": "Firstname Lastname",
"email": "firstname.lastname@gmail.com",
"github_username": "example"
}
}
self.assertEqual(conf, expected_conf)
def cookiecutter(input_dir, checkout=None, no_input=False):
"""
API equivalent to using Cookiecutter at the command line.
:param input_dir: A directory containing a project template dir,
or a URL to git repo.
:param checkout: The branch, tag or commit ID to checkout after clone
"""
# Get user config from ~/.cookiecutterrc or equivalent
# If no config file, sensible defaults from config.DEFAULT_CONFIG are used
config_dict = get_user_config()
# TODO: find a better way to tell if it's a repo URL
if "git@" in input_dir or "https://" in input_dir:
repo_dir = clone(
repo_url=input_dir,
checkout=checkout,
clone_to_dir=config_dict['cookiecutters_dir']
)
else:
# If it's a local repo, no need to clone or copy to your cookiecutters_dir
repo_dir = input_dir
context_file = os.path.join(repo_dir, 'cookiecutter.json')
logging.debug('context_file is {0}'.format(context_file))
context = generate_context(
def main():
repo_dir = sys.argv[1]
user_config_path = sys.argv[2]
user_config_path = user_config_path if os.path.isfile(user_config_path) else None
output_folder_path = sys.argv[3]
context_json_path = sys.argv[4]
extra_context = json.load(open(context_json_path, 'r'))
context_file = os.path.join(repo_dir, 'cookiecutter.json')
config_dict = get_user_config(user_config_path)
context = generate_context(context_file, config_dict['default_context'], extra_context=extra_context)
rendered_context = render_context(context, output_folder_path)
print(json.dumps(rendered_context))
last_commit = repo.head.object.hexsha
except Exception as e: # pragma: no cover
raise InvalidCookiecutterRepository(e)
main_cookiecutter_directory: Optional[Path] = None
for dir_item in cookiecutter_template_dir.glob("*cookiecutter.*"):
if dir_item.is_dir() and "{{" in dir_item.name and "}}" in dir_item.name:
main_cookiecutter_directory = dir_item
break
if not main_cookiecutter_directory: # pragma: no cover
raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)
context_file = cookiecutter_template_dir / "cookiecutter.json"
config_dict = get_user_config(config_file=config_file, default_config=default_config)
context = generate_context(
context_file=context_file,
default_context=config_dict["default_context"],
extra_context=extra_context,
)
# prompt the user to manually configure at the command line.
# except when 'no-input' flag is set
context["cookiecutter"] = prompt_for_config(context, no_input)
context["cookiecutter"]["_template"] = template_git_url
if use_latest or no_input:
use_commit = last_commit
else: # pragma: no cover
print("")