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

compare_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: 5ee5e18d5afdd107e8ef0ec5a644f183a5922a45 (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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::Git::Compare, feature_category: :source_code_management do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:repository) { project.repository.raw }

  let(:compare) { described_class.new(repository, base, head, straight: false) }
  let(:compare_straight) { described_class.new(repository, base, head, straight: true) }
  let(:base) { SeedRepo::BigCommit::ID }
  let(:head) { SeedRepo::Commit::ID }

  describe '#commits' do
    subject do
      compare.commits.map(&:id)
    end

    it 'has 8 elements' do
      expect(subject.size).to eq(8)
    end

    it { is_expected.to include(SeedRepo::Commit::PARENT_ID) }
    it { is_expected.not_to include(SeedRepo::BigCommit::PARENT_ID) }

    context 'non-existing base ref' do
      let(:compare) { described_class.new(repository, 'no-such-branch', SeedRepo::Commit::ID) }

      it { is_expected.to be_empty }
    end

    context 'non-existing head ref' do
      let(:compare) { described_class.new(repository, SeedRepo::BigCommit::ID, '1234567890') }

      it { is_expected.to be_empty }
    end

    context 'base ref is equal to head ref' do
      let(:compare) { described_class.new(repository, SeedRepo::BigCommit::ID, SeedRepo::BigCommit::ID) }

      it { is_expected.to be_empty }
    end

    context 'providing nil as base ref or head ref' do
      let(:compare) { described_class.new(repository, nil, nil) }

      it { is_expected.to be_empty }
    end
  end

  describe '#diffs' do
    subject do
      compare.diffs.map(&:new_path)
    end

    it 'has 10 elements' do
      expect(subject.size).to eq(10)
    end

    it { is_expected.to include('files/ruby/popen.rb') }
    it { is_expected.not_to include('LICENSE') }

    context 'non-existing base ref' do
      let(:compare) { described_class.new(repository, 'no-such-branch', SeedRepo::Commit::ID) }

      it { is_expected.to be_empty }
    end

    context 'non-existing head ref' do
      let(:compare) { described_class.new(repository, SeedRepo::BigCommit::ID, '1234567890') }

      it { is_expected.to be_empty }
    end
  end

  describe '#same' do
    subject do
      compare.same
    end

    it { is_expected.to eq(false) }

    context 'base ref is equal to head ref' do
      let(:compare) { described_class.new(repository, SeedRepo::BigCommit::ID, SeedRepo::BigCommit::ID) }

      it { is_expected.to eq(true) }
    end
  end

  describe '#commits', 'straight compare' do
    subject do
      compare_straight.commits.map(&:id)
    end

    it 'has 8 elements' do
      expect(subject.size).to eq(8)
    end

    it { is_expected.to include(SeedRepo::Commit::PARENT_ID) }
    it { is_expected.not_to include(SeedRepo::BigCommit::PARENT_ID) }
  end

  describe '#diffs', 'straight compare' do
    subject do
      compare_straight.diffs.map(&:new_path)
    end

    it 'has 10 elements' do
      expect(subject.size).to eq(10)
    end

    it { is_expected.to include('files/ruby/popen.rb') }
    it { is_expected.not_to include('LICENSE') }
  end

  describe '#generated_files' do
    subject(:generated_files) { compare.generated_files }

    context 'with a detected generated file' do
      let_it_be(:project) { create(:project, :repository) }
      let_it_be(:repository) { project.repository.raw }
      let_it_be(:branch) { 'generated-file-test' }
      let_it_be(:base) do
        project
          .repository
          .create_file(
            project.creator,
            '.gitattributes',
            "*.txt gitlab-generated\n",
            branch_name: branch,
            message: 'Add .gitattributes file')
      end

      let_it_be(:head) do
        project
          .repository
          .create_file(
            project.creator,
            'file1.rb',
            "some content\n",
            branch_name: branch,
            message: 'Add file1')
        project
          .repository
          .create_file(
            project.creator,
            'file1.txt',
            "some content\n",
            branch_name: branch,
            message: 'Add file2')
      end

      it 'sets the diff as generated' do
        expect(generated_files).to eq Set.new(['file1.txt'])
      end

      context 'when base is nil' do
        let(:base) { nil }

        it 'does not try to detect generated files' do
          expect(repository).not_to receive(:detect_generated_files)
          expect(repository).not_to receive(:find_changed_paths)
          expect(generated_files).to eq Set.new
        end
      end

      context 'when head is nil' do
        let(:head) { nil }

        it 'does not try to detect generated files' do
          expect(repository).not_to receive(:detect_generated_files)
          expect(repository).not_to receive(:find_changed_paths)
          expect(generated_files).to eq Set.new
        end
      end
    end

    context 'with updated .gitattributes in the HEAD' do
      let_it_be(:project) { create(:project, :repository) }
      let_it_be(:repository) { project.repository.raw }
      let_it_be(:branch) { 'generated-file-test' }
      let_it_be(:head) do
        project
          .repository
          .create_file(
            project.creator,
            '.gitattributes',
            "*.txt gitlab-generated\n",
            branch_name: branch,
            message: 'Add .gitattributes file')
        project
          .repository
          .create_file(
            project.creator,
            'file1.rb',
            "some content\n",
            branch_name: branch,
            message: 'Add file1')
        project
          .repository
          .create_file(
            project.creator,
            'file1.txt',
            "some content\n",
            branch_name: branch,
            message: 'Add file2')
      end

      it 'does not set any files as generated' do
        expect(generated_files).to eq Set.new
      end
    end
  end
end