restore_original_rc()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
"""
4
test_get_user_config
5
--------------------
6
7
Tests formerly known from a unittest residing in test_config.py named
8
TestGetUserConfig.test_get_user_config_valid
9
TestGetUserConfig.test_get_user_config_invalid
10
TestGetUserConfig.test_get_user_config_nonexistent
11
"""
12
13
import os
14
import shutil
15
import pytest
16
17
from cookiecutter import config
18
from cookiecutter.exceptions import InvalidConfiguration
19
20
21
@pytest.fixture(scope='module')
22
def user_config_path():
23
    return os.path.expanduser('~/.cookiecutterrc')
24
25
26
@pytest.fixture(scope='function')
27
def back_up_rc(request, user_config_path):
28
    """
29
    Back up an existing cookiecutter rc and restore it after the test.
30
    If ~/.cookiecutterrc is pre-existing, move it to a temp location
31
    """
32
    user_config_path_backup = os.path.expanduser('~/.cookiecutterrc.backup')
33
34
    if os.path.exists(user_config_path):
35
        shutil.copy(user_config_path, user_config_path_backup)
36
        os.remove(user_config_path)
37
38
    def remove_test_rc():
39
        """
40
        Remove the ~/.cookiecutterrc that has been created in the test.
41
        """
42
        if os.path.exists(user_config_path):
43
            os.remove(user_config_path)
44
45
    def restore_original_rc():
46
        """
47
        If it existed, restore the original ~/.cookiecutterrc
48
        """
49
        if os.path.exists(user_config_path_backup):
50
            shutil.copy(user_config_path_backup, user_config_path)
51
            os.remove(user_config_path_backup)
52
53
    # According to the py.test source code finalizers are popped from an
54
    # internal list that we populated via 'addfinalizer'. As a result the
55
    # last-added finalizer function is executed first.
56
    request.addfinalizer(restore_original_rc)
57
    request.addfinalizer(remove_test_rc)
58
59
60
@pytest.fixture
61
def custom_config():
62
    return {
63
        'default_context': {
64
            'full_name': 'Firstname Lastname',
65
            'email': '[email protected]',
66
            'github_username': 'example',
67
        },
68
        'cookiecutters_dir': '/home/example/some-path-to-templates',
69
        'replay_dir': '/home/example/some-path-to-replay-files',
70
        'abbreviations': {
71
            'gh': 'https://github.com/{0}.git',
72
            'gl': 'https://gitlab.com/{0}.git',
73
            'bb': 'https://bitbucket.org/{0}',
74
            'helloworld': 'https://github.com/hackebrot/helloworld',
75
        }
76
    }
77
78
79
@pytest.mark.usefixtures('back_up_rc')
80
def test_get_user_config_valid(user_config_path, custom_config):
81
    """
82
    Get config from a valid ~/.cookiecutterrc file
83
    """
84
    shutil.copy('tests/test-config/valid-config.yaml', user_config_path)
85
    conf = config.get_user_config()
86
87
    assert conf == custom_config
88
89
90
@pytest.mark.usefixtures('back_up_rc')
91
def test_get_user_config_invalid(user_config_path):
92
    """
93
    Get config from an invalid ~/.cookiecutterrc file
94
    """
95
    shutil.copy('tests/test-config/invalid-config.yaml', user_config_path)
96
    with pytest.raises(InvalidConfiguration):
97
        config.get_user_config()
98
99
100
@pytest.mark.usefixtures('back_up_rc')
101
def test_get_user_config_nonexistent():
102
    """
103
    Get config from a nonexistent ~/.cookiecutterrc file
104
    """
105
    assert config.get_user_config() == config.DEFAULT_CONFIG
106
107
108
@pytest.fixture
109
def custom_config_path(custom_config):
110
    return 'tests/test-config/valid-config.yaml'
111
112
113
def test_specify_config_path(mocker, custom_config_path, custom_config):
114
    spy_get_config = mocker.spy(config, 'get_config')
115
116
    user_config = config.get_user_config(custom_config_path)
117
    spy_get_config.assert_called_once_with(custom_config_path)
118
119
    assert user_config == custom_config
120
121
122
def test_default_config_path(user_config_path):
123
    assert config.USER_CONFIG_PATH == user_config_path
124
125
126
def test_default_config_from_env_variable(
127
        monkeypatch, custom_config_path, custom_config):
128
    monkeypatch.setenv('COOKIECUTTER_CONFIG', custom_config_path)
129
130
    user_config = config.get_user_config()
131
    assert user_config == custom_config
132
133
134
def test_force_default_config(mocker):
135
    spy_get_config = mocker.spy(config, 'get_config')
136
137
    user_config = config.get_user_config(None, default_config=True)
138
139
    assert user_config == config.DEFAULT_CONFIG
140
    assert not spy_get_config.called
141
142
143
def test_expand_user_for_directories_in_config(monkeypatch):
144
    def _expanduser(path):
145
        return path.replace('~', 'Users/bob')
146
    monkeypatch.setattr('os.path.expanduser', _expanduser)
147
148
    config_file = 'tests/test-config/config-expand-user.yaml'
149
150
    user_config = config.get_user_config(config_file)
151
    assert user_config['replay_dir'] == 'Users/bob/replay-files'
152
    assert user_config['cookiecutters_dir'] == 'Users/bob/templates'
153
154
155
def test_expand_vars_for_directories_in_config(monkeypatch):
156
    monkeypatch.setenv('COOKIES', 'Users/bob/cookies')
157
158
    config_file = 'tests/test-config/config-expand-vars.yaml'
159
160
    user_config = config.get_user_config(config_file)
161
    assert user_config['replay_dir'] == 'Users/bob/cookies/replay-files'
162
    assert user_config['cookiecutters_dir'] == 'Users/bob/cookies/templates'
163