def test_apply_overwrites_sets_default_for_choice_variable(template_context):
    generate.apply_overwrites_to_context(
        template_context,
        {'orientation': 'landscape'}
    )

    assert template_context['orientation'] == ['landscape', 'all', 'portrait']
def test_apply_overwrites_sets_non_list_value(template_context):
    generate.apply_overwrites_to_context(
        template_context,
        {'repo_name': 'foobar'}
    )

    assert template_context['repo_name'] == 'foobar'
def test_apply_overwrites_sets_default_for_choice_variable(template_context):
    """Verify overwritten list member became a default value."""
    generate.apply_overwrites_to_context(
        context=template_context,
        overwrite_context={'orientation': 'landscape'})

    assert template_context['orientation'] == ['landscape', 'all', 'portrait']
def test_apply_overwrites_does_include_unused_variables(template_context):
    generate.apply_overwrites_to_context(
        template_context,
        {'not in template': 'foobar'}
    )

    assert 'not in template' not in template_context
def test_apply_overwrites_does_include_unused_variables(template_context):
    """Verify `apply_overwrites_to_context` skips variables that are not in context."""
    generate.apply_overwrites_to_context(
        context=template_context,
        overwrite_context={'not in template': 'foobar'})

    assert 'not in template' not in template_context
def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite(
    template_context, ):
    """Verify variables overwrite for list if variable not in list ignored."""
    generate.apply_overwrites_to_context(
        context=template_context, overwrite_context={'orientation': 'foobar'})

    assert template_context['orientation'] == ['all', 'landscape', 'portrait']
def test_apply_overwrites_sets_non_list_value(template_context):
    """Verify `apply_overwrites_to_context` work with string variables."""
    generate.apply_overwrites_to_context(
        context=template_context, overwrite_context={'repo_name': 'foobar'}
    )

    assert template_context['repo_name'] == 'foobar'
def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite(
        template_context):
    generate.apply_overwrites_to_context(
        template_context,
        {'orientation': 'foobar'}
    )

    assert template_context['orientation'] == ['all', 'landscape', 'portrait']
Exemple #9
0
def get_config(repo_name, directory, user_org, token):
    """Fetches the cookiecutter input template and the user defaults stored in Github."""
    client = get_git_client(token)
    cc_repo = client.get_repo(repo_name)
    config_file_path = "cookiecutter.json"
    if directory:
        config_file_path = os.path.join(directory, config_file_path)
    context = json.loads(
        cc_repo.get_contents(config_file_path).decoded_content)
    try:
        config_repo = client.get_repo(f"{user_org}/.github")
    except GithubException:
        return context
    user_config = os.path.join("cookiecutter", repo_name, config_file_path)
    try:
        user_defaults = json.loads(
            config_repo.get_contents(user_config).decoded_content)
        apply_overwrites_to_context(context, user_defaults)
    except GithubException:
        pass
    return context
def test_apply_overwrites_sets_default_for_choice_variable(template_context):
    generate.apply_overwrites_to_context(template_context, {"orientation": "landscape"})

    assert template_context["orientation"] == ["landscape", "all", "portrait"]
def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite(
    template_context,
):
    generate.apply_overwrites_to_context(template_context, {"orientation": "foobar"})

    assert template_context["orientation"] == ["all", "landscape", "portrait"]
def test_apply_overwrites_sets_non_list_value(template_context):
    generate.apply_overwrites_to_context(template_context, {"repo_name": "foobar"})

    assert template_context["repo_name"] == "foobar"
def test_apply_overwrites_does_include_unused_variables(template_context):
    generate.apply_overwrites_to_context(
        template_context, {"not in template": "foobar"}
    )

    assert "not in template" not in template_context
def test_apply_overwrites_invalid_overwrite(template_context):
    """Verify variables overwrite for list if variable not in list not ignored."""
    with pytest.raises(ValueError):
        generate.apply_overwrites_to_context(
            context=template_context,
            overwrite_context={'orientation': 'foobar'})