summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/rev_list_spec.rb
blob: f76aca2910759d6797ad0e411e78c4273458abde (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
require 'spec_helper'

describe Gitlab::Git::RevList, lib: true do
  let(:project) { create(:project) }

  context "validations" do
    context "GIT_OBJECT_DIRECTORY" do
      it "accepts values starting with the project repo path" do
        env = { "GIT_OBJECT_DIRECTORY" => "#{project.repository.path_to_repo}/objects" }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).to be_valid
      end

      it "rejects values starting not with the project repo path" do
        env = { "GIT_OBJECT_DIRECTORY" => "/some/other/path" }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).not_to be_valid
      end

      it "rejects values containing the project repo path but not starting with it" do
        env = { "GIT_OBJECT_DIRECTORY" => "/some/other/path/#{project.repository.path_to_repo}" }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).not_to be_valid
      end
    end

    context "GIT_ALTERNATE_OBJECT_DIRECTORIES" do
      it "accepts values starting with the project repo path" do
        env = { "GIT_ALTERNATE_OBJECT_DIRECTORIES" => project.repository.path_to_repo }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).to be_valid
      end

      it "rejects values starting not with the project repo path" do
        env = { "GIT_ALTERNATE_OBJECT_DIRECTORIES" => "/some/other/path" }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).not_to be_valid
      end

      it "rejects values containing the project repo path but not starting with it" do
        env = { "GIT_ALTERNATE_OBJECT_DIRECTORIES" => "/some/other/path/#{project.repository.path_to_repo}" }
        rev_list = described_class.new('oldrev', 'newrev', project: project, env: env)

        expect(rev_list).not_to be_valid
      end
    end
  end

  context "#execute" do
    let(:env) { { "GIT_OBJECT_DIRECTORY" => project.repository.path_to_repo } }
    let(:rev_list) { Gitlab::Git::RevList.new('oldrev', 'newrev', project: project, env: env) }

    it "calls out to `popen` without environment variables if the record is invalid" do
      allow(rev_list).to receive(:valid?).and_return(false)
      allow(Open3).to receive(:popen3)

      rev_list.execute

      expect(Open3).to have_received(:popen3).with(hash_excluding(env), any_args)
    end

    it "calls out to `popen` with environment variables if the record is valid" do
      allow(rev_list).to receive(:valid?).and_return(true)
      allow(Open3).to receive(:popen3)

      rev_list.execute

      expect(Open3).to have_received(:popen3).with(hash_including(env), any_args)
    end
  end
end