1
|
|
|
import os |
2
|
|
|
import shlex |
3
|
|
|
|
4
|
|
|
import pytest |
5
|
|
|
import responses |
|
|
|
|
6
|
|
|
from click.testing import CliRunner |
|
|
|
|
7
|
|
|
from plumbum.cmd import git |
|
|
|
|
8
|
|
|
|
9
|
|
|
pytest_plugins = 'pytester' |
|
|
|
|
10
|
|
|
|
11
|
|
|
# TODO: textwrap.dedent.heredoc |
12
|
|
|
INIT_CONTENT = [ |
13
|
|
|
'"""A test app"""', |
14
|
|
|
'', |
15
|
|
|
"__version__ = '0.0.1'", |
16
|
|
|
"__url__ = 'https://github.com/someuser/test_app'", |
17
|
|
|
"__author__ = 'Some User'", |
18
|
|
|
"__email__ = '[email protected]'" |
19
|
|
|
] |
20
|
|
|
SETUP_PY = [ |
21
|
|
|
'from setuptools import setup', |
22
|
|
|
"setup(name='test_app'", |
23
|
|
|
] |
24
|
|
|
README_MARKDOWN = [ |
25
|
|
|
'# Test App', |
26
|
|
|
'', |
27
|
|
|
'This is the test application.' |
28
|
|
|
] |
29
|
|
|
|
30
|
|
|
PYTHON_MODULE = 'test_app' |
31
|
|
|
|
32
|
|
|
FILE_CONTENT = { |
33
|
|
|
'%s/__init__.py' % PYTHON_MODULE: INIT_CONTENT, |
34
|
|
|
'setup.py': SETUP_PY, |
35
|
|
|
'requirements.txt': ['pytest'], |
36
|
|
|
'README.md': README_MARKDOWN, |
37
|
|
|
'CHANGELOG.md': [''], |
38
|
|
|
} |
39
|
|
|
ISSUE_URL = 'https://api.github.com/repos/michaeljoseph/test_app/issues/{}' |
40
|
|
|
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN' |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.fixture |
44
|
|
|
def python_module(): |
45
|
|
|
with CliRunner().isolated_filesystem(): |
46
|
|
|
os.mkdir(PYTHON_MODULE) |
47
|
|
|
|
48
|
|
|
for file_path, content in FILE_CONTENT.items(): |
49
|
|
|
open(file_path, 'w').write( |
50
|
|
|
'\n'.join(content) |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
git_init(FILE_CONTENT.keys()) |
54
|
|
|
|
55
|
|
|
yield |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
@pytest.fixture |
59
|
|
|
def git_repo(): |
60
|
|
|
with CliRunner().isolated_filesystem(): |
61
|
|
|
readme_path = 'README.md' |
62
|
|
|
open(readme_path, 'w').write( |
63
|
|
|
'\n'.join(README_MARKDOWN) |
64
|
|
|
) |
65
|
|
|
git_init([readme_path]) |
66
|
|
|
|
67
|
|
|
yield |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def git_init(files_to_add): |
71
|
|
|
git('init') |
72
|
|
|
git(shlex.split('config --global user.email "[email protected]"')) |
73
|
|
|
git('remote', 'add', 'origin', 'https://github.com/michaeljoseph/test_app.git') |
|
|
|
|
74
|
|
|
for file_to_add in files_to_add: |
75
|
|
|
git('add', file_to_add) |
76
|
|
|
git('commit', '-m', 'Initial commit') |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
@pytest.fixture |
80
|
|
|
@responses.activate |
81
|
|
|
def git_repo_with_merge_commit(git_repo): |
|
|
|
|
82
|
|
|
pull_request_number = '111' |
83
|
|
|
github_merge_commit(pull_request_number) |
84
|
|
|
|
85
|
|
|
responses.add( |
86
|
|
|
responses.GET, |
87
|
|
|
ISSUE_URL.format(pull_request_number), |
88
|
|
|
json={ |
89
|
|
|
'number': int(pull_request_number), |
90
|
|
|
'title': 'The title of the pull request', |
91
|
|
|
'body': 'An optional, longer description.', |
92
|
|
|
'user': { |
93
|
|
|
'login': 'someone' |
94
|
|
|
}, |
95
|
|
|
'labels': [ |
96
|
|
|
{'id': 1, 'name': 'feature'} |
97
|
|
|
], |
98
|
|
|
}, |
99
|
|
|
status=200, |
100
|
|
|
content_type='application/json' |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def github_merge_commit(pull_request_number): |
105
|
|
|
commands = [ |
106
|
|
|
'tag 0.0.1', |
107
|
|
|
'checkout -b test-branch', |
108
|
|
|
'commit --allow-empty -m "Test branch commit message"', |
109
|
|
|
'checkout master', |
110
|
|
|
'merge --no-ff test-branch', |
111
|
|
|
|
112
|
|
|
'commit --allow-empty --amend -m ' |
113
|
|
|
'"Merge pull request #{} from test_app/test-branch"'.format( |
114
|
|
|
pull_request_number |
115
|
|
|
) |
116
|
|
|
] |
117
|
|
|
for command in commands: |
118
|
|
|
git(shlex.split(command)) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
@pytest.fixture |
122
|
|
|
def with_auth_token_prompt(mocker): |
123
|
|
|
_ = mocker.patch('changes.commands.init.click.launch') |
124
|
|
|
|
125
|
|
|
prompt = mocker.patch('changes.commands.init.click.prompt') |
126
|
|
|
prompt.return_value = 'foo' |
127
|
|
|
|
128
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
129
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
130
|
|
|
del os.environ[AUTH_TOKEN_ENVVAR] |
131
|
|
|
|
132
|
|
|
yield |
133
|
|
|
|
134
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
@pytest.fixture |
138
|
|
|
def with_auth_token_envvar(): |
139
|
|
|
if os.environ.get(AUTH_TOKEN_ENVVAR): |
140
|
|
|
saved_token = os.environ[AUTH_TOKEN_ENVVAR] |
141
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = 'foo' |
142
|
|
|
|
143
|
|
|
yield |
144
|
|
|
|
145
|
|
|
os.environ[AUTH_TOKEN_ENVVAR] = saved_token |
146
|
|
|
|
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.