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

Add safe invocation CLI syntax #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,24 @@ def thread_pool # :nodoc:

# Invokes a task with arguments that are extracted from +task_string+
def invoke_task(task_string) # :nodoc:
name, args = parse_task_string(task_string)
name, args, safe_call = parse_task_string(task_string)
return if safe_call && !lookup(name)

t = self[name]
t.invoke(*args)
end

def parse_task_string(string) # :nodoc:
/^([^\[]+)(?:\[(.*)\])$/ =~ string.to_s
string = string.to_s.dup
safe_call = !!string.chomp!('?')

/^([^\[]+)(?:\[(.*)\])$/ =~ string

name = $1
remaining_args = $2
casperisfine marked this conversation as resolved.
Show resolved Hide resolved

return string, [] unless name
return name, [] if remaining_args.empty?
return string, [], safe_call unless name
return name, [], safe_call if remaining_args.empty?

args = []

Expand All @@ -178,7 +183,7 @@ def parse_task_string(string) # :nodoc:
args << $1.gsub(/\\(.)/, '\1')
end while remaining_args

return name, args
return name, args, safe_call
end

# Provide standard exception handling for the given block.
Expand Down
20 changes: 20 additions & 0 deletions test/test_rake_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ def test_good_run
assert_equal "DEFAULT\n", out
end

def test_safe_run
ran = false

@app.options.silent = true

@app.instance_eval do
intern(Rake::Task, "default").enhance { ran = true }
end

rakefile_default

out, err = capture_io do
@app.run %w[--rakelib="" default missing?]
end

assert ran
assert_empty err
assert_equal "DEFAULT\n", out
end

def test_display_task_run
ran = false
@app.last_description = "COMMENT"
Expand Down