summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/diff/position_tracer/image_strategy_spec.rb
blob: 1414056ad6a70ac0b0b31de50956220ba16e0dfe (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::PositionTracer::ImageStrategy do
  include PositionTracerHelpers

  let(:project) { create(:project, :repository) }
  let(:current_user) { project.first_owner }
  let(:file_name) { 'test-file' }
  let(:new_file_name) { "#{file_name}-new" }
  let(:second_file_name) { "#{file_name}-2" }
  let(:branch_name) { 'position-tracer-test' }
  let(:old_position) { position(old_path: file_name, new_path: file_name, position_type: 'image') }

  let(:tracer) do
    Gitlab::Diff::PositionTracer.new(
      project: project,
      old_diff_refs: old_diff_refs,
      new_diff_refs: new_diff_refs
    )
  end

  let(:strategy) { described_class.new(tracer) }

  subject { strategy.trace(old_position) }

  let(:initial_commit) do
    project.commit(create_branch(branch_name, 'master')[:branch].name)
  end

  describe '#trace' do
    describe 'diff scenarios' do
      let(:create_file_commit) do
        initial_commit

        create_file(
          branch_name,
          file_name,
          Base64.encode64('content')
        )
      end

      let(:update_file_commit) do
        create_file_commit

        update_file(
          branch_name,
          file_name,
          Base64.encode64('updatedcontent')
        )
      end

      let(:update_file_again_commit) do
        update_file_commit

        update_file(
          branch_name,
          file_name,
          Base64.encode64('updatedcontentagain')
        )
      end

      let(:delete_file_commit) do
        create_file_commit
        delete_file(branch_name, file_name)
      end

      let(:rename_file_commit) do
        delete_file_commit

        create_file(
          branch_name,
          new_file_name,
          Base64.encode64('renamedcontent')
        )
      end

      let(:create_second_file_commit) do
        create_file_commit

        create_file(
          branch_name,
          second_file_name,
          Base64.encode64('morecontent')
        )
      end

      let(:create_another_file_commit) do
        create_file(
          branch_name,
          second_file_name,
          Base64.encode64('morecontent')
        )
      end

      let(:update_another_file_commit) do
        update_file(
          branch_name,
          second_file_name,
          Base64.encode64('updatedmorecontent')
        )
      end

      context 'when the file was created in the old diff' do
        context 'when the file is unchanged between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
          let(:new_diff_refs) { diff_refs(initial_commit, create_second_file_commit) }

          it 'returns the new position' do
            expect_new_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was updated between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
          let(:new_diff_refs) { diff_refs(initial_commit, update_file_commit) }
          let(:change_diff_refs) { diff_refs(create_file_commit, update_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was renamed in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
          let(:new_diff_refs) { diff_refs(initial_commit, rename_file_commit) }
          let(:change_diff_refs) { diff_refs(create_file_commit, rename_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was removed in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
          let(:new_diff_refs) { diff_refs(initial_commit, delete_file_commit) }
          let(:change_diff_refs) { diff_refs(create_file_commit, delete_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file is unchanged in the new diff' do
          let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
          let(:new_diff_refs) { diff_refs(create_another_file_commit, update_another_file_commit) }
          let(:change_diff_refs) { diff_refs(initial_commit, create_another_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end
      end

      context 'when the file was changed in the old diff' do
        context 'when the file is unchanged in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
          let(:new_diff_refs) { diff_refs(create_file_commit, create_second_file_commit) }

          it 'returns the new position' do
            expect_new_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was updated in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
          let(:new_diff_refs) { diff_refs(create_file_commit, update_file_again_commit) }
          let(:change_diff_refs) { diff_refs(update_file_commit, update_file_again_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was renamed in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
          let(:new_diff_refs) { diff_refs(create_file_commit, rename_file_commit) }
          let(:change_diff_refs) { diff_refs(update_file_commit, rename_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file was removed in between the old and the new diff' do
          let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
          let(:new_diff_refs) { diff_refs(create_file_commit, delete_file_commit) }
          let(:change_diff_refs) { diff_refs(update_file_commit, delete_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end

        context 'when the file is unchanged in the new diff' do
          let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
          let(:new_diff_refs) { diff_refs(create_another_file_commit, update_another_file_commit) }
          let(:change_diff_refs) { diff_refs(create_file_commit, create_another_file_commit) }

          it 'returns the position of the change' do
            expect_change_position(
              old_path: file_name,
              new_path: file_name
            )
          end
        end
      end
    end

    describe 'symlink scenarios' do
      let(:new_file) { old_file_status == :new }
      let(:deleted_file) { old_file_status == :deleted }
      let(:renamed_file) { old_file_status == :renamed }

      let(:file_identifier) { "#{file_name}-#{new_file}-#{deleted_file}-#{renamed_file}" }
      let(:file_identifier_hash) { Digest::SHA1.hexdigest(file_identifier) }
      let(:old_position) { position(old_path: file_name, new_path: file_name, position_type: 'image', file_identifier_hash: file_identifier_hash) }

      let(:update_file_commit) do
        initial_commit

        update_file(
          branch_name,
          file_name,
          Base64.encode64('morecontent')
        )
      end

      let(:delete_file_commit) do
        initial_commit

        delete_file(branch_name, file_name)
      end

      let(:create_second_file_commit) do
        initial_commit

        create_file(
          branch_name,
          second_file_name,
          Base64.encode64('morecontent')
        )
      end

      before do
        stub_feature_flags(file_identifier_hash: true)
      end

      describe 'from symlink to image' do
        let(:initial_commit) { project.commit('a19c7f9a147e35e535c797cf148d29c24dac5544') }
        let(:symlink_to_image_commit) { project.commit('8cfca8420812e5bd7479aa32cf33e0c95a3ca576') }
        let(:branch_name) { 'diff-files-symlink-to-image' }
        let(:file_name) { 'symlink-to-image.png' }

        context "when the old position is on the new image file" do
          let(:old_file_status) { :new }

          context "when the image file's content was unchanged between the old and the new diff" do
            let(:old_diff_refs) { diff_refs(initial_commit, symlink_to_image_commit) }
            let(:new_diff_refs) { diff_refs(initial_commit, create_second_file_commit) }

            it "returns the new position" do
              expect_new_position(
                old_path: file_name,
                new_path: file_name
              )
            end
          end

          context "when the image file's content was changed between the old and the new diff" do
            let(:old_diff_refs) { diff_refs(initial_commit, symlink_to_image_commit) }
            let(:new_diff_refs) { diff_refs(initial_commit, update_file_commit) }
            let(:change_diff_refs) { diff_refs(symlink_to_image_commit, update_file_commit) }

            it "returns the position of the change" do
              expect_change_position(
                old_path: file_name,
                new_path: file_name
              )
            end
          end

          context "when the image file was removed between the old and the new diff" do
            let(:old_diff_refs) { diff_refs(initial_commit, symlink_to_image_commit) }
            let(:new_diff_refs) { diff_refs(initial_commit, delete_file_commit) }
            let(:change_diff_refs) { diff_refs(symlink_to_image_commit, delete_file_commit) }

            it "returns the position of the change" do
              expect_change_position(
                old_path: file_name,
                new_path: file_name
              )
            end
          end
        end
      end

      describe 'from image to symlink' do
        let(:initial_commit) { project.commit('d10dcdfbbb2b59a959a5f5d66a4adf28f0ea4008') }
        let(:image_to_symlink_commit) { project.commit('3e94fdaa60da8aed38401b91bc56be70d54ca424') }
        let(:branch_name) { 'diff-files-image-to-symlink' }
        let(:file_name) { 'image-to-symlink.png' }

        context "when the old position is on the added image file" do
          let(:old_file_status) { :new }

          context "when the image file gets changed to a symlink between the old and the new diff" do
            let(:old_diff_refs) { diff_refs(initial_commit.parent, initial_commit) }
            let(:new_diff_refs) { diff_refs(initial_commit.parent, image_to_symlink_commit) }
            let(:change_diff_refs) { diff_refs(initial_commit, image_to_symlink_commit) }

            it "returns the position of the change" do
              expect_change_position(
                old_path: file_name,
                new_path: file_name
              )
            end
          end
        end
      end
    end
  end
end