def test_get_config_does_not_exist(): """ Check that `exceptions.ConfigDoesNotExistException` is raised when attempting to get a non-existent config file. """ with pytest.raises(ConfigDoesNotExistException): config.get_config('tests/test-config/this-does-not-exist.yaml')
def test_get_config_does_not_exist(): """Check that `exceptions.ConfigDoesNotExistException` is raised when \ attempting to get a non-existent config file.""" expected_error_msg = 'Config file tests/not-exist.yaml does not exist.' with pytest.raises(ConfigDoesNotExistException) as exc_info: config.get_config('tests/not-exist.yaml') assert str(exc_info.value) == expected_error_msg
def test_invalid_config(): """An invalid config file should raise an `InvalidConfiguration` \ exception.""" expected_error_msg = ( 'Unable to parse YAML file tests/test-config/invalid-config.yaml.') with pytest.raises(InvalidConfiguration) as exc_info: config.get_config('tests/test-config/invalid-config.yaml') assert expected_error_msg in str(exc_info.value) assert isinstance(exc_info.value.__cause__, yaml.YAMLError)
def test_invalid_config(): """An invalid config file should raise an `InvalidConfiguration` \ exception.""" with pytest.raises(InvalidConfiguration) as exc_info: config.get_config('tests/config/test-config/invalid-config.yaml') expected_error_msg = ( 'Unable to parse YAML file tests/config/test-config/invalid-config.yaml. Error:' ) assert expected_error_msg in str(exc_info.value)
def test_invalid_config(): """ An invalid config file should raise an `InvalidConfiguration` exception. """ with pytest.raises(InvalidConfiguration) as excinfo: config.get_config('tests/test-config/invalid-config.yaml') expected_error_msg = ( 'tests/test-config/invalid-config.yaml is not a valid YAML file: ' 'line 1: mapping values are not allowed here') assert str(excinfo.value) == expected_error_msg
def test_invalid_config(): """ An invalid config file should raise an `InvalidConfiguration` exception. """ with pytest.raises(InvalidConfiguration) as excinfo: config.get_config('tests/test-config/invalid-config.yaml') expected_error_msg = ( 'tests/test-config/invalid-config.yaml is not a valid YAML file: ' 'line 1: mapping values are not allowed here' ) assert str(excinfo.value) == expected_error_msg
def test_invalid_config(): """ An invalid config file should raise an `InvalidConfiguration` exception. """ with pytest.raises(InvalidConfiguration) as excinfo: config.get_config('tests/test-config/invalid-config.yaml') expected_error_msg = ( 'Unable to parse YAML file ' 'tests/test-config/invalid-config.yaml. ' 'Error: ' ) assert expected_error_msg in str(excinfo.value)
def main(template, target, no_input, checkout, verbose): if verbose: logging.basicConfig( format='%(levelname)s %(filename)s: %(message)s', level=logging.DEBUG ) else: logging.basicConfig( format='%(levelname)s: %(message)s', level=logging.INFO ) try: src = os.path.join(target, '.cookiecutterrc') if os.path.exists(src): logger.info("Loading config from %r", src) extra_context = get_config(src) logger.debug("Loaded %r", extra_context) extra_context = extra_context.get('cookiecutter') or extra_context.get('default_context') logger.debug("Loaded %r", extra_context) else: logger.info("No .cookiecutterrc in %r", target) extra_context = None with weave('cookiecutter.main.generate_files', save_context): cookiecutter( template, checkout, no_input, overwrite_if_exists=True, output_dir=os.path.dirname(target), extra_context=extra_context, ) except (OutputDirExistsException, InvalidModeException) as e: click.echo(e) sys.exit(1)
def main(template, target, no_input, checkout, verbose): if verbose: logging.basicConfig(format='%(levelname)s %(filename)s: %(message)s', level=logging.DEBUG) else: logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) try: src = os.path.join(target, '.cookiecutterrc') if os.path.exists(src): logger.info("Loading config from %r", src) extra_context = get_config(src) logger.debug("Loaded %r", extra_context) extra_context = extra_context.get( 'cookiecutter') or extra_context.get('default_context') logger.debug("Loaded %r", extra_context) else: logger.info("No .cookiecutterrc in %r", target) extra_context = None with weave('cookiecutter.main.generate_files', save_context): cookiecutter( template, checkout, no_input, overwrite_if_exists=True, output_dir=os.path.dirname(target), extra_context=extra_context, ) except (OutputDirExistsException, InvalidModeException) as e: click.echo(e) sys.exit(1)
def test_get_config_with_nested_struct_defaults(): """ A config file that overrides 1 of 3 defaults Verifies that if defaults have nested structure, the resulting configuration does contain the nested elements. see issue #1149 for additional details. """ conf = config.get_config('tests/test-config/config-nested-structures.yaml') default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/') default_replay_dir = os.path.expanduser('~/.cookiecutter_replay/') expected_conf = { 'cookiecutters_dir': default_cookiecutters_dir, 'replay_dir': default_replay_dir, 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******', 'nested_level_1': { 'option_1': "value 1", 'choice_1': ['choice A', 'choice B'], 'nested_level_2': { 'option_2': 'value 2', }, }, }, 'abbreviations': { 'gh': 'https://github.com/{0}.git', 'gl': 'https://gitlab.com/{0}.git', 'bb': 'https://bitbucket.org/{0}', } } assert conf == expected_conf
def test_get_config(): """Verify valid config opened and rendered correctly.""" conf = config.get_config('tests/test-config/valid-config.yaml') expected_conf = { 'cookiecutters_dir': '/home/example/some-path-to-templates', 'replay_dir': '/home/example/some-path-to-replay-files', 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******', 'project': { 'description': 'description', 'tags': [ 'first', 'second', 'third', ], }, }, 'abbreviations': { 'gh': 'https://github.com/{0}.git', 'gl': 'https://gitlab.com/{0}.git', 'bb': 'https://bitbucket.org/{0}', 'helloworld': 'https://github.com/hackebrot/helloworld', }, } assert conf == expected_conf
def test_get_config(self): """ Opening and reading config file """ conf = config.get_config('tests/test-config/valid-config.yaml') expected_conf = { 'cookiecutters_dir': '/home/example/some-path-to-templates', 'default_context': { "full_name": "Firstname Lastname", "email": "*****@*****.**", "github_username": "******" } } self.assertEqual(conf, expected_conf)
def test_get_config(): """ Opening and reading config file """ conf = config.get_config('tests/test-config/valid-config.yaml') expected_conf = { 'cookiecutters_dir': '/home/example/some-path-to-templates', 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******' } } assert conf == expected_conf
def test_get_config_with_defaults(self): """ A config file that overrides 1 of 2 defaults """ conf = config.get_config('tests/test-config/valid-partial-config.yaml') default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/') expected_conf = { 'cookiecutters_dir': default_cookiecutters_dir, 'default_context': { "full_name": "Firstname Lastname", "email": "*****@*****.**", "github_username": "******" } } self.assertEqual(conf, expected_conf)
def test_get_config_with_defaults(): """ A config file that overrides 1 of 2 defaults """ conf = config.get_config('tests/test-config/valid-partial-config.yaml') default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/') expected_conf = { 'cookiecutters_dir': default_cookiecutters_dir, 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******' } } assert conf == expected_conf
def test_get_config(): """ Opening and reading config file """ conf = config.get_config('tests/test-config/valid-config.yaml') expected_conf = { 'cookiecutters_dir': '/home/example/some-path-to-templates', 'replay_dir': '/home/example/some-path-to-replay-files', 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******' }, 'abbreviations': { 'gh': 'https://github.com/{0}.git', 'bb': 'https://bitbucket.org/{0}', } } assert conf == expected_conf
def test_get_config(): """Opening and reading config file""" conf = config.get_config("tests/test-config/valid-config.yaml") expected_conf = { "cookiecutters_dir": "/home/example/some-path-to-templates", "replay_dir": "/home/example/some-path-to-replay-files", "default_context": { "full_name": "Firstname Lastname", "email": "*****@*****.**", "github_username": "******", }, "abbreviations": { "gh": "https://github.com/{0}.git", "gl": "https://gitlab.com/{0}.git", "bb": "https://bitbucket.org/{0}", "helloworld": "https://github.com/hackebrot/helloworld", }, } assert conf == expected_conf
def test_get_config_with_defaults(): """A config file that overrides 1 of 3 defaults.""" conf = config.get_config('tests/test-config/valid-partial-config.yaml') default_cookiecutters_dir = Path('~/.cookiecutters').expanduser() default_replay_dir = Path('~/.cookiecutter_replay').expanduser() expected_conf = { 'cookiecutters_dir': str(default_cookiecutters_dir), 'replay_dir': str(default_replay_dir), 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******', }, 'abbreviations': { 'gh': 'https://github.com/{0}.git', 'gl': 'https://gitlab.com/{0}.git', 'bb': 'https://bitbucket.org/{0}', }, } assert conf == expected_conf
def test_get_config_with_defaults(): """A config file that overrides 1 of 3 defaults""" conf = config.get_config("tests/test-config/valid-partial-config.yaml") default_cookiecutters_dir = os.path.expanduser("~/.cookiecutters/") default_replay_dir = os.path.expanduser("~/.cookiecutter_replay/") expected_conf = { "cookiecutters_dir": default_cookiecutters_dir, "replay_dir": default_replay_dir, "default_context": { "full_name": "Firstname Lastname", "email": "*****@*****.**", "github_username": "******", }, "abbreviations": { "gh": "https://github.com/{0}.git", "gl": "https://gitlab.com/{0}.git", "bb": "https://bitbucket.org/{0}", }, } assert conf == expected_conf
def test_get_config_with_defaults(): """ A config file that overrides 1 of 3 defaults """ conf = config.get_config('tests/test-config/valid-partial-config.yaml') default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/') default_replay_dir = os.path.expanduser('~/.cookiecutter_replay/') expected_conf = { 'cookiecutters_dir': default_cookiecutters_dir, 'replay_dir': default_replay_dir, 'default_context': { 'full_name': 'Firstname Lastname', 'email': '*****@*****.**', 'github_username': '******' }, 'abbreviations': { 'gh': 'https://github.com/{0}.git', 'bb': 'https://bitbucket.org/{0}', } } assert conf == expected_conf
def test_invalid_config(): """ An invalid config file should raise an `InvalidConfiguration` exception. """ with pytest.raises(InvalidConfiguration): config.get_config('tests/test-config/invalid-config.yaml')