codecov 0.2.1 → 0.2.6

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 +4 -4
  2. data/lib/codecov.rb +29 -13
  3. data/test/test_codecov.rb +106 -20
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8a7910be87b931e578ca118951e56d194e4eacce5bae5c43f00008ee5da7ba6
4
- data.tar.gz: 10ebc17802361f6940bbec66dd8bf0407af083b0eaeccad17c480e72a587b79f
3
+ metadata.gz: 8b2e67c7bb37041adf9adb9dbe2fd8f9c2966131d338961785a29b25090e7206
4
+ data.tar.gz: 2ada93e046953b7e77397831259a21fcaef09012f4ea0c2c4584668b080d68a6
5
5
  SHA512:
6
- metadata.gz: bcbb1f88c22749834ca20f87869a8d189fb0ac592a1b2a5507c69d7177376850085caaf0f414cfbb778af8e9c9002fa2e6c4994be59fdc5fb42a3b39149ccf9d
7
- data.tar.gz: e02d2cda3d6bcc3e5d26391b49a737b31ea992679444dfbc53633eb999574929e2d7e67d2f180c7200daa00a7c6b9f8e94e5c6c80da2c94440748dd81b734a1b
6
+ metadata.gz: faf56288e9f75ee77f230da561da1ee87e3305fa7220e4b5962e2011e7f7d8c1f3c1a234a386ac698b00ad8999f500787f6b218c78c9009cb25bc096c056093c
7
+ data.tar.gz: 233cbebb75102d82b62daf1087a490493ee3cef84de7e91163bc8c5040076f2f1798ebde32aa7971c2234c306adcc13c7a3abcc1929489c1aba124a319f195cb
@@ -8,7 +8,7 @@ require 'colorize'
8
8
  require 'zlib'
9
9
 
10
10
  class SimpleCov::Formatter::Codecov
11
- VERSION = '0.2.1'
11
+ VERSION = '0.2.6'
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
@@ -280,20 +292,21 @@ class SimpleCov::Formatter::Codecov
280
292
  retries = 3
281
293
  begin
282
294
  response = https.request(req)
283
- rescue Timeout::Error => e
295
+ rescue Timeout::Error, SocketError => e
284
296
  retries -= 1
285
297
 
286
298
  if retries.zero?
287
- puts 'Timeout error uploading coverage reports to Codecov. Out of retries.'
299
+ puts 'Timeout or connection error uploading coverage reports to Codecov. Out of retries.'
288
300
  puts e
289
301
  return response
290
302
  end
291
303
 
292
- puts 'Timeout error uploading coverage reports to Codecov. Retrying...'
304
+ puts 'Timeout or connection error uploading coverage reports to Codecov. Retrying...'
293
305
  puts e
294
306
  retry
295
307
  rescue StandardError => e
296
308
  puts 'Error uploading coverage reports to Codecov. Sorry'
309
+ puts e.class.name
297
310
  puts e
298
311
  return response
299
312
  end
@@ -304,7 +317,7 @@ class SimpleCov::Formatter::Codecov
304
317
  def create_report(report)
305
318
  result = {
306
319
  'meta' => {
307
- 'version' => 'codecov-ruby/v' + SimpleCov::Formatter::Codecov::VERSION
320
+ 'version' => 'codecov-ruby/v' + VERSION
308
321
  }
309
322
  }
310
323
  result.update(result_to_codecov(report))
@@ -314,10 +327,12 @@ class SimpleCov::Formatter::Codecov
314
327
  def gzip_report(report)
315
328
  puts ['==>'.green, 'Gzipping contents'].join(' ')
316
329
 
317
- gzip = Zlib::GzipWriter.new(StringIO.new)
330
+ io = StringIO.new
331
+ gzip = Zlib::GzipWriter.new(io)
318
332
  gzip << report
333
+ gzip.close
319
334
 
320
- gzip.close.string
335
+ io.string
321
336
  end
322
337
 
323
338
  def upload_to_codecov(ci, report)
@@ -351,7 +366,7 @@ class SimpleCov::Formatter::Codecov
351
366
  https.use_ssl = !url.match(/^https/).nil?
352
367
 
353
368
  puts ['-> '.green, 'Pinging Codecov'].join(' ')
354
- puts "#{url}/#{uri.path}?#{query_without_token}"
369
+ puts "#{url}#{uri.path}?#{query_without_token}"
355
370
 
356
371
  req = Net::HTTP::Post.new(
357
372
  "#{uri.path}?#{query}",
@@ -362,8 +377,8 @@ class SimpleCov::Formatter::Codecov
362
377
  }
363
378
  )
364
379
  response = retry_request(req, https)
365
- if response.code == '400'
366
- puts response.body.red
380
+ if !response&.code || response.code == '400'
381
+ puts response&.body&.red
367
382
  return false
368
383
  end
369
384
 
@@ -429,8 +444,8 @@ class SimpleCov::Formatter::Codecov
429
444
  end
430
445
  end
431
446
 
432
- def format(result)
433
- net_blockers(:off)
447
+ def format(result, disable_net_blockers = true)
448
+ net_blockers(:off) if disable_net_blockers
434
449
 
435
450
  display_header
436
451
  ci = detect_ci
@@ -443,7 +458,8 @@ class SimpleCov::Formatter::Codecov
443
458
 
444
459
  report['result'] = JSON.parse(response)
445
460
  handle_report_response(report)
446
- net_blockers(:on)
461
+
462
+ net_blockers(:on) if disable_net_blockers
447
463
  report
448
464
  end
449
465
 
@@ -3,14 +3,32 @@
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::GITHUB
10
+ {
11
+ 'GITHUB_ACTIONS' => ENV['GITHUB_ACTIONS'],
12
+ 'GITHUB_HEAD_REF' => ENV['GITHUB_HEAD_REF'],
13
+ 'GITHUB_REF' => ENV['GITHUB_REF'],
14
+ 'GITHUB_REPOSITORY' => ENV['GITHUB_REPOSITORY'],
15
+ 'GITHUB_RUN_ID' => ENV['GITHUB_RUN_ID'],
16
+ 'GITHUB_SHA' => ENV['GITHUB_SHA']
17
+ }
18
+ elsif CI == SimpleCov::Formatter::Codecov::TRAVIS
19
+ {
20
+ 'TRAVIS' => ENV['TRAVIS'],
21
+ 'TRAVIS_BRANCH' => ENV['TRAVIS_BRANCH'],
22
+ 'TRAVIS_COMMIT' => ENV['TRAVIS_COMMIT'],
23
+ 'TRAVIS_REPO_SLUG' => ENV['TRAVIS_REPO_SLUG'],
24
+ 'TRAVIS_JOB_NUMBER' => ENV['TRAVIS_JOB_NUMBER'],
25
+ 'TRAVIS_PULL_REQUEST' => ENV['TRAVIS_PULL_REQUEST'],
26
+ 'TRAVIS_JOB_ID' => ENV['TRAVIS_JOB_ID']
27
+ }
28
+ else
29
+ {}
30
+ end.freeze
31
+
14
32
  def url
15
33
  ENV['CODECOV_URL'] || 'https://codecov.io'
16
34
  end
@@ -32,22 +50,36 @@ class TestCodecov < Minitest::Test
32
50
  stub('SimpleCov::SourceFile', filename: filename, lines: lines)
33
51
  end
34
52
 
35
- def upload(success=true)
53
+ def upload(success = true)
36
54
  formatter = SimpleCov::Formatter::Codecov.new
37
55
  result = stub('SimpleCov::Result', files: [
38
56
  stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
39
57
  stub_file('/path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil])
40
58
  ])
41
59
  SimpleCov.stubs(:root).returns('/path')
42
- data = formatter.format(result)
60
+ success_stubs if success
61
+ data = formatter.format(result, false)
43
62
  puts data
44
63
  puts data['params']
45
- if success
46
- assert_successful_upload(data)
47
- end
64
+ assert_successful_upload(data) if success
65
+ WebMock.reset!
48
66
  data
49
67
  end
50
68
 
69
+ def success_stubs
70
+ stub_request(:post, %r{https:\/\/codecov.io\/upload})
71
+ .to_return(
72
+ status: 200,
73
+ body: "https://codecov.io/gh/fake\n" \
74
+ 'https://storage.googleapis.com/codecov/fake'
75
+ )
76
+ stub_request(:put, %r{https:\/\/storage.googleapis.com\/})
77
+ .to_return(
78
+ status: 200,
79
+ body: ''
80
+ )
81
+ end
82
+
51
83
  def assert_successful_upload(data)
52
84
  assert_equal(data['result']['uploaded'], true)
53
85
  assert_equal(data['result']['message'], 'Coverage reports upload successfully')
@@ -60,6 +92,7 @@ class TestCodecov < Minitest::Test
60
92
 
61
93
  def setup
62
94
  ENV['CI'] = nil
95
+ ENV['GITHUB_ACTIONS'] = nil
63
96
  ENV['TRAVIS'] = nil
64
97
  end
65
98
 
@@ -134,6 +167,12 @@ class TestCodecov < Minitest::Test
134
167
  ENV['ghprbSourceBranch'] = nil
135
168
  ENV['GIT_BRANCH'] = nil
136
169
  ENV['GIT_COMMIT'] = nil
170
+ ENV['GITHUB_ACTIONS'] = nil
171
+ ENV['GITHUB_REF'] = nil
172
+ ENV['GITHUB_HEAD_REF'] = nil
173
+ ENV['GITHUB_REPOSITORY'] = nil
174
+ ENV['GITHUB_RUN_ID'] = nil
175
+ ENV['GITHUB_SHA'] = nil
137
176
  ENV['GITLAB_CI'] = nil
138
177
  ENV['HEROKU_TEST_RUN_ID'] = nil
139
178
  ENV['HEROKU_TEST_RUN_BRANCH'] = nil
@@ -149,13 +188,13 @@ class TestCodecov < Minitest::Test
149
188
  ENV['SEMAPHORE_REPO_SLUG'] = nil
150
189
  ENV['SHIPPABLE'] = nil
151
190
  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']
191
+ ENV['TRAVIS'] = nil
192
+ ENV['TRAVIS_BRANCH'] = nil
193
+ ENV['TRAVIS_COMMIT'] = nil
194
+ ENV['TRAVIS_JOB_ID'] = nil
195
+ ENV['TRAVIS_JOB_NUMBER'] = nil
196
+ ENV['TRAVIS_PULL_REQUEST'] = nil
197
+ ENV['TRAVIS_REPO_SLUG'] = nil
159
198
  ENV['VCS_COMMIT_ID'] = nil
160
199
  ENV['WERCKER_GIT_BRANCH'] = nil
161
200
  ENV['WERCKER_GIT_COMMIT'] = nil
@@ -163,6 +202,8 @@ class TestCodecov < Minitest::Test
163
202
  ENV['WERCKER_GIT_REPOSITORY'] = nil
164
203
  ENV['WERCKER_MAIN_PIPELINE_STARTED'] = nil
165
204
  ENV['WORKSPACE'] = nil
205
+
206
+ REALENV.each_pair { |k, v| ENV[k] = v }
166
207
  end
167
208
 
168
209
  def test_git
@@ -384,6 +425,44 @@ class TestCodecov < Minitest::Test
384
425
  assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
385
426
  end
386
427
 
428
+ def test_github_pull_request
429
+ ENV['CI'] = 'true'
430
+ ENV['GITHUB_ACTIONS'] = 'true'
431
+ ENV['GITHUB_HEAD_REF'] = 'patch-2'
432
+ ENV['GITHUB_REF'] = 'refs/pull/7/merge'
433
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
434
+ ENV['GITHUB_RUN_ID'] = '1'
435
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
436
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
437
+ result = upload
438
+ assert_equal('github-actions', result['params'][:service])
439
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
440
+ assert_equal('codecov/ci-repo', result['params'][:slug])
441
+ assert_equal('1', result['params'][:build])
442
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
443
+ assert_equal('patch-2', result['params'][:branch])
444
+ assert_equal('7', result['params'][:pr])
445
+ end
446
+
447
+ def test_github_push
448
+ ENV['CI'] = 'true'
449
+ ENV['GITHUB_ACTIONS'] = 'true'
450
+ ENV['GITHUB_HEAD_REF'] = nil
451
+ ENV['GITHUB_REF'] = 'refs/head/master'
452
+ ENV['GITHUB_REPOSITORY'] = 'codecov/ci-repo'
453
+ ENV['GITHUB_RUN_ID'] = '1'
454
+ ENV['GITHUB_SHA'] = 'c739768fcac68144a3a6d82305b9c4106934d31a'
455
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
456
+ result = upload
457
+ assert_equal('github-actions', result['params'][:service])
458
+ assert_equal('c739768fcac68144a3a6d82305b9c4106934d31a', result['params'][:commit])
459
+ assert_equal('codecov/ci-repo', result['params'][:slug])
460
+ assert_equal('1', result['params'][:build])
461
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
462
+ assert_equal('master', result['params'][:branch])
463
+ assert_equal(false, result['params'].key?(:pr))
464
+ end
465
+
387
466
  def test_gitlab
388
467
  ENV['GITLAB_CI'] = 'true'
389
468
  ENV['CI_BUILD_REF_NAME'] = 'master'
@@ -521,6 +600,13 @@ class TestCodecov < Minitest::Test
521
600
  end
522
601
 
523
602
  def test_invalid_token
603
+ stub_request(:post, %r{https:\/\/codecov.io\/upload})
604
+ .to_return(
605
+ status: 400,
606
+ body: "HTTP 400\n" \
607
+ 'Provided token is not a UUID.'
608
+ )
609
+
524
610
  ENV['CODECOV_TOKEN'] = 'fake'
525
611
  result = upload(false)
526
612
  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.1
4
+ version: 0.2.6
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-15 00:00:00.000000000 Z
11
+ date: 2020-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: hosted code coverage
112
126
  email:
113
127
  - hello@codecov.io
@@ -129,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
143
  requirements:
130
144
  - - ">="
131
145
  - !ruby/object:Gem::Version
132
- version: '0'
146
+ version: '2.4'
133
147
  required_rubygems_version: !ruby/object:Gem::Requirement
134
148
  requirements:
135
149
  - - ">="