From 1e46b80a3e280da646ce5fe7fcd2eb4b8bdb1b4a Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 15:40:18 -0500 Subject: [PATCH 1/8] Update formatter to write to file --- CHANGELOG.md | 3 + lib/codecov.rb | 2 +- lib/codecov/formatter.rb | 193 +++++++++++++++++++-------------------- lib/codecov/version.rb | 2 +- 4 files changed, 98 insertions(+), 102 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b4240..d488876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### `0.4.1` +- # + ### `0.4.0` - #130 Split uploader from formatter diff --git a/lib/codecov.rb b/lib/codecov.rb index 17f5ade..50dc98e 100644 --- a/lib/codecov.rb +++ b/lib/codecov.rb @@ -11,7 +11,7 @@ class SimpleCov::Formatter::Codecov def format(result, disable_net_blockers = true) - report = Codecov::SimpleCov::Formatter.format(result) + report = Codecov::SimpleCov::Formatter.new.format(result) Codecov::Uploader.upload(report, disable_net_blockers) end end diff --git a/lib/codecov/formatter.rb b/lib/codecov/formatter.rb index 7e27f4c..ff63e43 100644 --- a/lib/codecov/formatter.rb +++ b/lib/codecov/formatter.rb @@ -4,122 +4,115 @@ require_relative 'version' -module Codecov::SimpleCov - class Formatter - def self.format(report) - result = { - 'meta' => { - 'version' => "codecov-ruby/v#{::Codecov::VERSION}" +module Codecov + module SimpleCov + class Formatter + RESULT_FILE_NAME = 'codecov-result.json' + + def format(report) + result = { + 'meta' => { + 'version' => "codecov-ruby/v#{::Codecov::VERSION}" + } } - } - result.update(result_to_codecov(report)) - result - end + result.update(result_to_codecov(report)) - private - - # Format SimpleCov coverage data for the Codecov.io API. - # - # @param result [SimpleCov::Result] The coverage data to process. - # @return [Hash] - def self.result_to_codecov(result) - { - 'codecov' => result_to_codecov_report(result), - 'coverage' => result_to_codecov_coverage(result), - 'messages' => result_to_codecov_messages(result) - } - end + result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME) + File.write(result_path, result['codecov']) + puts "Coverage report generated to #{result_path}. #{result}" + end - def self.result_to_codecov_report(result) - report = file_network.join("\n").concat("\n") - report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json) - end + private + + # Format SimpleCov coverage data for the Codecov.io API. + # + # @param result [SimpleCov::Result] The coverage data to process. + # @return [Hash] + def result_to_codecov(result) + { + 'codecov' => result_to_codecov_report(result), + 'coverage' => result_to_codecov_coverage(result), + 'messages' => result_to_codecov_messages(result) + } + end - def self.file_network - invalid_file_types = [ - 'woff', 'eot', 'otf', # fonts - 'gif', 'png', 'jpg', 'jpeg', 'psd', # images - 'ptt', 'pptx', 'numbers', 'pages', 'md', 'txt', 'xlsx', 'docx', 'doc', 'pdf', 'csv', # docs - 'yml', 'yaml', '.gitignore' - ].freeze - - invalid_directories = [ - 'node_modules/', - 'public/', - 'storage/', - 'tmp/', - 'vendor/' - ] - - puts [green('==>'), 'Appending file network'].join(' ') - network = [] - Dir['**/*'].keep_if do |file| - if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) } - network.push(file) - end + def result_to_codecov_report(result) + report = file_network.join("\n").concat("\n") + report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json) end - network.push('<<<<<< network') - network - end + def file_network + invalid_file_types = [ + 'woff', 'eot', 'otf', # fonts + 'gif', 'png', 'jpg', 'jpeg', 'psd', # images + 'ptt', 'pptx', 'numbers', 'pages', 'md', 'txt', 'xlsx', 'docx', 'doc', 'pdf', 'csv', # docs + 'yml', 'yaml', '.gitignore' + ].freeze + + invalid_directories = [ + 'node_modules/', + 'public/', + 'storage/', + 'tmp/', + 'vendor/' + ] + + network = [] + Dir['**/*'].keep_if do |file| + if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) } + network.push(file) + end + end - # Format SimpleCov coverage data for the Codecov.io coverage API. - # - # @param result [SimpleCov::Result] The coverage data to process. - # @return [Hash] - def self.result_to_codecov_coverage(result) - result.files.each_with_object({}) do |file, memo| - memo[shortened_filename(file)] = file_to_codecov(file) + network.push('<<<<<< network') + network end - end - # Format SimpleCov coverage data for the Codecov.io messages API. - # - # @param result [SimpleCov::Result] The coverage data to process. - # @return [Hash] - def self.result_to_codecov_messages(result) - result.files.each_with_object({}) do |file, memo| - memo[shortened_filename(file)] = file.lines.each_with_object({}) do |line, lines_memo| - lines_memo[line.line_number.to_s] = 'skipped' if line.skipped? + # Format SimpleCov coverage data for the Codecov.io coverage API. + # + # @param result [SimpleCov::Result] The coverage data to process. + # @return [Hash] + def result_to_codecov_coverage(result) + result.files.each_with_object({}) do |file, memo| + memo[shortened_filename(file)] = file_to_codecov(file) end end - end - # Format coverage data for a single file for the Codecov.io API. - # - # @param file [SimpleCov::SourceFile] The file to process. - # @return [Array] - def self.file_to_codecov(file) - # Initial nil is required to offset line numbers. - [nil] + file.lines.map do |line| - if line.skipped? - nil - else - line.coverage + # Format SimpleCov coverage data for the Codecov.io messages API. + # + # @param result [SimpleCov::Result] The coverage data to process. + # @return [Hash] + def result_to_codecov_messages(result) + result.files.each_with_object({}) do |file, memo| + memo[shortened_filename(file)] = file.lines.each_with_object({}) do |line, lines_memo| + lines_memo[line.line_number.to_s] = 'skipped' if line.skipped? + end end end - end - - # Get a filename relative to the project root. Based on - # https://github.com/colszowka/simplecov-html, copyright Christoph Olszowka. - # - # @param file [SimpleCov::SourceFile] The file to use. - # @return [String] - def self.shortened_filename(file) - file.filename.gsub(/^#{SimpleCov.root}/, '.').gsub(%r{^\./}, '') - end - - # Convenience color methods - def self.black(str) - str.nil? ? '' : "\e[30m#{str}\e[0m" - end - def self.red(str) - str.nil? ? '' : "\e[31m#{str}\e[0m" - end + # Format coverage data for a single file for the Codecov.io API. + # + # @param file [SimpleCov::SourceFile] The file to process. + # @return [Array] + def file_to_codecov(file) + # Initial nil is required to offset line numbers. + [nil] + file.lines.map do |line| + if line.skipped? + nil + else + line.coverage + end + end + end - def self.green(str) - str.nil? ? '' : "\e[32m#{str}\e[0m" + # Get a filename relative to the project root. Based on + # https://github.com/colszowka/simplecov-html, copyright Christoph Olszowka. + # + # @param file [SimpleCov::SourceFile] The file to use. + # @return [String] + def shortened_filename(file) + file.filename.gsub(/^#{::SimpleCov.root}/, '.').gsub(%r{^\./}, '') + end end end end diff --git a/lib/codecov/version.rb b/lib/codecov/version.rb index 516a9dd..63acaac 100644 --- a/lib/codecov/version.rb +++ b/lib/codecov/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Codecov - VERSION = '0.4.0' + VERSION = '0.4.1' end From b1150f6f4c1ae24910672415dd1f66ff85dcff87 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 15:41:49 -0500 Subject: [PATCH 2/8] Write file to disk when using the formatter --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d488876..75f5e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ### `0.4.1` -- # +- #133 Write down to file when using the formatter ### `0.4.0` - #130 Split uploader from formatter From 9d29041a1394e3394f8cabea6be0f7b588d59173 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 15:49:21 -0500 Subject: [PATCH 3/8] Test if file is writeable --- lib/codecov/formatter.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/codecov/formatter.rb b/lib/codecov/formatter.rb index ff63e43..b9a00f5 100644 --- a/lib/codecov/formatter.rb +++ b/lib/codecov/formatter.rb @@ -18,8 +18,14 @@ def format(report) result.update(result_to_codecov(report)) result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME) - File.write(result_path, result['codecov']) - puts "Coverage report generated to #{result_path}. #{result}" + if File.writeable?(result_path) + File.write(result_path, result['codecov']) + puts "Coverage report generated to #{result_path}.\#{result}" + else + puts "Could not write coverage report to file #{result_path}.\n#{result}" + end + + result end private From e7581a913d931cbda704d8c6a7519f5ccfe92318 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 18:19:25 -0500 Subject: [PATCH 4/8] Logs --- lib/codecov/formatter.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/codecov/formatter.rb b/lib/codecov/formatter.rb index b9a00f5..7378960 100644 --- a/lib/codecov/formatter.rb +++ b/lib/codecov/formatter.rb @@ -17,6 +17,9 @@ def format(report) } result.update(result_to_codecov(report)) + puts "Root #{::SimpleCov.root}" + puts "Coverage dir #{::SimpleCov.coverage_dir}" + puts "Coverage path #{::SimpleCov.coverage_path}" result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME) if File.writeable?(result_path) File.write(result_path, result['codecov']) From e3db99e76b9c1348421114c2f0def699e48cc2b7 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 18:22:06 -0500 Subject: [PATCH 5/8] not full path --- test/test_codecov.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/test_codecov.rb b/test/test_codecov.rb index a23c68e..a4880b7 100644 --- a/test/test_codecov.rb +++ b/test/test_codecov.rb @@ -66,10 +66,10 @@ def upload(success = true) WebMock.enable! formatter = SimpleCov::Formatter::Codecov.new result = stub('SimpleCov::Result', files: [ - stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]), - stub_file('/path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil]) + stub_file('path/lib/something.rb', [1, 0, 0, nil, 1, nil]), + stub_file('path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil]) ]) - SimpleCov.stubs(:root).returns('/path') + SimpleCov.stubs(:root).returns('path') success_stubs if success data = formatter.format(result, false) puts data @@ -665,16 +665,16 @@ def test_filenames_are_shortened_correctly formatter = SimpleCov::Formatter::Codecov.new result = stub('SimpleCov::Result', files: [ - stub_file('/path/lib/something.rb', []), - stub_file('/path/path/lib/path_somefile.rb', []) + stub_file('path/lib/something.rb', []), + stub_file('pathpath/libpath_somefile.rb', []) ]) - SimpleCov.stubs(:root).returns('/path') + SimpleCov.stubs(:root).returns('path') data = formatter.format(result) puts data puts data['params'] assert_equal(data['coverage'].to_json, { 'lib/something.rb' => [nil], - 'path/lib/path_somefile.rb' => [nil] + 'path/libpath_somefile.rb' => [nil] }.to_json) end From 76a421617e8d06ab6ce409c461f1ebee0dd5430f Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 18:23:16 -0500 Subject: [PATCH 6/8] Spelling --- lib/codecov/formatter.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/codecov/formatter.rb b/lib/codecov/formatter.rb index 7378960..25689ed 100644 --- a/lib/codecov/formatter.rb +++ b/lib/codecov/formatter.rb @@ -17,11 +17,8 @@ def format(report) } result.update(result_to_codecov(report)) - puts "Root #{::SimpleCov.root}" - puts "Coverage dir #{::SimpleCov.coverage_dir}" - puts "Coverage path #{::SimpleCov.coverage_path}" result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME) - if File.writeable?(result_path) + if File.writable?(result_path) File.write(result_path, result['codecov']) puts "Coverage report generated to #{result_path}.\#{result}" else From c68a30174d018d36909cb9f8ce5ebbe05156a282 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 18:24:53 -0500 Subject: [PATCH 7/8] Fix stubs --- test/test_codecov.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_codecov.rb b/test/test_codecov.rb index a4880b7..7e3d7ef 100644 --- a/test/test_codecov.rb +++ b/test/test_codecov.rb @@ -666,7 +666,7 @@ def test_filenames_are_shortened_correctly formatter = SimpleCov::Formatter::Codecov.new result = stub('SimpleCov::Result', files: [ stub_file('path/lib/something.rb', []), - stub_file('pathpath/libpath_somefile.rb', []) + stub_file('path/path/lib/path_somefile.rb', []) ]) SimpleCov.stubs(:root).returns('path') data = formatter.format(result) From 112f3987b44736359416676aeaef592f038d4216 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 25 Jan 2021 18:25:44 -0500 Subject: [PATCH 8/8] Fix test --- test/test_codecov.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_codecov.rb b/test/test_codecov.rb index 7e3d7ef..8fca4b1 100644 --- a/test/test_codecov.rb +++ b/test/test_codecov.rb @@ -674,7 +674,7 @@ def test_filenames_are_shortened_correctly puts data['params'] assert_equal(data['coverage'].to_json, { 'lib/something.rb' => [nil], - 'path/libpath_somefile.rb' => [nil] + 'path/lib/path_somefile.rb' => [nil] }.to_json) end