Skip to content

Commit ab8ff39

Browse files
committed
Merge in simplecov_json_formatter.
1 parent ce45fa6 commit ab8ff39

18 files changed

+450
-15
lines changed

Gemfile.lock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ PATH
1212
simplecov (0.22.0)
1313
docile (~> 1.1)
1414
simplecov-html (~> 0.11)
15-
simplecov_json_formatter (~> 0.1)
1615

1716
GEM
1817
remote: https://rubygems.org/
@@ -142,7 +141,6 @@ GEM
142141
parser (>= 3.3.1.0)
143142
ruby-progressbar (1.13.0)
144143
simplecov-html (0.13.1)
145-
simplecov_json_formatter (0.1.4)
146144
spoon (0.0.6)
147145
ffi
148146
sys-uname (1.3.1)
@@ -192,4 +190,4 @@ DEPENDENCIES
192190
webrick
193191

194192
BUNDLED WITH
195-
2.7.0.dev
193+
2.6.9

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,18 +869,17 @@ SimpleCov.formatters = [
869869

870870
## JSON formatter
871871

872-
SimpleCov is packaged with a separate gem called [simplecov_json_formatter](https://github.com/codeclimate-community/simplecov_json_formatter) that provides you with a JSON formatter, this formatter could be useful for different use cases, such as for CI consumption or for reporting to external services.
873-
874-
In order to use it you will need to manually load the installed gem like so:
872+
SimpleCov is packaged with a `SimpleCov::Formatter::JSONFormatter` that provides you with a JSON formatter, this formatter could be useful for different use cases, such as for CI consumption or for reporting to external services.
875873

876874
```ruby
877-
require "simplecov_json_formatter"
878875
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
879876
```
880877

881878
> _Note:_ In case you plan to report your coverage results to CodeClimate services, know that SimpleCov will automatically use the
882879
> JSON formatter along with the HTML formatter when the `CC_TEST_REPORTER_ID` variable is present in the environment.
883880
881+
> This exporter was originally separate [simplecov_json_formatter](https://github.com/codeclimate-community/simplecov_json_formatter) gem and it was needed to require manually using `require 'simplecov_json_formatter'`. Currently it is loaded by default.
882+
884883
## Available formatters, editor integrations and hosted services
885884

886885
* [Open Source formatter and integration plugins for SimpleCov](doc/alternate-formatters.md)

features/config_json_formatter.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Feature:
1212
Given SimpleCov for Test/Unit is configured with:
1313
"""
1414
require 'simplecov'
15-
require 'simplecov_json_formatter'
1615
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
1716
SimpleCov.at_exit do
1817
puts SimpleCov.result.format!

lib/simplecov/default_formatter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def from_env(env)
99

1010
# When running under a CI that uses CodeClimate, JSON output is expected
1111
if env.fetch("CC_TEST_REPORTER_ID", nil)
12-
require "simplecov_json_formatter"
1312
formatters.push(SimpleCov::Formatter::JSONFormatter)
1413
end
1514

lib/simplecov/formatter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ module Formatter
88

99
require_relative "formatter/simple_formatter"
1010
require_relative "formatter/multi_formatter"
11+
require_relative "formatter/json_formatter"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "json_formatter/result_hash_formatter"
4+
require_relative "json_formatter/result_exporter"
5+
require "json"
6+
7+
module SimpleCov
8+
module Formatter
9+
class JSONFormatter
10+
def format(result, verbose: true)
11+
result_hash = format_result(result)
12+
13+
export_formatted_result(result_hash)
14+
15+
puts output_message(result) if verbose
16+
end
17+
18+
private
19+
20+
def format_result(result)
21+
result_hash_formater = ResultHashFormatter.new(result)
22+
result_hash_formater.format
23+
end
24+
25+
def export_formatted_result(result_hash)
26+
result_exporter = ResultExporter.new(result_hash)
27+
result_exporter.export
28+
end
29+
30+
def output_message(result)
31+
"JSON Coverage report generated for #{result.command_name} to #{SimpleCov.coverage_path}. " \
32+
"#{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
33+
end
34+
end
35+
end
36+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
module SimpleCov
4+
module Formatter
5+
class JSONFormatter
6+
class ResultExporter
7+
FILENAME = "coverage.json"
8+
9+
def initialize(result_hash)
10+
@result = result_hash
11+
end
12+
13+
def export
14+
File.open(export_path, "w") do |file|
15+
file << json_result
16+
end
17+
end
18+
19+
private
20+
21+
def json_result
22+
JSON.pretty_generate(@result)
23+
end
24+
25+
def export_path
26+
File.join(SimpleCov.coverage_path, FILENAME)
27+
end
28+
end
29+
end
30+
end
31+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "source_file_formatter"
4+
5+
module SimpleCov
6+
module Formatter
7+
class JSONFormatter
8+
class ResultHashFormatter
9+
def initialize(result)
10+
@result = result
11+
end
12+
13+
def format
14+
format_files
15+
format_groups
16+
17+
formatted_result
18+
end
19+
20+
private
21+
22+
def format_files
23+
@result.files.each do |source_file|
24+
formatted_result[:coverage][source_file.filename] =
25+
format_source_file(source_file)
26+
end
27+
end
28+
29+
def format_groups
30+
@result.groups.each do |name, file_list|
31+
formatted_result[:groups][name] = {
32+
lines: {
33+
covered_percent: file_list.covered_percent
34+
}
35+
}
36+
end
37+
end
38+
39+
def formatted_result
40+
@formatted_result ||= {
41+
meta: {
42+
simplecov_version: SimpleCov::VERSION
43+
},
44+
coverage: {},
45+
groups: {}
46+
}
47+
end
48+
49+
def format_source_file(source_file)
50+
source_file_formatter = SourceFileFormatter.new(source_file)
51+
source_file_formatter.format
52+
end
53+
end
54+
end
55+
end
56+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
module SimpleCov
4+
module Formatter
5+
class JSONFormatter
6+
class SourceFileFormatter
7+
def initialize(source_file)
8+
@source_file = source_file
9+
@line_coverage = nil
10+
end
11+
12+
def format
13+
if SimpleCov.branch_coverage?
14+
line_coverage.merge(branch_coverage)
15+
else
16+
line_coverage
17+
end
18+
end
19+
20+
private
21+
22+
def line_coverage
23+
@line_coverage ||= {
24+
lines: lines
25+
}
26+
end
27+
28+
def branch_coverage
29+
{
30+
branches: branches
31+
}
32+
end
33+
34+
def lines
35+
@source_file.lines.collect do |line|
36+
parse_line(line)
37+
end
38+
end
39+
40+
def branches
41+
@source_file.branches.collect do |branch|
42+
parse_branch(branch)
43+
end
44+
end
45+
46+
def parse_line(line)
47+
return line.coverage unless line.skipped?
48+
49+
"ignored"
50+
end
51+
52+
def parse_branch(branch)
53+
{
54+
type: branch.type,
55+
start_line: branch.start_line,
56+
end_line: branch.end_line,
57+
coverage: parse_line(branch)
58+
}
59+
end
60+
end
61+
end
62+
end
63+
end

simplecov.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Gem::Specification.new do |gem|
3737

3838
gem.add_dependency "docile", "~> 1.1"
3939
gem.add_dependency "simplecov-html", "~> 0.11"
40-
gem.add_dependency "simplecov_json_formatter", "~> 0.1"
4140

4241
gem.files = Dir["{lib}/**/*.*", "bin/*", "LICENSE", "CHANGELOG.md", "README.md", "doc/*"]
4342
gem.require_paths = ["lib"]

0 commit comments

Comments
 (0)