/
cli_test.py
167 lines (130 loc) · 5.37 KB
/
cli_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
import json
import os
from unittest import mock
import responses
import coveralls.cli
from coveralls.exception import CoverallsException
EXC = CoverallsException('bad stuff happened')
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
EXAMPLE_DIR = os.path.join(BASE_DIR, 'example')
def req_json(request):
return json.loads(request.body.decode('utf-8'))
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
@mock.patch.object(coveralls.cli.log, 'info')
@mock.patch.object(coveralls.Coveralls, 'wear')
def test_debug(mock_wear, mock_log):
coveralls.cli.main(argv=['debug'])
mock_wear.assert_called_with(dry_run=True)
mock_log.assert_has_calls([mock.call('Testing coveralls-python...')])
@mock.patch.dict(os.environ, clear=True)
@mock.patch.object(coveralls.cli.log, 'info')
@mock.patch.object(coveralls.Coveralls, 'wear')
def test_debug_no_token(mock_wear, mock_log):
coveralls.cli.main(argv=['debug'])
mock_wear.assert_called_with(dry_run=True)
mock_log.assert_has_calls([mock.call('Testing coveralls-python...')])
@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_REPOSITORY': 'test/repo',
'GITHUB_TOKEN': 'xxx',
'GITHUB_RUN_ID': '123456789',
'GITHUB_RUN_NUMBER': '123'},
clear=True)
@mock.patch.object(coveralls.cli.log, 'info')
@responses.activate
def test_finish(mock_log):
responses.add(responses.POST, 'https://coveralls.io/webhook',
json={'done': True}, status=200)
expected_json = {
'repo_token': 'xxx',
'repo_name': 'test/repo',
'payload': {
'status': 'done',
'build_num': '123456789'
}
}
coveralls.cli.main(argv=['--finish'])
mock_log.assert_has_calls(
[mock.call('Finishing parallel jobs...'),
mock.call('Done')])
assert len(responses.calls) == 1
assert req_json(responses.calls[0].request) == expected_json
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
@mock.patch.object(coveralls.cli.log, 'exception')
@responses.activate
def test_finish_exception(mock_log):
responses.add(responses.POST, 'https://coveralls.io/webhook',
json={'error': 'Mocked'}, status=200)
expected_json = {
'payload': {
'status': 'done'
}
}
msg = 'Parallel finish failed: Mocked'
try:
coveralls.cli.main(argv=['--finish'])
assert 0 == 1 # Should never reach this line
except SystemExit:
pass
mock_log.assert_has_calls([mock.call(CoverallsException(msg))])
assert len(responses.calls) == 1
assert req_json(responses.calls[0].request) == expected_json
@mock.patch.object(coveralls.cli.log, 'info')
@mock.patch.object(coveralls.Coveralls, 'wear')
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
def test_real(mock_wear, mock_log):
coveralls.cli.main(argv=[])
mock_wear.assert_called_with()
mock_log.assert_has_calls(
[mock.call('Submitting coverage to coveralls.io...'),
mock.call('Coverage submitted!')])
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
@mock.patch('coveralls.cli.Coveralls')
def test_rcfile(mock_coveralls):
coveralls.cli.main(argv=['--rcfile=coveragerc'])
mock_coveralls.assert_called_with(True, config_file='coveragerc',
service_name=None,
base_dir='')
@mock.patch.dict(os.environ, {}, clear=True)
@mock.patch('coveralls.cli.Coveralls')
def test_service_name(mock_coveralls):
coveralls.cli.main(argv=['--service=travis-pro'])
mock_coveralls.assert_called_with(True, config_file='.coveragerc',
service_name='travis-pro',
base_dir='')
@mock.patch.object(coveralls.cli.log, 'exception')
@mock.patch.object(coveralls.Coveralls, 'wear', side_effect=EXC)
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
def test_exception(_mock_coveralls, mock_log):
try:
coveralls.cli.main(argv=[])
assert 0 == 1 # Should never reach this line
except SystemExit:
pass
mock_log.assert_has_calls([mock.call(EXC)])
@mock.patch.object(coveralls.Coveralls, 'save_report')
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
def test_save_report_to_file(mock_coveralls):
"""Check save_report api usage."""
coveralls.cli.main(argv=['--output=test.log'])
mock_coveralls.assert_called_with('test.log')
@mock.patch.dict(os.environ, clear=True)
@mock.patch.object(coveralls.Coveralls, 'save_report')
def test_save_report_to_file_no_token(mock_coveralls):
"""Check save_report api usage when token is not set."""
coveralls.cli.main(argv=['--output=test.log'])
mock_coveralls.assert_called_with('test.log')
@mock.patch.object(coveralls.Coveralls, 'submit_report')
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
def test_submit(mock_submit):
json_file = os.path.join(EXAMPLE_DIR, 'example.json')
json_string = open(json_file).read()
coveralls.cli.main(argv=['--submit=' + json_file])
mock_submit.assert_called_with(json_string)
@mock.patch('coveralls.cli.Coveralls')
def test_base_dir_arg(mock_coveralls):
coveralls.cli.main(argv=['--basedir=foo'])
mock_coveralls.assert_called_with(True, config_file='.coveragerc',
service_name=None,
base_dir='foo')