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

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

require "spec_helper"

RSpec.describe Gitlab::Git::Tree, :seed_helper do
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') }

  shared_examples :repo do
    subject(:tree) { Gitlab::Git::Tree.where(repository, sha, path, recursive, pagination_params) }

    let(:sha) { SeedRepo::Commit::ID }
    let(:path) { nil }
    let(:recursive) { false }
    let(:pagination_params) { nil }

    let(:entries) { tree.first }
    let(:cursor) { tree.second }

    it { expect(entries).to be_kind_of Array }
    it { expect(entries.empty?).to be_falsey }
    it { expect(entries.count(&:dir?)).to eq(2) }
    it { expect(entries.count(&:file?)).to eq(10) }
    it { expect(entries.count(&:submodule?)).to eq(2) }
    it { expect(cursor&.next_cursor).to be_blank }

    context 'with an invalid ref' do
      let(:sha) { 'foobar-does-not-exist' }

      it { expect(entries).to eq([]) }
      it { expect(cursor).to be_nil }
    end

    context 'when path is provided' do
      let(:path) { 'files' }
      let(:recursive) { true }

      it 'returns a list of tree objects' do
        expect(entries.map(&:path)).to include('files/html',
                                               'files/markdown/ruby-style-guide.md')
        expect(entries.count).to be >= 10
        expect(entries).to all(be_a(Gitlab::Git::Tree))
      end
    end

    describe '#dir?' do
      let(:dir) { entries.find(&:dir?) }

      it { expect(dir).to be_kind_of Gitlab::Git::Tree }
      it { expect(dir.id).to eq('3c122d2b7830eca25235131070602575cf8b41a1') }
      it { expect(dir.commit_id).to eq(SeedRepo::Commit::ID) }
      it { expect(dir.name).to eq('encoding') }
      it { expect(dir.path).to eq('encoding') }
      it { expect(dir.mode).to eq('40000') }
      it { expect(dir.flat_path).to eq('encoding') }

      context :subdir do
        # rubocop: disable Rails/FindBy
        # This is not ActiveRecord where..first
        let(:path) { 'files' }
        let(:subdir) { entries.first }
        # rubocop: enable Rails/FindBy

        it { expect(subdir).to be_kind_of Gitlab::Git::Tree }
        it { expect(subdir.id).to eq('a1e8f8d745cc87e3a9248358d9352bb7f9a0aeba') }
        it { expect(subdir.commit_id).to eq(SeedRepo::Commit::ID) }
        it { expect(subdir.name).to eq('html') }
        it { expect(subdir.path).to eq('files/html') }
        it { expect(subdir.flat_path).to eq('files/html') }
      end

      context :subdir_file do
        # rubocop: disable Rails/FindBy
        # This is not ActiveRecord where..first
        let(:path) { 'files/ruby' }
        let(:subdir_file) { entries.first }
        # rubocop: enable Rails/FindBy

        it { expect(subdir_file).to be_kind_of Gitlab::Git::Tree }
        it { expect(subdir_file.id).to eq('7e3e39ebb9b2bf433b4ad17313770fbe4051649c') }
        it { expect(subdir_file.commit_id).to eq(SeedRepo::Commit::ID) }
        it { expect(subdir_file.name).to eq('popen.rb') }
        it { expect(subdir_file.path).to eq('files/ruby/popen.rb') }
        it { expect(subdir_file.flat_path).to eq('files/ruby/popen.rb') }
      end

      context :flat_path do
        let(:filename) { 'files/flat/path/correct/content.txt' }
        let(:sha) { create_file(filename) }
        let(:path) { 'files/flat' }
        # rubocop: disable Rails/FindBy
        # This is not ActiveRecord where..first
        let(:subdir_file) { entries.first }
        # rubocop: enable Rails/FindBy
        let(:repository_rugged) { Rugged::Repository.new(File.join(SEED_STORAGE_PATH, TEST_REPO_PATH)) }

        it { expect(subdir_file.flat_path).to eq('files/flat/path/correct') }
      end

      def create_file(path)
        oid = repository_rugged.write('test', :blob)
        index = repository_rugged.index
        index.add(path: filename, oid: oid, mode: 0100644)

        options = commit_options(
          repository_rugged,
          index,
          repository_rugged.head.target,
          'HEAD',
          'Add new file')

        Rugged::Commit.create(repository_rugged, options)
      end

      # Build the options hash that's passed to Rugged::Commit#create
      def commit_options(repo, index, target, ref, message)
        options = {}
        options[:tree] = index.write_tree(repo)
        options[:author] = {
          email: "test@example.com",
          name: "Test Author",
          time: Time.gm(2014, "mar", 3, 20, 15, 1)
        }
        options[:committer] = {
          email: "test@example.com",
          name: "Test Author",
          time: Time.gm(2014, "mar", 3, 20, 15, 1)
        }
        options[:message] ||= message
        options[:parents] = repo.empty? ? [] : [target].compact
        options[:update_ref] = ref

        options
      end
    end

    describe '#file?' do
      let(:file) { entries.find(&:file?) }

      it { expect(file).to be_kind_of Gitlab::Git::Tree }
      it { expect(file.id).to eq('dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82') }
      it { expect(file.commit_id).to eq(SeedRepo::Commit::ID) }
      it { expect(file.name).to eq('.gitignore') }
    end

    describe '#readme?' do
      let(:file) { entries.find(&:readme?) }

      it { expect(file).to be_kind_of Gitlab::Git::Tree }
      it { expect(file.name).to eq('README.md') }
    end

    describe '#contributing?' do
      let(:file) { entries.find(&:contributing?) }

      it { expect(file).to be_kind_of Gitlab::Git::Tree }
      it { expect(file.name).to eq('CONTRIBUTING.md') }
    end

    describe '#submodule?' do
      let(:submodule) { entries.find(&:submodule?) }

      it { expect(submodule).to be_kind_of Gitlab::Git::Tree }
      it { expect(submodule.id).to eq('79bceae69cb5750d6567b223597999bfa91cb3b9') }
      it { expect(submodule.commit_id).to eq('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
      it { expect(submodule.name).to eq('gitlab-shell') }
    end
  end

  describe '.where with Gitaly enabled' do
    it_behaves_like :repo do
      context 'with pagination parameters' do
        let(:pagination_params) { { limit: 3, page_token: nil } }

        it 'returns paginated list of tree objects' do
          expect(entries.count).to eq(3)
          expect(cursor.next_cursor).to be_present
        end
      end
    end
  end

  describe '.where with Rugged enabled', :enable_rugged do
    it 'calls out to the Rugged implementation' do
      allow_next_instance_of(Rugged) do |instance|
        allow(instance).to receive(:lookup).with(SeedRepo::Commit::ID)
      end

      described_class.where(repository, SeedRepo::Commit::ID, 'files', false)
    end

    it_behaves_like :repo do
      describe 'Pagination' do
        context 'with restrictive limit' do
          let(:pagination_params) { { limit: 3, page_token: nil } }

          it 'returns limited paginated list of tree objects' do
            expect(entries.count).to eq(3)
            expect(cursor.next_cursor).to be_present
          end
        end

        context 'when limit is equal to number of entries' do
          let(:entries_count) { entries.count }

          it 'returns all entries without a cursor' do
            result, cursor = Gitlab::Git::Tree.where(repository, sha, path, recursive, { limit: entries_count, page_token: nil })

            expect(cursor).to be_nil
            expect(result.entries.count).to eq(entries_count)
          end
        end

        context 'when limit is 0' do
          let(:pagination_params) { { limit: 0, page_token: nil } }

          it 'returns empty result' do
            expect(entries).to eq([])
            expect(cursor).to be_nil
          end
        end

        context 'when limit is missing' do
          let(:pagination_params) { { limit: nil, page_token: nil } }

          it 'returns empty result' do
            expect(entries).to eq([])
            expect(cursor).to be_nil
          end
        end

        context 'when limit is negative' do
          let(:entries_count) { entries.count }

          it 'returns all entries' do
            result, cursor = Gitlab::Git::Tree.where(repository, sha, path, recursive, { limit: -1, page_token: nil })

            expect(result.count).to eq(entries_count)
            expect(cursor).to be_nil
          end

          context 'when token is provided' do
            let(:pagination_params) { { limit: 1000, page_token: nil } }
            let(:token) { entries.second.id }

            it 'returns all entries after token' do
              result, cursor = Gitlab::Git::Tree.where(repository, sha, path, recursive, { limit: -1, page_token: token })

              expect(result.count).to eq(entries.count - 2)
              expect(cursor).to be_nil
            end
          end
        end

        context 'when token does not exist' do
          let(:pagination_params) { { limit: 5, page_token: 'aabbccdd' } }

          it 'raises a command error' do
            expect { entries }.to raise_error(Gitlab::Git::CommandError, 'could not find starting OID: aabbccdd')
          end
        end

        context 'when limit is bigger than number of entries' do
          let(:pagination_params) { { limit: 1000, page_token: nil } }

          it 'returns only available entries' do
            expect(entries.count).to be < 20
            expect(cursor).to be_nil
          end
        end

        it 'returns all tree entries in specific order during cursor pagination' do
          collected_entries = []
          token = nil

          expected_entries = entries

          loop do
            result, cursor = Gitlab::Git::Tree.where(repository, sha, path, recursive, { limit: 5, page_token: token })

            collected_entries += result.entries
            token = cursor&.next_cursor

            break if token.blank?
          end

          expect(collected_entries.map(&:path)).to match_array(expected_entries.map(&:path))

          expected_order = [
            collected_entries.select(&:dir?).map(&:path),
            collected_entries.select(&:file?).map(&:path),
            collected_entries.select(&:submodule?).map(&:path)
          ].flatten

          expect(collected_entries.map(&:path)).to eq(expected_order)
        end
      end
    end
  end
end