simplecov_json_formatter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +28 -0
- data/lib/simplecov_json_formatter.rb +37 -0
- data/lib/simplecov_json_formatter/result_exporter.rb +27 -0
- data/lib/simplecov_json_formatter/result_hash_formatter.rb +36 -0
- data/lib/simplecov_json_formatter/source_file_formatter.rb +64 -0
- data/lib/simplecov_json_formatter/version.rb +5 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b0d112d3b0643b66b0d605539bac724fb29fc39baa66c90d23acecb6ecedab84
|
4
|
+
data.tar.gz: 1d057ce0f5bbfe3f772c2f4174498d652994ad33d537d0975b6a3a23c0c8fbad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6941f2c0e5f625da3f3bff5530ecf9c8f4b8ef4234ff9c13604a2410019b6838753218d8e6745ac2552575658e74e52d855dbba1bef39d75604a80afe9e10e6
|
7
|
+
data.tar.gz: e7c14f4dd0ba5fcad9e058fd9c507b8e2ce32303eb91a94908c9d2a3364d48a25a7adb087f0b7b8ea28484f6d4ae708f7a8783bdeb29eaa82884ffe422f08d28
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# JSON formatter for SimpleCov
|
2
|
+
|
3
|
+
***Note: To learn more about SimpleCov, check out the main repo at [https://github.com/simplecov-ruby/simplecov](https://github.com/colszowka/simplecov***)***
|
4
|
+
|
5
|
+
Generates a formatted JSON report of your [SimpleCov](https://github.com/simplecov-ruby/simplecov) ruby code coverage results on ruby 2.4+. Originally intended to add `simplecov`'s results reading capacity to CI tools.
|
6
|
+
|
7
|
+
## Overview
|
8
|
+
|
9
|
+
You can expect for this gem to produce a `coverage.json` file, located at the `coverage` folder.
|
10
|
+
|
11
|
+
Depending on your `SimpleCoV`'s settings you will experiment different outcomes. Particularly depending on which type of coverage are you running `SimpleCov` with:
|
12
|
+
|
13
|
+
- If you configure `SimpleCov` to run with `branch` coverage you should expect an output formatted like [sample_with_branch.json](https://github.com/fede-moya/simplecov_json_formatter/blob/master/spec/fixtures/sample_with_branch.json)
|
14
|
+
- Otherwise you should expect an output formatted like [sample.json](https://github.com/fede-moya/simplecov_json_formatter/blob/master/spec/fixtures/sample.json)
|
15
|
+
|
16
|
+
## Development
|
17
|
+
|
18
|
+
We encourage you to use docker for common operations like running tests, or debugging your code. Running `make sh` will run a new container instance based on the `Dockerfile` provided at root, finally a shell prompt will be displayed on your terminal. Also, syncronization with your local files will be already set.
|
19
|
+
### Tests
|
20
|
+
`make test` will trigger the excution of both running tests and running rubocop as linter, by simply running `rake`, this actions will be run inside a new container but using your local files.
|
21
|
+
|
22
|
+
### Format
|
23
|
+
|
24
|
+
`make format` will run `rubocop -a` which stands for _autocorrect_ and format your code according to the `.rubocop.yml` config file.
|
25
|
+
|
26
|
+
## Copyright
|
27
|
+
|
28
|
+
See License ?
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'simplecov_json_formatter/result_hash_formatter'
|
5
|
+
require 'simplecov_json_formatter/result_exporter'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
module SimpleCov
|
9
|
+
module Formatter
|
10
|
+
class JSONFormatter
|
11
|
+
def format(result)
|
12
|
+
result_hash = format_result(result)
|
13
|
+
|
14
|
+
export_formatted_result(result_hash)
|
15
|
+
|
16
|
+
puts output_message(result)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def format_result(result)
|
22
|
+
result_hash_formater = SimpleCovJSONFormatter::ResultHashFormatter.new(result)
|
23
|
+
result_hash_formater.format
|
24
|
+
end
|
25
|
+
|
26
|
+
def export_formatted_result(result_hash)
|
27
|
+
result_exporter = SimpleCovJSONFormatter::ResultExporter.new(result_hash)
|
28
|
+
result_exporter.export
|
29
|
+
end
|
30
|
+
|
31
|
+
def output_message(result)
|
32
|
+
"JSON Coverage report generated for #{result.command_name} to #{SimpleCov.coverage_path}. \
|
33
|
+
#{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleCovJSONFormatter
|
4
|
+
class ResultExporter
|
5
|
+
FILENAME = 'coverage.json'
|
6
|
+
|
7
|
+
def initialize(result_hash)
|
8
|
+
@result = result_hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def export
|
12
|
+
File.open(export_path, 'w') do |file|
|
13
|
+
file << json_result
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def json_result
|
20
|
+
JSON.pretty_generate(@result)
|
21
|
+
end
|
22
|
+
|
23
|
+
def export_path
|
24
|
+
File.join(SimpleCov.coverage_path, FILENAME)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'simplecov_json_formatter/source_file_formatter'
|
4
|
+
|
5
|
+
module SimpleCovJSONFormatter
|
6
|
+
class ResultHashFormatter
|
7
|
+
def initialize(result)
|
8
|
+
@result = result
|
9
|
+
end
|
10
|
+
|
11
|
+
def format
|
12
|
+
@result.files.each do |source_file|
|
13
|
+
formatted_result[:coverage][source_file.filename] =
|
14
|
+
format_source_file(source_file)
|
15
|
+
end
|
16
|
+
|
17
|
+
formatted_result
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def formatted_result
|
23
|
+
@formatted_result ||= {
|
24
|
+
meta: {
|
25
|
+
simplecov_version: SimpleCov::VERSION
|
26
|
+
},
|
27
|
+
coverage: {}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_source_file(source_file)
|
32
|
+
source_file_formatter = SourceFileFormatter.new(source_file)
|
33
|
+
source_file_formatter.format
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleCovJSONFormatter
|
4
|
+
class SourceFileFormatter
|
5
|
+
def initialize(source_file)
|
6
|
+
@source_file = source_file
|
7
|
+
end
|
8
|
+
|
9
|
+
def format
|
10
|
+
if SimpleCov.branch_coverage?
|
11
|
+
line_coverage.merge(branch_coverage)
|
12
|
+
else
|
13
|
+
line_coverage
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def line_coverage
|
20
|
+
@line_coverage || {
|
21
|
+
lines: lines
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def branch_coverage
|
26
|
+
{
|
27
|
+
branches: branches
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def lines
|
32
|
+
lines = []
|
33
|
+
@source_file.lines.each do |line|
|
34
|
+
lines << parse_line(line)
|
35
|
+
end
|
36
|
+
|
37
|
+
lines
|
38
|
+
end
|
39
|
+
|
40
|
+
def branches
|
41
|
+
branches = []
|
42
|
+
@source_file.branches.each do |branch|
|
43
|
+
branches << parse_branch(branch)
|
44
|
+
end
|
45
|
+
|
46
|
+
branches
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse_line(line)
|
50
|
+
return line.coverage unless line.skipped?
|
51
|
+
|
52
|
+
'ignored'
|
53
|
+
end
|
54
|
+
|
55
|
+
def parse_branch(branch)
|
56
|
+
{
|
57
|
+
type: branch.type,
|
58
|
+
start_line: branch.start_line,
|
59
|
+
end_line: branch.end_line,
|
60
|
+
coverage: parse_line(branch)
|
61
|
+
}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simplecov_json_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Federico Moya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: JSON formatter for SimpleCov
|
14
|
+
email:
|
15
|
+
- federicomoyamartin@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/simplecov_json_formatter.rb
|
22
|
+
- lib/simplecov_json_formatter/result_exporter.rb
|
23
|
+
- lib/simplecov_json_formatter/result_hash_formatter.rb
|
24
|
+
- lib/simplecov_json_formatter/source_file_formatter.rb
|
25
|
+
- lib/simplecov_json_formatter/version.rb
|
26
|
+
homepage: https://github.com/fede-moya/simplecov_json_formatter
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.4.0
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.0.8
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: JSON formatter for SimpleCov
|
49
|
+
test_files: []
|