2
2
require "digest/sha1"
3
3
require "net/https"
4
4
require "uri"
5
+ require "tmpdir"
6
+ require "securerandom"
5
7
6
8
module CodeClimate
7
9
class TestReporter
@@ -43,8 +45,31 @@ def self.post_results(result)
43
45
44
46
class Formatter
45
47
def format ( result )
46
- totals = Hash . new ( 0 )
48
+ print "Coverage = #{ result . covered_percent . round ( 2 ) } %."
49
+
50
+ payload = to_payload ( result )
51
+ if tddium? || ENV [ "TO_FILE" ]
52
+ file_path = File . join ( Dir . tmpdir , "coverage-worker-#{ SecureRandom . uuid } .json" )
53
+ print "Coverage results saved to #{ file_path } ..."
54
+ File . open ( file_path , "w" ) { |file | file . write ( payload . to_json ) }
55
+ else
56
+ print "Sending report to #{ API . host } ... "
57
+ API . post_results ( payload )
58
+ end
59
+
60
+ puts "done."
61
+ true
62
+ rescue => ex
63
+ puts "\n Code Climate encountered an exception: #{ ex . class } "
64
+ puts ex . message
65
+ ex . backtrace . each do |line |
66
+ puts line
67
+ end
68
+ false
69
+ end
47
70
71
+ def to_payload ( result )
72
+ totals = Hash . new ( 0 )
48
73
source_files = result . files . map do |file |
49
74
totals [ :total ] += file . lines . count
50
75
totals [ :covered ] += file . covered_lines . count
@@ -64,9 +89,7 @@ def format(result)
64
89
}
65
90
end
66
91
67
- print "Coverage = #{ result . covered_percent . round ( 2 ) } %. Sending report to #{ API . host } ... "
68
-
69
- API . post_results ( {
92
+ {
70
93
repo_token : ENV [ "CODECLIMATE_REPO_TOKEN" ] ,
71
94
source_files : source_files ,
72
95
run_at : result . created_at ,
@@ -86,17 +109,7 @@ def format(result)
86
109
gem_version : VERSION
87
110
} ,
88
111
ci_service : ci_service_data
89
- } )
90
-
91
- puts "done."
92
- true
93
- rescue => ex
94
- puts "\n Code Climate encountered an exception: #{ ex . class } "
95
- puts ex . message
96
- ex . backtrace . each do |line |
97
- puts line
98
- end
99
- false
112
+ }
100
113
end
101
114
102
115
def ci_service_data
@@ -161,6 +174,10 @@ def git_branch
161
174
branch = [ branch ] . flatten . first
162
175
branch ? branch . gsub ( "* " , "" ) : nil
163
176
end
177
+
178
+ def tddium?
179
+ ci_service_data && ci_service_data [ :name ] == "tddium"
180
+ end
164
181
end
165
182
166
183
def self . start
0 commit comments