codecov 0.2.2 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/codecov.rb +27 -8
  3. data/test/test_codecov.rb +137 -20
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daba8db072c6615d0e1715680a1753b8e8edf9d8c587674acf22d92edceec613
4
- data.tar.gz: e139f061377a88e8a7266db95acd9354b20f81885e49c2d070c8075a13c701d4
3
+ metadata.gz: 7cd3288a24f1d4655e837e068c45a5be7817d60c08de8acebb5ca0f3afcb0569
4
+ data.tar.gz: a42575d126b29448adda5177e95ac961070098aca3761c7074b4b6a1b9827154
5
5
  SHA512:
6
- metadata.gz: 8a9ab0aee0e4c10821f6663b6f6577fba04f0408cbc1e76a55beb7d7113a07c6cbb2951c3ba88c9eb65af0f8599c1414caaa4b89f853c82441a21db0d37bd099
7
- data.tar.gz: 51f0f8a2ebf765daa5687b2337db53c3a5a2aaf086a307629f5f463a4d3b435f6e3586586059def772ede349c2d2eb8273c2e967eb9a14be01c432098d31b1fc
6
+ metadata.gz: 00210aa6e8cbf2837c5c199e3f7b2ce3d80a7c171f4bb84f94c0202da55fff0b33fe7ea6ad2ec39ed65086d41b6c375433954197d0fd9343e4de0bc7cf437c83
7
+ data.tar.gz: e492d90061f8db4c1df141d5d2e04d520cf4f8704a923f5556ed5a50d28bc6fcf2ec9cf72acea749671da975cef11420a60d1c8bf20498291bb5761f79e3e35f
@@ -8,7 +8,7 @@ require 'colorize'
8
8
  require 'zlib'
9
9
 
10
10
  class SimpleCov::Formatter::Codecov
11
- VERSION = '0.2.2'
11
+ VERSION = '0.2.7'
12
12
 
13
13
  ### CIs
