Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 9700fbe

Browse files
authored
Update formatter to write to file (#133)
* Update formatter to write to file * Write file to disk when using the formatter * Test if file is writeable * Logs * not full path * Spelling * Fix stubs * Fix test
1 parent 16f1af3 commit 9700fbe

File tree

5 files changed

+110
-108
lines changed

5 files changed

+110
-108
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### `0.4.1`
2+
- #133 Write down to file when using the formatter
3+
14
### `0.4.0`
25
- #130 Split uploader from formatter
36

lib/codecov.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class SimpleCov::Formatter::Codecov
1313
def format(result, disable_net_blockers = true)
14-
report = Codecov::SimpleCov::Formatter.format(result)
14+
report = Codecov::SimpleCov::Formatter.new.format(result)
1515
Codecov::Uploader.upload(report, disable_net_blockers)
1616
end
1717
end

lib/codecov/formatter.rb

Lines changed: 99 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,121 @@
44

55
require_relative 'version'
66

7-
module Codecov::SimpleCov
8-
class Formatter
9-
def self.format(report)
10-
result = {
11-
'meta' => {
12-
'version' => "codecov-ruby/v#{::Codecov::VERSION}"
7+
module Codecov
8+
module SimpleCov
9+
class Formatter
10+
RESULT_FILE_NAME = 'codecov-result.json'
11+
12+
def format(report)
13+
result = {
14+
'meta' => {
15+
'version' => "codecov-ruby/v#{::Codecov::VERSION}"
16+
}
1317
}
14-
}
15-
result.update(result_to_codecov(report))
16-
result
17-
end
18-
19-
private
20-
21-
# Format SimpleCov coverage data for the Codecov.io API.
22-
#
23-
# @param result [SimpleCov::Result] The coverage data to process.
24-
# @return [Hash]
25-
def self.result_to_codecov(result)
26-
{
27-
'codecov' => result_to_codecov_report(result),
28-
'coverage' => result_to_codecov_coverage(result),
29-
'messages' => result_to_codecov_messages(result)
30-
}
31-
end
18+
result.update(result_to_codecov(report))
3219

33-
def self.result_to_codecov_report(result)
34-
report = file_network.join("\n").concat("\n")
35-
report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json)
36-
end
37-
38-
def self.file_network
39-
invalid_file_types = [
40-
'woff', 'eot', 'otf', # fonts
41-
'gif', 'png', 'jpg', 'jpeg', 'psd', # images
42-
'ptt', 'pptx', 'numbers', 'pages', 'md', 'txt', 'xlsx', 'docx', 'doc', 'pdf', 'csv', # docs
43-
'yml', 'yaml', '.gitignore'
44-
].freeze
45-
46-
invalid_directories = [
47-
'node_modules/',
48-
'public/',
49-
'storage/',
50-
'tmp/',
51-
'vendor/'
52-
]
53-
54-
puts [green('==>'), 'Appending file network'].join(' ')
55-
network = []
56-
Dir['**/*'].keep_if do |file|
57-
if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) }
58-
network.push(file)
20+
result_path = File.join(::SimpleCov.coverage_path, RESULT_FILE_NAME)
21+
if File.writable?(result_path)
22+
File.write(result_path, result['codecov'])
23+
puts "Coverage report generated to #{result_path}.\#{result}"
24+
else
25+
puts "Could not write coverage report to file #{result_path}.\n#{result}"
5926
end
27+
28+
result
6029
end
6130

62-
network.push('<<<<<< network')
63-
network
64-
end
31+
private
32+
33+
# Format SimpleCov coverage data for the Codecov.io API.
34+
#
35+
# @param result [SimpleCov::Result] The coverage data to process.
36+
# @return [Hash]
37+
def result_to_codecov(result)
38+
{
39+
'codecov' => result_to_codecov_report(result),
40+
'coverage' => result_to_codecov_coverage(result),
41+
'messages' => result_to_codecov_messages(result)
42+
}
43+
end
6544

66-
# Format SimpleCov coverage data for the Codecov.io coverage API.
67-
#
68-
# @param result [SimpleCov::Result] The coverage data to process.
69-
# @return [Hash<String, Array>]
70-
def self.result_to_codecov_coverage(result)
71-
result.files.each_with_object({}) do |file, memo|
72-
memo[shortened_filename(file)] = file_to_codecov(file)
45+
def result_to_codecov_report(result)
46+
report = file_network.join("\n").concat("\n")
47+
report.concat({ 'coverage' => result_to_codecov_coverage(result) }.to_json)
7348
end
74-
end
7549

76-
# Format SimpleCov coverage data for the Codecov.io messages API.
77-
#
78-
# @param result [SimpleCov::Result] The coverage data to process.
79-
# @return [Hash<String, Hash>]
80-
def self.result_to_codecov_messages(result)
81-
result.files.each_with_object({}) do |file, memo|
82-
memo[shortened_filename(file)] = file.lines.each_with_object({}) do |line, lines_memo|
83-
lines_memo[line.line_number.to_s] = 'skipped' if line.skipped?
50+
def file_network
51+
invalid_file_types = [
52+
'woff', 'eot', 'otf', # fonts
53+
'gif', 'png', 'jpg', 'jpeg', 'psd', # images
54+
'ptt', 'pptx', 'numbers', 'pages', 'md', 'txt', 'xlsx', 'docx', 'doc', 'pdf', 'csv', # docs
55+
'yml', 'yaml', '.gitignore'
56+
].freeze
57+
58+
invalid_directories = [
59+
'node_modules/',
60+
'public/',
61+
'storage/',
62+
'tmp/',
63+
'vendor/'
64+
]
65+
66+
network = []
67+
Dir['**/*'].keep_if do |file|
68+
if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) }
69+
network.push(file)
70+
end
8471
end
72+
73+
network.push('<<<<<< network')
74+
network
8575
end
86-
end
8776

88-
# Format coverage data for a single file for the Codecov.io API.
89-
#
90-
# @param file [SimpleCov::SourceFile] The file to process.
91-
# @return [Array<nil, Integer>]
92-
def self.file_to_codecov(file)
93-
# Initial nil is required to offset line numbers.
94-
[nil] + file.lines.map do |line|
95-
if line.skipped?
96-
nil
97-
else
98-
line.coverage
77+
# Format SimpleCov coverage data for the Codecov.io coverage API.
78+
#
79+
# @param result [SimpleCov::Result] The coverage data to process.
80+
# @return [Hash<String, Array>]
81+
def result_to_codecov_coverage(result)
82+
result.files.each_with_object({}) do |file, memo|
83+
memo[shortened_filename(file)] = file_to_codecov(file)
9984
end
10085
end
101-
end
10286

103-
# Get a filename relative to the project root. Based on
104-
# https://github.com/colszowka/simplecov-html, copyright Christoph Olszowka.
105-
#
106-
# @param file [SimpleCov::SourceFile] The file to use.
107-
# @return [String]
108-
def self.shortened_filename(file)
109-
file.filename.gsub(/^#{SimpleCov.root}/, '.').gsub(%r{^\./}, '')
110-
end
111-
112-
# Convenience color methods
113-
def self.black(str)
114-
str.nil? ? '' : "\e[30m#{str}\e[0m"
115-
end
87+
# Format SimpleCov coverage data for the Codecov.io messages API.
88+
#
89+
# @param result [SimpleCov::Result] The coverage data to process.
90+
# @return [Hash<String, Hash>]
91+
def result_to_codecov_messages(result)
92+
result.files.each_with_object({}) do |file, memo|
93+
memo[shortened_filename(file)] = file.lines.each_with_object({}) do |line, lines_memo|
94+
lines_memo[line.line_number.to_s] = 'skipped' if line.skipped?
95+
end
96+
end
97+
end
11698

117-
def self.red(str)
118-
str.nil? ? '' : "\e[31m#{str}\e[0m"
119-
end
99+
# Format coverage data for a single file for the Codecov.io API.
100+
#
101+
# @param file [SimpleCov::SourceFile] The file to process.
102+
# @return [Array<nil, Integer>]
103+
def file_to_codecov(file)
104+
# Initial nil is required to offset line numbers.
105+
[nil] + file.lines.map do |line|
106+
if line.skipped?
107+
nil
108+
else
109+
line.coverage
110+
end
111+
end
112+
end
120113

121-
def self.green(str)
122-
str.nil? ? '' : "\e[32m#{str}\e[0m"
114+
# Get a filename relative to the project root. Based on
115+
# https://github.com/colszowka/simplecov-html, copyright Christoph Olszowka.
116+
#
117+
# @param file [SimpleCov::SourceFile] The file to use.
118+
# @return [String]
119+
def shortened_filename(file)
120+
file.filename.gsub(/^#{::SimpleCov.root}/, '.').gsub(%r{^\./}, '')
121+
end
123122
end
124123
end
125124
end

lib/codecov/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Codecov
4-
VERSION = '0.4.0'
4+
VERSION = '0.4.1'
55
end

test/test_codecov.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def upload(success = true)
6666
WebMock.enable!
6767
formatter = SimpleCov::Formatter::Codecov.new
6868
result = stub('SimpleCov::Result', files: [
69-
stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
70-
stub_file('/path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil])
69+
stub_file('path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
70+
stub_file('path/lib/somefile.rb', [1, nil, 1, 1, 1, 0, 0, nil, 1, nil])
7171
])
72-
SimpleCov.stubs(:root).returns('/path')
72+
SimpleCov.stubs(:root).returns('path')
7373
success_stubs if success
7474
data = formatter.format(result, false)
7575
puts data
@@ -665,10 +665,10 @@ def test_filenames_are_shortened_correctly
665665

666666
formatter = SimpleCov::Formatter::Codecov.new
667667
result = stub('SimpleCov::Result', files: [
668-
stub_file('/path/lib/something.rb', []),
669-
stub_file('/path/path/lib/path_somefile.rb', [])
668+
stub_file('path/lib/something.rb', []),
669+
stub_file('path/path/lib/path_somefile.rb', [])
670670
])
671-
SimpleCov.stubs(:root).returns('/path')
671+
SimpleCov.stubs(:root).returns('path')
672672
data = formatter.format(result)
673673
puts data
674674
puts data['params']

0 commit comments

Comments
 (0)