gitlab-org--gitlab-foss/spec/lib/gitlab/push_options_spec.rb
Luke Duncalfe 1883e320ea Use Gitlab::PushOptions for ci.skip push option
Previously the raw push option Array was sent to Pipeline::Chain::Skip.

This commit updates this class (and the chain of classes that pass the
push option parameters from the API internal `post_receive` endpoint to
that class) to treat push options as a Hash of options parsed by
GitLab::PushOptions.

The GitLab::PushOptions class takes options like this:

    -o ci.skip -o merge_request.create -o merge_request.target=branch

and turns them into a Hash like this:

    {
      ci: {
        skip: true
      },
      merge_request: {
        create: true,
        target: 'branch'
      }
    }

This now how Pipeline::Chain::Skip is determining if the `ci.skip` push
option was used.
2019-04-09 10:03:26 +12:00

103 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::PushOptions do
describe 'namespace and key validation' do
it 'ignores unrecognised namespaces' do
options = described_class.new(['invalid.key=value'])
expect(options.get(:invalid)).to eq(nil)
end
it 'ignores unrecognised keys' do
options = described_class.new(['merge_request.key=value'])
expect(options.get(:merge_request)).to eq(nil)
end
it 'ignores blank keys' do
options = described_class.new(['merge_request'])
expect(options.get(:merge_request)).to eq(nil)
end
it 'parses recognised namespace and key pairs' do
options = described_class.new(['merge_request.target=value'])
expect(options.get(:merge_request)).to include({
target: 'value'
})
end
end
describe '#get' do
it 'can emulate Hash#dig' do
options = described_class.new(['merge_request.target=value'])
expect(options.get(:merge_request, :target)).to eq('value')
end
end
describe '#as_json' do
it 'returns all options' do
options = described_class.new(['merge_request.target=value'])
expect(options.as_json).to include(
merge_request: {
target: 'value'
}
)
end
end
it 'can parse multiple push options' do
options = described_class.new([
'merge_request.create',
'merge_request.target=value'
])
expect(options.get(:merge_request)).to include({
create: true,
target: 'value'
})
expect(options.get(:merge_request, :create)).to eq(true)
expect(options.get(:merge_request, :target)).to eq('value')
end
it 'stores options internally as a HashWithIndifferentAccess' do
options = described_class.new([
'merge_request.create'
])
expect(options.get('merge_request', 'create')).to eq(true)
expect(options.get(:merge_request, :create)).to eq(true)
end
it 'selects the last option when options contain duplicate namespace and key pairs' do
options = described_class.new([
'merge_request.target=value1',
'merge_request.target=value2'
])
expect(options.get(:merge_request, :target)).to eq('value2')
end
it 'defaults values to true' do
options = described_class.new(['merge_request.create'])
expect(options.get(:merge_request, :create)).to eq(true)
end
it 'expands aliases' do
options = described_class.new(['mr.target=value'])
expect(options.get(:merge_request, :target)).to eq('value')
end
it 'forgives broken push options' do
options = described_class.new(['merge_request . target = value'])
expect(options.get(:merge_request, :target)).to eq('value')
end
end