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
|
|
|
FILE_CONTENT = { |
37
|
|
|
'%s/__init__.py' % PYTHON_MODULE: INIT_CONTENT, |
38
|
|
|
'setup.py': SETUP_PY, |
39
|
|
|
'requirements.txt': ['pytest'], |
40
|
|
|
'README.md': README_MARKDOWN, |
41
|
|
|
'CHANGELOG.md': [''], |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN' |
45
|
|
|
|
46
|
|
|
ISSUE_URL = 'https://api.github.com/repos/michaeljoseph/test_app/issues/{}' |
47
|
|
|
PULL_REQUEST_JSON = { |
48
|
|
|
'number': 111, |
49
|
|
|
'title': 'The title of the pull request', |
50
|
|
|
'body': 'An optional, longer description.', |
51
|
|
|
'user': { |
52
|
|
|
'login': 'someone' |
53
|
|
|
}, |
54
|
|
|
'labels': [ |
55
|
|
|
{'id': 1, 'name': 'bug'} |
56
|
|
|
], |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
LABEL_URL = 'https://api.github.com/repos/michaeljoseph/test_app/labels' |
60
|
|
|
BUG_LABEL_JSON = [ |
61
|
|
|
{ |
62
|
|
|
'id': 52048163, |
63
|
|
|
'url': 'https://api.github.com/repos/michaeljoseph/changes/labels/bug', |
64
|
|
|
'name': 'bug', |
65
|
|
|
'color': 'fc2929', |
66
|
|
|
'default': True |
67
|
|
|
} |
68
|
|
|
] |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
@pytest.fixture |
72
|
|
|
def git_repo(): |
73
|
|
|
with CliRunner().isolated_filesystem() as tempdir: |
74
|
|
|
readme_path = 'README.md' |
75
|
|
|
open(readme_path, 'w').write( |
76
|
|
|
'\n'.join(README_MARKDOWN) |
77
|
|
|
) |
78
|
|
|
version_path = 'version.txt' |
79
|
|
|
open(version_path, 'w').write('0.0.1') |
80
|
|
|
|
81
|
|
|
git_init([readme_path, version_path]) |
82
|
|
|
|
83
|
|
|
yield tempdir |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
@pytest.fixture |
87
|
|
|
def python_module(git_repo): |
|
|
|
|
88
|
|
|
os.mkdir(PYTHON_MODULE) |
89
|
|
|
|
90
|
|
|
for file_path, content in FILE_CONTENT.items(): |
91
|
|
|
open(file_path, 'w').write( |
92
|
|
|
'\n'.join(content) |
93
|
|
|
) |
94
|
|
|
|
95
|
|
|
git('add', [file for file in FILE_CONTENT.keys()]) |
96
|
|
|
|
97
|
|
|
yield |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
def git_init(files_to_add): |
101
|
|
|
git('init') |
102
|
|
|
git(shlex.split('config --local user.email "[email protected]"')) |
103
|
|
|
git('remote', 'add', 'origin', 'https://github.com/michaeljoseph/test_app.git') |
|
|
|
|
104
|
|
|
for file_to_add in files_to_add: |
105
|
|
|
git('add', file_to_add) |
106
|
|
|
git('commit', '-m', 'Initial commit') |
107
|
|
|
git(shlex.split('tag 0.0.1')) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def github_merge_commit(pull_request_number): |
111
|
|
|
from haikunator import Haikunator |
|
|
|
|
112
|
|
|
|
113
|
|
|
branch_name = Haikunator().haikunate() |
114
|
|
|
commands = [ |
115
|
|
|
'checkout -b {}'.format(branch_name), |
116
|
|
|
'commit --allow-empty -m "Test branch commit message"', |
117
|
|
|
'checkout master', |
118
|
|
|
'merge --no-ff {}'.format(branch_name), |
119
|
|
|
|
120
|
|
|
'commit --allow-empty --amend -m ' |
121
|
|
|
'"Merge pull request #{} from test_app/{}"'.format( |
122
|
|
|
pull_request_number, |
123
|
|
|
branch_name, |
124
|
|
|
) |
125
|
|
|
] |
126
|
|
|
for command in commands: |
127
|
|
|
git(shlex.split(command)) |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
@pytest.fixture |
131
|
|
|
def with_releases_directory_and_bumpversion_file_prompt(mocker): |
|
|
|
|
132
|
|
|
prompt = mocker.patch( |
133
|
|
|
'changes.config.click.prompt', |
134
|
|
|
autospec=True |
135
|
|
|
) |
136
|
|
|
prompt.side_effect = [ |
137
|
|
|
# release_directory |
138
|
|
|
'docs/releases', |
139
|
|
|
# bumpversion files |
140
|
|
|
'version.txt', |
141
|
|
|
# quit prompt |
142
|
|
|
'.', |
143
|
|
|
# label descriptions |
144
|
|
|
# 'Features', |
145
|
|
|
# 'Bug Fixes' |
146
|
|
|
] |
147
|
|
|
|
148
|
|
|
prompt = mocker.patch( |
149
|
|
|
'changes.config.choose_labels', |
150
|
|
|
autospec=True |
151
|
|
|
) |
152
|
|
|
prompt.return_value = ['bug'] |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
@pytest.fixture |
156
|
|
|
def with_auth_token_prompt(mocker): |
157
|
|
|
_ = mocker.patch('changes.config.click.launch') |
158
|
|
|
|
159
|
|
|
prompt = mocker.patch('changes.config.click.prompt') |
160
|
|
|
prompt.return_value = 'foo' |
161
|
|
|
|
162
|
|
|
saved_token = None |
163
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
164
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
165
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
166
|
|
|
|
167
|
|
|
yield |
168
|
|
|
|
169
|
|
|
if saved_token: |
170
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
@pytest.fixture |
174
|
|
|
def with_auth_token_envvar(): |
175
|
|
|
saved_token = None |
176
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
177
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
178
|
|
|
|
179
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = 'foo' |
180
|
|
|
|
181
|
|
|
yield |
182
|
|
|
|
183
|
|
|
if saved_token: |
184
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
185
|
|
|
else: |
186
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
@pytest.fixture |
190
|
|
|
def changes_config_in_tmpdir(monkeypatch, tmpdir): |
191
|
|
|
IS_WINDOWS = 'win32' in str(sys.platform).lower() |
|
|
|
|
192
|
|
|
|
193
|
|
|
changes_config_file = Path(str(tmpdir.join('.changes'))) |
194
|
|
|
monkeypatch.setattr( |
195
|
|
|
changes.config, |
196
|
|
|
'expandvars' if IS_WINDOWS else 'expanduser', |
197
|
|
|
lambda x: str(changes_config_file) |
198
|
|
|
) |
199
|
|
|
assert not changes_config_file.exists() |
200
|
|
|
return changes_config_file |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
@pytest.fixture |
204
|
|
|
def configured(git_repo, changes_config_in_tmpdir): |
|
|
|
|
205
|
|
|
changes_config_in_tmpdir.write_text(textwrap.dedent( |
206
|
|
|
"""\ |
207
|
|
|
[changes] |
208
|
|
|
auth_token = "foo" |
209
|
|
|
""" |
210
|
|
|
)) |
211
|
|
|
|
212
|
|
|
Path('.changes.toml').write_text(textwrap.dedent( |
|
|
|
|
213
|
|
|
"""\ |
214
|
|
|
[changes] |
215
|
|
|
releases_directory = "docs/releases" |
216
|
|
|
|
217
|
|
|
[changes.labels.bug] |
218
|
|
|
default = true |
219
|
|
|
id = 208045946 |
220
|
|
|
url = "https://api.github.com/repos/michaeljoseph/test_app/labels/bug" |
221
|
|
|
name = "bug" |
222
|
|
|
description = "Bug" |
223
|
|
|
color = "f29513" |
224
|
|
|
""" |
225
|
|
|
)) |
226
|
|
|
|
227
|
|
|
Path('.bumpversion.cfg').write_text(textwrap.dedent( |
|
|
|
|
228
|
|
|
"""\ |
229
|
|
|
[bumpversion] |
230
|
|
|
current_version = 0.0.1 |
231
|
|
|
|
232
|
|
|
[bumpversion:file:version.txt] |
233
|
|
|
""" |
234
|
|
|
)) |
235
|
|
|
|
236
|
|
|
return str(changes_config_in_tmpdir) |
237
|
|
|
|
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.