Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group support #2

Merged
merged 9 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GEM
ast (2.4.1)
byebug (11.1.3)
diff-lcs (1.4.4)
docile (1.3.2)
docile (1.4.0)
parallel (1.19.2)
parser (2.7.1.4)
ast (~> 2.4.1)
Expand Down Expand Up @@ -37,10 +37,12 @@ GEM
rubocop-ast (0.3.0)
parser (>= 2.7.1.4)
ruby-progressbar (1.10.1)
simplecov (0.18.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov-html (0.12.2)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
unicode-display_width (1.7.0)

PLATFORMS
Expand All @@ -54,4 +56,4 @@ DEPENDENCIES
simplecov (~> 0.18)

BUNDLED WITH
2.1.0
2.3.4
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

***Note: To learn more about SimpleCov, check out the main repo at [https://github.com/simplecov-ruby/simplecov](https://github.com/colszowka/simplecov***)***

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.
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.

## Overview

You can expect for this gem to produce a `coverage.json` file, located at the `coverage` folder.
You can expect for this gem to produce a `coverage.json` file, located at the `coverage` folder.

Depending on your `SimpleCoV`'s settings you will experiment different outcomes. Particularly depending on which type of coverage are you running `SimpleCov` with:

Expand All @@ -15,13 +15,14 @@ Depending on your `SimpleCoV`'s settings you will experiment different outcomes.

## Development

We encourage you to use docker for common operations like running tests, or debugging your code. Running `make sh` will start 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.
We encourage you to use docker for common operations like running tests, or debugging your code. Running `make sh` will start 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.

### Tests
`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.

### Format

`make format` will run `rubocop -a` which stands for _autocorrect_ and format your code according to the `.rubocop.yml` config file.
`make format` will run `rubocop -a` which stands for _autocorrect_ and format your code according to the `.rubocop.yml` config file.

## Copyright

Expand Down
24 changes: 20 additions & 4 deletions lib/simplecov_json_formatter/result_hash_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@ def initialize(result)
end

def format
format_files
format_groups

formatted_result
end

private

def format_files
@result.files.each do |source_file|
formatted_result[:coverage][source_file.filename] =
format_source_file(source_file)
end

formatted_result
end

private
def format_groups
@result.groups.each do |name, file_list|
formatted_result[:groups][name] = {
lines: {
covered_percent: file_list.covered_percent
}
}
end
end

def formatted_result
@formatted_result ||= {
meta: {
simplecov_version: SimpleCov::VERSION
},
coverage: {}
coverage: {},
groups: {}
}
end

Expand Down
9 changes: 5 additions & 4 deletions spec/fixtures/sample.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"meta": {
"simplecov_version": "0.18.5"
"simplecov_version": "0.21.2"
},
"coverage": {
"/gem/spec/fixtures/sample.rb": {
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
"lines": [
null,
1,
Expand Down Expand Up @@ -32,5 +32,6 @@
null
]
}
}
}
},
"groups": {}
}
43 changes: 43 additions & 0 deletions spec/fixtures/sample_groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"meta": {
"simplecov_version": "0.21.2"
},
"coverage": {
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
"lines": [
null,
1,
1,
1,
1,
null,
null,
1,
1,
null,
null,
1,
1,
0,
null,
1,
null,
null,
null,
"ignored",
"ignored",
"ignored",
"ignored",
"ignored",
null
]
}
},
"groups": {
"My Group": {
"lines": {
"covered_percent": 80.0
}
}
}
}
9 changes: 5 additions & 4 deletions spec/fixtures/sample_with_branch.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"meta": {
"simplecov_version": "0.18.5"
"simplecov_version": "0.21.2"
},
"coverage": {
"/gem/spec/fixtures/sample.rb": {
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
"lines": [
null,
1,
Expand Down Expand Up @@ -46,5 +46,6 @@
}
]
}
}
}
},
"groups": {}
}
25 changes: 23 additions & 2 deletions spec/simplecov_json_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
end

describe 'format' do
context 'whit line coverage' do
context 'with line coverage' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

it 'works' do
subject.format(result)
expect(json_ouput).to eq(json_result('sample'))
end
end

context 'whit branch coverage' do
context 'with branch coverage' do
let(:original_lines) do
[nil, 1, 1, 1, 1, nil, nil, 1, 1,
nil, nil, 1, 1, 0, nil, 1, nil,
Expand Down Expand Up @@ -55,5 +55,26 @@
expect(json_ouput).to eq(json_result('sample_with_branch'))
end
end

context 'with groups' do
let(:result) do
res = SimpleCov::Result.new({
source_fixture('sample.rb') => { 'lines' => [
nil, 1, 1, 1, 1, nil, nil, 1, 1, nil, nil,
1, 1, 0, nil, 1, nil, nil, nil, nil, 1, 0, nil, nil, nil
] }
})

# right now SimpleCov works mostly on global state, hence setting the groups that way
# would be global state --> Mocking is better here
allow(res).to receive_messages(groups: { 'My Group' => double('File List', covered_percent: 80.0) })
res
end

it 'displays groups correctly in the JSON' do
subject.format(result)
expect(json_ouput).to eq(json_result('sample_groups'))
end
end
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ def json_ouput

def json_result(filename)
file = File.read(source_fixture("#{filename}.json"))
file = use_current_working_directory(file)
JSON.parse(file)
end

DEFAULT_WORKING_DIRECTORY = 'STUB_WORKING_DIRECTORY'
def use_current_working_directory(file)
current_working_directory = File.expand_path('..', File.dirname(__FILE__))
file.gsub!("/#{DEFAULT_WORKING_DIRECTORY}/", "#{current_working_directory}/")

file
end