spec/kontena/command_spec.rb in kontena-cli-1.5.0.pre1 vs spec/kontena/command_spec.rb in kontena-cli-1.5.0.pre2

- old
+ new

@@ -49,6 +49,73 @@ it 'lets the error raise through' do expect{subject.run([])}.to raise_error(Kontena::Errors::StandardError) end end end + + context 'option placement handling' do + subject do + Class.new(Kontena::Command) do + parameter 'TESTPARAM', 'Test parameter' + option '--long-only', 'LONGONLY', 'Option with long only' + option ['--long-and-short', '-l'], :flag, 'Flag with short and long', default: false + + def execute + { param: testparam, longopt: long_only, shortflag: long_and_short? } + end + end + + end + + it 'allows using options before parameters' do + expect(subject.new('kontena').run(%w(-l --long-only longopt test))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: true + ) + expect(subject.new('kontena').run(%w(--long-only longopt test))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: false + ) + expect(subject.new('kontena').run(%w(--long-and-short test))).to match hash_including( + param: 'test', longopt: nil, shortflag: true + ) + end + + it 'allows using options after parameters' do + expect(subject.new('kontena').run(%w(test -l --long-only longopt))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: true + ) + expect(subject.new('kontena').run(%w(test --long-only longopt))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: false + ) + expect(subject.new('kontena').run(%w(test --long-and-short))).to match hash_including( + param: 'test', longopt: nil, shortflag: true + ) + end + + it 'allows using options mixed with parameters' do + expect(subject.new('kontena').run(%w(-l test --long-only longopt))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: true + ) + expect(subject.new('kontena').run(%w(--long-and-short test --long-only longopt))).to match hash_including( + param: 'test', longopt: 'longopt', shortflag: true + ) + end + + context 'with double dash' do + subject do + Class.new(Kontena::Command) do + parameter 'TESTPARAM ...', 'Test parameter' + option '--opt', 'OPT', 'Option' + + def execute + { param_list: testparam_list, opt: opt } + end + end + end + + it 'does not parse options after -- double dash' do + expect(subject.new('kontena').run(%w(--opt hello foo -- --bar hello))).to match hash_including( + param_list: %w(foo --bar hello), opt: 'hello' + ) + end + end + end end