From d7950bbf09ca8b3ccdbb94f86b6b6832203ca70c Mon Sep 17 00:00:00 2001 From: Mario de la Ossa Date: Fri, 13 Jul 2018 11:04:29 -0600 Subject: [PATCH] SubmoduleHelper - Project is not always available as an instance variable --- app/helpers/submodule_helper.rb | 8 ++++---- spec/helpers/submodule_helper_spec.rb | 21 ++++++++------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/app/helpers/submodule_helper.rb b/app/helpers/submodule_helper.rb index 9151543dfdc..ebfde993456 100644 --- a/app/helpers/submodule_helper.rb +++ b/app/helpers/submodule_helper.rb @@ -8,7 +8,7 @@ module SubmoduleHelper url = repository.submodule_url_for(ref, submodule_item.path) if url == '.' || url == './' - url = File.join(Gitlab.config.gitlab.url, @project.full_path) + url = File.join(Gitlab.config.gitlab.url, repository.project.full_path) end if url =~ %r{([^/:]+)/([^/]+(?:\.git)?)\Z} @@ -31,7 +31,7 @@ module SubmoduleHelper [namespace_project_path(namespace, project), namespace_project_tree_path(namespace, project, submodule_item.id)] elsif relative_self_url?(url) - relative_self_links(url, submodule_item.id) + relative_self_links(url, submodule_item.id, repository.project) elsif github_dot_com_url?(url) standard_links('github.com', namespace, project, submodule_item.id) elsif gitlab_dot_com_url?(url) @@ -73,7 +73,7 @@ module SubmoduleHelper [base, [base, '/tree/', commit].join('')] end - def relative_self_links(url, commit) + def relative_self_links(url, commit, project) url.rstrip! # Map relative links to a namespace and project # For example: @@ -85,7 +85,7 @@ module SubmoduleHelper namespace = components.pop.gsub(/^\.\.$/, '') if namespace.empty? - namespace = @project.namespace.full_path + namespace = project.namespace.full_path end begin diff --git a/spec/helpers/submodule_helper_spec.rb b/spec/helpers/submodule_helper_spec.rb index 5a2e4b34069..a64f8a11ef2 100644 --- a/spec/helpers/submodule_helper_spec.rb +++ b/spec/helpers/submodule_helper_spec.rb @@ -92,11 +92,10 @@ describe SubmoduleHelper do context 'in-repository submodule' do let(:group) { create(:group, name: "Master Project", path: "master-project") } let(:project) { create(:project, group: group) } - before do - self.instance_variable_set(:@project, project) - end it 'in-repository' do + allow(repo).to receive(:project).and_return(project) + stub_url('./') expect(submodule_links(submodule_item)).to eq(["/master-project/#{project.path}", "/master-project/#{project.path}/tree/hash"]) end @@ -167,32 +166,28 @@ describe SubmoduleHelper do let(:project) { create(:project, group: group) } let(:commit_id) { sample_commit[:id] } - before do - self.instance_variable_set(:@project, project) - end - it 'one level down' do - result = relative_self_links('../test.git', commit_id) + result = relative_self_links('../test.git', commit_id, project) expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"]) end it 'with trailing whitespace' do - result = relative_self_links('../test.git ', commit_id) + result = relative_self_links('../test.git ', commit_id, project) 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) + result = relative_self_links('../../test.git', commit_id, project) 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) + result = relative_self_links('../foobar/test.git', commit_id, project) 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) + result = relative_self_links('../foobar/baz/test.git', commit_id, project) expect(result).to eq(["/baz/test", "/baz/test/tree/#{commit_id}"]) end @@ -201,7 +196,7 @@ describe SubmoduleHelper do let(:project) { create(:project, namespace: user.namespace) } it 'one level down with personal project' do - result = relative_self_links('../test.git', commit_id) + result = relative_self_links('../test.git', commit_id, project) expect(result).to eq(["/#{user.username}/test", "/#{user.username}/test/tree/#{commit_id}"]) end end