Commit 7e6bc4dd authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve reporting of a CI/CD entry config location

parent 99dddac5
......@@ -8,6 +8,8 @@ module Gitlab
class_methods do
def attributes(*attributes)
attributes.flatten.each do |attribute|
raise ArgumentError if method_defined?(attribute)
define_method(attribute) do
return unless config.is_a?(Hash)
......
......@@ -71,6 +71,13 @@ module Gitlab
true
end
def location
name = @key.presence || self.class.name.to_s.demodulize
.underscore.humanize.downcase
ancestors.map(&:key).append(name).compact.join(':')
end
def inspect
val = leaf? ? config : descendants
unspecified = specified? ? '' : '(unspecified) '
......
......@@ -37,7 +37,7 @@ module Gitlab
class UnknownStrategy < Entry::Node
def errors
['policy has to be either an array of conditions or a hash']
["#{location} has to be either an array of conditions or a hash"]
end
end
......
......@@ -8,7 +8,6 @@ module Gitlab
def initialize(entry)
super(entry)
@entry = entry
end
def messages
......@@ -20,16 +19,6 @@ module Gitlab
def self.name
'Validator'
end
private
def location
ancestors.map(&:key).compact.append(key_name).join(':')
end
def key_name
key.presence || @entry.class.name.to_s.demodulize.underscore.humanize
end
end
end
end
......
......@@ -344,28 +344,32 @@ module Ci
let(:config) { { rspec: { script: "rspec", type: "test", only: only } } }
let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }
shared_examples 'raises an error' do
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'jobs:rspec:only config should be an array of strings or regexps')
end
end
context 'when it is integer' do
let(:only) { 1 }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:only has to be either an array of conditions or a hash')
end
end
context 'when it is an array of integers' do
let(:only) { [1, 1] }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:only config should be an array of strings or regexps')
end
end
context 'when it is invalid regex' do
let(:only) { ["/*invalid/"] }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:only config should be an array of strings or regexps')
end
end
end
end
......@@ -518,28 +522,31 @@ module Ci
let(:config) { { rspec: { script: "rspec", except: except } } }
let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) }
shared_examples 'raises an error' do
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError, 'jobs:rspec:except config should be an array of strings or regexps')
end
end
context 'when it is integer' do
let(:except) { 1 }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:except has to be either an array of conditions or a hash')
end
end
context 'when it is an array of integers' do
let(:except) { [1, 1] }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:except config should be an array of strings or regexps')
end
end
context 'when it is invalid regex' do
let(:except) { ["/*invalid/"] }
it_behaves_like 'raises an error'
it do
expect { processor }.to raise_error(GitlabCiYamlProcessor::ValidationError,
'jobs:rspec:except config should be an array of strings or regexps')
end
end
end
end
......
......@@ -81,7 +81,7 @@ describe Gitlab::Ci::Config::Entry::Policy do
it 'returns information about errors' do
expect(entry.errors)
.to include 'policy has to be either an array of conditions or a hash'
.to include /has to be either an array of conditions or a hash/
end
end
......
......@@ -48,7 +48,7 @@ describe Gitlab::Ci::Config::Entry::Validator do
validator_instance.validate
expect(validator_instance.messages)
.to include "node test attribute can't be blank"
.to include /test attribute can't be blank/
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment