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
|
|
|
'url': 'https://api.github.com/repos/michaeljoseph/test_app/issues/111' |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
LABEL_URL = 'https://api.github.com/repos/michaeljoseph/test_app/labels' |
75
|
|
|
BUG_LABEL_JSON = [ |
76
|
|
|
{ |
77
|
|
|
'id': 52048163, |
78
|
|
|
'url': 'https://api.github.com/repos/michaeljoseph/test_app/labels/bug', |
|
|
|
|
79
|
|
|
'name': 'bug', |
80
|
|
|
'color': 'fc2929', |
81
|
|
|
'default': True |
82
|
|
|
} |
83
|
|
|
] |
84
|
|
|
|
85
|
|
|
RELEASES_URL = 'https://api.github.com/repos/michaeljoseph/test_app/releases' |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
@pytest.fixture |
89
|
|
|
def git_repo(tmpdir): |
90
|
|
|
with CliRunner().isolated_filesystem() as repo_dir: |
91
|
|
|
for file_path, content in FILE_CONTENT.items(): |
92
|
|
|
open(file_path, 'w').write( |
93
|
|
|
'\n'.join(content) |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
git('init') |
97
|
|
|
git(shlex.split('config --local user.email "[email protected]"')) |
98
|
|
|
git(shlex.split('remote add origin https://github.com/michaeljoseph/test_app.git')) |
|
|
|
|
99
|
|
|
|
100
|
|
|
tmp_push_repo = Path(str(tmpdir)) |
101
|
|
|
git('init', '--bare', str(tmp_push_repo)) |
102
|
|
|
git(shlex.split('remote set-url --push origin {}'.format(tmp_push_repo.as_uri()))) |
|
|
|
|
103
|
|
|
|
104
|
|
|
git('add', [file for file in FILE_CONTENT.keys()]) |
105
|
|
|
|
106
|
|
|
git('commit', '-m', 'Initial commit') |
107
|
|
|
git(shlex.split('tag 0.0.1')) |
108
|
|
|
|
109
|
|
|
yield repo_dir |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
@pytest.fixture |
113
|
|
|
def python_module(git_repo): |
|
|
|
|
114
|
|
|
os.mkdir(PYTHON_MODULE) |
115
|
|
|
|
116
|
|
|
for file_path, content in PYTHON_PROJECT_CONTENT.items(): |
117
|
|
|
open(file_path, 'w').write( |
118
|
|
|
'\n'.join(content) |
119
|
|
|
) |
120
|
|
|
|
121
|
|
|
git('add', [file for file in PYTHON_PROJECT_CONTENT.keys()]) |
122
|
|
|
git('commit', '-m', 'Python project initialisation') |
123
|
|
|
|
124
|
|
|
yield |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def github_merge_commit(pull_request_number): |
128
|
|
|
from haikunator import Haikunator |
|
|
|
|
129
|
|
|
|
130
|
|
|
branch_name = Haikunator().haikunate() |
131
|
|
|
commands = [ |
132
|
|
|
'checkout -b {}'.format(branch_name), |
133
|
|
|
'commit --allow-empty -m "Test branch commit message"', |
134
|
|
|
'checkout master', |
135
|
|
|
'merge --no-ff {}'.format(branch_name), |
136
|
|
|
|
137
|
|
|
'commit --allow-empty --amend -m ' |
138
|
|
|
'"Merge pull request #{} from test_app/{}"'.format( |
139
|
|
|
pull_request_number, |
140
|
|
|
branch_name, |
141
|
|
|
) |
142
|
|
|
] |
143
|
|
|
for command in commands: |
144
|
|
|
git(shlex.split(command)) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
# prompts_for_tool_configuration |
148
|
|
|
@pytest.fixture |
149
|
|
|
def with_releases_directory_and_bumpversion_file_prompt(mocker): |
|
|
|
|
150
|
|
|
prompt = mocker.patch( |
151
|
|
|
'changes.config.click.prompt', |
152
|
|
|
autospec=True |
153
|
|
|
) |
154
|
|
|
prompt.side_effect = [ |
155
|
|
|
# release_directory |
156
|
|
|
'docs/releases', |
157
|
|
|
# bumpversion files |
158
|
|
|
'version.txt', |
159
|
|
|
# quit prompt |
160
|
|
|
'.', |
161
|
|
|
# label descriptions |
162
|
|
|
# 'Features', |
163
|
|
|
# 'Bug Fixes' |
164
|
|
|
] |
165
|
|
|
|
166
|
|
|
prompt = mocker.patch( |
167
|
|
|
'changes.config.prompt.choose_labels', |
168
|
|
|
autospec=True |
169
|
|
|
) |
170
|
|
|
prompt.return_value = ['bug'] |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
@pytest.fixture |
174
|
|
|
def with_auth_token_prompt(mocker): |
175
|
|
|
_ = mocker.patch('changes.config.click.launch') |
176
|
|
|
|
177
|
|
|
prompt = mocker.patch('changes.config.click.prompt') |
178
|
|
|
prompt.return_value = 'foo' |
179
|
|
|
|
180
|
|
|
saved_token = None |
181
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
182
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
183
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
184
|
|
|
|
185
|
|
|
yield |
186
|
|
|
|
187
|
|
|
if saved_token: |
188
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
@pytest.fixture |
192
|
|
|
def with_auth_token_envvar(): |
193
|
|
|
saved_token = None |
194
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
195
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
196
|
|
|
|
197
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = 'foo' |
198
|
|
|
|
199
|
|
|
yield |
200
|
|
|
|
201
|
|
|
if saved_token: |
202
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
203
|
|
|
else: |
204
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
@pytest.fixture |
208
|
|
|
def changes_config_in_tmpdir(monkeypatch, tmpdir): |
209
|
|
|
IS_WINDOWS = 'win32' in str(sys.platform).lower() |
|
|
|
|
210
|
|
|
|
211
|
|
|
changes_config_file = Path(str(tmpdir.join('.changes'))) |
212
|
|
|
monkeypatch.setattr( |
213
|
|
|
changes.config, |
214
|
|
|
'expandvars' if IS_WINDOWS else 'expanduser', |
215
|
|
|
lambda x: str(changes_config_file) |
216
|
|
|
) |
217
|
|
|
assert not changes_config_file.exists() |
218
|
|
|
return changes_config_file |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
@pytest.fixture |
222
|
|
|
def configured(git_repo, changes_config_in_tmpdir): |
|
|
|
|
223
|
|
|
changes_config_in_tmpdir.write_text(textwrap.dedent( |
224
|
|
|
"""\ |
225
|
|
|
[changes] |
226
|
|
|
auth_token = "foo" |
227
|
|
|
""" |
228
|
|
|
)) |
229
|
|
|
|
230
|
|
|
Path('.changes.toml').write_text(textwrap.dedent( |
|
|
|
|
231
|
|
|
"""\ |
232
|
|
|
[changes] |
233
|
|
|
releases_directory = "docs/releases" |
234
|
|
|
|
235
|
|
|
[changes.labels.bug] |
236
|
|
|
default = true |
237
|
|
|
id = 208045946 |
238
|
|
|
url = "https://api.github.com/repos/michaeljoseph/test_app/labels/bug" |
239
|
|
|
name = "bug" |
240
|
|
|
description = "Bug" |
241
|
|
|
color = "f29513" |
242
|
|
|
""" |
243
|
|
|
)) |
244
|
|
|
|
245
|
|
|
Path('.bumpversion.cfg').write_text(textwrap.dedent( |
|
|
|
|
246
|
|
|
"""\ |
247
|
|
|
[bumpversion] |
248
|
|
|
current_version = 0.0.1 |
249
|
|
|
|
250
|
|
|
[bumpversion:file:version.txt] |
251
|
|
|
""" |
252
|
|
|
)) |
253
|
|
|
|
254
|
|
|
for file_to_add in ['.changes.toml', '.bumpversion.cfg']: |
255
|
|
|
git('add', file_to_add) |
256
|
|
|
git('commit', '-m', 'Add changes configuration files') |
257
|
|
|
|
258
|
|
|
return str(changes_config_in_tmpdir) |
259
|
|
|
|
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__.py
files in your module folders. Make sure that you place one file in each sub-folder.