diff --git a/features/update/pull_request.feature b/features/update/pull_request.feature index 5b80156c..9ba2912e 100644 --- a/features/update/pull_request.feature +++ b/features/update/pull_request.feature @@ -95,7 +95,7 @@ Feature: Create a pull-request/merge-request after update <%= @configs['name'] %> """ When I run `msync update --noop --pr` - Then the stderr should contain "A token is require to use services from gitlab" + Then the stderr should contain "A token is required to use services from gitlab" And the exit status should be 1 And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" diff --git a/lib/modulesync/git_service.rb b/lib/modulesync/git_service.rb index cee3506a..7146ea75 100644 --- a/lib/modulesync/git_service.rb +++ b/lib/modulesync/git_service.rb @@ -6,13 +6,16 @@ module GitService class MissingCredentialsError < Error; end def self.instantiate(type:, endpoint:, token:) + raise MissingCredentialsError, <<~MESSAGE if token.nil? + A token is required to use services from #{type}: + Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options. + MESSAGE + case type when :github - raise ModuleSync::Error, 'No GitHub token specified to create a pull request' if token.nil? require 'modulesync/git_service/github' ModuleSync::GitService::GitHub.new(token, endpoint) when :gitlab - raise ModuleSync::Error, 'No GitLab token specified to create a merge request' if token.nil? require 'modulesync/git_service/gitlab' ModuleSync::GitService::GitLab.new(token, endpoint) else @@ -113,11 +116,6 @@ def self.token_for(sourcecode:, type:) ENV['GITLAB_TOKEN'] end - raise MissingCredentialsError, <<~MESSAGE if token.nil? - A token is require to use services from #{type}: - Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options. - MESSAGE - token end diff --git a/spec/unit/modulesync/git_service_spec.rb b/spec/unit/modulesync/git_service_spec.rb index 9f372363..d8dbd619 100644 --- a/spec/unit/modulesync/git_service_spec.rb +++ b/spec/unit/modulesync/git_service_spec.rb @@ -11,13 +11,13 @@ context 'when instantiate a GitHub service without credentials' do it 'raises an error' do - expect { ModuleSync::GitService.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::Error, 'No GitHub token specified to create a pull request') + expect { ModuleSync::GitService.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) end end context 'when instantiate a GitLab service without credentials' do it 'raises an error' do - expect { ModuleSync::GitService.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::Error, 'No GitLab token specified to create a merge request') + expect { ModuleSync::GitService.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) end end @@ -90,9 +90,12 @@ end context 'without any environment variable sets' do - it 'raises an error about missing credential' do - expect{ModuleSync::GitService.configuration_for(sourcecode: sourcecode)} - .to raise_error ModuleSync::GitService::MissingCredentialsError + it 'returns a nil token' do + expect(ModuleSync::GitService.configuration_for(sourcecode: sourcecode)).to eq({ + type: :gitlab, + endpoint: 'https://git.example.com/api/v4', + token: nil, + }) end end end @@ -120,9 +123,12 @@ end context 'without a GITLAB_TOKEN environment variable sets' do - it 'raise an error about missing credential' do - expect{ModuleSync::GitService.configuration_for(sourcecode: sourcecode)} - .to raise_error ModuleSync::Error + it 'returns a nil token' do + expect(ModuleSync::GitService.configuration_for(sourcecode: sourcecode)).to eq({ + type: :gitlab, + endpoint: 'https://gitlab.example.com/api/v4', + token: nil, + }) end end end