Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow single blueprint argument #17

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/dredd/rack/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ def api_remote?
end

def command_valid?
command.has_at_least_two_arguments?
if api_remote?
command.argument_count >= 2
else
command.argument_count >= 1
end
end

def start_server!
Expand Down Expand Up @@ -147,16 +151,16 @@ def method_missing(name, *args)

class String

# Verify that a command has at least two arguments (excluding options)
# Return the number of arguments in a command (excluding options)
#
# Examples:
#
# "dredd doc/*.apib http://api.example.com".valid? # => true
# "dredd doc/*.apib doc/*apib.md http://api.example.com".valid? # => true
# "dredd doc/*.apib http://api.example.com --level verbose".valid? # => true
# "dredd http://api.example.com".valid? # => false
# "dredd doc/*.apib --dry-run".valid? # => false
# "dredd --dry-run --level verbose".valid? # => false
# "dredd doc/*.apib http://api.example.com".argument_count # => 2
# "dredd doc/*.apib doc/*apib.md http://api.example.com".argument_count # => 3
# "dredd doc/*.apib http://api.example.com --level verbose".argument_count # => 2
# "dredd http://api.example.com".argument_count # => 1
# "dredd doc/*.apib --dry-run".argument_count # => 1
# "dredd --dry-run --level verbose".argument_count # => 0
#
# Known limitations:
#
Expand All @@ -165,14 +169,11 @@ class String
#
# Note:
#
# The known limitations imply that there may be false negatives: this method
# can return false for commands that do have two arguments or more. But there
# should not be false positives: if the method returns true, then the command
# does have at least two arguments.
#
# Returns true if the command String has at least two arguments, false otherwise.
def has_at_least_two_arguments?
split('--').first.split(' ').length >= 3
# The known limitations imply that the count may be lower than (long options
# are not # counted) but never higher than the true number of arguments.
def argument_count
tokens = split('--').first.split(' ')
arguments = tokens.length - 1
end
end

Expand Down
38 changes: 28 additions & 10 deletions spec/lib/dredd/rack/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,28 @@
describe '#command_valid?', private: true do

context 'when the generated command has less than two arguments' do
before do
allow(subject).to receive_message_chain(:command, :argument_count).and_return(1)
end

context 'when the API is remote' do
before(:each) do
allow(subject).to receive(:api_remote?).and_return(true)
end

it 'returns false' do
expect(subject.send(:command_valid?)).not_to be_truthy
end
end

context 'when the API is local' do
before(:each) do
allow(subject).to receive(:api_remote?).and_return(false)
end

it 'returns false' do
allow(subject).to receive_message_chain(:command, :has_at_least_two_arguments?).and_return(false)
expect(subject.send(:command_valid?)).not_to be_truthy
it 'returns false' do
expect(subject.send(:command_valid?)).to be_truthy
end
end
end
end
Expand All @@ -88,7 +106,7 @@
context 'when the generated command has less than two arguments' do

it 'executes a configration block in the context of the runner' do
allow(subject).to receive_message_chain(:command, :has_at_least_two_arguments?).and_return(false)
allow(subject).to receive_message_chain(:command, :argument_count).and_return(1)

expect(subject).to receive_message_chain(:dry_run!, :color!)
expect(subject).to receive(:level).with(:warning)
Expand Down Expand Up @@ -252,22 +270,22 @@

describe String, public: true do

it 'responds to :has_at_least_two_arguments?' do
expect(subject).to respond_to :has_at_least_two_arguments?
it 'responds to :argument_count' do
expect(subject).to respond_to :argument_count
end


describe '#has_at_least_two_arguments?' do
describe '#argument_count' do

context 'when the String can be interpreted as a command with at least two arguments' do
context 'returns true (reliable)' do
context 'returns at least two' do

['dredd doc/*.apib http://api.example.com',
'dredd doc/*.apib doc/*apib.md http://api.example.com',
'dredd doc/*.apib http://api.example.com --level verbose'].each do |subject|

it "e.g. '#{subject}'" do
expect(subject.has_at_least_two_arguments?).to be_truthy
expect(subject.argument_count).to be >= 2
end
end
end
Expand All @@ -281,7 +299,7 @@
'dredd --dry-run --level verbose'].each do |subject|

it "e.g. '#{subject}'" do
expect(subject.has_at_least_two_arguments?).to be_falsey
expect(subject.argument_count).to be < 2
end
end
end
Expand Down