BigW Consortium Gitlab

submodule_helper_spec.rb 7.48 KB
Newer Older
1 2 3
require 'spec_helper'

describe SubmoduleHelper do
4 5
  include RepoHelpers

6
  describe 'submodule links' do
7
    let(:submodule_item) { double(id: 'hash', path: 'rack') }
8
    let(:config) { Gitlab.config.gitlab }
9 10 11 12 13
    let(:repo) { double() }

    before do
      self.instance_variable_set(:@repository, repo)
    end
14 15 16

    context 'submodule on self' do
      before do
17
        allow(Gitlab.config.gitlab).to receive(:protocol).and_return('http') # set this just to be sure
18 19 20
      end

      it 'should detect ssh on standard port' do
21 22
        allow(Gitlab.config.gitlab_shell).to receive(:ssh_port).and_return(22) # set this just to be sure
        allow(Gitlab.config.gitlab_shell).to receive(:ssh_path_prefix).and_return(Settings.send(:build_gitlab_shell_ssh_path_prefix))
23
        stub_url([ config.user, '@', config.host, ':gitlab-org/gitlab-ce.git' ].join(''))
Vinnie Okada committed
24
        expect(submodule_links(submodule_item)).to eq([ namespace_project_path('gitlab-org', 'gitlab-ce'), namespace_project_tree_path('gitlab-org', 'gitlab-ce', 'hash') ])
25
      end
26

27
      it 'should detect ssh on non-standard port' do
28 29
        allow(Gitlab.config.gitlab_shell).to receive(:ssh_port).and_return(2222)
        allow(Gitlab.config.gitlab_shell).to receive(:ssh_path_prefix).and_return(Settings.send(:build_gitlab_shell_ssh_path_prefix))
30
        stub_url([ 'ssh://', config.user, '@', config.host, ':2222/gitlab-org/gitlab-ce.git' ].join(''))
Vinnie Okada committed
31
        expect(submodule_links(submodule_item)).to eq([ namespace_project_path('gitlab-org', 'gitlab-ce'), namespace_project_tree_path('gitlab-org', 'gitlab-ce', 'hash') ])
32 33 34
      end

      it 'should detect http on standard port' do
35 36
        allow(Gitlab.config.gitlab).to receive(:port).and_return(80)
        allow(Gitlab.config.gitlab).to receive(:url).and_return(Settings.send(:build_gitlab_url))
37
        stub_url([ 'http://', config.host, '/gitlab-org/gitlab-ce.git' ].join(''))
Vinnie Okada committed
38
        expect(submodule_links(submodule_item)).to eq([ namespace_project_path('gitlab-org', 'gitlab-ce'), namespace_project_tree_path('gitlab-org', 'gitlab-ce', 'hash') ])
39 40 41
      end

      it 'should detect http on non-standard port' do
42 43
        allow(Gitlab.config.gitlab).to receive(:port).and_return(3000)
        allow(Gitlab.config.gitlab).to receive(:url).and_return(Settings.send(:build_gitlab_url))
44
        stub_url([ 'http://', config.host, ':3000/gitlab-org/gitlab-ce.git' ].join(''))
Vinnie Okada committed
45
        expect(submodule_links(submodule_item)).to eq([ namespace_project_path('gitlab-org', 'gitlab-ce'), namespace_project_tree_path('gitlab-org', 'gitlab-ce', 'hash') ])
46 47 48
      end

      it 'should work with relative_url_root' do
49 50 51
        allow(Gitlab.config.gitlab).to receive(:port).and_return(80) # set this just to be sure
        allow(Gitlab.config.gitlab).to receive(:relative_url_root).and_return('/gitlab/root')
        allow(Gitlab.config.gitlab).to receive(:url).and_return(Settings.send(:build_gitlab_url))
52
        stub_url([ 'http://', config.host, '/gitlab/root/gitlab-org/gitlab-ce.git' ].join(''))
