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 ...@@ -8,6 +8,8 @@ module Gitlab
class_methods do class_methods do
def attributes(*attributes) def attributes(*attributes)
attributes.flatten.each do |attribute| attributes.flatten.each do |attribute|
raise ArgumentError if method_defined?(attribute)
define_method(attribute) do define_method(attribute) do
return unless config.is_a?(Hash) return unless config.is_a?(Hash)
......
...@@ -71,6 +71,13 @@ module Gitlab ...@@ -71,6 +71,13 @@ module Gitlab
true true
end 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 def inspect
val = leaf? ? config : descendants val = leaf? ? config : descendants
unspecified = specified? ? '' : '(unspecified) ' unspecified = specified? ? '' : '(unspecified) '
......
...@@ -37,7 +37,7 @@ module Gitlab ...@@ -37,7 +37,7 @@ module Gitlab
class UnknownStrategy < Entry::Node class UnknownStrategy < Entry::Node
def errors 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
end end
......
...@@ -8,7 +8,6 @@ module Gitlab ...@@ -8,7 +8,6 @@ module Gitlab
def initialize(entry) def initialize(entry)
super(entry) super(entry)
@entry = entry
end end
def messages def messages
...@@ -20,16 +19,6 @@ module Gitlab ...@@ -20,16 +19,6 @@ module Gitlab
def self.name def self.name
'Validator' 'Validator'
end 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 end
end end
......
...@@ -344,28 +344,32 @@ module Ci ...@@ -344,28 +344,32 @@ module Ci
let(:config) { { rspec: { script: "rspec", type: "test", only: only } } } let(:config) { { rspec: { script: "rspec", type: "test", only: only } } }
let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) } 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 context 'when it is integer' do
let(:only) { 1 } 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 end
context 'when it is an array of integers' do context 'when it is an array of integers' do
let(:only) { [1, 1] } 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 end
context 'when it is invalid regex' do context 'when it is invalid regex' do
let(:only) { ["/*invalid/"] } 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 end
end end
...@@ -518,28 +522,31 @@ module Ci ...@@ -518,28 +522,31 @@ module Ci
let(:config) { { rspec: { script: "rspec", except: except } } } let(:config) { { rspec: { script: "rspec", except: except } } }
let(:processor) { GitlabCiYamlProcessor.new(YAML.dump(config)) } 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 context 'when it is integer' do
let(:except) { 1 } 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 end
context 'when it is an array of integers' do context 'when it is an array of integers' do
let(:except) { [1, 1] } 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 end
context 'when it is invalid regex' do context 'when it is invalid regex' do
let(:except) { ["/*invalid/"] } 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 end
end end
......
...@@ -81,7 +81,7 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -81,7 +81,7 @@ describe Gitlab::Ci::Config::Entry::Policy do
it 'returns information about errors' do it 'returns information about errors' do
expect(entry.errors) 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
end end
......
...@@ -48,7 +48,7 @@ describe Gitlab::Ci::Config::Entry::Validator do ...@@ -48,7 +48,7 @@ describe Gitlab::Ci::Config::Entry::Validator do
validator_instance.validate validator_instance.validate
expect(validator_instance.messages) expect(validator_instance.messages)
.to include "node test attribute can't be blank" .to include /test attribute can't be blank/
end end
end 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