|
4 | 4 |
|
5 | 5 | require_relative 'version' |
6 | 6 |
|
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 | + } |
13 | 17 | } |
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)) |
32 | 19 |
|
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}" |
59 | 26 | end |
| 27 | + |
| 28 | + result |
60 | 29 | end |
61 | 30 |
|
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 |
65 | 44 |
|
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) |
73 | 48 | end |
74 | | - end |
75 | 49 |
|
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 |
84 | 71 | end |
| 72 | + |
| 73 | + network.push('<<<<<< network') |
| 74 | + network |
85 | 75 | end |
86 | | - end |
87 | 76 |
|
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) |
99 | 84 | end |
100 | 85 | end |
101 | | - end |
102 | 86 |
|
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 |
116 | 98 |
|
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 |
120 | 113 |
|
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 |
123 | 122 | end |
124 | 123 | end |
125 | 124 | end |
0 commit comments