Completed
Pull Request — master (#961)
by
unknown
01:20
created

test_zipfile_unzip()   B

Complexity

Conditions 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 30
rs 8.8571
1
# -*- coding: utf-8 -*-
2
import os
3
4
import pytest
5
6
from cookiecutter import repository, exceptions
7
8
9
@pytest.fixture(params=[
10
    ('/path/to/zipfile.zip', False),
11
    ('https://example.com/path/to/zipfile.zip', True),
12
    ('http://example.com/path/to/zipfile.zip', True),
13
])
14
def zipfile(request):
15
    return request.param
16
17
18
def test_zipfile_unzip(
19
        mocker, zipfile, user_config_data):
20
    """`unzip()` should be called with correct args when
21
    `determine_repo_dir()` is passed a zipfile, or a URL
22
    to a zipfile.
23
    """
24
25
    mock_clone = mocker.patch(
26
        'cookiecutter.repository.unzip',
27
        return_value='tests/fake-repo-tmpl',
28
        autospec=True
29
    )
30
31
    project_dir = repository.determine_repo_dir(
32
        zipfile[0],
33
        abbreviations={},
34
        clone_to_dir=user_config_data['cookiecutters_dir'],
35
        checkout=None,
36
        no_input=True
37
    )
38
39
    mock_clone.assert_called_once_with(
40
        zip_url=zipfile[0],
41
        is_url=zipfile[1],
42
        clone_to_dir=user_config_data['cookiecutters_dir'],
43
        no_input=True
44
    )
45
46
    assert os.path.isdir(project_dir)
47
    assert 'tests/fake-repo-tmpl' == project_dir
48
49
50
@pytest.fixture
51
def template_url():
52
    """URL to example Cookiecutter template on GitHub.
53
54
    Note: when used, git clone is mocked.
55
    """
56
    return 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git'
57
58
59
def test_repository_url_should_clone(
60
        mocker, template_url, user_config_data):
61
    """`clone()` should be called with correct args when
62
    `determine_repo_dir()` is passed a repository template url.
63
    """
64
65
    mock_clone = mocker.patch(
66
        'cookiecutter.repository.clone',
67
        return_value='tests/fake-repo-tmpl',
68
        autospec=True
69
    )
70
71
    project_dir = repository.determine_repo_dir(
72
        template_url,
73
        abbreviations={},
74
        clone_to_dir=user_config_data['cookiecutters_dir'],
75
        checkout=None,
76
        no_input=True
77
    )
78
79
    mock_clone.assert_called_once_with(
80
        repo_url=template_url,
81
        checkout=None,
82
        clone_to_dir=user_config_data['cookiecutters_dir'],
83
        no_input=True
84
    )
85
86
    assert os.path.isdir(project_dir)
87
    assert 'tests/fake-repo-tmpl' == project_dir
88
89
90
def test_repository_url_with_no_context_file(
91
        mocker, template_url, user_config_data):
92
    mocker.patch(
93
        'cookiecutter.repository.clone',
94
        return_value='tests/fake-repo-bad',
95
        autospec=True
96
    )
97
98
    with pytest.raises(exceptions.RepositoryNotFound) as err:
99
        repository.determine_repo_dir(
100
            template_url,
101
            abbreviations={},
102
            clone_to_dir=None,
103
            checkout=None,
104
            no_input=True
105
        )
106
107
    assert str(err.value) == (
108
        'A valid repository for "{}" could not be found in the following '
109
        'locations:\n{}'.format(
110
            template_url,
111
            'tests/fake-repo-bad',
112
        )
113
    )
114