summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/submodule_links_spec.rb
blob: e2bbda81780024f2b62f57dc4228de0f55aa9509 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::SubmoduleLinks do
  let(:submodule_item) { double(id: 'hash', path: 'gitlab-foss') }
  let(:repo) { double }
  let(:links) { described_class.new(repo) }

  describe '#for' do
    let(:ref) { 'ref' }

    subject { links.for(submodule_item, ref) }

    context 'when there is no .gitmodules file' do
      before do
        stub_urls(nil)
      end

      it 'returns no links' do
        expect(subject).to be_nil
      end
    end

    context 'when the submodule is unknown' do
      before do
        stub_urls({ 'path' => 'url' })
      end

      it 'returns no links' do
        expect(subject).to be_nil
      end
    end

    context 'when the submodule is known' do
      before do
        gitlab_foss = 'git@gitlab.com:gitlab-org/gitlab-foss.git'

        stub_urls({
          'ref' => { 'gitlab-foss' => gitlab_foss },
          'other_ref' => { 'gitlab-foss' => gitlab_foss },
          'signed-commits' => { 'gitlab-foss' => gitlab_foss },
          'special_ref' => { 'gitlab-foss' => 'git@OTHER.com:gitlab-org/gitlab-foss.git' }
        })
      end

      it 'returns links and caches the by ref' do
        aggregate_failures do
          expect(subject.web).to eq('https://gitlab.com/gitlab-org/gitlab-foss')
          expect(subject.tree).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/tree/hash')
          expect(subject.compare).to be_nil
        end

        cache_store = links.instance_variable_get("@cache_store")

        expect(cache_store[ref]).to eq({ "gitlab-foss" => "git@gitlab.com:gitlab-org/gitlab-foss.git" })
      end

      context 'when ref name contains a dash' do
        let(:ref) { 'signed-commits' }

        it 'returns links' do
          aggregate_failures do
            expect(subject.web).to eq('https://gitlab.com/gitlab-org/gitlab-foss')
            expect(subject.tree).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/tree/hash')
            expect(subject.compare).to be_nil
          end
        end
      end

      context 'and the diff information is available' do
        let(:old_ref) { 'other_ref' }
        let(:diff_file) { double(old_blob: double(id: 'old-hash', path: 'gitlab-foss'), old_content_sha: old_ref) }

        subject { links.for(submodule_item, ref, diff_file) }

        it 'the returned links include the compare link' do
          aggregate_failures do
            expect(subject.web).to eq('https://gitlab.com/gitlab-org/gitlab-foss')
            expect(subject.tree).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/tree/hash')
            expect(subject.compare).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/compare/old-hash...hash')
          end
        end

        context 'but the submodule url has changed' do
          let(:old_ref) { 'special_ref' }

          it 'the returned links do not include the compare link' do
            aggregate_failures do
              expect(subject.web).to eq('https://gitlab.com/gitlab-org/gitlab-foss')
              expect(subject.tree).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/tree/hash')
              expect(subject.compare).to be_nil
            end
          end
        end
      end
    end
  end

  def stub_urls(urls_by_ref)
    allow(repo).to receive(:submodule_urls_for) do |ref|
      urls_by_ref[ref] if urls_by_ref
    end
  end
end