2016-08-13 12:58:51 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
describe Gitlab::QuickActions::CommandDefinition do
|
2016-08-13 12:58:51 -04:00
|
|
|
subject { described_class.new(:command) }
|
|
|
|
|
|
|
|
describe "#all_names" do
|
|
|
|
context "when the command has aliases" do
|
|
|
|
before do
|
|
|
|
subject.aliases = [:alias1, :alias2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the name and aliases" do
|
|
|
|
expect(subject.all_names).to eq([:command, :alias1, :alias2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command doesn't have aliases" do
|
|
|
|
it "returns an array with the name" do
|
|
|
|
expect(subject.all_names).to eq([:command])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#noop?" do
|
|
|
|
context "when the command has an action block" do
|
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.action_block = proc { }
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
expect(subject.noop?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command doesn't have an action block" do
|
|
|
|
it "returns true" do
|
|
|
|
expect(subject.noop?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#available?" do
|
2018-02-23 09:23:09 -05:00
|
|
|
let(:opts) { OpenStruct.new(go: false) }
|
2016-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
context "when the command has a condition block" do
|
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.condition_block = proc { go }
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the condition block returns true" do
|
|
|
|
before do
|
|
|
|
opts[:go] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
expect(subject.available?(opts)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the condition block returns false" do
|
|
|
|
it "returns false" do
|
|
|
|
expect(subject.available?(opts)).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command doesn't have a condition block" do
|
|
|
|
it "returns true" do
|
|
|
|
expect(subject.available?(opts)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
let(:context) { OpenStruct.new(run: false) }
|
|
|
|
|
|
|
|
context "when the command is a noop" do
|
|
|
|
it "doesn't execute the command" do
|
|
|
|
expect(context).not_to receive(:instance_exec)
|
|
|
|
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, nil)
|
2016-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
expect(context.run).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command is not a noop" do
|
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.action_block = proc { self.run = true }
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command is not available" do
|
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.condition_block = proc { false }
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't execute the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, nil)
|
2016-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
expect(context.run).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command is available" do
|
2016-08-18 15:21:52 -04:00
|
|
|
context "when the commnd has no arguments" do
|
2016-08-13 12:58:51 -04:00
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.action_block = proc { self.run = true }
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
context "when the command is provided an argument" do
|
|
|
|
it "executes the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, true)
|
2016-08-13 12:58:51 -04:00
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
expect(context.run).to be true
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
context "when the command is not provided an argument" do
|
|
|
|
it "executes the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, nil)
|
2016-08-18 15:21:52 -04:00
|
|
|
|
|
|
|
expect(context.run).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command has 1 required argument" do
|
|
|
|
before do
|
|
|
|
subject.action_block = ->(arg) { self.run = arg }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command is provided an argument" do
|
2016-08-13 12:58:51 -04:00
|
|
|
it "executes the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, true)
|
2016-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
expect(context.run).to be true
|
|
|
|
end
|
|
|
|
end
|
2016-08-18 15:21:52 -04:00
|
|
|
|
|
|
|
context "when the command is not provided an argument" do
|
|
|
|
it "doesn't execute the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, nil)
|
2016-08-18 15:21:52 -04:00
|
|
|
|
|
|
|
expect(context.run).to be false
|
|
|
|
end
|
|
|
|
end
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
context "when the command has 1 optional argument" do
|
2016-08-13 12:58:51 -04:00
|
|
|
before do
|
2016-08-18 15:21:52 -04:00
|
|
|
subject.action_block = proc { |arg = nil| self.run = arg || true }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the command is provided an argument" do
|
|
|
|
it "executes the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, true)
|
2016-08-18 15:21:52 -04:00
|
|
|
|
|
|
|
expect(context.run).to be true
|
|
|
|
end
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
context "when the command is not provided an argument" do
|
2016-08-13 12:58:51 -04:00
|
|
|
it "executes the command" do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, nil)
|
2016-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
expect(context.run).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
context 'when the command defines parse_params block' do
|
|
|
|
before do
|
|
|
|
subject.parse_params_block = ->(raw) { raw.strip }
|
|
|
|
subject.action_block = ->(parsed) { self.received_arg = parsed }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes the command passing the parsed param' do
|
2018-02-23 09:23:09 -05:00
|
|
|
subject.execute(context, 'something ')
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
expect(context.received_arg).to eq('something')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#explain' do
|
|
|
|
context 'when the command is not available' do
|
|
|
|
before do
|
|
|
|
subject.condition_block = proc { false }
|
|
|
|
subject.explanation = 'Explanation'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2018-02-23 09:23:09 -05:00
|
|
|
result = subject.explain({}, nil)
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the explanation is a static string' do
|
|
|
|
before do
|
|
|
|
subject.explanation = 'Explanation'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns this static string' do
|
2018-02-23 09:23:09 -05:00
|
|
|
result = subject.explain({}, nil)
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
expect(result).to eq 'Explanation'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the explanation is dynamic' do
|
|
|
|
before do
|
|
|
|
subject.explanation = proc { |arg| "Dynamic #{arg}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invokes the proc' do
|
2018-02-23 09:23:09 -05:00
|
|
|
result = subject.explain({}, 'explanation')
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
expect(result).to eq 'Dynamic explanation'
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|