-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from mark-davenport-fountain/add-json-reporter
Add a JSON Reporter
- Loading branch information
Showing
5 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
# Outputs data in json format similar to what is shown in the HTML page | ||
# Top level and file level coverage numbers | ||
module Coverband | ||
module Reporters | ||
class JSONReport < Base | ||
attr_accessor :filtered_report_files | ||
|
||
def initialize(store, options = {}) | ||
coverband_reports = Coverband::Reporters::Base.report(store, options) | ||
self.filtered_report_files = self.class.fix_reports(coverband_reports) | ||
end | ||
|
||
def report | ||
report_as_json | ||
end | ||
|
||
private | ||
|
||
def report_as_json | ||
result = Coverband::Utils::Results.new(filtered_report_files) | ||
source_files = result.source_files | ||
{ | ||
**coverage_totals(source_files), | ||
files: coverage_files(result, source_files) | ||
}.to_json | ||
end | ||
|
||
def coverage_totals(source_files) | ||
{ | ||
total_files: source_files.length, | ||
lines_of_code: source_files.lines_of_code, | ||
lines_covered: source_files.covered_lines, | ||
lines_missed: source_files.missed_lines, | ||
covered_strength: source_files.covered_strength, | ||
covered_percent: source_files.covered_percent | ||
} | ||
end | ||
|
||
# Using a hash indexed by file name for quick lookups | ||
def coverage_files(result, source_files) | ||
source_files.each_with_object({}) do |source_file, hash| | ||
hash[source_file.short_name] = { | ||
never_loaded: source_file.never_loaded, | ||
runtime_percentage: result.runtime_relevant_coverage(source_file), | ||
lines_of_code: source_file.lines.count, | ||
lines_covered: source_file.covered_lines.count, | ||
lines_missed: source_file.missed_lines.count, | ||
covered_percent: source_file.covered_percent, | ||
covered_strength: source_file.covered_strength | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path("../../test_helper", File.dirname(__FILE__)) | ||
|
||
class ReportJSONTest < Minitest::Test | ||
def setup | ||
super | ||
@store = Coverband.configuration.store | ||
Coverband.configure do |config| | ||
config.store = @store | ||
config.root = fixtures_root | ||
config.ignore = ["notsomething.rb", "lib/*"] | ||
end | ||
mock_file_hash | ||
end | ||
|
||
test "includes totals" do | ||
@store.send(:save_report, basic_coverage) | ||
|
||
json = Coverband::Reporters::JSONReport.new(@store).report | ||
parsed = JSON.parse(json) | ||
expected_keys = ["total_files", "lines_of_code", "lines_covered", "lines_missed", "covered_strength", "covered_percent"] | ||
assert expected_keys - parsed.keys == [] | ||
end | ||
|
||
test "honors ignore list" do | ||
@store.send(:save_report, basic_coverage) | ||
|
||
json = Coverband::Reporters::JSONReport.new(@store).report | ||
parsed = JSON.parse(json) | ||
expected_files = ["app/controllers/sample_controller.rb", "app/models/user.rb"] | ||
assert_equal parsed["files"].keys, expected_files | ||
end | ||
|
||
test "includes metrics for files" do | ||
@store.send(:save_report, basic_coverage) | ||
|
||
json = Coverband::Reporters::JSONReport.new(@store).report | ||
parsed = JSON.parse(json) | ||
|
||
expected_keys = ["never_loaded", "runtime_percentage", "lines_of_code", "lines_covered", "lines_missed", "covered_percent", "covered_strength"] | ||
|
||
assert_equal parsed["files"].length, 2 | ||
parsed["files"].keys.each do |file| | ||
assert_equal parsed["files"][file].keys, expected_keys | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters