codeclimate-test-reporter 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZGE4Y2ZjZDBmYzMyMzdhNzhkN2M1NGNhZWI5N2JkNWVmY2FjNzAxNA==
5
+ data.tar.gz: !binary |-
6
+ YTc5MjYzNTJmMGViZDdkNTQ5MzhkNjQ4MTE3ODYwMzM4OTk4NGIyMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MTM2ZTY3YWU4MjlmZTdlM2U2NjgyNDVlYTQ1NWI5Y2EwMmRkYTU0MjM3YTM3
10
+ N2I4YzJjZWZmNmM3NGNlNTkwMDQzNTFjNmZhY2M2YWUwMzQ3YTA4MTc0ZTVi
11
+ NGI2NjI5NmM3Yzg5MzEyNjJlNWZiYzE0NGIxZGFkNTczMDk3Mjg=
12
+ data.tar.gz: !binary |-
13
+ NDc4NzgxNWY5ZWQzMzA2NDBkYmYyMzY2MGJiNjVlYzhlZjZkNjdlMDYzZGY3
14
+ MzNlNDI0NWJiZWM0YTA0M2U1ZmIzYTg2NDQ1OTIzODEwYjdlMDJkZTVlMTRm
15
+ ZjM3YWFiNmVkZTY0ZWNjNTIyODIzNjZmNmEyNTA4YWI5Y2VlZTE=
data/README.md CHANGED
@@ -14,9 +14,9 @@ first step is to create an account at: [https://codeclimate.com](https://codecli
14
14
 
15
15
  1. Add this to your Gemfile:
16
16
 
17
- gem install "codeclimate-test-reporter", group: :test
17
+ gem "codeclimate-test-reporter", group: :test
18
18
 
19
- 1. Start the test reporter **at the very beginning** of your `test_helper.rb` or
19
+ 1. Start the test reporter **on the very first line** of your `test_helper.rb` or
20
20
  `spec_helper.rb` file:
21
21
 
22
22
  require "codeclimate-test-reporter"
@@ -26,7 +26,7 @@ Then set the `CODECLIMATE_REPO_TOKEN` environment variable when you run your bui
26
26
  on your CI server, and the results will show up in your Code Climate account.
27
27
 
28
28
  The `CODECLIMATE_REPO_TOKEN` value is provided after you add your repo to your
29
- Code Climate account if you are in the test coverage private beta.
29
+ Code Climate account by clicking on "Setup Test Coverage" on the right hand side of your feed.
30
30
 
31
31
  Please contact hello@codeclimate.com if you need any assistance setting this up.
32
32
 
@@ -19,7 +19,7 @@ module CodeClimate
19
19
  end
20
20
 
21
21
  class Configuration
22
- attr_accessor :branch, :logger, :profile
22
+ attr_accessor :branch, :logger, :profile, :path_prefix
23
23
 
24
24
  def logger
25
25
  @logger ||= default_logger
@@ -5,6 +5,7 @@ require "digest/sha1"
5
5
  require "simplecov"
6
6
 
7
7
  require "code_climate/test_reporter/exception_message"
8
+ require "code_climate/test_reporter/payload_validator"
8
9
 
9
10
  module CodeClimate
10
11
  module TestReporter
@@ -13,6 +14,7 @@ module CodeClimate
13
14
  print "Coverage = #{round(result.covered_percent, 2)}%. "
14
15
 
15
16
  payload = to_payload(result)
17
+ PayloadValidator.validate(payload)
16
18
  if tddium? || ENV["TO_FILE"]
17
19
  file_path = File.join(Dir.tmpdir, "codeclimate-test-coverage-#{SecureRandom.uuid}.json")
18
20
  print "Coverage results saved to #{file_path}... "
@@ -84,7 +86,8 @@ module CodeClimate
84
86
 
85
87
  def short_filename(filename)
86
88
  return filename unless ::SimpleCov.root
87
- filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
89
+ filename = filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
90
+ apply_prefix filename
88
91
  end
89
92
 
90
93
  def tddium?
@@ -99,6 +102,12 @@ module CodeClimate
99
102
 
100
103
  private
101
104
 
105
+ def apply_prefix filename
106
+ prefix = CodeClimate::TestReporter.configuration.path_prefix
107
+ return filename if prefix.nil?
108
+ "#{prefix}/#{filename}"
109
+ end
110
+
102
111
  def ci_service_data
103
112
  @ci_service_data ||= Ci.service_data
104
113
  end
@@ -0,0 +1,59 @@
1
+ module CodeClimate
2
+ module TestReporter
3
+ InvalidPayload = Class.new(StandardError)
4
+
5
+ class PayloadValidator
6
+ def initialize(payload)
7
+ @payload = payload
8
+ end
9
+
10
+ def self.validate(payload)
11
+ new(payload).validate
12
+ end
13
+
14
+ def validate
15
+ raise InvalidPayload, "A git commit sha was not found in the test report payload" unless commit_sha
16
+ raise InvalidPayload, "A git commit timestamp was not found in the test report payload" unless committed_at
17
+ raise InvalidPayload, "A run at timestamp was not found in the test report payload" unless run_at
18
+ raise InvalidPayload, "No source files were found in the test report payload" unless source_files?
19
+ raise InvalidPayload, "Invalid source files were found in the test report payload" unless valid_source_files?
20
+ true
21
+ end
22
+
23
+ private
24
+
25
+ def commit_sha
26
+ commit_sha_from_git || commit_sha_from_ci_service
27
+ end
28
+
29
+ def committed_at
30
+ @payload[:git] && @payload[:git][:committed_at]
31
+ end
32
+
33
+ def run_at
34
+ @payload[:run_at]
35
+ end
36
+
37
+ def source_files?
38
+ @payload[:source_files] && @payload[:source_files].any?
39
+ end
40
+
41
+ def valid_source_files?
42
+ @payload[:source_files].all? { |s| valid_source_file?(s) }
43
+ end
44
+
45
+ def valid_source_file?(file)
46
+ file.is_a?(Hash) && file[:coverage] && file[:name]
47
+ end
48
+
49
+ def commit_sha_from_git
50
+ @payload[:git] && @payload[:git][:head]
51
+ end
52
+
53
+ def commit_sha_from_ci_service
54
+ @payload[:ci_service] && @payload[:ci_service][:commit_sha]
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -1,5 +1,5 @@
1
1
  module CodeClimate
2
2
  module TestReporter
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -13,6 +13,7 @@ module CodeClimate::TestReporter
13
13
  expect(CodeClimate::TestReporter.configuration.logger).to be_instance_of Logger
14
14
  expect(CodeClimate::TestReporter.configuration.logger.level).to eq Logger::INFO
15
15
  expect(CodeClimate::TestReporter.configuration.profile).to eq('test_frameworks')
16
+ expect(CodeClimate::TestReporter.configuration.path_prefix).to be_nil
16
17
  end
17
18
  end
18
19
 
@@ -47,6 +48,19 @@ module CodeClimate::TestReporter
47
48
 
48
49
  expect(CodeClimate::TestReporter.configuration.profile).to eq('custom')
49
50
  end
51
+
52
+ it 'stores path prefix' do
53
+ CodeClimate::TestReporter.configure do |config|
54
+ config.path_prefix = 'custom'
55
+ end
56
+
57
+ expect(CodeClimate::TestReporter.configuration.path_prefix).to eq('custom')
58
+
59
+ CodeClimate::TestReporter.configure do |config|
60
+ config.path_prefix = nil
61
+ end
62
+
63
+ end
50
64
  end
51
65
  end
52
66
  end
@@ -8,10 +8,10 @@ module CodeClimate::TestReporter
8
8
  let(:formatter) { Formatter.new }
9
9
  let(:files) {
10
10
  [
11
- mock(
12
- :lines => [mock, mock, mock],
13
- :covered_lines => [mock, mock],
14
- :missed_lines => [mock],
11
+ double(
12
+ :lines => [double, double, double],
13
+ :covered_lines => [double, double],
14
+ :missed_lines => [double],
15
15
  :filename => project_file,
16
16
  :coverage => [0,3,2,nil],
17
17
  :covered_percent => 33.2,
@@ -21,7 +21,7 @@ module CodeClimate::TestReporter
21
21
  }
22
22
 
23
23
  let(:simplecov_result) {
24
- mock(
24
+ double(
25
25
  :covered_percent => 24.3,
26
26
  :covered_strength => 33.2,
27
27
  :files => files,
@@ -98,4 +98,22 @@ module CodeClimate::TestReporter
98
98
  app.http_user_agent.should include("v#{CodeClimate::TestReporter::VERSION}")
99
99
  end
100
100
  end
101
+
102
+ describe '#short_filename' do
103
+ it 'should return the filename of the file relative to the SimpleCov root' do
104
+ CodeClimate::TestReporter::Formatter.new.short_filename('file1').should == 'file1'
105
+ CodeClimate::TestReporter::Formatter.new.short_filename("#{::SimpleCov.root}/file1").should == 'file1'
106
+ end
107
+
108
+ it 'should include the path prefix if set' do
109
+ CodeClimate::TestReporter.configure do |config|
110
+ config.path_prefix = 'custom'
111
+ end
112
+ CodeClimate::TestReporter::Formatter.new.short_filename('file1').should == 'custom/file1'
113
+ CodeClimate::TestReporter::Formatter.new.short_filename("#{::SimpleCov.root}/file1").should == 'custom/file1'
114
+ CodeClimate::TestReporter.configure do |config|
115
+ config.path_prefix = nil
116
+ end
117
+ end
118
+ end
101
119
  end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ module CodeClimate::TestReporter
4
+ describe PayloadValidator do
5
+ let(:payload) {
6
+ {
7
+ git: {
8
+ committed_at: 1389603672,
9
+ head: "4b968f076d169c3d98089fba27988f0d52ba803d"
10
+ },
11
+ run_at: 1379704336,
12
+ source_files: [
13
+ { coverage: "[0,3,4]", name: "user.rb" }
14
+ ]
15
+ }
16
+ }
17
+
18
+ it "does not raise if there's a minimally valid test report payload" do
19
+ expect {
20
+ PayloadValidator.validate(payload)
21
+ }.to_not raise_error
22
+ end
23
+
24
+ it "raises when there's no commit sha" do
25
+ payload[:git][:head] = nil
26
+ expect {
27
+ PayloadValidator.validate(payload)
28
+ }.to raise_error(InvalidPayload, /A git commit sha was not found/)
29
+ end
30
+
31
+ it "does not raise if there's a commit sha in ci_service data" do
32
+ payload[:git][:head] = nil
33
+ payload[:ci_service] = {}
34
+ payload[:ci_service][:commit_sha] = "4b968f076d169c3d98089fba27988f0d52ba803d"
35
+ expect {
36
+ PayloadValidator.validate(payload)
37
+ }.to_not raise_error
38
+ end
39
+
40
+ it "raises when there is no committed_at" do
41
+ payload[:git][:committed_at] = nil
42
+ expect {
43
+ PayloadValidator.validate(payload)
44
+ }.to raise_error(InvalidPayload, /A git commit timestamp was not found/)
45
+ end
46
+
47
+ it "raises when there's no run_at" do
48
+ payload[:run_at] = nil
49
+ expect {
50
+ PayloadValidator.validate(payload)
51
+ }.to raise_error(InvalidPayload, /A run at timestamp was not found/)
52
+ end
53
+
54
+ it "raises when no source_files parameter is passed" do
55
+ payload[:source_files] = nil
56
+ expect {
57
+ PayloadValidator.validate(payload)
58
+ }.to raise_error(InvalidPayload, /No source files were found/)
59
+ end
60
+
61
+ it "raises when there's no source files" do
62
+ payload[:source_files] = []
63
+ expect {
64
+ PayloadValidator.validate(payload)
65
+ }.to raise_error(InvalidPayload, /No source files were found/)
66
+ end
67
+
68
+ it "raises if source files aren't hashes" do
69
+ payload[:source_files] = [1,2,3]
70
+ expect {
71
+ PayloadValidator.validate(payload)
72
+ }.to raise_error(InvalidPayload, /Invalid source files/)
73
+ end
74
+
75
+ it "raises if source files don't have names" do
76
+ payload[:source_files] = [{ coverage: "[1,1]" }]
77
+ expect {
78
+ PayloadValidator.validate(payload)
79
+ }.to raise_error(InvalidPayload, /Invalid source files/)
80
+ end
81
+
82
+ it "raises if source files don't have coverage" do
83
+ payload[:source_files] = [{ name: "foo.rb" }]
84
+ expect {
85
+ PayloadValidator.validate(payload)
86
+ }.to raise_error(InvalidPayload, /Invalid source files/)
87
+ end
88
+ end
89
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeclimate-test-reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Bryan Helmkamp
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-18 00:00:00.000000000 Z
11
+ date: 2014-01-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: simplecov
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -25,7 +23,6 @@ dependencies:
25
23
  type: :runtime
26
24
  prerelease: false
27
25
  version_requirements: !ruby/object:Gem::Requirement
28
- none: false
29
26
  requirements:
30
27
  - - ! '>='
31
28
  - !ruby/object:Gem::Version
@@ -36,7 +33,6 @@ dependencies:
36
33
  - !ruby/object:Gem::Dependency
37
34
  name: bundler
38
35
  requirement: !ruby/object:Gem::Requirement
39
- none: false
40
36
  requirements:
41
37
  - - ~>
42
38
  - !ruby/object:Gem::Version
@@ -44,7 +40,6 @@ dependencies:
44
40
  type: :development
45
41
  prerelease: false
46
42
  version_requirements: !ruby/object:Gem::Requirement
47
- none: false
48
43
  requirements:
49
44
  - - ~>
50
45
  - !ruby/object:Gem::Version
@@ -52,7 +47,6 @@ dependencies:
52
47
  - !ruby/object:Gem::Dependency
53
48
  name: rake
54
49
  requirement: !ruby/object:Gem::Requirement
55
- none: false
56
50
  requirements:
57
51
  - - ! '>='
58
52
  - !ruby/object:Gem::Version
@@ -60,7 +54,6 @@ dependencies:
60
54
  type: :development
61
55
  prerelease: false
62
56
  version_requirements: !ruby/object:Gem::Requirement
63
- none: false
64
57
  requirements:
65
58
  - - ! '>='
66
59
  - !ruby/object:Gem::Version
@@ -68,7 +61,6 @@ dependencies:
68
61
  - !ruby/object:Gem::Dependency
69
62
  name: rspec
70
63
  requirement: !ruby/object:Gem::Requirement
71
- none: false
72
64
  requirements:
73
65
  - - ! '>='
74
66
  - !ruby/object:Gem::Version
@@ -76,7 +68,6 @@ dependencies:
76
68
  type: :development
77
69
  prerelease: false
78
70
  version_requirements: !ruby/object:Gem::Requirement
79
- none: false
80
71
  requirements:
81
72
  - - ! '>='
82
73
  - !ruby/object:Gem::Version
@@ -84,7 +75,6 @@ dependencies:
84
75
  - !ruby/object:Gem::Dependency
85
76
  name: artifice
86
77
  requirement: !ruby/object:Gem::Requirement
87
- none: false
88
78
  requirements:
89
79
  - - ! '>='
90
80
  - !ruby/object:Gem::Version
@@ -92,7 +82,6 @@ dependencies:
92
82
  type: :development
93
83
  prerelease: false
94
84
  version_requirements: !ruby/object:Gem::Requirement
95
- none: false
96
85
  requirements:
97
86
  - - ! '>='
98
87
  - !ruby/object:Gem::Version
@@ -100,7 +89,6 @@ dependencies:
100
89
  - !ruby/object:Gem::Dependency
101
90
  name: pry
102
91
  requirement: !ruby/object:Gem::Requirement
103
- none: false
104
92
  requirements:
105
93
  - - ! '>='
106
94
  - !ruby/object:Gem::Version
@@ -108,7 +96,6 @@ dependencies:
108
96
  type: :development
109
97
  prerelease: false
110
98
  version_requirements: !ruby/object:Gem::Requirement
111
- none: false
112
99
  requirements:
113
100
  - - ! '>='
114
101
  - !ruby/object:Gem::Version
@@ -116,7 +103,6 @@ dependencies:
116
103
  - !ruby/object:Gem::Dependency
117
104
  name: pry-debugger
118
105
  requirement: !ruby/object:Gem::Requirement
119
- none: false
120
106
  requirements:
121
107
  - - ! '>='
122
108
  - !ruby/object:Gem::Version
@@ -124,7 +110,6 @@ dependencies:
124
110
  type: :development
125
111
  prerelease: false
126
112
  version_requirements: !ruby/object:Gem::Requirement
127
- none: false
128
113
  requirements:
129
114
  - - ! '>='
130
115
  - !ruby/object:Gem::Version
@@ -154,43 +139,45 @@ files:
154
139
  - lib/code_climate/test_reporter/exception_message.rb
155
140
  - lib/code_climate/test_reporter/formatter.rb
156
141
  - lib/code_climate/test_reporter/git.rb
142
+ - lib/code_climate/test_reporter/payload_validator.rb
157
143
  - lib/code_climate/test_reporter/version.rb
158
144
  - lib/codeclimate-test-reporter.rb
159
145
  - spec/lib/ci_spec.rb
160
146
  - spec/lib/configuration_spec.rb
161
147
  - spec/lib/formatter_spec.rb
162
148
  - spec/lib/git_spec.rb
149
+ - spec/lib/payload_validator_spec.rb
163
150
  - spec/lib/test_reporter_spec.rb
164
151
  - spec/spec_helper.rb
165
152
  homepage: ''
166
153
  licenses:
167
154
  - MIT
155
+ metadata: {}
168
156
  post_install_message:
169
157
  rdoc_options: []
170
158
  require_paths:
171
159
  - lib
172
160
  required_ruby_version: !ruby/object:Gem::Requirement
173
- none: false
174
161
  requirements:
175
162
  - - ! '>='
176
163
  - !ruby/object:Gem::Version
177
164
  version: '0'
178
165
  required_rubygems_version: !ruby/object:Gem::Requirement
179
- none: false
180
166
  requirements:
181
167
  - - ! '>='
182
168
  - !ruby/object:Gem::Version
183
169
  version: '0'
184
170
  requirements: []
185
171
  rubyforge_project:
186
- rubygems_version: 1.8.23
172
+ rubygems_version: 2.2.1
187
173
  signing_key:
188
- specification_version: 3
174
+ specification_version: 4
189
175
  summary: Uploads Ruby test coverage data to Code Climate.
190
176
  test_files:
191
177
  - spec/lib/ci_spec.rb
192
178
  - spec/lib/configuration_spec.rb
193
179
  - spec/lib/formatter_spec.rb
194
180
  - spec/lib/git_spec.rb
181
+ - spec/lib/payload_validator_spec.rb
195
182
  - spec/lib/test_reporter_spec.rb
196
183
  - spec/spec_helper.rb