Permalink
Browse files

Rename commands to `always_trace!`/`never_trace!`

Improve spec to test behavior of commands
  • Loading branch information...
1 parent cf4c063 commit 0198e3f3a69e6458c1ebff82f049362c440ec347 @ggilder ggilder committed Feb 11, 2014
Showing with 70 additions and 21 deletions.
  1. +13 −1 README.md
  2. +4 −4 lib/commander/delegates.rb
  3. +12 −10 lib/commander/runner.rb
  4. +41 −6 spec/runner_spec.rb
View
@@ -352,7 +352,19 @@ This abstraction could be utilized to generate HTML documentation for your execu
### Tracing
-By default the -t and --trace global options are set which allow you to get a backtrace to aid debugging. You may not want this in your program though, the ````enable_tracing```` and ````disable_tracing```` symbols allow you to control this behaviour.
+By default the `-t` and `--trace` global options are provided to allow users to get a backtrace to aid debugging.
+
+You can disable these options:
+
+```ruby
+never_trace!
+```
+
+Or make it always on:
+
+```ruby
+always_trace!
+```
## Tips
@@ -1,14 +1,14 @@
module Commander
module Delegates
- %w( add_command command program run! global_option
- commands alias_command default_command
- enable_tracing disable_tracing ).each do |meth|
+ %w( add_command command program run! global_option
+ commands alias_command default_command
+ always_trace! never_trace! ).each do |meth|
eval <<-END, binding, __FILE__, __LINE__
def #{meth} *args, &block
::Commander::Runner.instance.#{meth} *args, &block
end
END
end
end
-end
+end
@@ -48,7 +48,7 @@ def self.instance
# Run command parsing and execution process.
def run!
- trace = @tracing_enabled || false
+ trace = @always_trace || false
require_program :version, :description
trap('INT') { abort program(:int_message) } if program(:int_message)
trap('INT') { program(:int_block).call } if program(:int_block)
@@ -58,7 +58,7 @@ def run!
return
end
global_option('-v', '--version', 'Display version information') { say version; return }
- global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true } unless @tracing_disabled
+ global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true } unless @never_trace
parse_global_options
remove_global_options options, @args
unless trace
@@ -72,7 +72,7 @@ def run!
OptionParser::MissingArgument => e
abort e.to_s
rescue => e
- if @tracing_disabled
+ if @never_trace
abort "error: #{e}."
else
abort "error: #{e}. Use --trace to view backtrace"
@@ -91,19 +91,21 @@ def version
end
##
- # Enabled tracing on all executions (bipasses --trace)
+ # Enable tracing on all executions (bypasses --trace)
- def enable_tracing
- @tracing_enabled = true
+ def always_trace!
+ @always_trace = true
+ @never_trace = false
end
##
- # Hide the trace option from the Help Menus and don't add it as a global option
+ # Hide the trace option from the help menus and don't add it as a global option
- def disable_tracing
- @tracing_disabled = true
+ def never_trace!
+ @never_trace = true
+ @always_trace = false
end
-
+
##
# Assign program information.
#
View
@@ -280,7 +280,7 @@
end.run!
}.should raise_error(SystemExit, /error: cookies!. Use --trace/)
end
-
+
it "should display callstack when using this switch" do
lambda {
new_command_runner 'foo', '--trace' do
@@ -290,18 +290,53 @@
end
end
- describe "#enable_tracing" do
+ describe "#always_trace!" do
it "should enable tracing globally, regardless of whether --trace was passed or not" do
- enable_tracing.should eq true
+ lambda {
+ new_command_runner 'foo' do
+ always_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ }.should raise_error(RuntimeError)
end
end
- describe "#disable_tracing" do
+ describe "#never_trace!" do
it "should disable tracing globally, regardless of whether --trace was passed or not" do
- disable_tracing.should eq true
+ lambda {
+ new_command_runner 'help', '--trace' do
+ never_trace!
+ end.run!
+ }.should raise_error(SystemExit, /invalid option: --trace/)
+ end
+
+ it "should not prompt to use --trace switch on errors" do
+ msg = nil
+ begin
+ new_command_runner 'foo' do
+ never_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ rescue SystemExit => e
+ msg = e.message
+ end
+ msg.should match(/error: cookies!/)
+ msg.should_not match(/--trace/)
end
end
-
+
+ context "conflict between #always_trace! and #never_trace!" do
+ it "respects the last used command" do
+ lambda {
+ new_command_runner 'foo' do
+ never_trace!
+ always_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ }.should raise_error(RuntimeError)
+ end
+ end
+
describe "--version" do
it "should output program version" do
run('--version').should eq("test 1.2.3\n")

0 comments on commit 0198e3f

Please sign in to comment.