Skip to content

Commit cd6952a

Browse files
author
Federico Moya
committed
Init project with functional code base
0 parents  commit cd6952a

13 files changed

+220
-0
lines changed

.gitignore

Whitespace-only changes.

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
group :development do
6+
gem 'byebug'
7+
end

Gemfile.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
PATH
2+
remote: .
3+
specs:
4+
simplecov-json-formatter (0.1.1)
5+
json
6+
simplecov (~> 0.18, >= 0.18.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
byebug (11.1.3)
12+
docile (1.3.2)
13+
json (2.3.1)
14+
power_assert (1.1.3)
15+
rake (13.0.1)
16+
simplecov (0.19.0)
17+
docile (~> 1.1)
18+
simplecov-html (~> 0.11)
19+
simplecov-html (0.12.2)
20+
test-unit (3.2.9)
21+
power_assert
22+
23+
PLATFORMS
24+
ruby
25+
26+
DEPENDENCIES
27+
byebug
28+
rake
29+
simplecov-json-formatter!
30+
test-unit
31+
32+
BUNDLED WITH
33+
2.0.1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# simplecov-json-formatter
2+
3+
JSON formatter for the SimpleCov gem.

Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'bundler/gem_tasks'
2+
require 'rake/testtask'
3+
4+
Rake::TestTask.new(:test) do |t|
5+
t.libs << 'test'
6+
t.libs << 'lib'
7+
t.test_files = FileList['test/**/*_test.rb']
8+
end
9+
10+
task :default => :test

lib/simplecov-json-formatter.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'simplecov'
2+
require 'simplecov-json-formatter/result_hash_formatter'
3+
require 'simplecov-json-formatter/result_exporter'
4+
require 'json'
5+
6+
module SimpleCov
7+
module Formatter
8+
class JSONFormatter
9+
def format(result)
10+
result_hash = format_result(result)
11+
12+
export_formatted_result(result_hash)
13+
14+
puts output_message(result)
15+
end
16+
17+
private
18+
19+
def format_result(result)
20+
result_hash_formater = SimpleCovJSONFormatter::ResultHashFormatter.new(result)
21+
result_hash_formater.format
22+
end
23+
24+
def export_formatted_result(result_hash)
25+
result_exporter = SimpleCovJSONFormatter::ResultExporter.new(result_hash)
26+
result_exporter.export
27+
end
28+
29+
def output_message(result)
30+
"JSON Coverage report generated for #{result.command_name} to #{SimpleCov.coverage_path}. #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
31+
end
32+
end
33+
end
34+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module SimpleCovJSONFormatter
2+
class ResultExporter
3+
FILENAME='coverage.json'
4+
5+
def initialize(result_hash)
6+
@result = result_hash
7+
end
8+
9+
def export
10+
File.open(export_path, 'w') do |file|
11+
file << json_result
12+
end
13+
end
14+
15+
private
16+
17+
def json_result
18+
JSON.pretty_generate(@result)
19+
end
20+
21+
def export_path
22+
File.join(SimpleCov.coverage_path, FILENAME)
23+
end
24+
end
25+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'simplecov-json-formatter/source_file_formatter'
2+
3+
module SimpleCovJSONFormatter
4+
class ResultHashFormatter
5+
def initialize(result)
6+
@result = result
7+
end
8+
9+
def format
10+
@result.files.each do |source_file|
11+
formatted_result[:coverage][source_file.filename] =
12+
format_source_file(source_file)
13+
end
14+
15+
formatted_result
16+
end
17+
18+
private
19+
20+
def formatted_result
21+
@formatted_result ||= {
22+
meta: {
23+
simplecov_version: SimpleCov::VERSION
24+
},
25+
coverage: {}
26+
}
27+
end
28+
29+
def format_source_file(source_file)
30+
source_file_formatter = SourceFileFormatter.new(source_file)
31+
source_file_formatter.format
32+
end
33+
end
34+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module SimpleCovJSONFormatter
2+
class SourceFileFormatter
3+
def initialize(source_file)
4+
@source_file = source_file
5+
end
6+
7+
def format
8+
lines = []
9+
@source_file.lines.each do |line|
10+
lines << parse_line(line)
11+
end
12+
branches = []
13+
@source_file.branches.each do |branch|
14+
branches << parse_branch(branch)
15+
end
16+
{
17+
lines: lines,
18+
branches: branches,
19+
}
20+
end
21+
22+
private
23+
24+
def parse_line(line)
25+
return line.coverage unless line.skipped?
26+
"ignored"
27+
end
28+
29+
def parse_branch(branch)
30+
{
31+
type: branch.type,
32+
start_line: branch.start_line,
33+
end_line: branch.end_line,
34+
coverage: parse_line(branch),
35+
}
36+
end
37+
end
38+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SimpleCovJSONFormatter
2+
VERSION = '0.1.1'
3+
end

0 commit comments

Comments
 (0)