codeclimate-test-reporter 0.1.1 → 0.2.0

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.
data/README.md CHANGED
@@ -30,6 +30,23 @@ Code Climate account if you are in the test coverage private beta.
30
30
 
31
31
  Please contact hello@codeclimate.com if you need any assistance setting this up.
32
32
 
33
+ ## Configuration
34
+
35
+ Certain behaviors of the test reporter can be configured. See the `Configuration`
36
+ class for more details. For example, you can change the logging level to not
37
+ print info messages:
38
+
39
+ *Note that the configuration block must come before TestReporter.start.*
40
+
41
+ ```ruby
42
+ CodeClimate::TestReporter.configure do |config|
43
+ config.logger.level = Logger::WARN
44
+ end
45
+
46
+ CodeClimate::TestReporter.start
47
+ ```
48
+
49
+
33
50
  ## Help! Your gem is raising a ...
34
51
 
35
52
  ### VCR::Errors::UnhandledHTTPRequestError
@@ -6,14 +6,45 @@ module CodeClimate
6
6
  require "simplecov"
7
7
  ::SimpleCov.add_filter 'vendor'
8
8
  ::SimpleCov.formatter = Formatter
9
- ::SimpleCov.start("test_frameworks")
10
- else
11
- puts("Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.")
9
+ ::SimpleCov.start(configuration.profile)
12
10
  end
13
11
  end
14
12
 
15
13
  def self.run?
16
- !!ENV["CODECLIMATE_REPO_TOKEN"]
14
+ environment_variable_set? && run_on_current_branch?
17
15
  end
16
+
17
+ def self.environment_variable_set?
18
+ environment_variable_set = !!ENV["CODECLIMATE_REPO_TOKEN"]
19
+ unless environment_variable_set
20
+ logger.info("Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.")
21
+ end
22
+
23
+ environment_variable_set
24
+ end
25
+
26
+ def self.run_on_current_branch?
27
+ return true if configured_branch.nil?
28
+
29
+ run_on_current_branch = !!(current_branch =~ /#{configured_branch}/i)
30
+ unless run_on_current_branch
31
+ logger.info("Not reporting to Code Climate because #{configured_branch} is set as the reporting branch.")
32
+ end
33
+
34
+ run_on_current_branch
35
+ end
36
+
37
+ def self.configured_branch
38
+ configuration.branch
39
+ end
40
+
41
+ def self.current_branch
42
+ Git.branch_from_git_or_ci
43
+ end
44
+
45
+ def self.logger
46
+ CodeClimate::TestReporter.configuration.logger
47
+ end
48
+
18
49
  end
19
50
  end
@@ -0,0 +1,55 @@
1
+ module CodeClimate
2
+ module TestReporter
3
+ class Ci
4
+
5
+ def self.service_data
6
+ if ENV['TRAVIS']
7
+ {
8
+ name: "travis-ci",
9
+ branch: ENV['TRAVIS_BRANCH'],
10
+ build_identifier: ENV['TRAVIS_JOB_ID'],
11
+ pull_request: ENV['TRAVIS_PULL_REQUEST']
12
+ }
13
+ elsif ENV['CIRCLECI']
14
+ {
15
+ name: "circlci",
16
+ build_identifier: ENV['CIRCLE_BUILD_NUM'],
17
+ branch: ENV['CIRCLE_BRANCH'],
18
+ commit_sha: ENV['CIRCLE_SHA1']
19
+ }
20
+ elsif ENV['SEMAPHORE']
21
+ {
22
+ name: "semaphore",
23
+ branch: ENV['BRANCH_NAME'],
24
+ build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
25
+ }
26
+ elsif ENV['JENKINS_URL']
27
+ {
28
+ name: "jenkins",
29
+ build_identifier: ENV['BUILD_NUMBER'],
30
+ build_url: ENV['BUILD_URL'],
31
+ branch: ENV['GIT_BRANCH'],
32
+ commit_sha: ENV['GIT_COMMIT']
33
+ }
34
+ elsif ENV['TDDIUM']
35
+ {
36
+ name: "tddium",
37
+ build_identifier: ENV['TDDIUM_SESSION_ID'],
38
+ worker_id: ENV['TDDIUM_TID']
39
+ }
40
+ elsif ENV['CI_NAME'] =~ /codeship/i
41
+ {
42
+ name: "codeship",
43
+ build_identifier: ENV['CI_BUILD_NUMBER'],
44
+ build_url: ENV['CI_BUILD_URL'],
45
+ branch: ENV['CI_BRANCH'],
46
+ commit_sha: ENV['CI_COMMIT_ID'],
47
+ }
48
+ else
49
+ {}
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ require 'logger'
2
+
3
+ module CodeClimate
4
+ module TestReporter
5
+ @@configuration = nil
6
+
7
+ def self.configure
8
+ @@configuration = Configuration.new
9
+
10
+ if block_given?
11
+ yield configuration
12
+ end
13
+
14
+ configuration
15
+ end
16
+
17
+ def self.configuration
18
+ @@configuration || configure
19
+ end
20
+
21
+ class Configuration
22
+ attr_accessor :branch, :logger, :profile
23
+
24
+ def logger
25
+ @logger ||= default_logger
26
+ end
27
+
28
+ def profile
29
+ @profile ||= "test_frameworks"
30
+ end
31
+
32
+ private
33
+
34
+ def default_logger
35
+ log = Logger.new($stderr)
36
+ log.level = Logger::INFO
37
+
38
+ log
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -19,8 +19,7 @@ module CodeClimate
19
19
  File.open(file_path, "w") { |file| file.write(payload.to_json) }
20
20
  else
21
21
  client = Client.new
22
- computed_branch = compute_branch(payload)
23
- print "Sending report to #{client.host} for branch #{computed_branch}... "
22
+ print "Sending report to #{client.host} for branch #{Git.branch_from_git_or_ci}... "
24
23
  client.post_results(payload)
25
24
  end
26
25
 
@@ -64,11 +63,7 @@ module CodeClimate
64
63
  covered_strength: round(result.covered_strength, 2),
65
64
  line_counts: totals,
66
65
  partial: partial?,
67
- git: {
68
- head: `git log -1 --pretty=format:'%H'`,
69
- committed_at: committed_at,
70
- branch: git_branch,
71
- },
66
+ git: Git.info,
72
67
  environment: {
73
68
  test_framework: result.command_name.downcase,
74
69
  pwd: Dir.pwd,
@@ -80,46 +75,6 @@ module CodeClimate
80
75
  }
81
76
  end
82
77
 
83
- def ci_service_data
84
- if ENV['TRAVIS']
85
- {
86
- name: "travis-ci",
87
- branch: ENV['TRAVIS_BRANCH'],
88
- build_identifier: ENV['TRAVIS_JOB_ID'],
89
- pull_request: ENV['TRAVIS_PULL_REQUEST']
90
- }
91
- elsif ENV['CIRCLECI']
92
- {
93
- name: "circlci",
94
- build_identifier: ENV['CIRCLE_BUILD_NUM'],
95
- branch: ENV['CIRCLE_BRANCH'],
96
- commit_sha: ENV['CIRCLE_SHA1']
97
- }
98
- elsif ENV['SEMAPHORE']
99
- {
100
- name: "semaphore",
101
- branch: ENV['BRANCH_NAME'],
102
- build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
103
- }
104
- elsif ENV['JENKINS_URL']
105
- {
106
- name: "jenkins",
107
- build_identifier: ENV['BUILD_NUMBER'],
108
- build_url: ENV['BUILD_URL'],
109
- branch: ENV['GIT_BRANCH'],
110
- commit_sha: ENV['GIT_COMMIT']
111
- }
112
- elsif ENV['TDDIUM']
113
- {
114
- name: "tddium",
115
- build_identifier: ENV['TDDIUM_SESSION_ID'],
116
- worker_id: ENV['TDDIUM_TID']
117
- }
118
- else
119
- {}
120
- end
121
- end
122
-
123
78
  def calculate_blob_id(path)
124
79
  content = File.open(path, "rb") {|f| f.read }
125
80
  header = "blob #{content.length}\0"
@@ -132,39 +87,21 @@ module CodeClimate
132
87
  filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
133
88
  end
134
89
 
135
- def committed_at
136
- committed_at = `git log -1 --pretty=format:'%ct'`
137
- committed_at.to_i.zero? ? nil : committed_at.to_i
138
- end
139
-
140
- def git_branch
141
- branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
142
- branch = [branch].flatten.first
143
- branch ? branch.gsub("* ","") : nil
144
- end
145
-
146
90
  def tddium?
147
91
  ci_service_data && ci_service_data[:name] == "tddium"
148
92
  end
149
93
 
150
- def compute_branch(payload)
151
- git_branch = payload[:git][:branch]
152
- ci_branch = payload[:ci_service][:branch]
153
-
154
- if ci_branch.to_s.strip.size > 0
155
- ci_branch.sub(/^origin\//, "")
156
- elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
157
- git_branch.sub(/^origin\//, "")
158
- else
159
- "master"
160
- end
161
- end
162
-
163
94
  # Convert to Float before rounding.
164
95
  # Fixes [#7] possible segmentation fault when calling #round on a Rational
165
96
  def round(numeric, precision)
166
97
  Float(numeric).round(precision)
167
98
  end
99
+
100
+ private
101
+
102
+ def ci_service_data
103
+ @ci_service_data ||= Ci.service_data
104
+ end
168
105
  end
169
106
  end
170
107
  end
@@ -0,0 +1,42 @@
1
+ module CodeClimate
2
+ module TestReporter
3
+ class Git
4
+
5
+ class << self
6
+ def info
7
+ {
8
+ head: `git log -1 --pretty=format:'%H'`,
9
+ committed_at: committed_at,
10
+ branch: branch_from_git,
11
+ }
12
+ end
13
+
14
+ def branch_from_git_or_ci
15
+ git_branch = branch_from_git
16
+ ci_branch = Ci.service_data[:branch]
17
+
18
+ if ci_branch.to_s.strip.size > 0
19
+ ci_branch.sub(/^origin\//, "")
20
+ elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
21
+ git_branch.sub(/^origin\//, "")
22
+ else
23
+ "master"
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def committed_at
30
+ committed_at = `git log -1 --pretty=format:'%ct'`
31
+ committed_at.to_i.zero? ? nil : committed_at.to_i
32
+ end
33
+
34
+ def branch_from_git
35
+ branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
36
+ branch = [branch].flatten.first
37
+ branch ? branch.gsub("* ","") : nil
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module CodeClimate
2
2
  module TestReporter
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,4 +2,7 @@ require "code_climate/test_reporter"
2
2
  require "code_climate/test_reporter/version"
3
3
  require "code_climate/test_reporter/client"
4
4
  require "code_climate/test_reporter/formatter"
5
+ require "code_climate/test_reporter/configuration"
6
+ require "code_climate/test_reporter/git"
7
+ require "code_climate/test_reporter/ci"
5
8
 
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ module CodeClimate::TestReporter
4
+ describe Ci do
5
+
6
+ describe '.service_data' do
7
+ before :each do
8
+ ENV['SEMAPHORE'] = 'yes?'
9
+ ENV['BRANCH_NAME'] = 'master'
10
+ ENV['SEMAPHORE_BUILD_NUMBER'] = '1234'
11
+ end
12
+
13
+ after :each do
14
+ ENV.delete('SEMAPHORE')
15
+ ENV.delete('BRANCH_NAME')
16
+ ENV.delete('SEMAPHORE_BUILD_NUMBER')
17
+ end
18
+
19
+ it 'returns a hash of CI environment info' do
20
+ expected_semaphore_hash = {
21
+ name: 'semaphore',
22
+ branch: 'master',
23
+ build_identifier: '1234'
24
+ }
25
+
26
+ expect(Ci.service_data).to include expected_semaphore_hash
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ module CodeClimate::TestReporter
5
+ describe Configuration do
6
+ describe 'none given' do
7
+ before do
8
+ CodeClimate::TestReporter.configure
9
+ end
10
+
11
+ it 'provides defaults' do
12
+ expect(CodeClimate::TestReporter.configuration.branch).to be_nil
13
+ expect(CodeClimate::TestReporter.configuration.logger).to be_instance_of Logger
14
+ expect(CodeClimate::TestReporter.configuration.logger.level).to eq Logger::INFO
15
+ expect(CodeClimate::TestReporter.configuration.profile).to eq('test_frameworks')
16
+ end
17
+ end
18
+
19
+ describe 'with config block' do
20
+ after do
21
+ CodeClimate::TestReporter.configure
22
+ end
23
+
24
+ it 'stores logger' do
25
+ logger = Logger.new($stderr)
26
+
27
+ CodeClimate::TestReporter.configure do |config|
28
+ logger.level = Logger::DEBUG
29
+ config.logger = logger
30
+ end
31
+
32
+ expect(CodeClimate::TestReporter.configuration.logger).to eq logger
33
+ end
34
+
35
+ it 'stores branch' do
36
+ CodeClimate::TestReporter.configure do |config|
37
+ config.branch = :master
38
+ end
39
+
40
+ expect(CodeClimate::TestReporter.configuration.branch).to eq :master
41
+ end
42
+
43
+ it 'stores profile' do
44
+ CodeClimate::TestReporter.configure do |config|
45
+ config.profile = 'custom'
46
+ end
47
+
48
+ expect(CodeClimate::TestReporter.configuration.profile).to eq('custom')
49
+ end
50
+ end
51
+ end
52
+ end
@@ -61,7 +61,7 @@ module CodeClimate::TestReporter
61
61
  "pwd" => Dir.pwd,
62
62
  "rails_root" => nil,
63
63
  "simplecov_root" => Dir.pwd,
64
- "gem_version" => "0.0.11"
64
+ "gem_version" => VERSION
65
65
  },
66
66
  "ci_service" => {}
67
67
  }
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module CodeClimate::TestReporter
4
+ describe Git do
5
+ describe '.info' do
6
+ it 'returns a hash with git information.' do
7
+ expected_git_hash = {
8
+ head: `git log -1 --pretty=format:'%H'`,
9
+ committed_at: `git log -1 --pretty=format:'%ct'`.to_i,
10
+ branch: Git.send(:branch_from_git)
11
+ }
12
+
13
+ expect(Git.info).to include expected_git_hash
14
+ end
15
+ end
16
+
17
+ describe 'branch_from_git_or_ci' do
18
+ it 'returns the branch from ci' do
19
+ allow(Ci).to receive(:service_data).and_return({branch: 'ci-branch'})
20
+
21
+ expect(Git.branch_from_git_or_ci).to eq 'ci-branch'
22
+ end
23
+
24
+ it 'returns the branch from git if there is no ci branch' do
25
+ allow(Ci).to receive(:service_data).and_return({})
26
+
27
+ expect(Git.branch_from_git_or_ci).to eq Git.send(:branch_from_git)
28
+ end
29
+
30
+ it 'returns master otherwise' do
31
+ allow(Ci).to receive(:service_data).and_return({})
32
+ allow(Git).to receive(:branch_from_git).and_return(nil)
33
+
34
+ expect(Git.branch_from_git_or_ci).to eq 'master'
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe CodeClimate::TestReporter do
4
+
5
+ describe '.run_on_current_branch?' do
6
+ it 'returns true if there is no branch configured' do
7
+ allow(CodeClimate::TestReporter).to receive(:configured_branch).and_return(nil)
8
+ expect(CodeClimate::TestReporter.run_on_current_branch?).to be_true
9
+ end
10
+
11
+ it 'returns true if the current branch matches the configured branch' do
12
+ allow(CodeClimate::TestReporter).to receive(:current_branch).and_return("master\n")
13
+ allow(CodeClimate::TestReporter).to receive(:configured_branch).and_return(:master)
14
+
15
+ expect(CodeClimate::TestReporter.run_on_current_branch?).to be_true
16
+ end
17
+
18
+ it 'returns false if the current branch and configured branch dont match' do
19
+ allow(CodeClimate::TestReporter).to receive(:current_branch).and_return("some-branch")
20
+ allow(CodeClimate::TestReporter).to receive(:configured_branch).and_return(:master)
21
+
22
+ expect(CodeClimate::TestReporter.run_on_current_branch?).to be_false
23
+ end
24
+
25
+ it 'logs a message if false' do
26
+ expect_any_instance_of(Logger).to receive(:info)
27
+ allow(CodeClimate::TestReporter).to receive(:current_branch).and_return("another-branch")
28
+ allow(CodeClimate::TestReporter).to receive(:configured_branch).and_return(:master)
29
+
30
+ CodeClimate::TestReporter.run_on_current_branch?
31
+ end
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeclimate-test-reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-22 00:00:00.000000000 Z
12
+ date: 2013-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simplecov
@@ -148,12 +148,19 @@ files:
148
148
  - codeclimate-test-reporter.gemspec
149
149
  - config/cacert.pem
150
150
  - lib/code_climate/test_reporter.rb
151
+ - lib/code_climate/test_reporter/ci.rb
151
152
  - lib/code_climate/test_reporter/client.rb
153
+ - lib/code_climate/test_reporter/configuration.rb
152
154
  - lib/code_climate/test_reporter/exception_message.rb
153
155
  - lib/code_climate/test_reporter/formatter.rb
156
+ - lib/code_climate/test_reporter/git.rb
154
157
  - lib/code_climate/test_reporter/version.rb
155
158
  - lib/codeclimate-test-reporter.rb
159
+ - spec/lib/ci_spec.rb
160
+ - spec/lib/configuration_spec.rb
156
161
  - spec/lib/formatter_spec.rb
162
+ - spec/lib/git_spec.rb
163
+ - spec/lib/test_reporter_spec.rb
157
164
  - spec/spec_helper.rb
158
165
  homepage: ''
159
166
  licenses:
@@ -181,6 +188,9 @@ signing_key:
181
188
  specification_version: 3
182
189
  summary: Uploads Ruby test coverage data to Code Climate.
183
190
  test_files:
191
+ - spec/lib/ci_spec.rb
192
+ - spec/lib/configuration_spec.rb
184
193
  - spec/lib/formatter_spec.rb
194
+ - spec/lib/git_spec.rb
195
+ - spec/lib/test_reporter_spec.rb
185
196
  - spec/spec_helper.rb
186
- has_rdoc: