Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix specs.

  • Loading branch information...
commit 094cd62339d7259f2f89e9db84137dfe410e9a39 1 parent 5525cf7
@josevalim josevalim authored
View
2  Thorfile
@@ -53,7 +53,7 @@ class Default < Thor
s.bindir = "bin"
s.executables = %w( thor rake2thor )
s.files = s.extra_rdoc_files + Dir.glob("{bin,lib}/**/*")
- s.text_files.include 'spec/**/*'
+ s.test_files.include 'spec/**/*'
s.test_files.exclude 'spec/sandbox/**/*'
end
View
15 lib/thor.rb
@@ -78,14 +78,14 @@ def method_options(options=nil)
@method_options
end
- # Adds an option to the set of class options. If :for is given as option,
+ # Adds an option to the set of method options. If :for is given as option,
# it allows you to change the options from a previous defined task.
#
# def previous_task
# # magic
# end
#
- # method_options :foo => :bar, :for => :previous_task
+ # method_option :foo => :bar, :for => :previous_task
#
# def next_task
# # magic
@@ -101,7 +101,6 @@ def method_options(options=nil)
# :default - Default value for this argument. It cannot be required and have default values.
# :aliases - Aliases for this option.
# :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
- # :group - The group for this options. Use by class options to output options in different levels.
# :banner - String to show on usage notes.
#
def method_option(name, options={})
@@ -158,15 +157,15 @@ def help(shell, options={})
shell.say "Usage:"
shell.say " #{banner(task)}"
shell.say
- class_options_help(shell, "Method" => task.options.map { |_, o| o })
+ class_options_help(shell, nil => task.options.map { |_, o| o })
shell.say task.description
else
- list, klasses = [], [self]
+ list = printable_tasks(!options[:short])
- klasses += Thor::Util.thor_classes_in(self)
- klasses.each do |klass|
- list += klass.printable_tasks(!options[:short])
+ Thor::Util.thor_classes_in(self).each do |klass|
+ list += klass.printable_tasks(false)
end
+
list.sort!{ |a,b| a[0] <=> b[0] }
if options[:short]
View
8 lib/thor/base.rb
@@ -372,16 +372,16 @@ def class_options_help(shell, groups={}) #:nodoc:
# Deal with default group
global_options = groups.delete(nil) || []
- print_options(shell, "Class", global_options)
+ print_options(shell, global_options)
# Print all others
groups.each do |group_name, options|
- print_options(shell, group_name, options)
+ print_options(shell, options, group_name)
end
end
# Receives a set of options and print them.
- def print_options(shell, group_name, options)
+ def print_options(shell, options, group_name=nil)
return if options.empty?
list = []
@@ -395,7 +395,7 @@ def print_options(shell, group_name, options)
list << [ "", "# Default: #{option.default}" ] if option.show_default?
end
- shell.say("#{group_name} options:")
+ shell.say(group_name ? "#{group_name} options:" : "Options:")
shell.print_table(list, :ident => 2)
shell.say ""
end
View
39 lib/thor/task.rb
@@ -37,43 +37,44 @@ def run(instance, args=[])
parse_no_method_error(instance, e)
end
- # Returns the formatted usage. If a class is given, the class arguments are
- # injected in the usage.
- #
- def formatted_usage(klass=nil, namespace=nil)
+ # Returns the formatted usage by injecting given required arguments
+ # and required options into the given usage.
+ def formatted_usage(klass, namespace=nil)
namespace = klass.namespace if namespace.nil?
+ # Add namespace
formatted = if namespace
"#{namespace.gsub(/^(default|thor:runner:)/,'')}:"
else
""
end
- formatted << formatted_arguments(klass)
- formatted.strip!
- formatted
- end
-
- # Injects the class arguments into the task usage.
- #
- def formatted_arguments(klass)
- if klass && !klass.arguments.empty?
+ # Add usage with required arguments
+ formatted << if klass && !klass.arguments.empty?
usage.to_s.gsub(/^#{name}/) do |match|
- match << " " << klass.arguments.map{ |a| a.usage }.join(' ')
+ match << " " << required_arguments(klass)
end
else
usage.to_s
end
- end
- # Returns the options usage for this task.
- #
- def formatted_options
- @formatted_options ||= options.map{ |_, o| o.usage }.sort.join(" ")
+ # Add required options
+ formatted << " #{required_options}"
+
+ # Strip and go!
+ formatted.strip
end
protected
+ def required_arguments(klass)
+ klass.arguments.map{ |a| a.usage if a.required? }.compact.join(' ')
+ end
+
+ def required_options
+ @required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
+ end
+
# Given a target, checks if this class name is not a private/protected method.
#
def public_method?(instance) #:nodoc:
View
2  spec/base_spec.rb
@@ -134,7 +134,7 @@ def hello
it "allows extra options to be given" do
hash = { "Foo" => B.class_options.values }
- content = capture(:stdout) { MyCounter.send(:class_options_help, Thor::Base.shell.new, nil, hash) }
+ content = capture(:stdout) { MyCounter.send(:class_options_help, Thor::Base.shell.new, hash) }
content.must =~ /Foo options\:/
content.must =~ /--last-name=LAST_NAME/
end
View
6 spec/runner_spec.rb
@@ -110,7 +110,7 @@
it "gives a list of the available tasks" do
ARGV.replace ["list"]
content = capture(:stdout) { Thor::Runner.start }
- content.must =~ /amazing:describe NAME \[\-\-forcefully\]\s+# say that someone is amazing/m
+ content.must =~ /amazing:describe NAME\s+# say that someone is amazing/m
end
it "gives a list of the available Thor::Group classes" do
@@ -141,7 +141,7 @@
it "presents tasks in the default namespace with an empty namespace" do
ARGV.replace ["list"]
- capture(:stdout) { Thor::Runner.start }.must =~ /^:test\s+# prints 'test'/m
+ capture(:stdout) { Thor::Runner.start }.must =~ /^thor :test\s+# prints 'test'/m
end
it "runs tasks with an empty namespace from the default namespace" do
@@ -183,7 +183,7 @@
it "displays the modules installed in a pretty way" do
stdout = capture(:stdout) { Thor::Runner.start(["installed"]) }
stdout.must =~ /random\s*amazing/
- stdout.must =~ /amazing:describe NAME \[\-\-forcefully\]\s+# say that someone is amazing/m
+ stdout.must =~ /amazing:describe NAME\s+# say that someone is amazing/m
end
end
View
26 spec/task_spec.rb
@@ -10,32 +10,22 @@ def task(options={})
end
describe "#formatted_usage" do
- it "shows usage with options" do
- task('foo' => true, :bar => :required).formatted_usage.must == "can_has --bar=BAR [--foo]"
- end
-
it "includes namespace within usage" do
stub(Object).namespace{ "foo" }
stub(Object).arguments{ [] }
- task(:bar => :required).formatted_usage(Object, true).must == "foo:can_has --bar=BAR"
- end
-
- it "does not show options if required" do
- stub(Object).namespace{ "foo" }
- stub(Object).arguments{ [] }
- task(:bar => :required).formatted_usage(Object, true, false).must == "foo:can_has"
+ task(:bar => :required).formatted_usage(Object).must == "foo:can_has --bar=BAR"
end
it "removes default from namespace" do
stub(Object).namespace{ "default:foo" }
stub(Object).arguments{ [] }
- task(:bar => :required).formatted_usage(Object, true).must == ":foo:can_has --bar=BAR"
+ task(:bar => :required).formatted_usage(Object).must == ":foo:can_has --bar=BAR"
end
it "injects arguments into usage" do
stub(Object).namespace{ "foo" }
stub(Object).arguments{ [ Thor::Argument.new(:bar, nil, true, :string) ] }
- task(:foo => true).formatted_usage(Object).must == "can_has BAR [--foo]"
+ task(:foo => :required).formatted_usage(Object).must == "foo:can_has BAR --foo=FOO"
end
end
@@ -75,14 +65,4 @@ def task(options={})
}.must raise_error(Thor::UndefinedTaskError, "the 'can_has' task of Object is private")
end
end
-
- describe "#short_description" do
- it "returns the first line of the description" do
- Thor::Task.new(:task, "I can has\ncheezburger", "can_has").short_description == "I can has"
- end
-
- it "returns the whole description if it's one line" do
- Thor::Task.new(:task, "I can has cheezburger", "can_has").short_description == "I can has cheezburger"
- end
- end
end
View
22 spec/thor_spec.rb
@@ -72,7 +72,7 @@
describe "when :for is supplied" do
it "overwrites a previous defined task" do
- capture(:stdout) { MyChildScript.start(["help"]) }.must =~ /animal KIND \[\-\-other=OTHER\]\s+# fish around/m
+ capture(:stdout) { MyChildScript.start(["help"]) }.must =~ /animal KIND \s+# fish around/m
end
end
end
@@ -161,12 +161,12 @@ def shell
it "shows superclass tasks" do
content = capture(:stdout){ MyChildScript.help(shell) }
- content.must =~ /foo BAR \[\-\-force\]\s+# do some fooing/m
+ content.must =~ /foo BAR \s+# do some fooing/m
end
it "shows class options information" do
content = capture(:stdout){ MyChildScript.help(shell) }
- content.must =~ /Class options\:/
+ content.must =~ /Options\:/
content.must =~ /\[\-\-param=N\]/
end
@@ -178,11 +178,11 @@ def shell
describe "for a specific task" do
it "provides full help info when talking about a specific task" do
- capture(:stdout) { MyScript.help(shell, "foo") }.must == <<END
+ capture(:stdout) { MyScript.help(shell, :task => "foo") }.must == <<END
Usage:
- foo BAR
+ thor my_script:foo BAR
-Method options:
+Options:
[--force] # Force to do some fooing
do some fooing
@@ -193,24 +193,20 @@ def shell
it "raises an error if the task can't be found" do
lambda {
- MyScript.help(shell, "unknown")
+ MyScript.help(shell, :task => "unknown")
}.must raise_error(Thor::Error, "task 'unknown' could not be found in namespace 'my_script'")
end
end
describe "options" do
- it "shows the namespace if required" do
- capture(:stdout){ MyScript.help(shell, nil, :namespace => true) }.must =~ /my_script:foo BAR/
- end
-
it "does not show superclass tasks if required" do
- capture(:stdout){ MyScript.help(shell, nil, :short => true) }.must_not =~ /help/
+ capture(:stdout){ MyScript.help(shell, :short => true) }.must_not =~ /help/
end
end
describe "instance method" do
it "calls the class method" do
- stub(MyScript).help(mock.instance_of(Thor::Base.shell), nil, :namespace => nil)
+ stub(MyScript).help(mock.instance_of(Thor::Base.shell), :task => nil)
MyScript.start(["help"])
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.