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