This repository was archived by the owner on Jul 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +68
-21
lines changed
code_climate/test_reporter Expand file tree Collapse file tree 5 files changed +68
-21
lines changed Original file line number Diff line number Diff line change
1
+ module CodeClimate
2
+ module TestReporter
3
+
4
+ class CalculateBlob
5
+
6
+ def initialize ( file_path )
7
+ @file_path = file_path
8
+ end
9
+
10
+ def blob_id
11
+ calculate_with_file or calculate_with_git
12
+ end
13
+
14
+ private
15
+
16
+ def calculate_with_file
17
+ File . open ( @file_path , "rb" ) do |file |
18
+ header = "blob #{ file . size } \0 "
19
+ content = file . read
20
+ store = header + content
21
+
22
+ return Digest ::SHA1 . hexdigest ( store )
23
+ end
24
+ rescue EncodingError
25
+ puts "WARNING: Unable to read #{ @file_path } \n Using git for blob calculation"
26
+ nil
27
+ end
28
+
29
+ def calculate_with_git
30
+ output = `git hash-object -t blob #{ @file_path } ` . chomp
31
+ raise 'ERROR: Failed to calculate blob with git' unless $?. success?
32
+
33
+ output
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ def to_payload(result)
49
49
50
50
{
51
51
name : short_filename ( file . filename ) ,
52
- blob_id : calculate_blob_id ( file . filename ) ,
52
+ blob_id : CalculateBlob . new ( file . filename ) . blob_id ,
53
53
coverage : file . coverage . to_json ,
54
54
covered_percent : round ( file . covered_percent , 2 ) ,
55
55
covered_strength : round ( file . covered_strength , 2 ) ,
@@ -81,15 +81,6 @@ def to_payload(result)
81
81
}
82
82
end
83
83
84
- def calculate_blob_id ( path )
85
- File . open ( path , "rb" ) do |file |
86
- header = "blob #{ file . size } \0 "
87
- content = file . read . force_encoding ( "iso-8859-1" ) . encode ( "utf-8" , replace : nil )
88
- store = header + content
89
-
90
- return Digest ::SHA1 . hexdigest ( store )
91
- end
92
- end
93
84
94
85
def short_filename ( filename )
95
86
return filename unless ::SimpleCov . root
Original file line number Diff line number Diff line change 1
1
require "code_climate/test_reporter"
2
+ require "code_climate/test_reporter/calculate_blob"
2
3
require "code_climate/test_reporter/version"
3
4
require "code_climate/test_reporter/client"
4
5
require "code_climate/test_reporter/formatter"
5
6
require "code_climate/test_reporter/configuration"
6
7
require "code_climate/test_reporter/git"
7
8
require "code_climate/test_reporter/ci"
8
-
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ module CodeClimate ::TestReporter
4
+
5
+ describe CalculateBlob do
6
+
7
+ subject { CalculateBlob . new ( fixture ) }
8
+ let ( :fixture ) { File . expand_path ( "../../fixtures/encoding_test.rb" , __FILE__ ) }
9
+
10
+ it 'hex digests content of file' do
11
+ expect ( subject . blob_id ) . to_not be_nil
12
+ end
13
+
14
+ context 'encoding error' do
15
+
16
+ let ( :fixture ) { File . expand_path ( "../../fixtures/encoding_test_iso.rb" , __FILE__ ) }
17
+
18
+ it 'falls back to git' do
19
+ expect ( File ) . to receive ( :open ) . and_raise ( EncodingError )
20
+ expect ( subject . blob_id ) . to eq ( 'eb82c22dadb9c47a7fed87211623f6856e112f46' )
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
Original file line number Diff line number Diff line change @@ -137,15 +137,5 @@ module CodeClimate::TestReporter
137
137
end
138
138
end
139
139
end
140
-
141
- describe "#calculate_blob_id" do
142
- it "forces UTF-8 as encoding for the file content" do
143
- blob_id = formatter . calculate_blob_id ( File . expand_path ( "../../fixtures/encoding_test_iso.rb" , __FILE__ ) )
144
- expect ( blob_id ) . to_not be_nil
145
-
146
- blob_id = formatter . calculate_blob_id ( File . expand_path ( "../../fixtures/encoding_test.rb" , __FILE__ ) )
147
- expect ( blob_id ) . to_not be_nil
148
- end
149
- end
150
140
end
151
141
end
You can’t perform that action at this time.
0 commit comments