Skip to content

Commit

Permalink
Make pending specs pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 29, 2009
1 parent 3ebd34b commit 6bc478e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
34 changes: 29 additions & 5 deletions lib/thor.rb
Expand Up @@ -117,6 +117,35 @@ def method_option(name, options)
build_option(name, options, scope)
end

# Parses the task and options from the given args, instantiate the class
# and invoke the task. This method is used when the arguments must be parsed
# from an array. If you are inside Ruby and want to use a Thor class, you
# can simply initialize it:
#
# script = MyScript.new(args, options, config)
# script.invoke(:task, first_arg, second_arg, third_arg)
#
def start(given_args=ARGV, config={})
config[:shell] ||= Thor::Base.shell.new

meth = normalize_task_name(given_args.shift)
task = all_tasks[meth]

if task
args, opts = Thor::Options.split(given_args)
config.merge!(:task_options => task.options)
else
args, opts = given_args, {}
end

task ||= Task.dynamic(meth)
trailing = args[Range.new(arguments.size, -1)]

new(args, opts, config).invoke(task, trailing || [])
rescue Thor::Error => e
config[:shell].error e.message
end

# Prints help information. If a task name is given, it shows information
# only about the specific task.
#
Expand Down Expand Up @@ -185,11 +214,6 @@ def initialize_added #:nodoc:
@method_options = nil
end

def normalize_arguments(args, config) #:nodoc:
meth = normalize_task_name(args.shift)
all_tasks[meth] || Task.dynamic(meth)
end

# Receives a task name (can be nil), and try to get a map from it.
# If a map can't be found use the sent name or the default task.
#
Expand Down
34 changes: 0 additions & 34 deletions lib/thor/base.rb
Expand Up @@ -359,33 +359,6 @@ def namespace(name=nil)
end
end

# Parses the task and options from the given args, instantiate the class
# and invoke the task. This method is used when the arguments must be parsed
# from an array. If you are inside Ruby and want to use a Thor class, you
# can simply initialize it:
#
# script = MyScript.new(args, options, config)
# script.invoke(:task, first_arg, second_arg, third_arg)
#
def start(given_args=ARGV, config={})
config[:shell] ||= Thor::Base.shell.new

task = normalize_arguments(given_args, config)
args, opts = Thor::Options.split(given_args)

if task
trailing = args[Range.new(arguments.size, -1)]
config.merge!(:task_options => task.options)
elsif Thor::HELP_MAPPINGS.include?(given_args.first)
help(config[:shell])
return
end

new(args, opts, config).invoke(task, trailing || [])
rescue Thor::Error => e
config[:shell].error e.message
end

protected

# Prints the class options per group. If an option does not belong to
Expand Down Expand Up @@ -539,13 +512,6 @@ def create_task(meth) #:nodoc:
# class.
def initialize_added #:nodoc:
end

# SIGNATURE: Normalize arguments when invoked through start. Should
# return the name of the task to be invoked. Returning nil makes the
# start process to exist without error message.
def normalize_arguments(args, config) #:nodoc:
end

end
end
end
17 changes: 17 additions & 0 deletions lib/thor/group.rb
Expand Up @@ -18,6 +18,23 @@ def desc(description=nil)
end
end

# Start works differently in Thor::Group, it simply invokes all tasks
# inside the class.
#
def start(given_args=ARGV, config={})
config[:shell] ||= Thor::Base.shell.new

if Thor::HELP_MAPPINGS.include?(given_args.first)
help(config[:shell])
return
end

args, opts = Thor::Options.split(given_args)
new(args, opts, config).invoke
rescue Thor::Error => e
config[:shell].error e.message
end

# Prints help information.
#
# ==== Options
Expand Down
4 changes: 3 additions & 1 deletion lib/thor/runner.rb
Expand Up @@ -25,7 +25,9 @@ def help(meth=nil)
def method_missing(meth, *args)
meth = meth.to_s
initialize_thorfiles(meth)
invoke(meth)
klass, task = Thor::Util.namespace_to_thor_class(meth)
args.unshift(task) if task
klass.start(args, _shared_config)
end

desc "install NAME", "Install a Thor file into your system tasks, optionally named for future updates"
Expand Down
18 changes: 6 additions & 12 deletions spec/runner_spec.rb
Expand Up @@ -52,17 +52,13 @@
end

it "forwads arguments to the invoked task" do
pending do
ARGV.replace ["my_script:animal", "horse"]
Thor::Runner.start.must == ["horse"]
end
ARGV.replace ["my_script:animal", "horse"]
Thor::Runner.start.must == ["horse"]
end

it "invokes tasks through shortcuts" do
pending do
ARGV.replace ["my_script", "-T", "horse"]
Thor::Runner.start.must == ["horse"]
end
ARGV.replace ["my_script", "-T", "horse"]
Thor::Runner.start.must == ["horse"]
end

it "invokes a Thor::Group" do
Expand All @@ -71,10 +67,8 @@
end

it "raises an error if class/task can't be found" do
pending do
ARGV.replace ["unknown"]
capture(:stderr){ Thor::Runner.start }.must =~ /could not find Thor class or task 'unknown'/
end
ARGV.replace ["unknown"]
capture(:stderr){ Thor::Runner.start }.must =~ /could not find Thor class or task 'unknown'/
end

it "does not swallow NoMethodErrors that occur inside the called method" do
Expand Down

0 comments on commit 6bc478e

Please sign in to comment.