Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More work on changing from rr to spec mock framework

  • Loading branch information...
commit f32bdbf0cd547171df4f50342a10aac9b2ef3f52 1 parent 7f892ff
@josevalim josevalim authored
View
62 lib/thor.rb
@@ -139,42 +139,42 @@ def start(given_args=ARGV, config={})
end
end
- # Prints help information. If a task name is given, it shows information
- # only about the specific task.
+ # Prints help information for the given task.
#
# ==== Parameters
- # meth<String>:: An optional task name to print usage information about.
- #
- # ==== Options
- # namespace:: When true, shows the namespace in the output before the usage.
- # skip_inherited:: When true, does not show tasks from superclass.
- #
- def help(shell, options={})
- if options[:task]
- task = all_tasks[options[:task]]
- raise UndefinedTaskError, "task '#{options[:task]}' could not be found in namespace '#{self.namespace}'" unless task
-
- shell.say "Usage:"
- shell.say " #{banner(task)}"
- shell.say
- class_options_help(shell, nil => task.options.map { |_, o| o })
- shell.say task.description
- else
- list = printable_tasks
-
- Thor::Util.thor_classes_in(self).each do |klass|
- list += klass.printable_tasks(false)
- end
-
- list.sort!{ |a,b| a[0] <=> b[0] }
+ # shell<Thor::Shell>
+ # task_name<String>
+ #
+ def task_help(shell, task_name)
+ task = all_tasks[task_name]
+ raise UndefinedTaskError, "task '#{task_name}' could not be found in namespace '#{self.namespace}'" unless task
+
+ shell.say "Usage:"
+ shell.say " #{banner(task)}"
+ shell.say
+ class_options_help(shell, nil => task.options.map { |_, o| o })
+ shell.say task.description
+ end
- shell.say "Tasks:"
- shell.print_table(list, :ident => 2, :truncate => true)
- shell.say
- class_options_help(shell)
+ # Prints help information for this class.
+ #
+ # ==== Parameters
+ # shell<Thor::Shell>
+ #
+ def help(shell)
+ list = printable_tasks
+ Thor::Util.thor_classes_in(self).each do |klass|
+ list += klass.printable_tasks(false)
end
+ list.sort!{ |a,b| a[0] <=> b[0] }
+
+ shell.say "Tasks:"
+ shell.print_table(list, :ident => 2, :truncate => true)
+ shell.say
+ class_options_help(shell)
end
+ # Returns tasks ready to be printed.
def printable_tasks(all=true)
(all ? all_tasks : tasks).map do |_, task|
item = []
@@ -235,6 +235,6 @@ def normalize_task_name(meth) #:nodoc:
desc "help [TASK]", "Describe available tasks or one specific task"
def help(task=nil)
- self.class.help(shell, :task => task)
+ task ? self.class.task_help(shell, task) : self.class.help(shell)
end
end
View
3  lib/thor/group.rb
@@ -41,7 +41,7 @@ def start(given_args=ARGV, config={})
# ==== Options
# short:: When true, shows only usage.
#
- def help(shell, options={})
+ def help(shell)
shell.say "Usage:"
shell.say " #{banner}\n"
shell.say
@@ -210,6 +210,7 @@ def get_options_from_invocations(group_options, base_options) #:nodoc:
end
end
+ # Returns tasks ready to be printed.
def printable_tasks(*)
item = []
item << banner
View
2  lib/thor/runner.rb
@@ -253,7 +253,7 @@ def thorfiles_relevant_to(meth)
def display_klasses(with_modules=false, show_internal=false, klasses=Thor::Base.subclasses)
klasses -= [Thor, Thor::Runner, Thor::Group] unless show_internal
- raise Error, "No Thor tasks available" if klasses.empty?
+ raise Error, "No Thor tasks available" if klasses.empty?
show_modules if with_modules && !thor_yaml.empty?
# Remove subclasses
View
1  lib/thor/util.rb
@@ -78,6 +78,7 @@ def self.namespaces_in_content(contents, file=__FILE__)
def self.thor_classes_in(klass)
stringfied_constants = klass.constants.map { |c| c.to_s }
Thor::Base.subclasses.select do |subclass|
+ next unless subclass.name
stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
end
end
View
2  spec/actions/directory_spec.rb
@@ -4,7 +4,7 @@
describe Thor::Actions::Directory do
before(:each) do
::FileUtils.rm_rf(destination_root)
- stub(invoker).file_name{ "rdoc" }
+ invoker.stub!(:file_name).and_return("rdoc")
end
def invoker
View
22 spec/actions_spec.rb
@@ -195,7 +195,7 @@ def file
@template.instance_eval "def read; self; end" # Make the string respond to read
@file = "http://gist.github.com/103208.txt"
- mock(runner).open(@file){ @template }
+ runner.should_receive(:open).and_return(@template)
end
it "opens a file and executes its content in the instance binding" do
@@ -220,7 +220,7 @@ def file
describe "#run" do
before(:each) do
- mock(runner).system("ls")
+ runner.should_receive(:system).with("ls")
end
it "executes the command given" do
@@ -236,15 +236,15 @@ def file
end
it "accepts a color as status" do
- mock(runner.shell).say_status(:run, 'ls from "."', :yellow)
+ runner.shell.should_receive(:say_status).with(:run, 'ls from "."', :yellow)
action :run, "ls", :verbose => :yellow
end
end
describe "#run_ruby_script" do
before(:each) do
- stub(Thor::Util).ruby_command{ "/opt/jruby" }
- mock(runner).system("/opt/jruby script.rb")
+ Thor::Util.stub!(:ruby_command).and_return("/opt/jruby")
+ runner.should_receive(:system).with("/opt/jruby script.rb")
end
it "executes the ruby script" do
@@ -262,30 +262,30 @@ def file
describe "#thor" do
it "executes the thor command" do
- mock(runner).system("thor list")
+ runner.should_receive(:system).with("thor list")
action :thor, :list, :verbose => true
end
it "converts extra arguments to command arguments" do
- mock(runner).system("thor list foo bar")
+ runner.should_receive(:system).with("thor list foo bar")
action :thor, :list, "foo", "bar"
end
it "converts options hash to switches" do
- mock(runner).system("thor list foo bar --foo")
+ runner.should_receive(:system).with("thor list foo bar --foo")
action :thor, :list, "foo", "bar", :foo => true
- mock(runner).system("thor list --foo 1 2 3")
+ runner.should_receive(:system).with("thor list --foo 1 2 3")
action :thor, :list, :foo => [1,2,3]
end
it "logs status" do
- mock(runner).system("thor list")
+ runner.should_receive(:system).with("thor list")
action(:thor, :list).must == " run thor list from \".\"\n"
end
it "does not log status if required" do
- mock(runner).system("thor list --foo 1 2 3")
+ runner.should_receive(:system).with("thor list --foo 1 2 3")
action(:thor, :list, :foo => [1,2,3], :verbose => false).must be_empty
end
end
View
86 spec/shell/basic_spec.rb
@@ -17,81 +17,81 @@ def shell
describe "#ask" do
it "prints a message to the user and gets the response" do
- mock($stdout).print("Should I overwrite it? ")
- mock($stdin).gets{ "Sure" }
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
+ $stdin.should_receive(:gets).and_return('Sure')
shell.ask("Should I overwrite it?").must == "Sure"
end
end
describe "#yes?" do
it "asks the user and returns true if the user replies yes" do
- mock($stdout).print("Should I overwrite it? ")
- mock($stdin).gets{ "y" }
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
+ $stdin.should_receive(:gets).and_return('y')
shell.yes?("Should I overwrite it?").must be_true
- mock($stdout).print("Should I overwrite it? ")
- mock($stdin).gets{ "n" }
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
+ $stdin.should_receive(:gets).and_return('n')
shell.yes?("Should I overwrite it?").must_not be_true
end
end
describe "#no?" do
it "asks the user and returns true if the user replies no" do
- mock($stdout).print("Should I overwrite it? ")
- mock($stdin).gets{ "n" }
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
+ $stdin.should_receive(:gets).and_return('n')
shell.no?("Should I overwrite it?").must be_true
- mock($stdout).print("Should I overwrite it? ")
- mock($stdin).gets{ "Yes" }
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
+ $stdin.should_receive(:gets).and_return('Yes')
shell.no?("Should I overwrite it?").must be_false
end
end
describe "#say" do
it "prints a message to the user" do
- mock($stdout).puts("Running...")
+ $stdout.should_receive(:puts).with("Running...")
shell.say("Running...")
end
it "prints a message to the user without new line if it ends with a whitespace" do
- mock($stdout).print("Running... ")
+ $stdout.should_receive(:print).with("Running... ")
shell.say("Running... ")
end
it "prints a message to the user without new line" do
- mock($stdout).print("Running...")
+ $stdout.should_receive(:print).with("Running...")
shell.say("Running...", nil, false)
end
end
describe "#say_status" do
it "prints a message to the user with status" do
- mock($stdout).puts(" create ~/.thor/task.thor")
+ $stdout.should_receive(:puts).with(" create ~/.thor/task.thor")
shell.say_status(:create, "~/.thor/task.thor")
end
it "always use new line" do
- mock($stdout).puts(" create ")
+ $stdout.should_receive(:puts).with(" create ")
shell.say_status(:create, "")
end
it "does not print a message if base is set to quiet" do
base = MyCounter.new [1,2]
- mock(base).options { Hash.new(:quiet => true) }
+ base.should_receive(:options).and_return(:quiet => true)
- dont_allow($stdout).puts
+ $stdout.should_not_receive(:puts)
shell.base = base
shell.say_status(:created, "~/.thor/task.thor")
end
it "does not print a message if log status is set to false" do
- dont_allow($stdout).puts
+ $stdout.should_not_receive(:puts)
shell.say_status(:created, "~/.thor/task.thor", false)
end
it "uses padding to set messages left margin" do
shell.padding = 2
- mock($stdout).puts(" create ~/.thor/task.thor")
+ $stdout.should_receive(:puts).with(" create ~/.thor/task.thor")
shell.say_status(:create, "~/.thor/task.thor")
end
end
@@ -123,7 +123,7 @@ def shell
end
it "uses maximum terminal width" do
- mock(shell).terminal_width { 20 }
+ shell.should_receive(:terminal_width).and_return(20)
content = capture(:stdout){ shell.print_table(@table, :ident => 2, :truncate => true) }
content.must == <<-TABLE
abc #123 firs...
@@ -135,41 +135,41 @@ def shell
describe "#file_collision" do
it "shows a menu with options" do
- mock($stdout).print('Overwrite foo? (enter "h" for help) [Ynaqh] ')
- mock($stdin).gets{ 'n' }
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
+ $stdin.should_receive(:gets).and_return('n')
shell.file_collision('foo')
end
it "returns true if the user choose default option" do
- stub($stdout).print
- mock($stdin).gets{ '' }
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('')
shell.file_collision('foo').must be_true
end
it "returns false if the user choose no" do
- stub($stdout).print
- mock($stdin).gets{ 'n' }
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('n')
shell.file_collision('foo').must be_false
end
it "returns true if the user choose yes" do
- stub($stdout).print
- mock($stdin).gets{ 'y' }
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('y')
shell.file_collision('foo').must be_true
end
it "shows help usage if the user choose help" do
- stub($stdout).print
- mock($stdin).gets{ 'h' }
- mock($stdin).gets{ 'n' }
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('h')
+ $stdin.should_receive(:gets).and_return('n')
help = capture(:stdout){ shell.file_collision('foo') }
help.must =~ /h \- help, show this help/
end
it "quits if the user choose quit" do
- stub($stdout).print
- mock($stdout).puts('Aborting...')
- mock($stdin).gets{ 'q' }
+ $stdout.stub!(:print)
+ $stdout.should_receive(:puts).with('Aborting...')
+ $stdin.should_receive(:gets).and_return('q')
lambda {
shell.file_collision('foo')
@@ -177,27 +177,27 @@ def shell
end
it "always returns true if the user choose always" do
- mock($stdout).print('Overwrite foo? (enter "h" for help) [Ynaqh] ')
- mock($stdin).gets{ 'a' }
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
+ $stdin.should_receive(:gets).and_return('a')
shell.file_collision('foo').must be_true
- dont_allow($stdout).print
+ $stdout.should_not_receive(:print)
shell.file_collision('foo').must be_true
end
describe "when a block is given" do
it "displays diff options to the user" do
- mock($stdout).print('Overwrite foo? (enter "h" for help) [Ynaqdh] ')
- mock($stdin).gets{ 's' }
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ')
+ $stdin.should_receive(:gets).and_return('s')
shell.file_collision('foo'){ }
end
it "invokes the diff command" do
- stub($stdout).print
- mock($stdin).gets{ 'd' }
- mock($stdin).gets{ 'n' }
- mock(shell).system(/diff -u/)
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('d')
+ $stdin.should_receive(:gets).and_return('n')
+ shell.should_receive(:system).with(/diff -u/)
capture(:stdout){ shell.file_collision('foo'){ } }
end
end
View
12 spec/shell/color_spec.rb
@@ -7,19 +7,19 @@ def shell
describe "#say" do
it "set the color if specified" do
- mock($stdout).puts("\e[32mWow! Now we have colors!\e[0m")
+ $stdout.should_receive(:puts).with("\e[32mWow! Now we have colors!\e[0m")
shell.say "Wow! Now we have colors!", :green
end
it "does not use a new line even with colors" do
- mock($stdout).print("\e[32mWow! Now we have colors! \e[0m")
+ $stdout.should_receive(:print).with("\e[32mWow! Now we have colors! \e[0m")
shell.say "Wow! Now we have colors! ", :green
end
end
describe "#say_status" do
it "uses color to say status" do
- mock($stdout).puts("\e[1m\e[31m conflict\e[0m README")
+ $stdout.should_receive(:puts).with("\e[1m\e[31m conflict\e[0m README")
shell.say_status :conflict, "README", :red
end
end
@@ -27,9 +27,9 @@ def shell
describe "#file_collision" do
describe "when a block is given" do
it "invokes the diff command" do
- stub($stdout).print
- mock($stdin).gets{ 'd' }
- mock($stdin).gets{ 'n' }
+ $stdout.stub!(:print)
+ $stdin.should_receive(:gets).and_return('d')
+ $stdin.should_receive(:gets).and_return('n')
output = capture(:stdout){ shell.file_collision('spec/fixtures/doc/README'){ "README\nEND\n" } }
output.must =~ /\e\[31m\- __start__\e\[0m/
View
1  spec/spec_helper.rb
@@ -5,7 +5,6 @@
require 'stringio'
require 'rubygems'
-require 'rr'
require 'rdoc'
require 'diff/lcs' # You need diff/lcs installed to run specs (but not to run Thor).
View
20 spec/task_spec.rb
@@ -11,20 +11,20 @@ def task(options={})
describe "#formatted_usage" do
it "includes namespace within usage" do
- stub(Object).namespace{ "foo" }
- stub(Object).arguments{ [] }
+ Object.stub!(:namespace).and_return("foo")
+ Object.stub!(:arguments).and_return([])
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{ [] }
+ Object.stub!(:namespace).and_return("default:foo")
+ Object.stub!(:arguments).and_return([])
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) ] }
+ Object.stub!(:namespace).and_return("foo")
+ Object.stub!(:arguments).and_return([ Thor::Argument.new(:bar, nil, true, :string) ])
task(:foo => :required).formatted_usage(Object).must == "foo:can_has BAR --foo=FOO"
end
end
@@ -54,15 +54,17 @@ def task(options={})
describe "#run" do
it "runs a task by calling a method in the given instance" do
- mock = mock!.send("can_has", 1, 2, 3).subject
+ mock = mock()
+ mock.should_receive(:send).with("can_has", 1, 2, 3)
task.run(mock, [1, 2, 3])
end
it "raises an error if the method to be invoked is private" do
- mock = mock!.private_methods{ [ 'can_has' ] }.subject
+ mock = mock()
+ mock.should_receive(:private_methods).and_return(['can_has'])
lambda {
task.run(mock)
- }.must raise_error(Thor::UndefinedTaskError, "the 'can_has' task of Object is private")
+ }.must raise_error(Thor::UndefinedTaskError, "the 'can_has' task of Spec::Mocks::Mock is private")
end
end
end
View
16 spec/thor_spec.rb
@@ -156,7 +156,7 @@ def shell
end
it "uses the maximum terminal size to show tasks" do
- stub(@shell).terminal_width { 80 }
+ @shell.should_receive(:terminal_width).and_return(80)
content = capture(:stdout){ MyScript.help(shell) }
content.must =~ /aaa\.\.\.$/
end
@@ -184,7 +184,7 @@ 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, :task => "foo") }.must == <<END
+ capture(:stdout) { MyScript.task_help(shell, "foo") }.must == <<END
Usage:
thor my_script:foo BAR
@@ -199,21 +199,19 @@ def shell
it "raises an error if the task can't be found" do
lambda {
- MyScript.help(shell, :task => "unknown")
+ MyScript.task_help(shell, "unknown")
}.must raise_error(Thor::Error, "task 'unknown' could not be found in namespace 'my_script'")
end
end
- describe "options" do
- it "does not show superclass tasks if required" do
- capture(:stdout){ MyScript.help(shell, :short => true) }.must_not =~ /help/
- end
- end
-
describe "instance method" do
it "calls the class method" do
capture(:stdout){ MyScript.start(["help"]) }.must =~ /Tasks:/
end
+
+ it "calls the class method" do
+ capture(:stdout){ MyScript.start(["help", "foo"]) }.must =~ /Usage:/
+ end
end
end
View
14 spec/util_spec.rb
@@ -129,7 +129,7 @@ def self.clear_user_home!
describe "#user_home" do
before(:each) do
- stub(ENV)[]
+ ENV.stub!(:[])
Thor::Util.clear_user_home!
end
@@ -138,7 +138,7 @@ def self.clear_user_home!
end
it "returns the *unix system path if file cannot be expanded and separator does not exist" do
- stub(File).expand_path("~"){ raise }
+ File.should_receive(:expand_path).with("~").and_raise(RuntimeError)
previous_value = File::ALT_SEPARATOR
capture(:stderr){ File.const_set(:ALT_SEPARATOR, false) }
Thor::Util.user_home.must == "/"
@@ -146,7 +146,7 @@ def self.clear_user_home!
end
it "returns the windows system path if file cannot be expanded and a separator exists" do
- stub(File).expand_path("~"){ raise }
+ File.should_receive(:expand_path).with("~").and_raise(RuntimeError)
previous_value = File::ALT_SEPARATOR
capture(:stderr){ File.const_set(:ALT_SEPARATOR, true) }
Thor::Util.user_home.must == "C:/"
@@ -154,18 +154,18 @@ def self.clear_user_home!
end
it "returns HOME/.thor if set" do
- stub(ENV)["HOME"].returns{ "/home/user/" }
+ ENV.stub!(:[]).with("HOME").and_return("/home/user/")
Thor::Util.user_home.must == "/home/user/"
end
it "returns path with HOMEDRIVE and HOMEPATH if set" do
- stub(ENV)["HOMEDRIVE"].returns{ "D:/" }
- stub(ENV)["HOMEPATH"].returns{ "Documents and Settings/James" }
+ ENV.stub!(:[]).with("HOMEDRIVE").and_return("D:/")
+ ENV.stub!(:[]).with("HOMEPATH").and_return("Documents and Settings/James")
Thor::Util.user_home.must == "D:/Documents and Settings/James"
end
it "returns APPDATA/.thor if set" do
- stub(ENV)["APPDATA"].returns{ "/home/user/" }
+ ENV.stub!(:[]).with("APPDATA").and_return("/home/user/")
Thor::Util.user_home.must == "/home/user/"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.