From 44ad65f03be7dd902e8a7caf2178b4c9b3943325 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 16:29:03 +0100 Subject: [PATCH 1/9] small README formatting fix --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5654694..601dfc1 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 From 3aeda51841d4b44b7391c9f5dfe8c8ab4d965674 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 16:36:10 +0100 Subject: [PATCH 2/9] Make tests run independent of work directory Nicer dev experience, to avoid using docker for a relatively simple gem. --- spec/fixtures/sample.json | 4 ++-- spec/fixtures/sample_with_branch.json | 4 ++-- spec/spec_helper.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/fixtures/sample.json b/spec/fixtures/sample.json index 359ee47..735c071 100644 --- a/spec/fixtures/sample.json +++ b/spec/fixtures/sample.json @@ -3,7 +3,7 @@ "simplecov_version": "0.18.5" }, "coverage": { - "/gem/spec/fixtures/sample.rb": { + "/stub_working_directory/spec/fixtures/sample.rb": { "lines": [ null, 1, @@ -33,4 +33,4 @@ ] } } -} \ No newline at end of file +} diff --git a/spec/fixtures/sample_with_branch.json b/spec/fixtures/sample_with_branch.json index 201c7f8..19f6af7 100644 --- a/spec/fixtures/sample_with_branch.json +++ b/spec/fixtures/sample_with_branch.json @@ -3,7 +3,7 @@ "simplecov_version": "0.18.5" }, "coverage": { - "/gem/spec/fixtures/sample.rb": { + "/stub_working_directory/spec/fixtures/sample.rb": { "lines": [ null, 1, @@ -47,4 +47,4 @@ ] } } -} \ No newline at end of file +} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 787d33e..e410b35 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 From c6284964d400fb987e183b9a65b06529232b58a5 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 16:42:20 +0100 Subject: [PATCH 3/9] fix typos --- spec/simplecov_json_formatter_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/simplecov_json_formatter_spec.rb b/spec/simplecov_json_formatter_spec.rb index c29aaa8..a83ec65 100644 --- a/spec/simplecov_json_formatter_spec.rb +++ b/spec/simplecov_json_formatter_spec.rb @@ -14,14 +14,14 @@ end describe 'format' do - context 'whit line coverage' do + context 'with line coverage' do 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, From eb8742673f4e0cc722070590e4a9a4d477e3a94c Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 16:45:42 +0100 Subject: [PATCH 4/9] run against newest SimpleCov --- Gemfile.lock | 10 ++++++---- spec/fixtures/sample.json | 2 +- spec/fixtures/sample_with_branch.json | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a04c7f8..51fe047 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -54,4 +56,4 @@ DEPENDENCIES simplecov (~> 0.18) BUNDLED WITH - 2.1.0 + 2.3.4 diff --git a/spec/fixtures/sample.json b/spec/fixtures/sample.json index 735c071..6b56faa 100644 --- a/spec/fixtures/sample.json +++ b/spec/fixtures/sample.json @@ -1,6 +1,6 @@ { "meta": { - "simplecov_version": "0.18.5" + "simplecov_version": "0.21.2" }, "coverage": { "/stub_working_directory/spec/fixtures/sample.rb": { diff --git a/spec/fixtures/sample_with_branch.json b/spec/fixtures/sample_with_branch.json index 19f6af7..25915c2 100644 --- a/spec/fixtures/sample_with_branch.json +++ b/spec/fixtures/sample_with_branch.json @@ -1,6 +1,6 @@ { "meta": { - "simplecov_version": "0.18.5" + "simplecov_version": "0.21.2" }, "coverage": { "/stub_working_directory/spec/fixtures/sample.rb": { From 11e868c6918eb060decb3920f6e28ce4df468245 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 17:05:14 +0100 Subject: [PATCH 5/9] Report groups overall coverage percentage --- .../result_hash_formatter.rb | 20 +++++++-- spec/fixtures/sample.json | 3 +- spec/fixtures/sample_groups.json | 41 +++++++++++++++++++ spec/fixtures/sample_with_branch.json | 3 +- spec/simplecov_json_formatter_spec.rb | 21 ++++++++++ 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/sample_groups.json diff --git a/lib/simplecov_json_formatter/result_hash_formatter.rb b/lib/simplecov_json_formatter/result_hash_formatter.rb index 9dfe47d..ef4ad58 100644 --- a/lib/simplecov_json_formatter/result_hash_formatter.rb +++ b/lib/simplecov_json_formatter/result_hash_formatter.rb @@ -9,22 +9,34 @@ 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] = {covered_percent: file_list.covered_percent} + end + end def formatted_result @formatted_result ||= { meta: { simplecov_version: SimpleCov::VERSION }, - coverage: {} + coverage: {}, + groups: {} } end diff --git a/spec/fixtures/sample.json b/spec/fixtures/sample.json index 6b56faa..db534e3 100644 --- a/spec/fixtures/sample.json +++ b/spec/fixtures/sample.json @@ -32,5 +32,6 @@ null ] } - } + }, + "groups": {} } diff --git a/spec/fixtures/sample_groups.json b/spec/fixtures/sample_groups.json new file mode 100644 index 0000000..e95a61b --- /dev/null +++ b/spec/fixtures/sample_groups.json @@ -0,0 +1,41 @@ +{ + "meta": { + "simplecov_version": "0.21.2" + }, + "coverage": { + "/home/tobi/github/simplecov_json_formatter/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": { + "covered_percent": 80.0 + } + } +} diff --git a/spec/fixtures/sample_with_branch.json b/spec/fixtures/sample_with_branch.json index 25915c2..a25da3e 100644 --- a/spec/fixtures/sample_with_branch.json +++ b/spec/fixtures/sample_with_branch.json @@ -46,5 +46,6 @@ } ] } - } + }, + "groups": {} } diff --git a/spec/simplecov_json_formatter_spec.rb b/spec/simplecov_json_formatter_spec.rb index a83ec65..cd6d14d 100644 --- a/spec/simplecov_json_formatter_spec.rb +++ b/spec/simplecov_json_formatter_spec.rb @@ -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 From 9c78d46bd6577b2e74fba91335f013750f57dd14 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sun, 30 Jan 2022 17:07:36 +0100 Subject: [PATCH 6/9] Accomodate for multiple coverage types --- lib/simplecov_json_formatter/result_hash_formatter.rb | 6 +++++- spec/fixtures/sample_groups.json | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/simplecov_json_formatter/result_hash_formatter.rb b/lib/simplecov_json_formatter/result_hash_formatter.rb index ef4ad58..e9c8fc8 100644 --- a/lib/simplecov_json_formatter/result_hash_formatter.rb +++ b/lib/simplecov_json_formatter/result_hash_formatter.rb @@ -26,7 +26,11 @@ def format_files def format_groups @result.groups.each do |name, file_list| - formatted_result[:groups][name] = {covered_percent: file_list.covered_percent} + formatted_result[:groups][name] = { + lines: { + covered_percent: file_list.covered_percent + } + } end end diff --git a/spec/fixtures/sample_groups.json b/spec/fixtures/sample_groups.json index e95a61b..c8c25b2 100644 --- a/spec/fixtures/sample_groups.json +++ b/spec/fixtures/sample_groups.json @@ -35,7 +35,9 @@ }, "groups": { "My Group": { - "covered_percent": 80.0 + "lines": { + "covered_percent": 80.0 + } } } } From 4a372fb937532218fa0a69c182d6b3b9a8b578a8 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Thu, 10 Feb 2022 21:03:54 +0100 Subject: [PATCH 7/9] force ci From 43cabfe58578606177d327832d78958c4699ee78 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sat, 12 Feb 2022 20:48:53 +0100 Subject: [PATCH 8/9] rubocop autofix --- spec/simplecov_json_formatter_spec.rb | 12 ++++++------ spec/spec_helper.rb | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/simplecov_json_formatter_spec.rb b/spec/simplecov_json_formatter_spec.rb index cd6d14d..099d899 100644 --- a/spec/simplecov_json_formatter_spec.rb +++ b/spec/simplecov_json_formatter_spec.rb @@ -59,15 +59,15 @@ 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 - ] } - }) + 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)}) + allow(res).to receive_messages(groups: { 'My Group' => double('File List', covered_percent: 80.0) }) res end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e410b35..7b61dae 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,7 +26,7 @@ def json_result(filename) DEFAULT_WORKING_DIRECTORY = 'stub_working_directory' def use_current_working_directory(file) - current_working_directory = File.expand_path("..", File.dirname(__FILE__)) + current_working_directory = File.expand_path('..', File.dirname(__FILE__)) file.gsub!("/#{DEFAULT_WORKING_DIRECTORY}/", "#{current_working_directory}/") file From afd917a72546291670ceb05ea15fa029a8048abf Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sat, 12 Feb 2022 20:53:21 +0100 Subject: [PATCH 9/9] ah missed on instance of paths --- spec/fixtures/sample.json | 2 +- spec/fixtures/sample_groups.json | 2 +- spec/fixtures/sample_with_branch.json | 2 +- spec/spec_helper.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/fixtures/sample.json b/spec/fixtures/sample.json index db534e3..058cea3 100644 --- a/spec/fixtures/sample.json +++ b/spec/fixtures/sample.json @@ -3,7 +3,7 @@ "simplecov_version": "0.21.2" }, "coverage": { - "/stub_working_directory/spec/fixtures/sample.rb": { + "/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": { "lines": [ null, 1, diff --git a/spec/fixtures/sample_groups.json b/spec/fixtures/sample_groups.json index c8c25b2..c825056 100644 --- a/spec/fixtures/sample_groups.json +++ b/spec/fixtures/sample_groups.json @@ -3,7 +3,7 @@ "simplecov_version": "0.21.2" }, "coverage": { - "/home/tobi/github/simplecov_json_formatter/spec/fixtures/sample.rb": { + "/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": { "lines": [ null, 1, diff --git a/spec/fixtures/sample_with_branch.json b/spec/fixtures/sample_with_branch.json index a25da3e..33fc1c3 100644 --- a/spec/fixtures/sample_with_branch.json +++ b/spec/fixtures/sample_with_branch.json @@ -3,7 +3,7 @@ "simplecov_version": "0.21.2" }, "coverage": { - "/stub_working_directory/spec/fixtures/sample.rb": { + "/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": { "lines": [ null, 1, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7b61dae..5e3bc69 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,7 +24,7 @@ def json_result(filename) JSON.parse(file) end -DEFAULT_WORKING_DIRECTORY = 'stub_working_directory' +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}/")