diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b4240..75f5e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### `0.4.1` +- #133 Write down to file when using the formatter + ### `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..25689ed 100644 --- a/lib/codecov/formatter.rb +++ b/lib/codecov/formatter.rb @@ -4,122 +4,121 @@ 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 - - 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.update(result_to_codecov(report)) - 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 - - 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) + result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME) + if File.writable?(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 - network.push('<<<<<< network') - network - 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 - # 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) + def result_to_codecov_report(result) + report = file_network.join("\n").concat("\n") + report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json) 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? + 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 + + network.push('<<<<<< network') + network 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 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 - # 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 + # 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 - 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 diff --git a/test/test_codecov.rb b/test/test_codecov.rb index a23c68e..8fca4b1 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,10 +665,10 @@ 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('path/path/lib/path_somefile.rb', []) ]) - SimpleCov.stubs(:root).returns('/path') + SimpleCov.stubs(:root).returns('path') data = formatter.format(result) puts data puts data['params']