Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/rex/parser/arguments_spec.rb
Views: 11623
require 'rspec'12RSpec.describe Rex::Parser::Arguments do3let(:subject) do4Rex::Parser::Arguments.new(5['-h', '--help'] => [false, 'Help banner.'],6['-d', '--delete'] => [false, 'Delete'],7['-j', '--job'] => [false, 'Run in the context of a job.'],8'--long-flag-with-no-corresponding-short-option-name' => [false, 'A long flag with no corresponding short option name'],9['-o', '--options'] => [true, 'A comma separated list of options in VAR=VAL format.', '<option>'],10['-q', '--quiet'] => [false, 'Run the module in quiet mode with no output'],11['-S', '--save'] => [false, 'Save to disk'],12['-sd', '--set-default'] => [true, 'A comma separated list of defaults in VAR=VAL format.', '<option>']13)14end1516describe '#parse' do17context 'when flags are provided' do18it 'parses a single flag correctly' do19input = ['-h']20expected_yields = [21['-h', 0, nil]22]23expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)24end2526it 'parses multiple flags correctly' do27input = ['-h', '-h', '-h']28expected_yields = [29['-h', 0, nil],30['-h', 1, nil],31['-h', 2, nil],32]33expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)34end3536it 'parses multiple flags combined correctly' do37input = ['-hq']38expected_yields = [39['-h', 0, nil],40['-q', 0, nil],41]42expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)43end4445it 'ignores unknown flags' do46input = ['-a', 'action_name']47expected_yields = [48# '-a' is dropped, 'action_name' is used as an argument49[nil, 1, 'action_name'],50]51expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)52end5354it 'treats combined flags that do not exist as an argument' do55input = ['-unknown-flags']56expected_yields = [57[nil, 0, '-unknown-flags']58]59expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)60end61end6263it 'parses a single long flag correctly' do64input = ['--help']65expected_yields = [66['-h', 0, nil]67]68expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)69end7071it 'parses multiple long flags correctly' do72input = ['--help', '--job']73expected_yields = [74['-h', 0, nil],75['-j', 1, nil]76]77expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)78end7980it 'parses a long flag and short flag correctly' do81input = ['--help', '-h']82expected_yields = [83['-h', 0, nil],84['-h', 1, nil]85]86expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)87end8889it 'parses a short flag when Rex Arguments are in an array correctly' do90input = ['-o']91expected_yields = [92['-o', 0, nil]93]94expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)95end9697it 'parses a long flag when in arguments array correctly' do98input = ['--options', 'option-arg']99expected_yields = [100['-o', 0, 'option-arg']101]102expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)103end104105it 'parses multiple long flags when in an arguments array correctly' do106input = ['--quiet', '--options', 'sample-option']107expected_yields = [108['-q', 0, nil],109['-o', 1, 'sample-option']110]111expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)112end113114it 'parses a multi character flag correctly' do115input = ['-sd', 'options-arg']116expected_yields = [117['-sd', 0, 'options-arg']118]119expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)120end121122it 'parses an aggregate flag with varied casing flag correctly' do123input = ['-Ssd', 'set-default-arg']124expected_yields = [125['-S', 0, nil],126['-sd', 0, 'set-default-arg']127]128expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)129end130131it 'parses an aggregate flag with arguments array correctly' do132input = ['-Sosd', 'options-arg', 'set-default-arg']133expected_yields = [134['-S', 0, nil],135['-o', 0, 'options-arg'],136['-sd', 0, 'set-default-arg']137]138expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)139end140141it 'parses an aggregate flag with arguments array and an additional long flag correctly' do142input = ['-So', 'options-arg', '--set-default', 'set-default-arg']143expected_yields = [144['-S', 0, nil],145['-o', 0, 'options-arg'],146['-sd', 2, 'set-default-arg']147]148expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)149end150151it 'parses a non-existent long flag correctly' do152input = ['--ultra-quiet']153expected_yields = [154[nil, 0, '--ultra-quiet']155]156expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)157end158159it 'parses a long flag that is not in an array correctly' do160input = ['--long-flag-with-no-corresponding-short-option-name']161expected_yields = [162['--long-flag-with-no-corresponding-short-option-name', 0, nil]163]164expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)165end166167context 'when arguments are supplied' do168it 'treats an ip address as an argument' do169input = ['127.0.0.1']170expected_yields = [171[nil, 0, '127.0.0.1'],172]173expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)174end175176it 'treats assignment as an argument' do177input = ['foo=bar']178expected_yields = [179[nil, 0, 'foo=bar'],180]181expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)182end183184it 'treats urls as an argument' do185input = ['https://example.com:443/foo/bar?baz=qux&a=b']186expected_yields = [187[nil, 0, 'https://example.com:443/foo/bar?baz=qux&a=b'],188]189expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)190end191end192end193194describe '#inspect' do195it 'prints usage in a sorted order correctly' do196expected_output = <<~EXPECTED197198OPTIONS:199200--long-flag-with-no-corresponding-short-option-name A long flag with no corresponding short option name201-d, --delete Delete202-h, --help Help banner.203-j, --job Run in the context of a job.204-o, --options <option> A comma separated list of options in VAR=VAL format.205-q, --quiet Run the module in quiet mode with no output206-S, --save Save to disk207-sd, --set-default <option> A comma separated list of defaults in VAR=VAL format.208EXPECTED209expect(subject.usage).to eq(expected_output)210end211end212end213214215