Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_spec.rb « diff « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fbb646542971739bff3830274e311712288beff (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::File do
  include RepoHelpers

  let_it_be(:project) { create(:project, :repository) }
  let(:commit) { project.commit(sample_commit.id) }
  let(:diff) { commit.raw_diffs.first }
  let(:diff_file) do
    described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository)
  end

  def create_file(file_name, content)
    Files::CreateService.new(
      project,
      project.first_owner,
      commit_message: 'Update',
      start_branch: branch_name,
      branch_name: branch_name,
      file_path: file_name,
      file_content: content
    ).execute

    project.commit(branch_name).diffs.diff_files.first
  end

  def update_file(file_name, content)
    Files::UpdateService.new(
      project,
      project.first_owner,
      commit_message: 'Update',
      start_branch: branch_name,
      branch_name: branch_name,
      file_path: file_name,
      file_content: content
    ).execute

    project.commit(branch_name).diffs.diff_files.first
  end

  def delete_file(file_name)
    Files::DeleteService.new(
      project,
      project.first_owner,
      commit_message: 'Update',
      start_branch: branch_name,
      branch_name: branch_name,
      file_path: file_name
    ).execute

    project.commit(branch_name).diffs.diff_files.first
  end

  describe 'delegated methods' do
    subject { diff_file }

    %i[
      new_file?
      deleted_file?
      renamed_file?
      unidiff
      old_path
      new_path
      a_mode
      b_mode
      mode_changed?
      submodule?
      expanded?
      too_large?
      collapsed?
      line_count
      has_binary_notice?
      generated?
    ].each do |method|
      it { is_expected.to delegate_method(method).to(:diff) }
    end
  end

  describe '#initialize' do
    let(:commit) { project.commit("532c837") }

    context 'when file is ipynb' do
      it 'creates a rendered diff file' do
        expect(diff_file.rendered).not_to be_nil
      end
    end
  end

  describe '#ipynb?' do
    context 'is ipynb' do
      let(:commit) { project.commit("532c837") }

      it 'is true' do
        expect(diff_file.ipynb?).to be_truthy
      end
    end

    context 'is not ipynb' do
      it 'is false' do
        expect(diff_file.ipynb?).to be_falsey
      end
    end
  end

  describe '#has_renderable?' do
    context 'file is ipynb' do
      let(:commit) { project.commit("532c837") }

      it 'has renderable viewer' do
        expect(diff_file.has_renderable?).to be_truthy
      end
    end

    context 'file is not ipynb' do
      let(:commit) { project.commit("d59c60028b053793cecfb4022de34602e1a9218e") }

      it 'does not have renderable viewer' do
        expect(diff_file.has_renderable?).to be_falsey
      end

      it 'does not create a Notebook DiffFile' do
        expect(diff_file.rendered).to be_nil

        expect(::Gitlab::Diff::Rendered::Notebook::DiffFile).not_to receive(:new)
      end
    end
  end

  describe '#rendered' do
    context 'when not ipynb' do
      it 'is nil' do
        expect(diff_file.rendered).to be_nil
      end
    end

    context 'when ipynb' do
      let(:commit) { project.commit("532c837") }

      it 'creates a NotebookDiffFile for rendering' do
        expect(diff_file.rendered).to be_kind_of(Gitlab::Diff::Rendered::Notebook::DiffFile)
      end

      context 'when collapsed' do
        it 'is nil' do
          expect(diff).to receive(:collapsed?).and_return(true)

          expect(diff_file.rendered).to be_nil
        end
      end

      context 'when too large' do
        it 'is nil' do
          expect(diff).to receive(:too_large?).and_return(true)

          expect(diff_file.rendered).to be_nil
        end
      end

      context 'when not modified' do
        it 'is nil' do
          expect(diff_file).to receive(:modified_file?).and_return(false)

          expect(diff_file.rendered).to be_nil
        end
      end
    end
  end

  describe '#diff_lines' do
    let(:diff_lines) { diff_file.diff_lines }

    it { expect(diff_lines.size).to eq(30) }
    it { expect(diff_lines.first).to be_kind_of(Gitlab::Diff::Line) }
  end

  describe '#highlighted_diff_lines' do
    it 'highlights the diff and memoises the result' do
      expect(Gitlab::Diff::Highlight).to receive(:new)
                                           .with(diff_file, repository: project.repository)
                                           .once
                                           .and_call_original

      diff_file.highlighted_diff_lines
    end
  end

  describe '#diff_lines_for_serializer' do
    it 'includes bottom match line if not in the end' do
      expect(diff_file.diff_lines_for_serializer.last.type).to eq('match')
    end

    context 'when called multiple times' do
      it 'only adds bottom match line once' do
        expect(diff_file.diff_lines_for_serializer.size).to eq(31)
        expect(diff_file.diff_lines_for_serializer.size).to eq(31)
      end
    end

    context 'when deleted' do
      let(:commit) { project.commit('d59c60028b053793cecfb4022de34602e1a9218e') }
      let(:diff_file) { commit.diffs.diff_file_with_old_path('files/js/commit.js.coffee') }

      it 'does not include bottom match line' do
        expect(diff_file.diff_lines_for_serializer.last.type).not_to eq('match')
      end
    end
  end

  describe '#unfold_diff_lines' do
    let(:unfolded_lines) { double('expanded-lines') }
    let(:unfolder) { instance_double(Gitlab::Diff::LinesUnfolder) }
    let(:position) { instance_double(Gitlab::Diff::Position, old_line: 10) }

    before do
      allow(Gitlab::Diff::LinesUnfolder).to receive(:new) { unfolder }
    end

    context 'when unfold required' do
      before do
        allow(unfolder).to receive(:unfold_required?) { true }
        allow(unfolder).to receive(:unfolded_diff_lines) { unfolded_lines }
      end

      it 'changes @unfolded to true' do
        diff_file.unfold_diff_lines(position)

        expect(diff_file).to be_unfolded
      end

      it 'updates @diff_lines' do
        diff_file.unfold_diff_lines(position)

        expect(diff_file.diff_lines).to eq(unfolded_lines)
      end
    end

    context 'when unfold not required' do
      before do
        allow(unfolder).to receive(:unfold_required?) { false }
      end

      it 'keeps @unfolded false' do
        diff_file.unfold_diff_lines(position)

        expect(diff_file).not_to be_unfolded
      end

      it 'does not update @diff_lines' do
        expect { diff_file.unfold_diff_lines(position) }
          .not_to change(diff_file, :diff_lines)
      end
    end
  end

  describe '#mode_changed?' do
    it { expect(diff_file.mode_changed?).to be_falsey }
  end

  describe '#too_large?' do
    it 'returns true for a file that is too large' do
      expect(diff).to receive(:too_large?).and_return(true)

      expect(diff_file.too_large?).to eq(true)
    end

    it 'returns false for a file that is small enough' do
      expect(diff).to receive(:too_large?).and_return(false)

      expect(diff_file.too_large?).to eq(false)
    end
  end

  describe '#collapsed?' do
    it 'returns true for a file that is quite big' do
      expect(diff).to receive(:collapsed?).and_return(true)

      expect(diff_file.collapsed?).to eq(true)
    end

    it 'returns false for a file that is small enough' do
      expect(diff).to receive(:collapsed?).and_return(false)

      expect(diff_file.collapsed?).to eq(false)
    end
  end

  describe '#old_blob and #new_blob' do
    context 'when increase_diff_file_performance is on' do
      let(:diff_file) do
        described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository, max_blob_size: max_blob_size)
      end

      let(:max_blob_size) { 1000 }

      before do
        stub_feature_flags(increase_diff_file_performance: true)
      end

      context 'when the blobs are truncated' do
        let(:max_blob_size) { 10 }

        it 'returns the truncated blobs' do
          items = [
            [diff_file.new_content_sha, diff_file.new_path], [diff_file.old_content_sha, diff_file.old_path]
          ]

          expect(project.repository).to receive(:blobs_at).with(items, blob_size_limit: max_blob_size).and_call_original

          old_data = diff_file.old_blob.data
          data = diff_file.new_blob.data

          expect(old_data.size).to eq(max_blob_size)
          expect(data.size).to eq(max_blob_size)
        end
      end

      it 'returns blob of base commit and the new commit' do
        items = [
          [diff_file.new_content_sha, diff_file.new_path], [diff_file.old_content_sha, diff_file.old_path]
        ]

        expect(project.repository).to receive(:blobs_at).with(items, blob_size_limit: max_blob_size).and_call_original

        old_data = diff_file.old_blob.data
        data = diff_file.new_blob.data

        expect(old_data).to include('raise "System commands must be given as an array of strings"')
        expect(data).to include('raise RuntimeError, "System commands must be given as an array of strings"')
      end
    end

    context 'when increase_diff_file_performance is off' do
      let(:diff_file) do
        described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository)
      end

      before do
        stub_feature_flags(increase_diff_file_performance: false)
      end

      it 'returns blob of base commit and the new commit' do
        items = [
          [diff_file.new_content_sha, diff_file.new_path], [diff_file.old_content_sha, diff_file.old_path]
        ]

        expect(project.repository).to receive(:blobs_at).with(items, blob_size_limit: Gitlab::Git::Blob::MAX_DATA_DISPLAY_SIZE).and_call_original

        old_data = diff_file.old_blob.data
        data = diff_file.new_blob.data

        expect(old_data).to include('raise "System commands must be given as an array of strings"')
        expect(data).to include('raise RuntimeError, "System commands must be given as an array of strings"')
      end
    end
  end

  describe '#diffable?' do
    context 'when attributes exist' do
      let(:commit) { project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') }
      let(:diffs) { commit.diffs }

      before do
        project.repository.commit_files(
          project.creator,
          branch_name: 'master',
          message: 'Add attributes',
          actions: [{ action: :update, file_path: '.gitattributes', content: "*.md -diff\n" }]
        )
        project.repository.copy_gitattributes('master')
      end

      it "returns true for files that do not have attributes" do
        diff_file = diffs.diff_file_with_new_path('LICENSE')
        expect(diff_file.diffable?).to be_truthy
      end

      it "returns false for files that have been marked as not being diffable in attributes" do
        diff_file = diffs.diff_file_with_new_path('README.md')
        expect(diff_file.diffable?).to be_falsey
      end
    end

    context 'when the text has binary notice' do
      let(:commit) { project.commit('f05a98786e4274708e1fa118c7ad3a29d1d1b9a3') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('VERSION') }

      it "returns false" do
        expect(diff_file.diffable?).to be_falsey
      end
    end

    context 'when the content is binary' do
      let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

      it "returns true" do
        expect(diff_file.diffable?).to be_truthy
      end
    end
  end

  describe '#content_changed?' do
    context 'when created' do
      let(:commit) { project.commit('33f3729a45c02fc67d00adb1b8bca394b0e761d9') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

      it 'returns false' do
        expect(diff_file.content_changed?).to be_falsey
      end
    end

    context 'when deleted' do
      let(:commit) { project.commit('d59c60028b053793cecfb4022de34602e1a9218e') }
      let(:diff_file) { commit.diffs.diff_file_with_old_path('files/js/commit.js.coffee') }

      it 'returns false' do
        expect(diff_file.content_changed?).to be_falsey
      end
    end

    context 'when renamed' do
      let(:commit) { project.commit('94bb47ca1297b7b3731ff2a36923640991e9236f') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('CHANGELOG.md') }

      it 'returns false' do
        expect(diff_file.content_changed?).to be_falsey
      end
    end

    context 'when content changed' do
      context 'when binary' do
        let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
        let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

        context 'when the blobs are different' do
          it 'returns true' do
            expect(diff_file.content_changed?).to be_truthy
          end
        end

        context 'when there are no diff refs' do
          before do
            allow(diff_file).to receive(:diff_refs).and_return(nil)
          end

          it 'returns false' do
            expect(diff_file.content_changed?).to be_falsey
          end
        end
      end

      context 'when not binary' do
        let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
        let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }

        context 'when the blobs are different' do
          it 'returns true' do
            expect(diff_file.content_changed?).to be_truthy
          end
        end

        context 'when there are no diff refs' do
          before do
            allow(diff_file).to receive(:diff_refs).and_return(nil)
          end

          it 'returns true' do
            expect(diff_file.content_changed?).to be_truthy
          end
        end
      end
    end
  end

  describe '#file_hash' do
    it 'returns a hash of file_path' do
      expect(diff_file.file_hash).to eq(Digest::SHA1.hexdigest(diff_file.file_path))
    end
  end

  describe '#file_identifier_hash' do
    it 'returns a hash of file_identifier' do
      expect(diff_file.file_identifier_hash).to eq(Digest::SHA1.hexdigest(diff_file.file_identifier))
    end
  end

  context 'diff file stats' do
    let(:diff_file) do
      described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository, stats: stats)
    end

    let(:raw_diff) do
      <<~EOS
        --- a/files/ruby/popen.rb
        +++ b/files/ruby/popen.rb
        @@ -6,12 +6,18 @@ module Popen

           def popen(cmd, path=nil)
             unless cmd.is_a?(Array)
        -      raise "System commands must be given as an array of strings"
        +      raise RuntimeError, "System commands must be given as an array of strings"
        +      # foobar
             end
      EOS
    end

    describe '#added_lines' do
      context 'when stats argument given' do
        let(:stats) { double(Gitaly::DiffStats, additions: 10, deletions: 15) }

        it 'returns added lines from stats' do
          expect(diff_file.added_lines).to eq(stats.additions)
        end
      end

      context 'when stats argument not given' do
        let(:stats) { nil }

        it 'returns added lines by parsing raw diff' do
          allow(diff_file).to receive(:raw_diff) { raw_diff }

          expect(diff_file.added_lines).to eq(2)
        end
      end
    end

    describe '#removed_lines' do
      context 'when stats argument given' do
        let(:stats) { double(Gitaly::DiffStats, additions: 10, deletions: 15) }

        it 'returns removed lines from stats' do
          expect(diff_file.removed_lines).to eq(stats.deletions)
        end
      end

      context 'when stats argument not given' do
        let(:stats) { nil }

        it 'returns removed lines by parsing raw diff' do
          allow(diff_file).to receive(:raw_diff) { raw_diff }

          expect(diff_file.removed_lines).to eq(1)
        end
      end
    end
  end

  describe '#simple_viewer' do
    context 'when the file is collapsed' do
      before do
        allow(diff_file).to receive(:collapsed?).and_return(true)
      end

      it 'returns a Collapsed viewer' do
        expect(diff_file.simple_viewer).to be_a(DiffViewer::Collapsed)
      end
    end

    context 'when the file is not diffable' do
      before do
        allow(diff_file).to receive(:diffable?).and_return(false)
      end

      it 'returns a Not Diffable viewer' do
        expect(diff_file.simple_viewer).to be_a(DiffViewer::NotDiffable)
      end
    end

    context 'when the content changed' do
      context 'when the file represented by the diff file is binary' do
        before do
          allow(diff_file).to receive(:binary?).and_return(true)
        end

        it 'returns a No Preview viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::NoPreview)
        end
      end

      context 'when the diff file old and new blob types are different' do
        before do
          allow(diff_file).to receive(:different_type?).and_return(true)
        end

        it 'returns a No Preview viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::NoPreview)
        end
      end

      context 'when the file represented by the diff file is text-based' do
        it 'returns a text viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Text)
        end
      end
    end

    context 'when created' do
      let(:commit) { project.commit('913c66a37b4a45b9769037c55c2d238bd0942d2e') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }

      before do
        allow(diff_file).to receive(:content_changed?).and_return(nil)
      end

      context 'when the file represented by the diff file is binary' do
        before do
          allow(diff_file).to receive(:binary?).and_return(true)
        end

        it 'returns an Added viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Added)
        end
      end

      context 'when the diff file old and new blob types are different' do
        before do
          allow(diff_file).to receive(:different_type?).and_return(true)
        end

        it 'returns an Added viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Added)
        end
      end

      context 'when the file represented by the diff file is text-based' do
        it 'returns a text viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Text)
        end
      end
    end

    context 'when deleted' do
      let(:commit) { project.commit('d59c60028b053793cecfb4022de34602e1a9218e') }
      let(:diff_file) { commit.diffs.diff_file_with_old_path('files/js/commit.js.coffee') }

      before do
        allow(diff_file).to receive(:content_changed?).and_return(nil)
      end

      context 'when the file represented by the diff file is binary' do
        before do
          allow(diff_file).to receive(:binary?).and_return(true)
        end

        it 'returns a Deleted viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Deleted)
        end
      end

      context 'when the diff file old and new blob types are different' do
        before do
          allow(diff_file).to receive(:different_type?).and_return(true)
        end

        it 'returns a Deleted viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Deleted)
        end
      end

      context 'when the file represented by the diff file is text-based' do
        it 'returns a text viewer' do
          expect(diff_file.simple_viewer).to be_a(DiffViewer::Text)
        end
      end
    end

    context 'when renamed' do
      let(:commit) { project.commit('6907208d755b60ebeacb2e9dfea74c92c3449a1f') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/js/commit.coffee') }

      before do
        allow(diff_file).to receive(:content_changed?).and_return(nil)
      end

      it 'returns a Renamed viewer' do
        expect(diff_file.simple_viewer).to be_a(DiffViewer::Renamed)
      end
    end

    context 'when mode changed' do
      before do
        allow(diff_file).to receive(:content_changed?).and_return(nil)
        allow(diff_file).to receive(:mode_changed?).and_return(true)
      end

      it 'returns a Mode Changed viewer' do
        expect(diff_file.simple_viewer).to be_a(DiffViewer::ModeChanged)
      end
    end

    context 'when no other conditions apply' do
      before do
        allow(diff_file).to receive(:content_changed?).and_return(false)
        allow(diff_file).to receive(:new_file?).and_return(false)
        allow(diff_file).to receive(:deleted_file?).and_return(false)
        allow(diff_file).to receive(:renamed_file?).and_return(false)
        allow(diff_file).to receive(:mode_changed?).and_return(false)
        allow(diff_file).to receive(:text?).and_return(false)
      end

      it 'returns a No Preview viewer' do
        expect(diff_file.simple_viewer).to be_a(DiffViewer::NoPreview)
      end
    end
  end

  describe '#rich_viewer' do
    let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
    let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

    context 'when the diff file has a matching viewer' do
      context 'when the diff file content did not change' do
        before do
          allow(diff_file).to receive(:content_changed?).and_return(false)
        end

        it 'returns nil' do
          expect(diff_file.rich_viewer).to be_nil
        end
      end

      context 'when the diff file is not diffable' do
        before do
          allow(diff_file).to receive(:diffable?).and_return(false)
        end

        it 'returns nil' do
          expect(diff_file.rich_viewer).to be_nil
        end
      end

      context 'when the diff file old and new blob types are different' do
        before do
          allow(diff_file).to receive(:different_type?).and_return(true)
        end

        it 'returns nil' do
          expect(diff_file.rich_viewer).to be_nil
        end
      end

      context 'when the diff file has an external storage error' do
        before do
          allow(diff_file).to receive(:external_storage_error?).and_return(true)
        end

        it 'returns nil' do
          expect(diff_file.rich_viewer).to be_nil
        end
      end

      context 'when everything is right' do
        it 'returns the viewer' do
          expect(diff_file.rich_viewer).to be_a(DiffViewer::Image)
        end
      end
    end

    context 'when the diff file does not have a matching viewer' do
      let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }

      it 'returns nil' do
        expect(diff_file.rich_viewer).to be_nil
      end
    end
  end

  describe '#alternate_viewer' do
    subject { diff_file.alternate_viewer }

    where(:viewer_class) do
      [
        DiffViewer::Image,
        DiffViewer::Collapsed,
        DiffViewer::NotDiffable,
        DiffViewer::Text,
        DiffViewer::NoPreview,
        DiffViewer::Added,
        DiffViewer::Deleted,
        DiffViewer::ModeChanged,
        DiffViewer::ModeChanged,
        DiffViewer::NoPreview
      ]
    end

    with_them do
      let(:viewer) { viewer_class.new(diff_file) }

      before do
        allow(diff_file).to receive(:viewer).and_return(viewer)
      end

      it { is_expected.to be_nil }
    end

    context 'when viewer is DiffViewer::Renamed' do
      let(:viewer) { DiffViewer::Renamed.new(diff_file) }

      before do
        allow(diff_file).to receive(:viewer).and_return(viewer)
      end

      context 'when it can be rendered as text' do
        it { is_expected.to be_a(DiffViewer::Text) }
      end

      context 'when it can be rendered as image' do
        let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
        let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

        it { is_expected.to be_a(DiffViewer::Image) }
      end

      context 'when it is something else' do
        let(:commit) { project.commit('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
        let(:diff_file) { commit.diffs.diff_file_with_new_path('Gemfile.zip') }

        it { is_expected.to be_nil }
      end
    end
  end

  describe '#rendered_as_text?' do
    context 'when the simple viewer is text-based' do
      let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }

      context 'when ignoring errors' do
        context 'when the viewer has render errors' do
          before do
            diff_file.diff.too_large!
          end

          it 'returns true' do
            expect(diff_file.rendered_as_text?).to be_truthy
          end
        end

        context "when the viewer doesn't have render errors" do
          it 'returns true' do
            expect(diff_file.rendered_as_text?).to be_truthy
          end
        end
      end

      context 'when not ignoring errors' do
        context 'when the viewer has render errors' do
          before do
            diff_file.diff.too_large!
          end

          it 'returns false' do
            expect(diff_file.rendered_as_text?(ignore_errors: false)).to be_falsey
          end
        end

        context "when the viewer doesn't have render errors" do
          it 'returns true' do
            expect(diff_file.rendered_as_text?(ignore_errors: false)).to be_truthy
          end
        end
      end
    end

    context 'when the simple viewer is binary' do
      let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
      let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }

      it 'returns false' do
        expect(diff_file.rendered_as_text?).to be_falsey
      end
    end
  end

  context 'when neither blob exists' do
    let(:blank_diff_refs) { Gitlab::Diff::DiffRefs.new(base_sha: Gitlab::Git::SHA1_BLANK_SHA, head_sha: Gitlab::Git::SHA1_BLANK_SHA) }
    let(:diff_file) { described_class.new(diff, diff_refs: blank_diff_refs, repository: project.repository) }

    describe '#blob' do
      it 'returns a concrete nil so it can be used in boolean expressions' do
        actual = diff_file.blob && true

        expect(actual).to be_nil
      end
    end

    describe '#binary?' do
      it 'returns false' do
        expect(diff_file).not_to be_binary
      end
    end

    describe '#size' do
      it 'returns zero' do
        expect(diff_file.size).to be_zero
      end
    end

    describe '#empty?' do
      it 'returns true' do
        expect(diff_file.empty?).to be_truthy
      end
    end

    describe '#different_type?' do
      it 'returns false' do
        expect(diff_file).not_to be_different_type
      end
    end

    describe '#content_changed?' do
      it 'returns false' do
        expect(diff_file).not_to be_content_changed
      end
    end
  end

  context 'when the the encoding of the file is unsupported' do
    let(:commit) { project.commit('f05a98786e4274708e1fa118c7ad3a29d1d1b9a3') }
    let(:diff_file) { commit.diffs.diff_file_with_new_path('VERSION') }

    it 'returns a Not Diffable viewer' do
      expect(diff_file.simple_viewer).to be_a(DiffViewer::NotDiffable)
    end

    it { expect(diff_file.highlighted_diff_lines).to eq([]) }
    it { expect(diff_file.parallel_diff_lines).to eq([]) }
  end

  describe '#diff_hunk' do
    context 'when first line is a match' do
      let(:raw_diff) do
        <<~EOS
          --- a/files/ruby/popen.rb
          +++ b/files/ruby/popen.rb
          @@ -6,12 +6,18 @@ module Popen

             def popen(cmd, path=nil)
               unless cmd.is_a?(Array)
          -      raise "System commands must be given as an array of strings"
          +      raise RuntimeError, "System commands must be given as an array of strings"
               end
        EOS
      end

      it 'returns raw diff up to given line index' do
        allow(diff_file).to receive(:raw_diff) { raw_diff }
        diff_line = instance_double(Gitlab::Diff::Line, index: 4)

        diff_hunk = <<~EOS
          @@ -6,12 +6,18 @@ module Popen

             def popen(cmd, path=nil)
               unless cmd.is_a?(Array)
          -      raise "System commands must be given as an array of strings"
          +      raise RuntimeError, "System commands must be given as an array of strings"
        EOS

        expect(diff_file.diff_hunk(diff_line)).to eq(diff_hunk.strip)
      end
    end

    context 'when first line is not a match' do
      let(:raw_diff) do
        <<~EOS
          @@ -1,4 +1,4 @@
          -Copyright (c) 2011-2017 GitLab B.V.
          +Copyright (c) 2011-2019 GitLab B.V.

          With regard to the GitLab Software:

          @@ -9,17 +9,21 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
          copies of the Software, and to permit persons to whom the Software is
          furnished to do so, subject to the following conditions:
        EOS
      end

      it 'returns raw diff up to given line index' do
        allow(diff_file).to receive(:raw_diff) { raw_diff }
        diff_line = instance_double(Gitlab::Diff::Line, index: 5)

        diff_hunk = <<~EOS
          -Copyright (c) 2011-2017 GitLab B.V.
          +Copyright (c) 2011-2019 GitLab B.V.

          With regard to the GitLab Software:

          @@ -9,17 +9,21 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
          copies of the Software, and to permit persons to whom the Software is
        EOS

        expect(diff_file.diff_hunk(diff_line)).to eq(diff_hunk.strip)
      end
    end
  end

  describe '#empty?' do
    let(:project) do
      create(:project, :custom_repo, files: {})
    end

    let(:branch_name) { 'master' }

    context 'when empty file is created' do
      it 'returns true' do
        diff_file = create_file('empty.md', '')

        expect(diff_file.empty?).to be_truthy
      end
    end

    context 'when empty file is deleted' do
      it 'returns true' do
        create_file('empty.md', '')
        diff_file = delete_file('empty.md')

        expect(diff_file.empty?).to be_truthy
      end
    end

    context 'when file with content is truncated' do
      it 'returns false' do
        create_file('with-content.md', 'file content')
        diff_file = update_file('with-content.md', '')

        expect(diff_file.empty?).to be_falsey
      end
    end

    context 'when empty file has content added' do
      it 'returns false' do
        create_file('empty.md', '')
        diff_file = update_file('empty.md', 'new content')

        expect(diff_file.empty?).to be_falsey
      end
    end
  end

  describe '#fully_expanded?' do
    let(:project) do
      create(:project, :custom_repo, files: {})
    end

    let(:branch_name) { 'master' }

    context 'when empty file is created' do
      it 'returns true' do
        diff_file = create_file('empty.md', '')

        expect(diff_file.fully_expanded?).to be_truthy
      end
    end

    context 'when empty file is deleted' do
      it 'returns true' do
        create_file('empty.md', '')
        diff_file = delete_file('empty.md')

        expect(diff_file.fully_expanded?).to be_truthy
      end
    end

    context 'when short file with last line removed' do
      it 'returns true' do
        create_file('with-content.md', (1..3).to_a.join("\n"))
        diff_file = update_file('with-content.md', (1..2).to_a.join("\n"))

        expect(diff_file.fully_expanded?).to be_truthy
      end
    end

    context 'when a single line is added to empty file' do
      it 'returns true' do
        create_file('empty.md', '')
        diff_file = update_file('empty.md', 'new content')

        expect(diff_file.fully_expanded?).to be_truthy
      end
    end

    context 'when single line file is changed' do
      it 'returns true' do
        create_file('file.md', 'foo')
        diff_file = update_file('file.md', 'bar')

        expect(diff_file.fully_expanded?).to be_truthy
      end
    end

    context 'when long file is changed' do
      before do
        create_file('file.md', (1..999).to_a.join("\n"))
      end

      context 'when first line is removed' do
        it 'returns true' do
          diff_file = update_file('file.md', (2..999).to_a.join("\n"))

          expect(diff_file.fully_expanded?).to be_falsey
        end
      end

      context 'when last line is removed' do
        it 'returns true' do
          diff_file = update_file('file.md', (1..998).to_a.join("\n"))

          expect(diff_file.fully_expanded?).to be_falsey
        end
      end

      context 'when first and last lines are removed' do
        it 'returns false' do
          diff_file = update_file('file.md', (2..998).to_a.join("\n"))

          expect(diff_file.fully_expanded?).to be_falsey
        end
      end

      context 'when first and last lines are changed' do
        it 'returns false' do
          content = (2..998).to_a
          content.prepend('a')
          content.append('z')
          content = content.join("\n")

          diff_file = update_file('file.md', content)

          expect(diff_file.fully_expanded?).to be_falsey
        end
      end

      context 'when every line are changed' do
        it 'returns true' do
          diff_file = update_file('file.md', "hi\n" * 999)

          expect(diff_file.fully_expanded?).to be_truthy
        end
      end

      context 'when all contents are cleared' do
        it 'returns true' do
          diff_file = update_file('file.md', "")

          expect(diff_file.fully_expanded?).to be_truthy
        end
      end

      context 'when file is binary' do
        it 'returns true' do
          diff_file = update_file('file.md', (1..998).to_a.join("\n"))
          allow(diff_file).to receive(:binary?).and_return(true)

          expect(diff_file.fully_expanded?).to be_truthy
        end
      end
    end
  end
end