Completed
Pull Request — master (#971)
by
unknown
32s
created

test_get_config_with_defaults()   B

Complexity

Conditions 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
1
# -*- coding: utf-8 -*-
2
import pytest
3
4
from cookiecutter import config
5
from cookiecutter.exceptions import (
6
    ConfigDoesNotExistException, InvalidConfiguration
7
)
8
9
10
def test_merge_configs():
11
    default = {
12
        'cookiecutters_dir': '/home/example/some-path-to-templates',
13
        'replay_dir': '/home/example/some-path-to-replay-files',
14
        'default_context': {},
15
        'abbreviations': {
16
            'gh': 'https://github.com/{0}.git',
17
            'gl': 'https://gitlab.com/{0}.git',
18
            'bb': 'https://bitbucket.org/{0}',
19
        }
20
    }
21
22
    user_config = {
23
        'default_context': {
24
            'full_name': 'Raphael Pierzina',
25
            'github_username': 'hackebrot',
26
        },
27
        'abbreviations': {
28
            'gl': 'https://gitlab.com/hackebrot/{0}.git',
29
            'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git',
30
        }
31
    }
32
33
    expected_config = {
34
        'cookiecutters_dir': '/home/example/some-path-to-templates',
35
        'replay_dir': '/home/example/some-path-to-replay-files',
36
        'default_context': {
37
            'full_name': 'Raphael Pierzina',
38
            'github_username': 'hackebrot',
39
        },
40
        'abbreviations': {
41
            'gh': 'https://github.com/{0}.git',
42
            'gl': 'https://gitlab.com/hackebrot/{0}.git',
43
            'bb': 'https://bitbucket.org/{0}',
44
            'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git',
45
        }
46
    }
47
48
    assert config.merge_configs(default, user_config) == expected_config
49
50
51
def test_get_config():
52
    """
53
    Opening and reading config file
54
    """
55
    conf = config.get_config('tests/test-config/valid-config.yaml')
56
    expected_conf = {
57
        'cookiecutters_dir': '/home/example/some-path-to-templates',
58
        'replay_dir': '/home/example/some-path-to-replay-files',
59
        'default_context': {
60
            'full_name': 'Firstname Lastname',
61
            'email': '[email protected]',
62
            'github_username': 'example'
63
        },
64
        'abbreviations': {
65
            'gh': 'https://github.com/{0}.git',
66
            'gl': 'https://gitlab.com/{0}.git',
67
            'bb': 'https://bitbucket.org/{0}',
68
            'helloworld': 'https://github.com/hackebrot/helloworld'
69
        }
70
    }
71
    assert conf == expected_conf
72
73
74
def test_get_config_does_not_exist():
75
    """
76
    Check that `exceptions.ConfigDoesNotExistException` is raised when
77
    attempting to get a non-existent config file.
78
    """
79
    with pytest.raises(ConfigDoesNotExistException):
80
        config.get_config('tests/test-config/this-does-not-exist.yaml')
81
82
83
def test_invalid_config():
84
    """
85
    An invalid config file should raise an `InvalidConfiguration` exception.
86
    """
87
    with pytest.raises(InvalidConfiguration) as excinfo:
88
        config.get_config('tests/test-config/invalid-config.yaml')
89
90
    expected_error_msg = (
91
        'Unable to parse YAML file '
92
        'tests/test-config/invalid-config.yaml. '
93
        'Error: '
94
    )
95
    assert expected_error_msg in str(excinfo.value)
96
97
98
def test_get_config_with_defaults():
99
    """
100
    A config file that overrides 1 of 3 defaults
101
    """
102
    conf = config.get_config('tests/test-config/valid-partial-config.yaml')
103
    # use the defaults directly rather than invoke config._find_user_data_dir;
104
    # _find_user_data_dir should be an implementation detail
105
    default_cookiecutters_dir = config.DEFAULT_CONFIG['cookiecutters_dir']
106
    default_replay_dir = config.DEFAULT_CONFIG['replay_dir']
107
    expected_conf = {
108
        'cookiecutters_dir': default_cookiecutters_dir,
109
        'replay_dir': default_replay_dir,
110
        'default_context': {
111
            'full_name': 'Firstname Lastname',
112
            'email': '[email protected]',
113
            'github_username': 'example'
114
        },
115
        'abbreviations': {
116
            'gh': 'https://github.com/{0}.git',
117
            'gl': 'https://gitlab.com/{0}.git',
118
            'bb': 'https://bitbucket.org/{0}',
119
        }
120
    }
121
    assert conf == expected_conf
122