codecov 0.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/codecov.rb +620 -0
  3. data/test/test_codecov.rb +675 -0
  4. metadata +158 -0
@@ -0,0 +1,675 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+
5
+ class TestCodecov < Minitest::Test
6
+ CI = SimpleCov::Formatter::Codecov.new.detect_ci
7
+
8
+ REALENV =
9
+ if CI == SimpleCov::Formatter::Codecov::CIRCLE
10
+ {
11
+ 'CIRCLECI' => ENV['CIRCLECI'],
12
+ 'CIRCLE_BUILD_NUM' => ENV['CIRCLE_BUILD_NUM'],
13
+ 'CIRCLE_NODE_INDEX' => ENV['CIRCLE_NODE_INDEX'],
14
+ 'CIRCLE_PROJECT_REPONAME' => ENV['CIRCLE_PROJECT_REPONAME'],
15
+ 'CIRCLE_PROJECT_USERNAME' => ENV['CIRCLE_PROJECT_USERNAME'],
16
+ 'CIRCLE_REPOSITORY_URL' => ENV['CIRCLE_REPOSITORY_URL'],
17
+ 'CIRCLE_PR_NUMBER' => ENV['CIRCLE_PR_NUMBER'],
18
+ 'CIRCLE_BRANCH' => ENV['CIRCLE_BRANCH'],
19
+ 'CIRCLE_SHA1' => ENV['CIRCLE_SHA1']
20
+ }
21
+ elsif CI == SimpleCov::Formatter::Codecov::GITHUB
22
+ {
23
+ 'GITHUB_ACTIONS' => ENV['GITHUB_ACTIONS'],
24
+ 'GITHUB_HEAD_REF' => ENV['GITHUB_HEAD_REF'],
25
+ 'GITHUB_REF' => ENV['GITHUB_REF'],
26
+ 'GITHUB_REPOSITORY' => ENV['GITHUB_REPOSITORY'],
27
+ 'GITHUB_RUN_ID' => ENV['GITHUB_RUN_ID'],
28
+ 'GITHUB_SHA' => ENV['GITHUB_SHA']
29
+ }
30
+ elsif CI == SimpleCov::Formatter::Codecov::TRAVIS
31
+ {
32
+ 'TRAVIS' => ENV['TRAVIS'],
33
+ 'TRAVIS_BRANCH' => ENV['TRAVIS_BRANCH'],
34
+ 'TRAVIS_COMMIT' => ENV['TRAVIS_COMMIT'],
35
+ 'TRAVIS_REPO_SLUG' => ENV['TRAVIS_REPO_SLUG'],
36
+ 'TRAVIS_JOB_NUMBER' => ENV['TRAVIS_JOB_NUMBER'],
37
+ 'TRAVIS_PULL_REQUEST' => ENV['TRAVIS_PULL_REQUEST'],
38
+ 'TRAVIS_JOB_ID' => ENV['TRAVIS_JOB_ID']
39
+ }
40
+ else
41
+ {}
42
+ end.freeze
43
+
44
+ def url
45
+ ENV['CODECOV_URL'] || 'https://codecov.io'
46
+ end
47
+
48
+ def test_defined
49
+ assert defined?(SimpleCov::Formatter::Codecov)
50
+ assert defined?(SimpleCov::Formatter::Codecov::VERSION)
51
+ end
52
+
53
+ def stub_file(filename, coverage)
54
+ lines = coverage.each_with_index.map do |cov, i|
55
+ skipped = false
56
+ if cov == :skipped
57
+ skipped = true
58
+ cov = 0
59
+ end
60
+ stub('SimpleCov::SourceFile::Line', skipped?: skipped, line_number: i + 1, coverage: cov)
61
+ end
62
+ stub('SimpleCov::SourceFile', filename: filename, lines: lines)
63
+ end
64
+
65
+ def upload(success = true)
66
+ WebMock.enable!
67
+ formatter = SimpleCov::Formatter::Codecov.new
68
+ result = stub('SimpleCov::Result', files: [
69
+ stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
70
+ stub_file('/path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil])
71
+ ])
72
+ SimpleCov.stubs(:root).returns('/path')
73
+ success_stubs if success
74
+ data = formatter.format(result, false)
75
+ puts data
76
+ puts data['params']
77
+ assert_successful_upload(data) if success
78
+ WebMock.reset!
79
+ data
80
+ end
81
+
82
+ def success_stubs
83
+ stub_request(:post, %r{https:\/\/codecov.io\/upload\/v4})
84
+ .to_return(
85
+ status: 200,
86
+ body: "https://codecov.io/gh/fake\n" \
87
+ 'https://storage.googleapis.com/codecov/fake'
88
+ )
89
+ stub_request(:put, %r{https:\/\/storage.googleapis.com\/})
90
+ .to_return(
91
+ status: 200,
92
+ body: ''
93
+ )
94
+ end
95
+
96
+ def assert_successful_upload(data)
97
+ assert_equal(data['result']['uploaded'], true)
98
+ assert_equal(data['result']['message'], 'Coverage reports upload successfully')
99
+ assert_equal(data['meta']['version'], 'codecov-ruby/v' + SimpleCov::Formatter::Codecov::VERSION)
100
+ assert_equal(data['coverage'].to_json, {
101
+ 'lib/something.rb' => [nil, 1, 0, 0, nil, 1, nil],
102
+ 'lib/somefile.rb' => [nil, 1, nil, 1, 1, 1, 0, 0, nil, 1, nil]
103
+ }.to_json)
104
+ end
105
+
106
+ def setup
107
+ ENV['CI'] = nil
108
+ ENV['CIRCLECI'] = nil
109
+ ENV['GITHUB_ACTIONS'] = nil
110
+ ENV['TRAVIS'] = nil
111
+ end
112
+
113
+ def teardown
114
+ # needed for sending this projects coverage
115
+ ENV['APPVEYOR'] = nil
116
+ ENV['APPVEYOR_ACCOUNT_NAME'] = nil
117
+ ENV['APPVEYOR_BUILD_VERSION'] = nil
118
+ ENV['APPVEYOR_JOB_ID'] = nil
119
+ ENV['APPVEYOR_PROJECT_SLUG'] = nil
120
+ ENV['APPVEYOR_PULL_REQUEST_NUMBER'] = nil
121
+ ENV['APPVEYOR_REPO_BRANCH'] = nil
122
+ ENV['APPVEYOR_REPO_COMMIT'] = nil
123
+ ENV['APPVEYOR_REPO_NAME'] = nil
124
+ ENV['BITBUCKET_BRANCH'] = nil
125
+ ENV['BITBUCKET_BUILD_NUMBER'] = nil
126
+ ENV['BITBUCKET_COMMIT'] = nil
127
+ ENV['BITRISE_BUILD_NUMBER'] = nil
128
+ ENV['BITRISE_BUILD_URL'] = nil
129
+ ENV['BITRISE_GIT_BRANCH'] = nil
130
+ ENV['BITRISE_GIT_COMMIT'] = nil
131
+ ENV['BITRISE_IO'] = nil
132
+ ENV['BITRISE_PULL_REQUEST'] = nil
133
+ ENV['BITRISEIO_GIT_REPOSITORY_OWNER'] = nil
134
+ ENV['BITRISEIO_GIT_REPOSITORY_SLUG'] = nil
135
+ ENV['BRANCH'] = nil
136
+ ENV['BRANCH_NAME'] = nil
137
+ ENV['BUILD_ID'] = nil
138
+ ENV['BUILD_NUMBER'] = nil
139
+ ENV['BUILD_NUMBER'] = nil
140
+ ENV['BUILD_URL'] = nil
141
+ ENV['BUILDKITE'] = nil
142
+ ENV['BUILDKITE_BRANCH'] = nil
143
+ ENV['BUILDKITE_JOB_ID'] = nil
144
+ ENV['BUILDKITE_BUILD_NUMBER'] = nil
145
+ ENV['BUILDKITE_BUILD_URL'] = nil
146
+ ENV['BUILDKITE_PROJECT_SLUG'] = nil
147
+ ENV['BUILDKITE_COMMIT'] = nil
148
+ ENV['CI'] = 'true'
149
+ ENV['CI_BRANCH'] = nil
150
+ ENV['CI_BUILD_ID'] = nil
151
+ ENV['CI_BUILD_NUMBER'] = nil
152
+ ENV['CI_BUILD_REF'] = nil
153
+ ENV['CI_BUILD_REF_NAME'] = nil
154
+ ENV['CI_BUILD_REPO'] = nil
155
+ ENV['CI_BUILD_URL'] = nil
156
+ ENV['CI_COMMIT'] = nil
157
+ ENV['CI_COMMIT_ID'] = nil
158
+ ENV['CI_NAME'] = nil
159
+ ENV['CI_PROJECT_DIR'] = nil
160
+ ENV['CI_SERVER_NAME'] = nil
161
+ ENV['CI_SERVER_NAME'] = nil
162
+ ENV['CIRCLE_BRANCH'] = nil
163
+ ENV['CIRCLE_BUILD_NUM'] = nil
164
+ ENV['CIRCLE_NODE_INDEX'] = nil
165
+ ENV['CIRCLE_PR_NUMBER'] = nil
166
+ ENV['CIRCLE_PROJECT_REPONAME'] = nil
167
+ ENV['CIRCLE_PROJECT_USERNAME'] = nil
168
+ ENV['CIRCLE_SHA1'] = nil
169
+ ENV['CIRCLECI'] = nil
170
+ ENV['CODEBUILD_CI'] = nil
171
+ ENV['CODEBUILD_BUILD_ID'] = nil
172
+ ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = nil
173
+ ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = nil
174
+ ENV['CODEBUILD_SOURCE_VERSION'] = nil
175
+ ENV['CODEBUILD_SOURCE_REPO_URL'] = nil
176
+ ENV['CODECOV_ENV'] = nil
177
+ ENV['CODECOV_SLUG'] = nil
178
+ ENV['CODECOV_TOKEN'] = nil
179
+ ENV['CODECOV_URL'] = nil
180
+ ENV['COMMIT'] = nil
181
+ ENV['DRONE'] = nil
182
+ ENV['DRONE_BRANCH'] = nil
183
+ ENV['DRONE_BUILD_URL'] = nil
184
+ ENV['DRONE_COMMIT'] = nil
185
+ ENV['ghprbActualCommit'] = nil
186
+ ENV['ghprbPullId'] = nil
187
+ ENV['ghprbSourceBranch'] = nil
188
+ ENV['GIT_BRANCH'] = nil
189
+ ENV['GIT_COMMIT'] = nil
190
+ ENV['GITHUB_ACTIONS'] = nil
191
+ ENV['GITHUB_REF'] = nil
192
+ ENV['GITHUB_HEAD_REF'] = nil
193
+ ENV['GITHUB_REPOSITORY'] = nil
194
+ ENV['GITHUB_RUN_ID'] = nil
195
+ ENV['GITHUB_SHA'] = nil
196
+ ENV['GITLAB_CI'] = nil
197
+ ENV['HEROKU_TEST_RUN_ID'] = nil
198
+ ENV['HEROKU_TEST_RUN_BRANCH'] = nil
199
+ ENV['HEROKU_TEST_RUN_COMMIT_VERSION'] = nil
200
+ ENV['JENKINS_URL'] = nil
201
+ ENV['MAGNUM'] = nil
202
+ ENV['PULL_REQUEST'] = nil
203
+ ENV['REPO_NAME'] = nil
204
+ ENV['REVISION'] = nil
205
+ ENV['SEMAPHORE'] = nil
206
+ ENV['SEMAPHORE_BUILD_NUMBER'] = nil
207
+ ENV['SEMAPHORE_CURRENT_THREAD'] = nil
208
+ ENV['SEMAPHORE_REPO_SLUG'] = nil
209
+ ENV['SHIPPABLE'] = nil
210
+ ENV['TF_BUILD'] = nil
211
+ ENV['TRAVIS'] = nil
212
+ ENV['TRAVIS_BRANCH'] = nil
213
+ ENV['TRAVIS_COMMIT'] = nil
214
+ ENV['TRAVIS_JOB_ID'] = nil
215
+ ENV['TRAVIS_JOB_NUMBER'] = nil
216
+ ENV['TRAVIS_PULL_REQUEST'] = nil
217
+ ENV['TRAVIS_REPO_SLUG'] = nil
218
+ ENV['VCS_COMMIT_ID'] = nil
219
+ ENV['WERCKER_GIT_BRANCH'] = nil
220
+ ENV['WERCKER_GIT_COMMIT'] = nil
221
+ ENV['WERCKER_GIT_OWNER'] = nil
222
+ ENV['WERCKER_GIT_REPOSITORY'] = nil
223
+ ENV['WERCKER_MAIN_PIPELINE_STARTED'] = nil
224
+ ENV['WORKSPACE'] = nil
225
+
226
+ REALENV.each_pair { |k, v| ENV[k] = v }
227
+ end
228
+
229
+ def test_git
230
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
231
+ result = upload
232
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
233
+ branch = `git rev-parse --abbrev-ref HEAD`.strip
234
+ assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
235
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
236
+ end
237
+
238
+ def test_enterprise
239
+ stub = stub_request(:post, %r{https:\/\/example.com\/upload\/v2})
240
+ .to_return(
241
+ status: 200,
242
+ body: "{\"id\": \"12345678-1234-abcd-ef12-1234567890ab\", \"message\": \"Coverage reports upload successfully\", \"meta\": { \"status\": 200 }, \"queued\": true, \"uploaded\": true, \"url\": \"https://example.com/github/codecov/codecov-bash/commit/2f6b51562b93e72c610671644fe2a303c5c0e8e5\"}"
243
+ )
244
+
245
+ ENV['CODECOV_URL'] = 'https://example.com'
246
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
247
+ result = upload
248
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
249
+ assert_equal('12345678-1234-abcd-ef12-1234567890ab', result['result']['id'])
250
+ branch = `git rev-parse --abbrev-ref HEAD`.strip
251
+ assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
252
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
253
+ end
254
+
255
+ def test_travis
256
+ ENV['CI'] = 'true'
257
+ ENV['TRAVIS'] = 'true'
258
+ ENV['TRAVIS_BRANCH'] = 'master'
259
+ ENV['TRAVIS_COMMIT'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
260
+ ENV['TRAVIS_JOB_ID'] = '33116958'
261
+ ENV['TRAVIS_PULL_REQUEST'] = 'false'
262
+ ENV['TRAVIS_JOB_NUMBER'] = '1'
263
+ ENV['TRAVIS_REPO_SLUG'] = 'codecov/ci-repo'
264
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
265
+ result = upload
266
+ assert_equal('travis', result['params'][:service])
267
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
268
+ assert_equal('codecov/ci-repo', result['params'][:slug])
269
+ assert_equal('1', result['params'][:build])
270
+ assert_equal('33116958', result['params'][:job])
271
+ assert_equal('false', result['params'][:pull_request])
272
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
273
+ end
274
+
275
+ def test_codeship
276
+ ENV['CI'] = 'true'
277
+ ENV['CI_NAME'] = 'codeship'
278
+ ENV['CI_BRANCH'] = 'master'
279
+ ENV['CI_BUILD_NUMBER'] = '1'
280
+ ENV['CI_COMMIT_ID'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
281
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
282
+ result = upload
283
+ assert_equal('codeship', result['params'][:service])
284
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
285
+ assert_equal('1', result['params'][:build])
286
+ assert_equal('master', result['params'][:branch])
287
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
288
+ end
289
+
290
+ def test_buildkite
291
+ ENV['CI'] = 'true'
292
+ ENV['BUILDKITE'] = 'true'
293
+ ENV['BUILDKITE_BRANCH'] = 'master'
294
+ ENV['BUILDKITE_BUILD_NUMBER'] = '1'
295
+ ENV['BUILDKITE_JOB_ID'] = '2'
296
+ ENV['BUILDKITE_BUILD_URL'] = 'http://demo'
297
+ ENV['BUILDKITE_PROJECT_SLUG'] = 'owner/repo'
298
+ ENV['BUILDKITE_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
299
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
300
+ result = upload
301
+ assert_equal('buildkite', result['params'][:service])
302
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
303
+ assert_equal('1', result['params'][:build])
304
+ assert_equal('2', result['params'][:job])
305
+ assert_equal('master', result['params'][:branch])
306
+ assert_equal('owner/repo', result['params'][:slug])
307
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
308
+ end
309
+
310
+ def test_jenkins
311
+ ENV['JENKINS_URL'] = 'true'
312
+ ENV['ghprbSourceBranch'] = 'master'
313
+ ENV['BUILD_NUMBER'] = '1'
314
+ ENV['ghprbActualCommit'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
315
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
316
+ ENV['BUILD_URL'] = 'https://jenkins'
317
+ ENV['ghprbPullId'] = '1'
318
+ result = upload
319
+ assert_equal('jenkins', result['params'][:service])
320
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
321
+ assert_equal('1', result['params'][:build])
322
+ assert_equal('1', result['params'][:pr])
323
+ assert_equal('master', result['params'][:branch])
324
+ assert_equal('https://jenkins', result['params'][:build_url])
325
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
326
+ end
327
+
328
+ def test_jenkins_2
329
+ ENV['JENKINS_URL'] = 'true'
330
+ ENV['GIT_BRANCH'] = 'master'
331
+ ENV['BUILD_NUMBER'] = '1'
332
+ ENV['GIT_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
333
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
334
+ ENV['BUILD_URL'] = 'https://jenkins'
335
+ result = upload
336
+ assert_equal('jenkins', result['params'][:service])
337
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
338
+ assert_equal('1', result['params'][:build])
339
+ assert_equal('master', result['params'][:branch])
340
+ assert_equal('https://jenkins', result['params'][:build_url])
341
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
342
+ end
343
+
344
+ def test_shippable
345
+ ENV['CI'] = 'true'
346
+ ENV['SHIPPABLE'] = 'true'
347
+ ENV['BRANCH'] = 'master'
348
+ ENV['BUILD_NUMBER'] = '1'
349
+ ENV['BUILD_URL'] = 'http://shippable.com/...'
350
+ ENV['PULL_REQUEST'] = 'false'
351
+ ENV['REPO_NAME'] = 'owner/repo'
352
+ ENV['COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
353
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
354
+ result = upload
355
+ assert_equal('shippable', result['params'][:service])
356
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
357
+ assert_equal('false', result['params'][:pull_request])
358
+ assert_equal('1', result['params'][:build])
359
+ assert_equal('http://shippable.com/...', result['params'][:build_url])
360
+ assert_equal('master', result['params'][:branch])
361
+ assert_equal('owner/repo', result['params'][:slug])
362
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
363
+ end
364
+
365
+ def test_appveyor
366
+ ENV['CI'] = 'True'
367
+ ENV['APPVEYOR'] = 'True'
368
+ ENV['APPVEYOR_REPO_BRANCH'] = 'master'
369
+ ENV['APPVEYOR_JOB_ID'] = 'build'
370
+ ENV['APPVEYOR_PULL_REQUEST_NUMBER'] = '1'
371
+ ENV['APPVEYOR_ACCOUNT_NAME'] = 'owner'
372
+ ENV['APPVEYOR_PROJECT_SLUG'] = 'repo'
373
+ ENV['APPVEYOR_BUILD_VERSION'] = 'job'
374
+ ENV['APPVEYOR_REPO_NAME'] = 'owner/repo'
375
+ ENV['APPVEYOR_REPO_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
376
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
377
+ result = upload
378
+ assert_equal('appveyor', result['params'][:service])
379
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
380
+ assert_equal('master', result['params'][:branch])
381
+ assert_equal('owner/repo', result['params'][:slug])
382
+ assert_equal('1', result['params'][:pr])
383
+ assert_equal('build', result['params'][:build])
384
+ assert_equal('owner/repo/job', result['params'][:job])
385
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
386
+ end
387
+
388
+ def test_circleci
389
+ ENV['CI'] = 'true'
390
+ ENV['CIRCLECI'] = 'true'
391
+ ENV['CIRCLE_BRANCH'] = 'master'
392
+ ENV['CIRCLE_BUILD_NUM'] = '1'
393
+ ENV['CIRCLE_NODE_INDEX'] = '2'
394
+ ENV['CIRCLE_PR_NUMBER'] = '3'
395
+ ENV['CIRCLE_PROJECT_USERNAME'] = 'owner'
396
+ ENV['CIRCLE_PROJECT_REPONAME'] = 'repo'
397
+ ENV['CIRCLE_SHA1'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
398
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
399
+ result = upload
400
+ assert_equal('circleci', result['params'][:service])
401
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
402
+ assert_equal('1', result['params'][:build])
403
+ assert_equal('2', result['params'][:job])
404
+ assert_equal('3', result['params'][:pr])
405
+ assert_equal('master', result['params'][:branch])
406
+ assert_equal('owner/repo', result['params'][:slug])
407
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
408
+ end
409
+
410
+ def test_semaphore
411
+ ENV['CI'] = 'true'
412
+ ENV['SEMAPHORE'] = 'true'
413
+ ENV['BRANCH_NAME'] = 'master'
414
+ ENV['SEMAPHORE_REPO_SLUG'] = 'owner/repo'
415
+ ENV['SEMAPHORE_BUILD_NUMBER'] = '1'
416
+ ENV['SEMAPHORE_CURRENT_THREAD'] = '2'
417
+ ENV['REVISION'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
418
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
419
+ result = upload
420
+ assert_equal('semaphore', result['params'][:service])
421
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
422
+ assert_equal('1', result['params'][:build])
423
+ assert_equal('2', result['params'][:job])
424
+ assert_equal('master', result['params'][:branch])
425
+ assert_equal('owner/repo', result['params'][:slug])
426
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
427
+ end
428
+
429
+ def test_drone
430
+ ENV['CI'] = 'true'
431
+ ENV['DRONE'] = 'true'
432
+ ENV['DRONE_BRANCH'] = 'master'
433
+ ENV['DRONE_BUILD_NUMBER'] = '1'
434
+ ENV['DRONE_BUILD_URL'] = 'https://drone.io/...'
435
+ ENV['DRONE_COMMIT'] = '1123566'
436
+ ENV['CODECOV_SLUG'] = 'codecov/ci-repo'
437
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
438
+ result = upload
439
+ assert_equal('drone.io', result['params'][:service])
440
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
441
+ assert_equal('1', result['params'][:build])
442
+ assert_equal('https://drone.io/...', result['params'][:build_url])
443
+ assert_equal('codecov/ci-repo', result['params'][:slug])
444
+ assert_equal('master', result['params'][:branch])
445
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
446
+ end
447
+
448
+ def test_wercker
449
+ ENV['CI'] = 'true'
450
+ ENV['WERCKER_GIT_BRANCH'] = 'master'
451
+ ENV['WERCKER_MAIN_PIPELINE_STARTED'] = '1'
452
+ ENV['WERCKER_GIT_OWNER'] = 'owner'
453
+ ENV['WERCKER_GIT_REPOSITORY'] = 'repo'
454
+ ENV['WERCKER_GIT_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
455
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
456
+ result = upload
457
+ assert_equal('wercker', result['params'][:service])
458
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
459
+ assert_equal('1', result['params'][:build])
460
+ assert_equal('master', result['params'][:branch])
461
+ assert_equal('owner/repo', result['params'][:slug])
462
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
463
+ end
464
+
465
+ def test_github_pull_request
466
+ ENV['CI'] = 'true'
467
+ ENV['GITHUB_ACTIONS'] = 'true'
468
+ ENV['GITHUB_HEAD_REF'] = 'patch-2'
469
+ ENV['GITHUB_REF'] = 'refs/pull/7/merge'
470
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
471
+ ENV['GITHUB_RUN_ID'] = '1'
472
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
473
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
474
+ result = upload
475
+ assert_equal('github-actions', result['params'][:service])
476
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
477
+ assert_equal('codecov/ci-repo', result['params'][:slug])
478
+ assert_equal('1', result['params'][:build])
479
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
480
+ assert_equal('patch-2', result['params'][:branch])
481
+ assert_equal('7', result['params'][:pr])
482
+ end
483
+
484
+ def test_github_push
485
+ ENV['CI'] = 'true'
486
+ ENV['GITHUB_ACTIONS'] = 'true'
487
+ ENV['GITHUB_HEAD_REF'] = nil
488
+ ENV['GITHUB_REF'] = 'refs/head/master'
489
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
490
+ ENV['GITHUB_RUN_ID'] = '1'
491
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
492
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
493
+ result = upload
494
+ assert_equal('github-actions', result['params'][:service])
495
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
496
+ assert_equal('codecov/ci-repo', result['params'][:slug])
497
+ assert_equal('1', result['params'][:build])
498
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
499
+ assert_equal('master', result['params'][:branch])
500
+ assert_equal(false, result['params'].key?(:pr))
501
+ end
502
+
503
+ def test_gitlab
504
+ ENV['GITLAB_CI'] = 'true'
505
+ ENV['CI_BUILD_REF_NAME'] = 'master'
506
+ ENV['CI_BUILD_ID'] = '1'
507
+ ENV['CI_BUILD_REPO'] = 'https://gitlab.com/owner/repo.git'
508
+ ENV['CI_BUILD_REF'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
509
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
510
+ result = upload
511
+ assert_equal('gitlab', result['params'][:service])
512
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
513
+ assert_equal('1', result['params'][:build])
514
+ assert_equal('master', result['params'][:branch])
515
+ assert_equal('owner/repo', result['params'][:slug])
516
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
517
+ end
518
+
519
+ def test_bitrise
520
+ ENV['CI'] = 'true'
521
+ ENV['BITRISE_IO'] = 'true'
522
+ ENV['BITRISE_BUILD_NUMBER'] = '1'
523
+ ENV['BITRISE_BUILD_URL'] = 'https://app.bitrise.io/build/123'
524
+ ENV['BITRISE_GIT_BRANCH'] = 'master'
525
+ ENV['BITRISE_PULL_REQUEST'] = '2'
526
+ ENV['BITRISEIO_GIT_REPOSITORY_OWNER'] = 'owner'
527
+ ENV['BITRISEIO_GIT_REPOSITORY_SLUG'] = 'repo'
528
+ ENV['BITRISE_GIT_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
529
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
530
+ result = upload
531
+ assert_equal('bitrise', result['params'][:service])
532
+ assert_equal('1', result['params'][:build])
533
+ assert_equal('https://app.bitrise.io/build/123', result['params'][:build_url])
534
+ assert_equal('master', result['params'][:branch])
535
+ assert_equal('2', result['params'][:pr])
536
+ assert_equal('owner/repo', result['params'][:slug])
537
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
538
+ end
539
+
540
+ def test_teamcity
541
+ ENV['CI_SERVER_NAME'] = 'TeamCity'
542
+ ENV['TEAMCITY_BUILD_BRANCH'] = 'master'
543
+ ENV['TEAMCITY_BUILD_ID'] = '1'
544
+ ENV['TEAMCITY_BUILD_URL'] = 'http://teamcity/...'
545
+ ENV['TEAMCITY_BUILD_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
546
+ ENV['TEAMCITY_BUILD_REPOSITORY'] = 'https://github.com/owner/repo.git'
547
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
548
+ result = upload
549
+ assert_equal('teamcity', result['params'][:service])
550
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
551
+ assert_equal('1', result['params'][:build])
552
+ assert_equal('master', result['params'][:branch])
553
+ assert_equal('owner/repo', result['params'][:slug])
554
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
555
+ end
556
+
557
+ def test_azure_pipelines
558
+ ENV['TF_BUILD'] = '1'
559
+ ENV['BUILD_SOURCEBRANCH'] = 'master'
560
+ ENV['SYSTEM_JOBID'] = '92a2fa25-f940-5df6-a185-81eb9ae2031d'
561
+ ENV['BUILD_BUILDID'] = '1'
562
+ ENV['SYSTEM_TEAMFOUNDATIONSERVERURI'] = 'https://dev.azure.com/codecov/'
563
+ ENV['SYSTEM_TEAMPROJECT'] = 'repo'
564
+ ENV['BUILD_SOURCEVERSION'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
565
+ ENV['BUILD_REPOSITORY_ID'] = 'owner/repo'
566
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
567
+ result = upload
568
+ assert_equal('azure_pipelines', result['params'][:service])
569
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
570
+ assert_equal('1', result['params'][:build])
571
+ assert_equal('master', result['params'][:branch])
572
+ assert_equal('owner/repo', result['params'][:slug])
573
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
574
+ end
575
+
576
+ def test_heroku
577
+ ENV['HEROKU_TEST_RUN_ID'] = '454f5dc9-afa4-433f-bb28-84678a00fd98'
578
+ ENV['HEROKU_TEST_RUN_BRANCH'] = 'master'
579
+ ENV['HEROKU_TEST_RUN_COMMIT_VERSION'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
580
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
581
+
582
+ result = upload
583
+ assert_equal('heroku', result['params'][:service])
584
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
585
+ assert_equal('454f5dc9-afa4-433f-bb28-84678a00fd98', result['params'][:build])
586
+ assert_equal('master', result['params'][:branch])
587
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
588
+ end
589
+
590
+ def test_bitbucket_pr
591
+ ENV['CI'] = 'true'
592
+ ENV['BITBUCKET_BUILD_NUMBER'] = '100'
593
+ ENV['BITBUCKET_BRANCH'] = 'master'
594
+ ENV['BITBUCKET_COMMIT'] = '743b04806ea67'
595
+ ENV['VCS_COMMIT_ID'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
596
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
597
+
598
+ result = upload
599
+ assert_equal('bitbucket', result['params'][:service])
600
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
601
+ assert_equal('100', result['params'][:build])
602
+ assert_equal('master', result['params'][:branch])
603
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
604
+ end
605
+
606
+ def test_bitbucket
607
+ ENV['CI'] = 'true'
608
+ ENV['BITBUCKET_BUILD_NUMBER'] = '100'
609
+ ENV['BITBUCKET_BRANCH'] = 'master'
610
+ ENV['BITBUCKET_COMMIT'] = '743b04806ea677403aa2ff26c6bdeb85005de658'
611
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
612
+
613
+ result = upload
614
+ assert_equal('bitbucket', result['params'][:service])
615
+ assert_equal('743b04806ea677403aa2ff26c6bdeb85005de658', result['params'][:commit])
616
+ assert_equal('100', result['params'][:build])
617
+ assert_equal('master', result['params'][:branch])
618
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
619
+ end
620
+
621
+ def test_codebuild
622
+ ENV['CODEBUILD_CI'] = "true"
623
+ ENV['CODEBUILD_BUILD_ID'] = "codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3"
624
+ ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = 'd653b934ed59c1a785cc1cc79d08c9aaa4eba73b'
625
+ ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = 'refs/heads/master'
626
+ ENV['CODEBUILD_SOURCE_VERSION'] = 'pr/123'
627
+ ENV['CODEBUILD_SOURCE_REPO_URL'] = 'https://github.com/owner/repo.git'
628
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
629
+
630
+ result = upload
631
+
632
+ assert_equal("codebuild", result['params'][:service])
633
+ assert_equal("d653b934ed59c1a785cc1cc79d08c9aaa4eba73b", result['params'][:commit])
634
+ assert_equal("codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3", result['params'][:build])
635
+ assert_equal("codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3", result['params'][:job])
636
+ assert_equal("owner/repo", result['params'][:slug])
637
+ assert_equal("master", result['params'][:branch])
638
+ assert_equal("123", result['params'][:pr])
639
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
640
+ end
641
+
642
+ def test_filenames_are_shortened_correctly
643
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
644
+
645
+ formatter = SimpleCov::Formatter::Codecov.new
646
+ result = stub('SimpleCov::Result', files: [
647
+ stub_file('/path/lib/something.rb', []),
648
+ stub_file('/path/path/lib/path_somefile.rb', [])
649
+ ])
650
+ SimpleCov.stubs(:root).returns('/path')
651
+ data = formatter.format(result)
652
+ puts data
653
+ puts data['params']
654
+ assert_equal(data['coverage'].to_json, {
655
+ 'lib/something.rb' => [nil],
656
+ 'path/lib/path_somefile.rb' => [nil]
657
+ }.to_json)
658
+ end
659
+
660
+ def test_invalid_token
661
+ stub_request(:post, %r{https:\/\/codecov.io\/upload})
662
+ .to_return(
663
+ status: 400,
664
+ body: "HTTP 400\n" \
665
+ 'Provided token is not a UUID.'
666
+ )
667
+
668
+ ENV['CODECOV_TOKEN'] = 'fake'
669
+ result = upload(false)
670
+ assert_equal(false, result['result']['uploaded'])
671
+ branch = `git rev-parse --abbrev-ref HEAD`.strip
672
+ assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
673
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
674
+ end
675
+ end