Vinnie Okada committed
53
        expect(submodule_links(submodule_item)).to eq([ namespace_project_path('gitlab-org', 'gitlab-ce'), namespace_project_tree_path('gitlab-org', 'gitlab-ce', 'hash') ])
54 55
      end
    end
56

57 58
    context 'submodule on github.com' do
      it 'should detect ssh' do
59
        stub_url('git@github.com:gitlab-org/gitlab-ce.git')
60
        expect(submodule_links(submodule_item)).to eq([ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ])
61
      end
62

63
      it 'should detect http' do
64
        stub_url('http://github.com/gitlab-org/gitlab-ce.git')
65
        expect(submodule_links(submodule_item)).to eq([ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ])
66
      end
67

68
      it 'should detect https' do
69
        stub_url('https://github.com/gitlab-org/gitlab-ce.git')
70
        expect(submodule_links(submodule_item)).to eq([ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ])
71
      end
72

73
      it 'should return original with non-standard url' do
74
        stub_url('http://github.com/gitlab-org/gitlab-ce')
75
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
76

77
        stub_url('http://github.com/another/gitlab-org/gitlab-ce.git')
78
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
79 80
      end
    end
81

82 83
    context 'submodule on gitlab.com' do
      it 'should detect ssh' do
84
        stub_url('git@gitlab.com:gitlab-org/gitlab-ce.git')
85
        expect(submodule_links(submodule_item)).to eq([ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ])
86
      end
87

88
      it 'should detect http' do
89
        stub_url('http://gitlab.com/gitlab-org/gitlab-ce.git')
90
        expect(submodule_links(submodule_item)).to eq([ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ])
91
      end
92

93
      it 'should detect https' do
94
        stub_url('https://gitlab.com/gitlab-org/gitlab-ce.git')
95
        expect(submodule_links(submodule_item)).to eq([ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ])
96
      end
97

98
      it 'should return original with non-standard url' do
99
        stub_url('http://gitlab.com/gitlab-org/gitlab-ce')
100
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
101

102
        stub_url('http://gitlab.com/another/gitlab-org/gitlab-ce.git')
103
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
104 105
      end
    end
106

107 108
    context 'submodule on unsupported' do
      it 'should return original' do
109
        stub_url('http://mygitserver.com/gitlab-org/gitlab-ce')
110
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
111

112
        stub_url('http://mygitserver.com/gitlab-org/gitlab-ce.git')
113
        expect(submodule_links(submodule_item)).to eq([ repo.submodule_url_for, nil ])
114 115
      end
    end
116 117

    context 'submodules with relative links' do
118
      let(:group) { create(:group, name: "Master Project", path: "master-project") }
119
      let(:project) { create(:project, group: group) }
120
      let(:commit_id) { sample_commit[:id] }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

      before do
        self.instance_variable_set(:@project, project)
      end

      it 'one level down' do
        result = relative_self_links('../test.git', commit_id)
        expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"])
      end

      it 'two levels down' do
        result = relative_self_links('../../test.git', commit_id)
        expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"])
      end

      it 'one level down with namespace and repo' do
        result = relative_self_links('../foobar/test.git', commit_id)
        expect(result).to eq(["/foobar/test", "/foobar/test/tree/#{commit_id}"])
      end

      it 'two levels down with namespace and repo' do
        result = relative_self_links('../foobar/baz/test.git', commit_id)
        expect(result).to eq(["/baz/test", "/baz/test/tree/#{commit_id}"])
      end
145 146 147 148 149 150 151 152 153 154

      context 'personal project' do
        let(:user) { create(:user) }
        let(:project) { create(:project, namespace: user.namespace) }

        it 'one level down with personal project' do
          result = relative_self_links('../test.git', commit_id)
          expect(result).to eq(["/#{user.username}/test", "/#{user.username}/test/tree/#{commit_id}"])
        end
      end
155
    end
156
  end
157 158

  def stub_url(url)
159
    allow(repo).to receive(:submodule_url_for).and_return(url)
160
  end
161
end