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

find_changes_spec.rb « tooling « lib « tooling « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fef29ad3f2c3348f4afa13807af1702131c59e0f (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
# frozen_string_literal: true

require_relative '../../../../tooling/lib/tooling/find_changes'
require 'gitlab/rspec/all'
require 'json'
require 'tempfile'

RSpec.describe Tooling::FindChanges, feature_category: :tooling do
  include StubENV

  attr_accessor :changed_files_file, :predictive_tests_file, :frontend_fixtures_mapping_file

  let(:instance) do
    described_class.new(
      changed_files_pathname: changed_files_pathname,
      predictive_tests_pathname: predictive_tests_pathname,
      frontend_fixtures_mapping_pathname: frontend_fixtures_mapping_pathname,
      from: from)
  end

  let(:changed_files_pathname)             { changed_files_file.path }
  let(:predictive_tests_pathname)          { predictive_tests_file.path }
  let(:frontend_fixtures_mapping_pathname) { frontend_fixtures_mapping_file.path }
  let(:from)                               { :api }
  let(:gitlab_client)                      { double('GitLab') } # rubocop:disable RSpec/VerifiedDoubles

  around do |example|
    self.changed_files_file             = Tempfile.new('changed_files_file')
    self.predictive_tests_file          = Tempfile.new('predictive_tests_file')
    self.frontend_fixtures_mapping_file = Tempfile.new('frontend_fixtures_mapping_file')

    # See https://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/
    #     Tempfile.html#class-Tempfile-label-Explicit+close
    begin
      example.run
    ensure
      frontend_fixtures_mapping_file.close
      frontend_fixtures_mapping_file.unlink
      predictive_tests_file.close
      predictive_tests_file.unlink
      changed_files_file.close
      changed_files_file.unlink
    end
  end

  before do
    stub_env(
      'CI_API_V4_URL' => 'gitlab_api_url',
      'CI_MERGE_REQUEST_IID' => '1234',
      'CI_MERGE_REQUEST_PROJECT_PATH' => 'dummy-project',
      'PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE' => 'dummy-token'
    )
  end

  describe '#initialize' do
    context 'when fetching changes from unknown' do
      let(:from) { :unknown }

      it 'raises an ArgumentError' do
        expect { instance }.to raise_error(
          ArgumentError, ":from can only be :api or :changed_files"
        )
      end
    end
  end

  describe '#execute' do
    subject { instance.execute }

    before do
      allow(instance).to receive(:gitlab).and_return(gitlab_client)
    end

    context 'when there is no changed files file' do
      let(:changed_files_pathname) { nil }

      it 'raises an ArgumentError' do
        expect { subject }.to raise_error(
          ArgumentError, "A path to the changed files file must be given as :changed_files_pathname"
        )
      end
    end

    context 'when fetching changes from API' do
      let(:from) { :api }

      it 'calls GitLab API to retrieve the MR diff' do
        expect(gitlab_client).to receive_message_chain(:merge_request_changes, :changes).and_return([])

        subject
      end
    end

    context 'when fetching changes from changed files' do
      let(:from) { :changed_files }

      it 'does not call GitLab API to retrieve the MR diff' do
        expect(gitlab_client).not_to receive(:merge_request_changes)

        subject
      end

      context 'when there are no file changes' do
        it 'writes an empty string to changed files file' do
          expect { subject }.not_to change { File.read(changed_files_pathname) }
        end
      end

      context 'when there are file changes' do
        before do
          File.write(changed_files_pathname, changed_files_file_content)
        end

        let(:changed_files_file_content) { 'first_file_changed second_file_changed' }

        # This is because we don't have frontend fixture mappings: we will just write the same data that we read.
        it 'does not change the changed files file' do
          expect { subject }.not_to change { File.read(changed_files_pathname) }
        end
      end

      context 'when there is no matched tests file' do
        let(:predictive_tests_pathname) { nil }

        it 'does not add frontend fixtures mapping to the changed files file' do
          expect { subject }.not_to change { File.read(changed_files_pathname) }
        end
      end

      context 'when there is no frontend fixture files' do
        let(:frontend_fixtures_mapping_pathname) { nil }

        it 'does not add frontend fixtures mapping to the changed files file' do
          expect { subject }.not_to change { File.read(changed_files_pathname) }
        end
      end

      context 'when the matched tests file and frontend fixture files are provided' do
        before do
          File.write(predictive_tests_pathname, matched_tests)
          File.write(frontend_fixtures_mapping_pathname, frontend_fixtures_mapping_json)
          File.write(changed_files_pathname, changed_files_file_content)
        end

        let(:changed_files_file_content) { '' }

        context 'when there are no mappings for the matched tests' do
          let(:matched_tests) { 'match_spec1 match_spec_2' }
          let(:frontend_fixtures_mapping_json) do
            { other_spec: ['other_mapping'] }.to_json
          end

          it 'does not change the changed files file' do
            expect { subject }.not_to change { File.read(changed_files_pathname) }
          end
        end

        context 'when there are available mappings for the matched tests' do
          let(:matched_tests) { 'match_spec1 match_spec_2' }
          let(:spec_mappings) { %w[spec1_mapping1 spec1_mapping2] }
          let(:frontend_fixtures_mapping_json) do
            { match_spec1: spec_mappings }.to_json
          end

          context 'when the changed files file is initially empty' do
            it 'adds the frontend fixtures mappings to the changed files file' do
              expect { subject }.to change { File.read(changed_files_pathname) }.from('').to(spec_mappings.join(' '))
            end
          end

          context 'when the changed files file is initially not empty' do
            let(:changed_files_file_content) { 'initial_content1 initial_content2' }

            it 'adds the frontend fixtures mappings to the changed files file' do
              expect { subject }.to change { File.read(changed_files_pathname) }
                .from(changed_files_file_content)
                .to("#{changed_files_file_content} #{spec_mappings.join(' ')}")
            end
          end
        end
      end
    end
  end

  describe '#only_allowed_files_changed' do
    subject { instance.only_allowed_files_changed }

    context 'when fetching changes from changed files' do
      let(:from) { :changed_files }

      before do
        File.write(changed_files_pathname, changed_files_file_content)
      end

      context 'when changed files contain only *.js changes' do
        let(:changed_files_file_content) { 'a.js b.js' }

        it 'returns true' do
          expect(subject).to be true
        end
      end

      context 'when changed files contain both *.vue and *.js changes' do
        let(:changed_files_file_content) { 'a.js b.vue' }

        it 'returns true' do
          expect(subject).to be true
        end
      end

      context 'when changed files contain not allowed changes' do
        let(:changed_files_file_content) { 'a.js b.vue c.rb' }

        it 'returns false' do
          expect(subject).to be false
        end
      end
    end

    context 'when fetching changes from API' do
      let(:from) { :api }

      let(:mr_changes_array) { [] }

      before do
        allow(instance).to receive(:gitlab).and_return(gitlab_client)

        # The class from the GitLab gem isn't public, so we cannot use verified doubles for it.
        #
        # rubocop:disable RSpec/VerifiedDoubles
        allow(gitlab_client).to receive(:merge_request_changes)
          .with('dummy-project', '1234')
          .and_return(double(changes: mr_changes_array))
        # rubocop:enable RSpec/VerifiedDoubles
      end

      context 'when a file is passed as an argument' do
        it 'calls GitLab API' do
          expect(gitlab_client).to receive(:merge_request_changes)
          .with('dummy-project', '1234')

          subject
        end
      end

      context 'when there are no file changes' do
        let(:mr_changes_array) { [] }

        it 'returns false' do
          expect(subject).to be false
        end
      end

      context 'when there are changes to files other than JS files' do
        let(:mr_changes_array) do
          [
            {
              "new_path" => "scripts/gitlab_component_helpers.sh",
              "old_path" => "scripts/gitlab_component_helpers.sh"
            },
            {
              "new_path" => "scripts/test.js",
              "old_path" => "scripts/test.js"
            }
          ]
        end

        it 'returns false' do
          expect(subject).to be false
        end
      end

      context 'when there are changes only to JS files' do
        let(:mr_changes_array) do
          [
            {
              "new_path" => "scripts/test.js",
              "old_path" => "scripts/test.js"
            }
          ]
        end

        it 'returns true' do
          expect(subject).to be true
        end
      end
    end
  end
end