1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
conftest |
6
|
|
|
-------- |
7
|
|
|
|
8
|
|
|
Contains pytest fixtures which are globally available throughout the suite. |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
import pytest |
12
|
|
|
import os |
13
|
|
|
import shutil |
14
|
|
|
from cookiecutter import utils |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def backup_dir(original_dir, backup_dir): |
18
|
|
|
# If the default original_dir is pre-existing, move it to a temp location |
19
|
|
|
if not os.path.isdir(original_dir): |
20
|
|
|
return False |
21
|
|
|
|
22
|
|
|
# Remove existing backups before backing up. If they exist, they're stale. |
23
|
|
|
if os.path.isdir(backup_dir): |
24
|
|
|
utils.rmtree(backup_dir) |
25
|
|
|
|
26
|
|
|
shutil.copytree(original_dir, backup_dir) |
27
|
|
|
return True |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def restore_backup_dir(original_dir, backup_dir, original_dir_found): |
31
|
|
|
# Carefully delete the created original_dir only in certain |
32
|
|
|
# conditions. |
33
|
|
|
original_dir_is_dir = os.path.isdir(original_dir) |
34
|
|
|
if original_dir_found: |
35
|
|
|
# Delete the created original_dir as long as a backup |
36
|
|
|
# exists |
37
|
|
|
if original_dir_is_dir and os.path.isdir(backup_dir): |
38
|
|
|
utils.rmtree(original_dir) |
39
|
|
|
else: |
40
|
|
|
# Delete the created original_dir. |
41
|
|
|
# There's no backup because it never existed |
42
|
|
|
if original_dir_is_dir: |
43
|
|
|
utils.rmtree(original_dir) |
44
|
|
|
|
45
|
|
|
# Restore the user's default original_dir contents |
46
|
|
|
if os.path.isdir(backup_dir): |
47
|
|
|
shutil.copytree(backup_dir, original_dir) |
48
|
|
|
if os.path.isdir(original_dir): |
49
|
|
|
utils.rmtree(backup_dir) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@pytest.fixture(scope='function') |
53
|
|
|
def clean_system(request): |
54
|
|
|
""" |
55
|
|
|
Fixture that simulates a clean system with no config/cloned cookiecutters. |
56
|
|
|
|
57
|
|
|
It runs code which can be regarded as setup code as known from a unittest |
58
|
|
|
TestCase. Additionally it defines a local function referring to values |
59
|
|
|
which have been stored to local variables in the setup such as the location |
60
|
|
|
of the cookiecutters on disk. This function is registered as a teardown |
61
|
|
|
hook with `request.addfinalizer` at the very end of the fixture. Pytest |
62
|
|
|
runs the named hook as soon as the fixture is out of scope, when the test |
63
|
|
|
finished to put it another way. |
64
|
|
|
|
65
|
|
|
During setup: |
66
|
|
|
|
67
|
|
|
* Back up the `~/.cookiecutterrc` config file to `~/.cookiecutterrc.backup` |
68
|
|
|
* Back up the `~/.cookiecutters/` dir to `~/.cookiecutters.backup/` |
69
|
|
|
* Back up the `~/.cookiecutter_replay/` dir to |
70
|
|
|
`~/.cookiecutter_replay.backup/` |
71
|
|
|
* Starts off a test case with no pre-existing `~/.cookiecutterrc` or |
72
|
|
|
`~/.cookiecutters/` or `~/.cookiecutter_replay/` |
73
|
|
|
|
74
|
|
|
During teardown: |
75
|
|
|
|
76
|
|
|
* Delete `~/.cookiecutters/` only if a backup is present at |
77
|
|
|
`~/.cookiecutters.backup/` |
78
|
|
|
* Delete `~/.cookiecutter_replay/` only if a backup is present at |
79
|
|
|
`~/.cookiecutter_replay.backup/` |
80
|
|
|
* Restore the `~/.cookiecutterrc` config file from |
81
|
|
|
`~/.cookiecutterrc.backup` |
82
|
|
|
* Restore the `~/.cookiecutters/` dir from `~/.cookiecutters.backup/` |
83
|
|
|
* Restore the `~/.cookiecutter_replay/` dir from |
84
|
|
|
`~/.cookiecutter_replay.backup/` |
85
|
|
|
|
86
|
|
|
""" |
87
|
|
|
|
88
|
|
|
# If ~/.cookiecutterrc is pre-existing, move it to a temp location |
89
|
|
|
user_config_path = os.path.expanduser('~/.cookiecutterrc') |
90
|
|
|
user_config_path_backup = os.path.expanduser( |
91
|
|
|
'~/.cookiecutterrc.backup' |
92
|
|
|
) |
93
|
|
|
if os.path.exists(user_config_path): |
94
|
|
|
user_config_found = True |
95
|
|
|
shutil.copy(user_config_path, user_config_path_backup) |
96
|
|
|
os.remove(user_config_path) |
97
|
|
|
else: |
98
|
|
|
user_config_found = False |
99
|
|
|
|
100
|
|
|
# If the default cookiecutters_dir is pre-existing, move it to a |
101
|
|
|
# temp location |
102
|
|
|
cookiecutters_dir = os.path.expanduser('~/.cookiecutters') |
103
|
|
|
cookiecutters_dir_backup = os.path.expanduser('~/.cookiecutters.backup') |
104
|
|
|
cookiecutters_dir_found = backup_dir( |
105
|
|
|
cookiecutters_dir, cookiecutters_dir_backup |
106
|
|
|
) |
107
|
|
|
|
108
|
|
|
# If the default cookiecutter_replay_dir is pre-existing, move it to a |
109
|
|
|
# temp location |
110
|
|
|
cookiecutter_replay_dir = os.path.expanduser('~/.cookiecutter_replay') |
111
|
|
|
cookiecutter_replay_dir_backup = os.path.expanduser( |
112
|
|
|
'~/.cookiecutter_replay.backup' |
113
|
|
|
) |
114
|
|
|
cookiecutter_replay_dir_found = backup_dir( |
115
|
|
|
cookiecutter_replay_dir, cookiecutter_replay_dir_backup |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
def restore_backup(): |
119
|
|
|
# If it existed, restore ~/.cookiecutterrc |
120
|
|
|
# We never write to ~/.cookiecutterrc, so this logic is simpler. |
121
|
|
|
if user_config_found and os.path.exists(user_config_path_backup): |
122
|
|
|
shutil.copy(user_config_path_backup, user_config_path) |
123
|
|
|
os.remove(user_config_path_backup) |
124
|
|
|
|
125
|
|
|
# Carefully delete the created ~/.cookiecutters dir only in certain |
126
|
|
|
# conditions. |
127
|
|
|
restore_backup_dir( |
128
|
|
|
cookiecutters_dir, |
129
|
|
|
cookiecutters_dir_backup, |
130
|
|
|
cookiecutters_dir_found |
131
|
|
|
) |
132
|
|
|
|
133
|
|
|
# Carefully delete the created ~/.cookiecutter_replay dir only in |
134
|
|
|
# certain conditions. |
135
|
|
|
restore_backup_dir( |
136
|
|
|
cookiecutter_replay_dir, |
137
|
|
|
cookiecutter_replay_dir_backup, |
138
|
|
|
cookiecutter_replay_dir_found |
139
|
|
|
) |
140
|
|
|
|
141
|
|
|
request.addfinalizer(restore_backup) |
142
|
|
|
|