1
|
|
|
"""Tests around locally cached cookiecutter template repositories.""" |
2
|
|
|
import os |
3
|
|
|
from pathlib import Path |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from cookiecutter import exceptions, repository |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
@pytest.fixture |
11
|
|
|
def template(): |
12
|
|
|
"""Fixture. Return simple string as template name.""" |
13
|
|
|
return 'cookiecutter-pytest-plugin' |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.fixture |
17
|
|
|
def cloned_cookiecutter_path(user_config_data, template): |
18
|
|
|
"""Fixture. Prepare folder structure for tests in this file.""" |
19
|
|
|
cookiecutters_dir = user_config_data['cookiecutters_dir'] |
20
|
|
|
|
21
|
|
|
cloned_template_path = os.path.join(cookiecutters_dir, template) |
22
|
|
|
if not os.path.exists(cloned_template_path): |
23
|
|
|
os.mkdir(cloned_template_path) # might exist from other tests. |
24
|
|
|
|
25
|
|
|
subdir_template_path = os.path.join(cloned_template_path, 'my-dir') |
26
|
|
|
if not os.path.exists(subdir_template_path): |
27
|
|
|
os.mkdir(subdir_template_path) |
28
|
|
|
Path(subdir_template_path, 'cookiecutter.json').touch() # creates file |
29
|
|
|
|
30
|
|
|
return subdir_template_path |
31
|
|
|
|
32
|
|
|
|
33
|
|
View Code Duplication |
def test_should_find_existing_cookiecutter( |
|
|
|
|
34
|
|
|
template, user_config_data, cloned_cookiecutter_path |
35
|
|
|
): |
36
|
|
|
"""Find `cookiecutter.json` in sub folder created by `cloned_cookiecutter_path`.""" |
37
|
|
|
project_dir, cleanup = repository.determine_repo_dir( |
38
|
|
|
template=template, |
39
|
|
|
abbreviations={}, |
40
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
41
|
|
|
checkout=None, |
42
|
|
|
no_input=True, |
43
|
|
|
directory='my-dir', |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
assert cloned_cookiecutter_path == project_dir |
47
|
|
|
assert not cleanup |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def test_local_repo_typo(template, user_config_data, cloned_cookiecutter_path): |
51
|
|
|
"""Wrong pointing to `cookiecutter.json` sub-directory should raise.""" |
52
|
|
|
with pytest.raises(exceptions.RepositoryNotFound) as err: |
53
|
|
|
repository.determine_repo_dir( |
54
|
|
|
template=template, |
55
|
|
|
abbreviations={}, |
56
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
57
|
|
|
checkout=None, |
58
|
|
|
no_input=True, |
59
|
|
|
directory='wrong-dir', |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
wrong_full_cookiecutter_path = os.path.join( |
63
|
|
|
os.path.dirname(cloned_cookiecutter_path), 'wrong-dir' |
64
|
|
|
) |
65
|
|
|
assert str(err.value) == ( |
66
|
|
|
'A valid repository for "{}" could not be found in the following ' |
67
|
|
|
'locations:\n{}'.format( |
68
|
|
|
template, |
69
|
|
|
'\n'.join( |
70
|
|
|
[os.path.join(template, 'wrong-dir'), wrong_full_cookiecutter_path] |
71
|
|
|
), |
72
|
|
|
) |
73
|
|
|
) |
74
|
|
|
|