1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MergeRequestDiffFile, feature_category: :code_review_workflow do
it_behaves_like 'a BulkInsertSafe model', described_class do
let(:valid_items_for_bulk_insertion) do
build_list(:merge_request_diff_file, 10) do |mr_diff_file|
mr_diff_file.merge_request_diff = create(:merge_request_diff)
end
end
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
let(:unpacked) { 'unpacked' }
let(:packed) { [unpacked].pack('m0') }
let(:file) { create(:merge_request).merge_request_diff.merge_request_diff_files.first }
describe '#diff' do
let(:file) { build(:merge_request_diff_file) }
context 'when diff is not stored' do
let(:unpacked) { 'unpacked' }
let(:packed) { [unpacked].pack('m0') }
before do
file.diff = packed
end
context 'when the diff is marked as binary' do
before do
file.binary = true
end
it 'unpacks from base 64' do
expect(file.diff).to eq(unpacked)
end
context 'invalid base64' do
let(:packed) { '---/dev/null' }
it 'returns the raw diff' do
expect(file.diff).to eq(packed)
end
end
end
context 'when the diff is not marked as binary' do
it 'returns the raw diff' do
expect(file.diff).to eq(packed)
end
end
end
context 'when diff is stored in DB' do
it 'returns UTF-8 string' do
expect(file.diff.encoding).to eq Encoding::UTF_8
end
end
context 'when diff is stored in external storage' do
let(:test_dir) { 'tmp/tests/external-diffs' }
around do |example|
FileUtils.mkdir_p(test_dir)
begin
example.run
ensure
FileUtils.rm_rf(test_dir)
end
end
before do
stub_external_diffs_setting(enabled: true, storage_path: test_dir)
end
it 'returns UTF-8 string' do
expect(file.diff.encoding).to eq Encoding::UTF_8
end
end
end
describe '#utf8_diff' do
it 'does not raise error when the diff is binary' do
file = build(:merge_request_diff_file)
file.diff = "\x05\x00\x68\x65\x6c\x6c\x6f"
expect { file.utf8_diff }.not_to raise_error
end
it 'calls #diff once' do
allow(file).to receive(:diff).and_return('test')
expect(file).to receive(:diff).once
file.utf8_diff
end
context 'externally stored diff caching' do
let(:test_dir) { 'tmp/tests/external-diffs' }
around do |example|
FileUtils.mkdir_p(test_dir)
begin
example.run
ensure
FileUtils.rm_rf(test_dir)
end
end
before do
stub_external_diffs_setting(enabled: true, storage_path: test_dir)
end
context 'when external diff is not cached' do
it 'caches external diffs' do
expect(file.merge_request_diff).to receive(:cache_external_diff).and_call_original
expect(file.utf8_diff).to eq(file.diff)
end
end
context 'when external diff is already cached' do
it 'reads diff from cached external diff' do
file_stub = double
allow(file.merge_request_diff).to receive(:cached_external_diff).and_yield(file_stub)
expect(file_stub).to receive(:seek).with(file.external_diff_offset)
expect(file_stub).to receive(:read).with(file.external_diff_size)
file.utf8_diff
end
end
context 'when the diff is marked as binary' do
let(:file) { build(:merge_request_diff_file) }
before do
allow(file.merge_request_diff).to receive(:stored_externally?).and_return(true)
allow(file.merge_request_diff).to receive(:cached_external_diff).and_return(packed)
end
context 'when the diff is marked as binary' do
before do
file.binary = true
end
it 'unpacks from base 64' do
expect(file.utf8_diff).to eq(unpacked)
end
context 'invalid base64' do
let(:packed) { '---/dev/null' }
it 'returns the raw diff' do
expect(file.utf8_diff).to eq(packed)
end
end
end
context 'when the diff is not marked as binary' do
it 'returns the raw diff' do
expect(file.utf8_diff).to eq(packed)
end
end
end
context 'when content responds to #encoding' do
it 'encodes content to utf8 encoding' do
expect(file.utf8_diff.encoding).to eq(Encoding::UTF_8)
end
end
context 'when content is blank' do
it 'returns an empty string' do
allow(file.merge_request_diff).to receive(:cached_external_diff).and_return(nil)
expect(file.utf8_diff).to eq('')
end
end
context 'when exception is raised' do
it 'falls back to #diff' do
allow(file).to receive(:binary?).and_raise(StandardError, 'Error!')
expect(file).to receive(:diff)
expect(Gitlab::AppLogger)
.to receive(:warn)
.with(
a_hash_including(
:message => 'Cached external diff export failed',
:merge_request_diff_file_id => file.id,
:merge_request_diff_id => file.merge_request_diff.id,
'exception.class' => 'StandardError',
'exception.message' => 'Error!'
)
)
file.utf8_diff
end
end
end
context 'when diff is not stored externally' do
it 'calls #diff' do
expect(file).to receive(:diff)
file.utf8_diff
end
end
context 'when exception is raised' do
it 'logs exception and returns an empty string' do
allow(file).to receive(:diff).and_raise(StandardError, 'Error!')
expect(Gitlab::AppLogger)
.to receive(:warn)
.with(
a_hash_including(
:message => 'Failed fetching merge request diff',
:merge_request_diff_file_id => file.id,
:merge_request_diff_id => file.merge_request_diff.id,
'exception.class' => 'StandardError',
'exception.message' => 'Error!'
)
)
expect(file.utf8_diff).to eq('')
end
end
end
end
|