File: diff_file_entity_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (165 lines) | stat: -rw-r--r-- 4,595 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.shared_examples 'diff file base entity' do
  it 'exposes essential attributes' do
    expect(subject).to include(
      :content_sha, :submodule, :submodule_link,
      :submodule_tree_url, :old_path_html,
      :new_path_html, :blob, :can_modify_blob,
      :file_hash, :file_path, :old_path, :new_path,
      :viewer, :diff_refs, :stored_externally,
      :external_storage, :renamed_file, :deleted_file,
      :a_mode, :b_mode, :new_file, :file_identifier_hash
    )
  end

  # Converted diff files from GitHub import does not contain blob file
  # and content sha.
  context 'when diff file does not have a blob and content sha' do
    it 'exposes some attributes as nil' do
      allow(diff_file).to receive(:content_sha).and_return(nil)
      allow(diff_file).to receive(:blob).and_return(nil)

      expect(subject[:context_lines_path]).to be_nil
      expect(subject[:view_path]).to be_nil
      expect(subject[:highlighted_diff_lines]).to be_nil
      expect(subject[:can_modify_blob]).to be_nil
    end
  end
end

RSpec.shared_examples 'diff file entity' do
  it_behaves_like 'diff file base entity'

  it 'exposes correct attributes' do
    expect(subject).to include(:added_lines, :removed_lines, :context_lines_path)
  end

  context 'when a viewer' do
    let(:collapsed) { false }
    let(:added_lines) { 1 }
    let(:removed_lines) { 0 }
    let(:highlighted_lines) { nil }

    before do
      allow(diff_file).to receive(:diff_lines_for_serializer)
        .and_return(highlighted_lines)

      allow(diff_file).to receive(:added_lines)
        .and_return(added_lines)

      allow(diff_file).to receive(:removed_lines)
        .and_return(removed_lines)

      allow(diff_file).to receive(:collapsed?)
        .and_return(collapsed)
    end

    it 'matches the schema' do
      expect(subject[:viewer].with_indifferent_access)
        .to match_schema('entities/diff_viewer')
    end

    context 'when it is a whitespace only change' do
      it 'has whitespace_only true' do
        expect(subject[:viewer][:whitespace_only])
          .to eq(true)
      end
    end

    context 'when the highlighted lines arent shown' do
      before do
        allow(diff_file).to receive(:text?)
          .and_return(false)
      end

      it 'has whitespace_only nil' do
        expect(subject[:viewer][:whitespace_only])
          .to eq(nil)
      end
    end

    context 'when it is a new file' do
      let(:added_lines) { 0 }

      it 'has whitespace_only false' do
        expect(subject[:viewer][:whitespace_only])
          .to eq(false)
      end
    end

    context 'when it is a collapsed file' do
      let(:collapsed) { true }

      it 'has whitespace_only false' do
        expect(subject[:viewer][:whitespace_only])
          .to eq(false)
      end
    end
  end

  context 'diff files' do
    context 'when diff_view is parallel' do
      let(:options) { { diff_view: :parallel } }

      it 'contains only the parallel diff lines', :aggregate_failures do
        expect(subject).to include(:parallel_diff_lines)
        expect(subject).not_to include(:highlighted_diff_lines)
      end
    end

    context 'when diff_view is parallel' do
      let(:options) { { diff_view: :inline } }

      it 'contains only the inline diff lines', :aggregate_failures do
        expect(subject).not_to include(:parallel_diff_lines)
        expect(subject).to include(:highlighted_diff_lines)
      end
    end
  end
end

RSpec.shared_examples 'diff file discussion entity' do
  it_behaves_like 'diff file base entity'
end

RSpec.shared_examples 'diff file with conflict_type' do
  describe '#conflict_type' do
    it 'returns nil by default' do
      expect(subject[:conflict_type]).to be_nil
    end

    context 'when there is matching conflict file' do
      let(:renamed_file?) { false }

      let(:options) do
        {
          conflicts: {
            diff_file.new_path => {
              conflict_type: :removed_target_renamed_source,
              conflict_type_when_renamed: :renamed_same_file
            }
          }
        }
      end

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

      it 'returns conflict_type' do
        expect(subject[:conflict_type]).to eq(:removed_target_renamed_source)
      end

      context 'when diff file is renamed' do
        let(:renamed_file?) { true }

        it 'returns conflict_type' do
          expect(subject[:conflict_type]).to eq(:renamed_same_file)
        end
      end
    end
  end
end