From de4c9a273867864a9033dab0624e0cfd72201384 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 13 Jul 2016 12:22:33 +0200 Subject: [PATCH] Improve CI stage configuration entry validations --- lib/gitlab/ci/config/node/stage.rb | 16 +++++++++------- lib/gitlab/ci/config/node/validators.rb | 2 +- spec/lib/gitlab/ci/config/node/stage_spec.rb | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/gitlab/ci/config/node/stage.rb b/lib/gitlab/ci/config/node/stage.rb index e8fae65a2a9..909358ea170 100644 --- a/lib/gitlab/ci/config/node/stage.rb +++ b/lib/gitlab/ci/config/node/stage.rb @@ -10,14 +10,16 @@ module Gitlab validations do validates :config, type: String - validates :global, required_attribute: true - validate :known_stage, on: :processed - def known_stage - unless known? - stages_list = global.stages.join(', ') - errors.add(:config, - "should be one of defined stages (#{stages_list})") + with_options on: :processed do + validates :global, required: true + + validate do + unless known? + errors.add(:config, + 'should be one of defined stages ' \ + "(#{global.stages.join(', ')})") + end end end end diff --git a/lib/gitlab/ci/config/node/validators.rb b/lib/gitlab/ci/config/node/validators.rb index 6f0e14e2f0a..d33b407af68 100644 --- a/lib/gitlab/ci/config/node/validators.rb +++ b/lib/gitlab/ci/config/node/validators.rb @@ -33,7 +33,7 @@ module Gitlab end end - class RequiredAttributeValidator < ActiveModel::EachValidator + class RequiredValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value.nil? raise Entry::InvalidError, diff --git a/spec/lib/gitlab/ci/config/node/stage_spec.rb b/spec/lib/gitlab/ci/config/node/stage_spec.rb index 6deeca1a6c0..004012f8b38 100644 --- a/spec/lib/gitlab/ci/config/node/stage_spec.rb +++ b/spec/lib/gitlab/ci/config/node/stage_spec.rb @@ -28,10 +28,10 @@ describe Gitlab::Ci::Config::Node::Stage do context 'when stage config is incorrect' do describe '#errors' do context 'when reference to global node is not set' do - let(:stage) { described_class.new(config) } + let(:stage) { described_class.new('test') } it 'raises error' do - expect { stage }.to raise_error( + expect { stage.validate! }.to raise_error( Gitlab::Ci::Config::Node::Entry::InvalidError, /Entry needs global attribute set internally./ )