forked from TheKevJames/coveralls-python
/
configuration_test.py
238 lines (211 loc) · 9.16 KB
/
configuration_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# pylint: disable=no-self-use
import os
import shutil
import tempfile
import unittest
from unittest import mock
import pytest
try:
import yaml
except ImportError:
yaml = None
from coveralls import Coveralls
from coveralls.api import log
@mock.patch.object(Coveralls, 'config_filename', '.coveralls.mock')
class Configuration(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
os.chdir(self.dir)
with open('.coveralls.mock', 'w+') as fp:
fp.write('repo_token: xxx\n')
fp.write('service_name: jenkins\n')
def tearDown(self):
shutil.rmtree(self.dir)
@pytest.mark.skipif(yaml is None, reason='requires PyYAML')
@mock.patch.dict(os.environ, {}, clear=True)
def test_local_with_config(self):
cover = Coveralls()
assert cover.config['service_name'] == 'jenkins'
assert cover.config['repo_token'] == 'xxx'
assert 'service_job_id' not in cover.config
@pytest.mark.skipif(yaml is not None, reason='requires no PyYAML')
@mock.patch.object(log, 'warning')
@mock.patch.dict(os.environ, {'COVERALLS_REPO_TOKEN': 'xxx'}, clear=True)
def test_local_with_config_without_yaml_module(self, mock_logger):
"""test local with config in yaml, but without yaml-installed"""
cover = Coveralls()
mock_logger.assert_called_once_with(
'PyYAML is not installed, skipping %s.', cover.config_filename)
@mock.patch.object(Coveralls, 'config_filename', '.coveralls.mock')
class NoConfiguration(unittest.TestCase):
@mock.patch.dict(os.environ, {'TRAVIS': 'True',
'TRAVIS_JOB_ID': '777',
'COVERALLS_REPO_TOKEN': 'yyy'}, clear=True)
def test_repo_token_from_env(self):
cover = Coveralls()
assert cover.config['service_name'] == 'travis-ci'
assert cover.config['service_job_id'] == '777'
assert cover.config['repo_token'] == 'yyy'
@mock.patch.dict(os.environ, {}, clear=True)
def test_misconfigured(self):
with pytest.raises(Exception) as excinfo:
Coveralls()
assert str(excinfo.value) == (
'Not on TravisCI. You have to provide either repo_token in '
'.coveralls.mock or set the COVERALLS_REPO_TOKEN env var.')
@mock.patch.dict(os.environ, {'GITHUB_ACTIONS': 'true'}, clear=True)
def test_misconfigured_github(self):
with pytest.raises(Exception) as excinfo:
Coveralls()
assert str(excinfo.value).startswith(
'Running on Github Actions but GITHUB_TOKEN is not set.')
@mock.patch.dict(os.environ, {'APPVEYOR': 'True',
'APPVEYOR_BUILD_ID': '1234567',
'APPVEYOR_PULL_REQUEST_NUMBER': '1234'},
clear=True)
def test_appveyor_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'appveyor'
assert cover.config['service_job_id'] == '1234567'
assert cover.config['service_pull_request'] == '1234'
@mock.patch.dict(os.environ, {'BUILDKITE': 'True',
'BUILDKITE_JOB_ID': '1234567',
'BUILDKITE_PULL_REQUEST': '1234'},
clear=True)
def test_buildkite_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'buildkite'
assert cover.config['service_job_id'] == '1234567'
assert cover.config['service_pull_request'] == '1234'
@mock.patch.dict(os.environ, {'BUILDKITE': 'True',
'BUILDKITE_JOB_ID': '1234567',
'BUILDKITE_PULL_REQUEST': 'false'},
clear=True)
def test_buildkite_no_config_no_pr(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'buildkite'
assert cover.config['service_job_id'] == '1234567'
assert 'service_pull_request' not in cover.config
@mock.patch.dict(
os.environ,
{'CIRCLECI': 'True',
'CIRCLE_BUILD_NUM': '888',
'CI_PULL_REQUEST': 'https://github.com/org/repo/pull/9999'},
clear=True)
def test_circleci_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'circle-ci'
assert cover.config['service_job_id'] == '888'
assert cover.config['service_pull_request'] == '9999'
@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_RUN_ID': '123456789',
'GITHUB_RUN_NUMBER': '12',
'GITHUB_HEAD_REF': 'fixup-branch',
'COVERALLS_REPO_TOKEN': 'xxx'},
clear=True)
def test_github_no_config(self):
cover = Coveralls()
assert cover.config['service_name'] == 'github-actions'
assert cover.config['service_pull_request'] == '1234'
assert cover.config['service_number'] == '123456789'
assert 'service_job_id' in cover.config
@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_TOKEN': 'xxx',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_RUN_ID': '987654321',
'GITHUB_RUN_NUMBER': '21',
'GITHUB_HEAD_REF': ''},
clear=True)
def test_github_no_config_no_pr(self):
cover = Coveralls()
assert cover.config['service_name'] == 'github-actions'
assert cover.config['service_number'] == '987654321'
assert 'service_job_id' in cover.config
assert 'service_pull_request' not in cover.config
@mock.patch.dict(
os.environ,
{'JENKINS_HOME': '/var/lib/jenkins',
'BUILD_NUMBER': '888',
'CI_PULL_REQUEST': 'https://github.com/org/repo/pull/9999'},
clear=True)
def test_jenkins_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'jenkins'
assert cover.config['service_job_id'] == '888'
assert cover.config['service_pull_request'] == '9999'
@mock.patch.dict(os.environ, {'TRAVIS': 'True',
'TRAVIS_JOB_ID': '777'}, clear=True)
def test_travis_no_config(self):
cover = Coveralls()
assert cover.config['service_name'] == 'travis-ci'
assert cover.config['service_job_id'] == '777'
assert 'repo_token' not in cover.config
@mock.patch.dict(
os.environ,
{'SEMAPHORE': 'True',
'SEMAPHORE_EXECUTABLE_UUID': '36980c73',
'SEMAPHORE_JOB_UUID': 'a26d42cf',
'SEMAPHORE_BRANCH_ID': '9999'},
clear=True)
def test_semaphore_classic_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'semaphore-ci'
assert cover.config['service_job_id'] == 'a26d42cf'
assert cover.config['service_number'] == '36980c73'
assert cover.config['service_pull_request'] == '9999'
@mock.patch.dict(
os.environ,
{'SEMAPHORE': 'True',
'SEMAPHORE_WORKFLOW_ID': 'b86b3adf',
'SEMAPHORE_JOB_ID': '2b942b49',
'SEMAPHORE_GIT_PR_NUMBER': '9999'},
clear=True)
def test_semaphore_20_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'semaphore-ci'
assert cover.config['service_job_id'] == '2b942b49'
assert cover.config['service_number'] == 'b86b3adf'
assert cover.config['service_pull_request'] == '9999'
@mock.patch.dict(
os.environ,
{'CI_NAME': 'generic-ci',
'CI_PULL_REQUEST': 'pull/1234',
'CI_JOB_ID': 'bb0e00166',
'CI_BUILD_NUMBER': '3',
'CI_BUILD_URL': 'https://generic-ci.local/build/123456789',
'CI_BRANCH': 'fixup-branch',
'COVERALLS_REPO_TOKEN': 'xxx'},
clear=True)
def test_generic_no_config(self):
cover = Coveralls()
assert cover.config['service_name'] == 'generic-ci'
assert cover.config['service_job_id'] == 'bb0e00166'
assert cover.config['service_branch'] == 'fixup-branch'
assert cover.config['service_pull_request'] == '1234'
@mock.patch.dict(
os.environ,
{'CI_NAME': 'generic-ci',
'CI_PULL_REQUEST': '',
'CI_JOB_ID': 'bb0e00166',
'CI_BUILD_NUMBER': '3',
'CI_BUILD_URL': 'https://generic-ci.local/build/123456789',
'CI_BRANCH': 'fixup-branch',
'COVERALLS_REPO_TOKEN': 'xxx'},
clear=True)
def test_generic_no_config_no_pr(self):
cover = Coveralls()
assert cover.config['service_name'] == 'generic-ci'
assert cover.config['service_job_id'] == 'bb0e00166'
assert cover.config['service_branch'] == 'fixup-branch'
assert 'service_pull_request' not in cover.config
@mock.patch.dict(os.environ, {'COVERALLS_SERVICE_NAME': 'xxx'}, clear=True)
def test_service_name_from_env(self):
cover = Coveralls(repo_token='yyy')
assert cover.config['service_name'] == 'xxx'