14
14
  RECOGNIZED_CIS = [
@@ -20,6 +20,7 @@ class SimpleCov::Formatter::Codecov
20
20
  CIRCLE = 'Circle CI',
21
21
  CODESHIP = 'Codeship CI',
22
22
  DRONEIO = 'Drone CI',
23
+ GITHUB = 'GitHub Actions',
23
24
  GITLAB = 'GitLab CI',
24
25
  HEROKU = 'Heroku CI',
25
26
  JENKINS = 'Jenkins CI',
@@ -62,6 +63,8 @@ class SimpleCov::Formatter::Codecov
62
63
  CODESHIP
63
64
  elsif ((ENV['CI'] == 'true') || (ENV['CI'] == 'drone')) && (ENV['DRONE'] == 'true')
64
65
  DRONEIO
66
+ elsif (ENV['CI'] == 'true') && (ENV['GITHUB_ACTIONS'] == 'true')
67
+ GITHUB
65
68
  elsif !ENV['GITLAB_CI'].nil?
66
69
  GITLAB
67
70
  elsif ENV['HEROKU_TEST_RUN_ID']
@@ -172,6 +175,15 @@ class SimpleCov::Formatter::Codecov
172
175
  params[:build_url] = ENV['DRONE_BUILD_LINK'] || ENV['DRONE_BUILD_URL'] || ENV['CI_BUILD_URL']
173
176
  params[:pr] = ENV['DRONE_PULL_REQUEST']
174
177
  params[:tag] = ENV['DRONE_TAG']
178
+ when GITHUB
179
+ # https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
180
+ params[:service] = 'github-actions'
181
+ params[:branch] = ENV['GITHUB_HEAD_REF'] || ENV['GITHUB_REF'].sub('refs/head/', '')
182
+ # PR refs are in the format: refs/pull/7/merge for pull_request events
183
+ params[:pr] = ENV['GITHUB_REF'].split('/')[2] unless ENV['GITHUB_HEAD_REF'].nil? || ENV['GITHUB_HEAD_REF'].empty?
184
+ params[:slug] = ENV['GITHUB_REPOSITORY']
185
+ params[:build] = ENV['GITHUB_RUN_ID']
186
+ params[:commit] = ENV['GITHUB_SHA']
175
187
  when GITLAB
176
188
  # http://doc.gitlab.com/ci/examples/README.html#environmental-variables
177
189
  # https://gitlab.com/gitlab-org/gitlab-ci-runner/blob/master/lib/build.rb#L96
@@ -305,7 +317,7 @@ class SimpleCov::Formatter::Codecov
305
317
  def create_report(report)
306
318
  result = {
307
319
  'meta' => {
308
- 'version' => 'codecov-ruby/v' + SimpleCov::Formatter::Codecov::VERSION
320
+ 'version' => 'codecov-ruby/v' + VERSION
309
321
  }
310
322
  }
311
323
  result.update(result_to_codecov(report))
@@ -315,14 +327,17 @@ class SimpleCov::Formatter::Codecov
315
327
  def gzip_report(report)
316
328
  puts ['==>'.green, 'Gzipping contents'].join(' ')
317
329
 
318
- gzip = Zlib::GzipWriter.new(StringIO.new)
330
+ io = StringIO.new
331
+ gzip = Zlib::GzipWriter.new(io)
319
332
  gzip << report
333
+ gzip.close
320
334
 
321
- gzip.close.string
335
+ io.string
322
336
  end
323
337
 
324
338
  def upload_to_codecov(ci, report)
325
339
  url = ENV['CODECOV_URL'] || 'https://codecov.io'
340
+ is_enterprise = url != 'https://codecov.io'
326
341
 
327
342
  params = build_params(ci)
328
343
  params_secret_token = params.clone
@@ -340,8 +355,11 @@ class SimpleCov::Formatter::Codecov
340
355
  puts " url: #{url}"
341
356
  puts " query: #{query_without_token}"
342
357
 
343
- response = upload_to_v4(url, gzipped_report, query, query_without_token)
344
- return false if response == false
358
+ response = false
359
+ unless is_enterprise
360
+ response = upload_to_v4(url, gzipped_report, query, query_without_token)
361
+ return false if response == false
362
+ end
345
363
 
346
364
  response || upload_to_v2(url, gzipped_report, query, query_without_token)
347
365
  end
@@ -352,7 +370,7 @@ class SimpleCov::Formatter::Codecov
352
370
  https.use_ssl = !url.match(/^https/).nil?
353
371
 
354
372
  puts ['-> '.green, 'Pinging Codecov'].join(' ')
355
- puts "#{url}/#{uri.path}?#{query_without_token}"
373
+ puts "#{url}#{uri.path}?#{query_without_token}"
356
374
 
357
375
  req = Net::HTTP::Post.new(
358
376
  "#{uri.path}?#{query}",
@@ -407,11 +425,12 @@ class SimpleCov::Formatter::Codecov
407
425
  https.use_ssl = !url.match(/^https/).nil?
408
426
 
409
427
  puts ['-> '.green, 'Uploading to Codecov'].join(' ')
410
- puts "#{url}/#{uri.path}?#{query_without_token}"
428
+ puts "#{url}#{uri.path}?#{query_without_token}"
411
429
 
412
430
  req = Net::HTTP::Post.new(
413
431
  "#{uri.path}?#{query}",
414
432
  {
433
+ 'Accept' => 'application/json',
415
434
  'Content-Encoding' => 'gzip',
416
435
  'Content-Type' => 'text/plain',
417
436
  'X-Content-Encoding' => 'gzip'
@@ -3,14 +3,44 @@
3
3
  require 'helper'
4
4
 
5
5
  class TestCodecov < Minitest::Test
6
- REALENV = {
7
- 'TRAVIS_BRANCH' => ENV['TRAVIS_BRANCH'],
8
- 'TRAVIS_COMMIT' => ENV['TRAVIS_COMMIT'],
9
- 'TRAVIS_REPO_SLUG' => ENV['TRAVIS_REPO_SLUG'],
10
- 'TRAVIS_JOB_NUMBER' => ENV['TRAVIS_JOB_NUMBER'],
11
- 'TRAVIS_PULL_REQUEST' => ENV['TRAVIS_PULL_REQUEST'],
12
- 'TRAVIS_JOB_ID' => ENV['TRAVIS_JOB_ID']
13
- }.freeze
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
+
14
44
  def url
15
45
  ENV['CODECOV_URL'] || 'https://codecov.io'
16
46
  end
@@ -32,22 +62,37 @@ class TestCodecov < Minitest::Test
32
62
  stub('SimpleCov::SourceFile', filename: filename, lines: lines)
33
63
  end
34
64
 
35
- def upload(success=true)
65
+ def upload(success = true)
66
+ WebMock.enable!
36
67
  formatter = SimpleCov::Formatter::Codecov.new
37
68
  result = stub('SimpleCov::Result', files: [
38
69
  stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
39
70
  stub_file('/path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil])
40
71
  ])
41
72
  SimpleCov.stubs(:root).returns('/path')
42
- data = formatter.format(result)
73
+ success_stubs if success
74
+ data = formatter.format(result, false)
43
75
  puts data
44
76
  puts data['params']
45
- if success
46
- assert_successful_upload(data)
47
- end
77
+ assert_successful_upload(data) if success
78
+ WebMock.reset!
48
79
  data
49
80
  end
50
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
+
51
96
  def assert_successful_upload(data)
52
97
  assert_equal(data['result']['uploaded'], true)
53
98
  assert_equal(data['result']['message'], 'Coverage reports upload successfully')
@@ -60,6 +105,8 @@ class TestCodecov < Minitest::Test
60
105
 
61
106
  def setup
62
107
  ENV['CI'] = nil
108
+ ENV['CIRCLECI'] = nil
109
+ ENV['GITHUB_ACTIONS'] = nil
63
110
  ENV['TRAVIS'] = nil
64
111
  end
65
112
 
@@ -134,6 +181,12 @@ class TestCodecov < Minitest::Test
134
181
  ENV['ghprbSourceBranch'] = nil
135
182
  ENV['GIT_BRANCH'] = nil
136
183
  ENV['GIT_COMMIT'] = nil
184
+ ENV['GITHUB_ACTIONS'] = nil
185
+ ENV['GITHUB_REF'] = nil
186
+ ENV['GITHUB_HEAD_REF'] = nil
187
+ ENV['GITHUB_REPOSITORY'] = nil
188
+ ENV['GITHUB_RUN_ID'] = nil
189
+ ENV['GITHUB_SHA'] = nil
137
190
  ENV['GITLAB_CI'] = nil
138
191
  ENV['HEROKU_TEST_RUN_ID'] = nil
139
192
  ENV['HEROKU_TEST_RUN_BRANCH'] = nil
@@ -149,13 +202,13 @@ class TestCodecov < Minitest::Test
149
202
  ENV['SEMAPHORE_REPO_SLUG'] = nil
150
203
  ENV['SHIPPABLE'] = nil
151
204
  ENV['TF_BUILD'] = nil
152
- ENV['TRAVIS'] = 'true'
153
- ENV['TRAVIS_BRANCH'] = REALENV['TRAVIS_BRANCH']
154
- ENV['TRAVIS_COMMIT'] = REALENV['TRAVIS_COMMIT']
155
- ENV['TRAVIS_JOB_ID'] = REALENV['TRAVIS_JOB_ID']
156
- ENV['TRAVIS_JOB_NUMBER'] = REALENV['TRAVIS_JOB_NUMBER']
157
- ENV['TRAVIS_PULL_REQUEST'] = REALENV['TRAVIS_PULL_REQUEST']
158
- ENV['TRAVIS_REPO_SLUG'] = REALENV['TRAVIS_REPO_SLUG']
205
+ ENV['TRAVIS'] = nil
206
+ ENV['TRAVIS_BRANCH'] = nil
207
+ ENV['TRAVIS_COMMIT'] = nil
208
+ ENV['TRAVIS_JOB_ID'] = nil
209
+ ENV['TRAVIS_JOB_NUMBER'] = nil
210
+ ENV['TRAVIS_PULL_REQUEST'] = nil
211
+ ENV['TRAVIS_REPO_SLUG'] = nil
159
212
  ENV['VCS_COMMIT_ID'] = nil
160
213
  ENV['WERCKER_GIT_BRANCH'] = nil
161
214
  ENV['WERCKER_GIT_COMMIT'] = nil
@@ -163,6 +216,8 @@ class TestCodecov < Minitest::Test
163
216
  ENV['WERCKER_GIT_REPOSITORY'] = nil
164
217
  ENV['WERCKER_MAIN_PIPELINE_STARTED'] = nil
165
218
  ENV['WORKSPACE'] = nil
219
+
220
+ REALENV.each_pair { |k, v| ENV[k] = v }
166
221
  end
167
222
 
168
223
  def test_git
@@ -174,6 +229,23 @@ class TestCodecov < Minitest::Test
174
229
  assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
175
230
  end
176
231
 
232
+ def test_enterprise
233
+ stub = stub_request(:post, %r{https:\/\/example.com\/upload\/v2})
234
+ .to_return(
235
+ status: 200,
236
+ 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\"}"
237
+ )
238
+
239
+ ENV['CODECOV_URL'] = 'https://example.com'
240
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
241
+ result = upload
242
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
243
+ assert_equal('12345678-1234-abcd-ef12-1234567890ab', result['result']['id'])
244
+ branch = `git rev-parse --abbrev-ref HEAD`.strip
245
+ assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
246
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
247
+ end
248
+
177
249
  def test_travis
178
250
  ENV['CI'] = 'true'
179
251
  ENV['TRAVIS'] = 'true'
@@ -384,6 +456,44 @@ class TestCodecov < Minitest::Test
384
456
  assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
385
457
  end
386
458
 
459
+ def test_github_pull_request
460
+ ENV['CI'] = 'true'
461
+ ENV['GITHUB_ACTIONS'] = 'true'
462
+ ENV['GITHUB_HEAD_REF'] = 'patch-2'
463
+ ENV['GITHUB_REF'] = 'refs/pull/7/merge'
464
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
465
+ ENV['GITHUB_RUN_ID'] = '1'
466
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
467
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
468
+ result = upload
469
+ assert_equal('github-actions', result['params'][:service])
470
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
471
+ assert_equal('codecov/ci-repo', result['params'][:slug])
472
+ assert_equal('1', result['params'][:build])
473
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
474
+ assert_equal('patch-2', result['params'][:branch])
475
+ assert_equal('7', result['params'][:pr])
476
+ end
477
+
478
+ def test_github_push
479
+ ENV['CI'] = 'true'
480
+ ENV['GITHUB_ACTIONS'] = 'true'
481
+ ENV['GITHUB_HEAD_REF'] = nil
482
+ ENV['GITHUB_REF'] = 'refs/head/master'
483
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
484
+ ENV['GITHUB_RUN_ID'] = '1'
485
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
486
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
487
+ result = upload
488
+ assert_equal('github-actions', result['params'][:service])
489
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
490
+ assert_equal('codecov/ci-repo', result['params'][:slug])
491
+ assert_equal('1', result['params'][:build])
492
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
493
+ assert_equal('master', result['params'][:branch])
494
+ assert_equal(false, result['params'].key?(:pr))
495
+ end
496
+
387
497
  def test_gitlab
388
498
  ENV['GITLAB_CI'] = 'true'
389
499
  ENV['CI_BUILD_REF_NAME'] = 'master'
@@ -521,6 +631,13 @@ class TestCodecov < Minitest::Test
521
631
  end
522
632
 
523
633
  def test_invalid_token
634
+ stub_request(:post, %r{https:\/\/codecov.io\/upload})
635
+ .to_return(
636
+ status: 400,
637
+ body: "HTTP 400\n" \
638
+ 'Provided token is not a UUID.'
639
+ )
640
+
524
641
  ENV['CODECOV_TOKEN'] = 'fake'
525
642
  result = upload(false)
526
643
  assert_equal(false, result['result']['uploaded'])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codecov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - codecov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2020-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-ci
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: mocha
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -143,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
157
  requirements:
144
158
  - - ">="
145
159
  - !ruby/object:Gem::Version
146
- version: '0'
160
+ version: '2.4'
147
161
  required_rubygems_version: !ruby/object:Gem::Requirement
148
162
  requirements:
149
163
  - - ">="