Completed
Push — master ( 14c8ac...ce45ac )
by Michael
7s
created

tests.back_up_rc()   B

Complexity

Conditions 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 32
rs 7.5384
cc 6

2 Methods

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