diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 22:37:37 +0200 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 22:37:37 +0200 |
commit | 6a8133b943a8e06571f5497bea0f36c236b2bf90 (patch) | |
tree | 6541b565978f9571cbc41c42a8cfac34ac86c54f | |
parent | 3af363ecb2cdc764bb9034e81c7159f65c19cb44 (diff) | |
download | gitlab-ce-6a8133b943a8e06571f5497bea0f36c236b2bf90.tar.gz |
Stub http request on specs intead of mocking HTTParty
CE mirror of bb2a9fde8e6a4d1df13638fe336f641b9c72ef59
-rw-r--r-- | lib/gitlab/ci/external/file/remote.rb | 13 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external/processor_spec.rb | 17 | ||||
-rw-r--r-- | spec/models/blob_viewer/gitlab_ci_yml_spec.rb | 10 |
4 files changed, 32 insertions, 24 deletions
diff --git a/lib/gitlab/ci/external/file/remote.rb b/lib/gitlab/ci/external/file/remote.rb index aa089860525..e728e3de77d 100644 --- a/lib/gitlab/ci/external/file/remote.rb +++ b/lib/gitlab/ci/external/file/remote.rb @@ -3,6 +3,7 @@ module Gitlab module External module File class Remote + include Gitlab::Utils::StrongMemoize attr_reader :location def initialize(location, opts = {}) @@ -16,11 +17,13 @@ module Gitlab def content return @content if defined?(@content) - @content ||= begin - HTTParty.get(location) - rescue HTTParty::Error, Timeout::Error - false - end + @content = strong_memoize(:content) do + begin + HTTParty.get(location) + rescue HTTParty::Error, Timeout::Error + false + end + end end end end diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 1ab5ccb63d0..83a30de199f 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -127,7 +127,7 @@ describe Gitlab::Ci::Config do end context "when gitlab_ci_yml has valid 'include' defined" do - let(:http_file_content) do + let(:remote_file_content) do <<~HEREDOC variables: AUTO_DEVOPS_DOMAIN: domain.example.com @@ -138,11 +138,12 @@ describe Gitlab::Ci::Config do HEREDOC end let(:local_file_content) { File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") } - let(:yml) do + let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } + let(:gitlab_ci_yml) do <<-EOS include: - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml - - https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml + - #{remote_location} image: ruby:2.2 EOS @@ -150,7 +151,7 @@ describe Gitlab::Ci::Config do before do allow_any_instance_of(::Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content) - allow(HTTParty).to receive(:get).and_return(http_file_content) + WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content) end it 'should return a composed hash' do @@ -194,23 +195,24 @@ describe Gitlab::Ci::Config do end context "when both external files and gitlab_ci.yml defined the same key" do + let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:gitlab_ci_yml) do <<~HEREDOC include: - - https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.gitlab_ci_yml + - #{remote_location} image: ruby:2.2 HEREDOC end - let(:http_file_content) do + let(:remote_file_content) do <<~HEREDOC image: php:5-fpm-alpine HEREDOC end it 'should take precedence' do - allow(HTTParty).to receive(:get).and_return(http_file_content) + WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content) expect(config.to_hash).to eq({ image: 'ruby:2.2' }) end end diff --git a/spec/lib/gitlab/ci/external/processor_spec.rb b/spec/lib/gitlab/ci/external/processor_spec.rb index ca28558bad3..588baf92bee 100644 --- a/spec/lib/gitlab/ci/external/processor_spec.rb +++ b/spec/lib/gitlab/ci/external/processor_spec.rb @@ -36,8 +36,8 @@ describe Gitlab::Ci::External::Processor do end context 'with a valid remote external file is defined' do - let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } - let(:values) { { include: remote_url, image: 'ruby:2.2' } } + let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } + let(:values) { { include: remote_file, image: 'ruby:2.2' } } let(:external_file_content) do <<-HEREDOC before_script: @@ -58,7 +58,7 @@ describe Gitlab::Ci::External::Processor do end before do - WebMock.stub_request(:get, remote_url).to_return(body: external_file_content) + WebMock.stub_request(:get, remote_file).to_return(body: external_file_content) end it 'should append the file to the values' do @@ -99,11 +99,11 @@ describe Gitlab::Ci::External::Processor do end context 'with multiple external files are defined' do - let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } + let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:external_files) do [ "/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml", - remote_url + remote_file ] end let(:values) do @@ -125,7 +125,7 @@ describe Gitlab::Ci::External::Processor do before do local_file_content = File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content) - WebMock.stub_request(:get, remote_url).to_return(body: remote_file_content) + WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content) end it 'should append the files to the values' do @@ -152,9 +152,10 @@ describe Gitlab::Ci::External::Processor do end context "when both external files and values defined the same key" do + let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:values) do { - include: 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', + include: remote_file, image: 'ruby:2.2' } end @@ -166,7 +167,7 @@ describe Gitlab::Ci::External::Processor do end it 'should take precedence' do - allow(HTTParty).to receive(:get).and_return(remote_file_content) + WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content) expect(processor.perform[:image]).to eq('ruby:2.2') end end diff --git a/spec/models/blob_viewer/gitlab_ci_yml_spec.rb b/spec/models/blob_viewer/gitlab_ci_yml_spec.rb index 4c8a5aae83a..48345b192d1 100644 --- a/spec/models/blob_viewer/gitlab_ci_yml_spec.rb +++ b/spec/models/blob_viewer/gitlab_ci_yml_spec.rb @@ -2,22 +2,24 @@ require 'spec_helper' describe BlobViewer::GitlabCiYml do include FakeBlobHelpers + include RepoHelpers - let(:project) { build_stubbed(:project) } + let(:project) { build_stubbed(:project, :repository) } let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) } let(:blob) { fake_blob(path: '.gitlab-ci.yml', data: data) } + let(:sha) { sample_commit.id } subject { described_class.new(blob) } describe '#validation_message' do it 'calls prepare! on the viewer' do expect(subject).to receive(:prepare!) - subject.validation_message(project, project.default_branch) + subject.validation_message(project, sha) end context 'when the configuration is valid' do it 'returns nil' do - expect(subject.validation_message(project, project.default_branch)).to be_nil + expect(subject.validation_message(project, sha)).to be_nil end end @@ -25,7 +27,7 @@ describe BlobViewer::GitlabCiYml do let(:data) { 'oof' } it 'returns the error message' do - expect(subject.validation_message(project, project.default_branch)).to eq('Invalid configuration format') + expect(subject.validation_message(project, sha)).to eq('Invalid configuration format') end end end |