|
1
|
|
|
import os |
|
2
|
|
|
import shlex |
|
3
|
|
|
import textwrap |
|
4
|
|
|
from pathlib import Path |
|
5
|
|
|
|
|
6
|
|
|
import pytest |
|
7
|
|
|
import sys |
|
8
|
|
|
from click.testing import CliRunner |
|
|
|
|
|
|
9
|
|
|
from plumbum.cmd import git |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
import changes |
|
12
|
|
|
|
|
13
|
|
|
pytest_plugins = 'pytester' |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
# TODO: textwrap.dedent.heredoc |
|
16
|
|
|
INIT_CONTENT = [ |
|
17
|
|
|
'"""A test app"""', |
|
18
|
|
|
'', |
|
19
|
|
|
"__version__ = '0.0.1'", |
|
20
|
|
|
"__url__ = 'https://github.com/someuser/test_app'", |
|
21
|
|
|
"__author__ = 'Some User'", |
|
22
|
|
|
"__email__ = '[email protected]'" |
|
23
|
|
|
] |
|
24
|
|
|
SETUP_PY = [ |
|
25
|
|
|
'from setuptools import setup', |
|
26
|
|
|
"setup(name='test_app'", |
|
27
|
|
|
] |
|
28
|
|
|
README_MARKDOWN = [ |
|
29
|
|
|
'# Test App', |
|
30
|
|
|
'', |
|
31
|
|
|
'This is the test application.' |
|
32
|
|
|
] |
|
33
|
|
|
|
|
34
|
|
|
PYTHON_MODULE = 'test_app' |
|
35
|
|
|
|
|
36
|
|
|
PYTHON_PROJECT_CONTENT = { |
|
37
|
|
|
'%s/__init__.py' % PYTHON_MODULE: INIT_CONTENT, |
|
38
|
|
|
'setup.py': SETUP_PY, |
|
39
|
|
|
'requirements.txt': ['pytest'], |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
FILE_CONTENT = { |
|
43
|
|
|
'version.txt': ['0.0.1'], |
|
44
|
|
|
'README.md': README_MARKDOWN, |
|
45
|
|
|
'CHANGELOG.md': [''], |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN' |
|
49
|
|
|
|
|
50
|
|
|
BUG_LABEL_JSON = [ |
|
51
|
|
|
{ |
|
52
|
|
|
'id': 52048163, |
|
53
|
|
|
'url': 'https://api.github.com/repos/michaeljoseph/changes/labels/bug', |
|
54
|
|
|
'name': 'bug', |
|
55
|
|
|
'color': 'fc2929', |
|
56
|
|
|
'default': True |
|
57
|
|
|
} |
|
58
|
|
|
] |
|
59
|
|
|
|
|
60
|
|
|
ISSUE_URL = 'https://api.github.com/repos/michaeljoseph/test_app/issues/111' |
|
61
|
|
|
PULL_REQUEST_JSON = { |
|
62
|
|
|
'number': 111, |
|
63
|
|
|
'title': 'The title of the pull request', |
|
64
|
|
|
'body': 'An optional, longer description.', |
|
65
|
|
|
'user': { |
|
66
|
|
|
'login': 'michaeljoseph' |
|
67
|
|
|
}, |
|
68
|
|
|
'labels': [ |
|
69
|
|
|
{'id': 1, 'name': 'bug'} |
|
70
|
|
|
], |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
LABEL_URL = 'https://api.github.com/repos/michaeljoseph/test_app/labels' |
|
74
|
|
|
BUG_LABEL_JSON = [ |
|
75
|
|
|
{ |
|
76
|
|
|
'id': 52048163, |
|
77
|
|
|
'url': 'https://api.github.com/repos/michaeljoseph/test_app/labels/bug', |
|
|
|
|
|
|
78
|
|
|
'name': 'bug', |
|
79
|
|
|
'color': 'fc2929', |
|
80
|
|
|
'default': True |
|
81
|
|
|
} |
|
82
|
|
|
] |
|
83
|
|
|
|
|
84
|
|
|
RELEASES_URL = 'https://api.github.com/repos/michaeljoseph/test_app/releases' |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
@pytest.fixture |
|
88
|
|
|
def git_repo(tmpdir): |
|
89
|
|
|
with CliRunner().isolated_filesystem() as repo_dir: |
|
90
|
|
|
for file_path, content in FILE_CONTENT.items(): |
|
91
|
|
|
open(file_path, 'w').write( |
|
92
|
|
|
'\n'.join(content) |
|
93
|
|
|
) |
|
94
|
|
|
|
|
95
|
|
|
git('init') |
|
96
|
|
|
git(shlex.split('config --local user.email "[email protected]"')) |
|
97
|
|
|
git(shlex.split('remote add origin https://github.com/michaeljoseph/test_app.git')) |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
tmp_push_repo = Path(str(tmpdir)) |
|
100
|
|
|
git('init', '--bare', str(tmp_push_repo)) |
|
101
|
|
|
git(shlex.split('remote set-url --push origin {}'.format(tmp_push_repo.as_uri()))) |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
git('add', [file for file in FILE_CONTENT.keys()]) |
|
104
|
|
|
|
|
105
|
|
|
git('commit', '-m', 'Initial commit') |
|
106
|
|
|
git(shlex.split('tag 0.0.1')) |
|
107
|
|
|
|
|
108
|
|
|
yield repo_dir |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
@pytest.fixture |
|
112
|
|
|
def python_module(git_repo): |
|
|
|
|
|
|
113
|
|
|
os.mkdir(PYTHON_MODULE) |
|
114
|
|
|
|
|
115
|
|
|
for file_path, content in PYTHON_PROJECT_CONTENT.items(): |
|
116
|
|
|
open(file_path, 'w').write( |
|
117
|
|
|
'\n'.join(content) |
|
118
|
|
|
) |
|
119
|
|
|
|
|
120
|
|
|
git('add', [file for file in PYTHON_PROJECT_CONTENT.keys()]) |
|
121
|
|
|
git('commit', '-m', 'Python project initialisation') |
|
122
|
|
|
|
|
123
|
|
|
yield |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
def github_merge_commit(pull_request_number): |
|
127
|
|
|
from haikunator import Haikunator |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
branch_name = Haikunator().haikunate() |
|
130
|
|
|
commands = [ |
|
131
|
|
|
'checkout -b {}'.format(branch_name), |
|
132
|
|
|
'commit --allow-empty -m "Test branch commit message"', |
|
133
|
|
|
'checkout master', |
|
134
|
|
|
'merge --no-ff {}'.format(branch_name), |
|
135
|
|
|
|
|
136
|
|
|
'commit --allow-empty --amend -m ' |
|
137
|
|
|
'"Merge pull request #{} from test_app/{}"'.format( |
|
138
|
|
|
pull_request_number, |
|
139
|
|
|
branch_name, |
|
140
|
|
|
) |
|
141
|
|
|
] |
|
142
|
|
|
for command in commands: |
|
143
|
|
|
git(shlex.split(command)) |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
# prompts_for_tool_configuration |
|
147
|
|
|
@pytest.fixture |
|
148
|
|
|
def with_releases_directory_and_bumpversion_file_prompt(mocker): |
|
|
|
|
|
|
149
|
|
|
prompt = mocker.patch( |
|
150
|
|
|
'changes.config.click.prompt', |
|
151
|
|
|
autospec=True |
|
152
|
|
|
) |
|
153
|
|
|
prompt.side_effect = [ |
|
154
|
|
|
# release_directory |
|
155
|
|
|
'docs/releases', |
|
156
|
|
|
# bumpversion files |
|
157
|
|
|
'version.txt', |
|
158
|
|
|
# quit prompt |
|
159
|
|
|
'.', |
|
160
|
|
|
# label descriptions |
|
161
|
|
|
# 'Features', |
|
162
|
|
|
# 'Bug Fixes' |
|
163
|
|
|
] |
|
164
|
|
|
|
|
165
|
|
|
prompt = mocker.patch( |
|
166
|
|
|
'changes.config.prompt.choose_labels', |
|
167
|
|
|
autospec=True |
|
168
|
|
|
) |
|
169
|
|
|
prompt.return_value = ['bug'] |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
@pytest.fixture |
|
173
|
|
|
def with_auth_token_prompt(mocker): |
|
174
|
|
|
_ = mocker.patch('changes.config.click.launch') |
|
175
|
|
|
|
|
176
|
|
|
prompt = mocker.patch('changes.config.click.prompt') |
|
177
|
|
|
prompt.return_value = 'foo' |
|
178
|
|
|
|
|
179
|
|
|
saved_token = None |
|
180
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
|
181
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
|
182
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
|
183
|
|
|
|
|
184
|
|
|
yield |
|
185
|
|
|
|
|
186
|
|
|
if saved_token: |
|
187
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
@pytest.fixture |
|
191
|
|
|
def with_auth_token_envvar(): |
|
192
|
|
|
saved_token = None |
|
193
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
|
194
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
|
195
|
|
|
|
|
196
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = 'foo' |
|
197
|
|
|
|
|
198
|
|
|
yield |
|
199
|
|
|
|
|
200
|
|
|
if saved_token: |
|
201
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
|
202
|
|
|
else: |
|
203
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
@pytest.fixture |
|
207
|
|
|
def changes_config_in_tmpdir(monkeypatch, tmpdir): |
|
208
|
|
|
IS_WINDOWS = 'win32' in str(sys.platform).lower() |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
changes_config_file = Path(str(tmpdir.join('.changes'))) |
|
211
|
|
|
monkeypatch.setattr( |
|
212
|
|
|
changes.config, |
|
213
|
|
|
'expandvars' if IS_WINDOWS else 'expanduser', |
|
214
|
|
|
lambda x: str(changes_config_file) |
|
215
|
|
|
) |
|
216
|
|
|
assert not changes_config_file.exists() |
|
217
|
|
|
return changes_config_file |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
@pytest.fixture |
|
221
|
|
|
def configured(git_repo, changes_config_in_tmpdir): |
|
|
|
|
|
|
222
|
|
|
changes_config_in_tmpdir.write_text(textwrap.dedent( |
|
223
|
|
|
"""\ |
|
224
|
|
|
[changes] |
|
225
|
|
|
auth_token = "foo" |
|
226
|
|
|
""" |
|
227
|
|
|
)) |
|
228
|
|
|
|
|
229
|
|
|
Path('.changes.toml').write_text(textwrap.dedent( |
|
|
|
|
|
|
230
|
|
|
"""\ |
|
231
|
|
|
[changes] |
|
232
|
|
|
releases_directory = "docs/releases" |
|
233
|
|
|
|
|
234
|
|
|
[changes.labels.bug] |
|
235
|
|
|
default = true |
|
236
|
|
|
id = 208045946 |
|
237
|
|
|
url = "https://api.github.com/repos/michaeljoseph/test_app/labels/bug" |
|
238
|
|
|
name = "bug" |
|
239
|
|
|
description = "Bug" |
|
240
|
|
|
color = "f29513" |
|
241
|
|
|
""" |
|
242
|
|
|
)) |
|
243
|
|
|
|
|
244
|
|
|
Path('.bumpversion.cfg').write_text(textwrap.dedent( |
|
|
|
|
|
|
245
|
|
|
"""\ |
|
246
|
|
|
[bumpversion] |
|
247
|
|
|
current_version = 0.0.1 |
|
248
|
|
|
|
|
249
|
|
|
[bumpversion:file:version.txt] |
|
250
|
|
|
""" |
|
251
|
|
|
)) |
|
252
|
|
|
|
|
253
|
|
|
for file_to_add in ['.changes.toml', '.bumpversion.cfg']: |
|
254
|
|
|
git('add', file_to_add) |
|
255
|
|
|
git('commit', '-m', 'Add changes configuration files') |
|
256
|
|
|
|
|
257
|
|
|
return str(changes_config_in_tmpdir) |
|
258
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.