boson 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,7 +33,6 @@ module Boson
33
33
  extend self
34
34
  # Handles all Scientist errors.
35
35
  class Error < StandardError; end
36
- class EscapeGlobalOption < StandardError; end
37
36
 
38
37
  attr_accessor :global_options, :rendered
39
38
  @no_option_commands ||= []
@@ -83,36 +82,63 @@ module Boson
83
82
  @option_commands[cmd] ||= OptionCommand.new(cmd)
84
83
  end
85
84
 
86
- def translate_and_render(obj, command, args)
87
- @global_options, @command = {}, command
88
- args = translate_args(obj, command, args)
89
- if @global_options[:verbose] || @global_options[:pretend]
90
- puts "Arguments: #{args.inspect}", "Global options: #{@global_options.inspect}"
91
- end
92
- return @rendered = true if @global_options[:pretend]
93
- render_or_raw yield(args)
94
- rescue EscapeGlobalOption
95
- Boson.invoke(:usage, command.name, :verbose=>@global_options[:verbose]) if @global_options[:help]
85
+ def call_original_command(args, &block)
86
+ block.call(args)
87
+ end
88
+
89
+ def translate_and_render(obj, command, args, &block)
90
+ @global_options, @command, original_args = {}, command, args.dup
91
+ args = translate_args(obj, args)
92
+ return run_help_option if @global_options[:help]
93
+ run_pretend_option(args)
94
+ render_or_raw call_original_command(args, &block) unless @global_options[:pretend]
95
+ rescue OptionCommand::CommandArgumentError
96
+ run_pretend_option(args ||= [])
97
+ return if !@global_options[:pretend] && run_verbose_help(option_command, original_args)
98
+ raise unless @global_options[:pretend]
96
99
  rescue OptionParser::Error, Error
97
- $stderr.puts "Error: " + $!.message
100
+ raise if Runner.in_shell?
101
+ message = @global_options[:verbose] ? "#{$!}\n#{$!.backtrace.inspect}" : $!.message
102
+ $stderr.puts "Error: " + message
98
103
  end
99
104
 
100
- def translate_args(obj, command, args)
101
- option_command.prepend_default_option(args)
105
+ def translate_args(obj, args)
106
+ option_command.modify_args(args)
102
107
  @global_options, parsed_options, args = option_command.parse(args)
103
- raise EscapeGlobalOption if @global_options[:help]
108
+ return if @global_options[:help]
109
+
110
+ (@global_options[:delete_options] || []).map {|e|
111
+ @global_options.keys.map {|k| k.to_s }.grep(/^#{e}/)
112
+ }.flatten.each {|e| @global_options.delete(e.to_sym) }
113
+
104
114
  if parsed_options
105
115
  option_command.add_default_args(args, obj)
106
- return args if @no_option_commands.include?(command)
116
+ return args if @no_option_commands.include?(@command)
107
117
  args << parsed_options
108
118
  option_command.check_argument_size(args)
109
119
  end
110
120
  args
111
- rescue Error, ArgumentError, EscapeGlobalOption
112
- raise
113
- rescue Exception
114
- message = @global_options[:verbose] ? "#{$!}\n#{$!.backtrace.inspect}" : $!.message
115
- raise Error, message
121
+ end
122
+
123
+ def run_verbose_help(option_command, original_args)
124
+ global_opts = option_command.parse_global_options(original_args)
125
+ if global_opts[:help] && global_opts[:verbose]
126
+ @global_options = global_opts
127
+ run_help_option
128
+ return true
129
+ end
130
+ false
131
+ end
132
+
133
+ def run_help_option
134
+ Boson.invoke(:usage, @command.name, :verbose=>@global_options[:verbose])
135
+ end
136
+
137
+ def run_pretend_option(args)
138
+ if @global_options[:verbose] || @global_options[:pretend]
139
+ puts "Arguments: #{args.inspect}", "Global options: #{@global_options.inspect}"
140
+ end
141
+ @rendered = true if @global_options[:pretend]
116
142
  end
117
143
 
118
144
  def render_or_raw(result)
@@ -123,9 +149,8 @@ module Boson
123
149
  else
124
150
  Pipe.process(result, @global_options)
125
151
  end
126
- rescue Exception
127
- message = @global_options[:verbose] ? "#{$!}\n#{$!.backtrace.inspect}" : $!.message
128
- raise Error, message
152
+ rescue StandardError
153
+ raise Error, $!.message, $!.backtrace
129
154
  end
130
155
 
131
156
  def render?
data/lib/boson/view.rb CHANGED
@@ -66,6 +66,14 @@ module Boson
66
66
  end
67
67
 
68
68
  #:stopdoc:
69
+ def class_config(klass)
70
+ opts = (Hirb::View.formatter_config[klass] || {}).dup
71
+ opts.delete(:ancestor)
72
+ opts.merge!((opts.delete(:options) || {}).dup)
73
+ OptionParser.make_mergeable!(opts)
74
+ opts
75
+ end
76
+
69
77
  def toggle_pager
70
78
  Hirb::View.toggle_pager
71
79
  end
@@ -7,6 +7,10 @@ module Boson
7
7
  ArgumentInspector.scrape_with_text(file_string, "blah")
8
8
  end
9
9
 
10
+ test "parses arguments of class method" do
11
+ args_from(" def YAML.blah( filepath )\n").should == [['filepath']]
12
+ end
13
+
10
14
  test "parses arguments with no spacing" do
11
15
  args_from("def bong; end\ndef blah(arg1,arg2='val2')\nend").should == [["arg1"], ['arg2', "'val2'"]]
12
16
  end
@@ -55,8 +55,8 @@ module Boson
55
55
  end
56
56
 
57
57
  test "global option takes value with whitespace" do
58
- View.expects(:render).with(anything, {:vertical=>true, :fields => [:name, :lib]}, anything)
59
- start('commands', '-g', 'f=name,lib vertical')
58
+ View.expects(:render).with {|*args| args[1][:fields] = %w{f1 f2} }
59
+ start('commands', '-f', 'f1, f2')
60
60
  end
61
61
 
62
62
  test "execute option errors are caught" do
@@ -30,8 +30,10 @@ module Boson
30
30
  command_exists?('blah')
31
31
  end
32
32
 
33
- test "prints error for file library with no module" do
34
- capture_stderr { load(:blah, :file_string=>"def blah; end") }.should =~ /Can't.*at least/
33
+ test "loads a plugin library by creating its module" do
34
+ load(:blah, :file_string=>"def blah; end")
35
+ library_has_module('blah', "Boson::Commands::Blah")
36
+ command_exists?('blah', false)
35
37
  end
36
38
 
37
39
  test "prints error for file library with multiple modules" do
data/test/loader_test.rb CHANGED
@@ -18,20 +18,20 @@ module Boson
18
18
 
19
19
  # if this test fails, other exists? using methods fail
20
20
  test "from callback recursively merges with user's config" do
21
- with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:description=>'bling', :options=>{:num=>3}}}}}) do
21
+ with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:desc=>'bling', :options=>{:num=>3}}}}}) do
22
22
  File.stubs(:exists?).returns(true)
23
23
  load :blah, :file_string=> "module Blah; def self.config; {:commands=>{'blang'=>{:alias=>'ba'}, " +
24
24
  "'bling'=>{:options=>{:verbose=>:boolean}}}}; end; end"
25
25
  library('blah').command_object('bling').options.should == {:verbose=>:boolean, :num=>3}
26
- library('blah').command_object('bling').description.should == 'bling'
26
+ library('blah').command_object('bling').desc.should == 'bling'
27
27
  library('blah').command_object('blang').alias.should == 'ba'
28
28
  end
29
29
  end
30
30
 
31
31
  test "non-hash from inspector overridden by user's config" do
32
- with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:description=>'already'}}}}) do
32
+ with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:desc=>'already'}}}}) do
33
33
  load :blah, :file_string=>"module Blah; #from file\ndef bling; end; end"
34
- library('blah').command_object('bling').description.should == 'already'
34
+ library('blah').command_object('bling').desc.should == 'already'
35
35
  end
36
36
  end
37
37
 
@@ -116,7 +116,12 @@ module Boson
116
116
  parse("--no-f")["foo"].should == false
117
117
  parse("--foo")["foo"].should == true
118
118
  end
119
-
119
+
120
+ it "accepts --[no-]opt variant for single letter booleans" do
121
+ create :e=>true
122
+ parse("--no-e")[:e].should == false
123
+ end
124
+
120
125
  it "will prefer 'no-opt' variant over inverting 'opt' if explicitly set" do
121
126
  create "--no-foo" => true
122
127
  parse("--no-foo")["no-foo"].should == true
@@ -166,6 +171,13 @@ module Boson
166
171
  @opt.non_opts.should == ["foo", "bar", "--baz", "-T", "bang"]
167
172
  end
168
173
 
174
+ it "stopped by --" do
175
+ create :foo=>:boolean, :dude=>:boolean
176
+ parse("foo", "bar", "--", "-f").should == {}
177
+ @opt.leading_non_opts.should == %w{foo bar}
178
+ @opt.trailing_non_opts.should == %w{-- -f}
179
+ end
180
+
169
181
  context "with parse flag" do
170
182
  it ":delete_invalid_opts deletes and warns of invalid options" do
171
183
  create(:foo=>:boolean)
@@ -175,6 +187,16 @@ module Boson
175
187
  @opt.non_opts.should == ['ok']
176
188
  end
177
189
 
190
+ it ":delete_invalid_opts deletes until - or --" do
191
+ create(:foo=>:boolean, :bar=>:boolean)
192
+ %w{- --}.each do |stop_char|
193
+ capture_stderr {
194
+ @opt.parse(%w{ok -b -d} << stop_char << '-f', :delete_invalid_opts=>true)
195
+ }.should =~ /'-d'/
196
+ @opt.non_opts.should == %w{ok -d} << stop_char << '-f'
197
+ end
198
+ end
199
+
178
200
  it ":opts_before_args only allows options before args" do
179
201
  create(:foo=>:boolean)
180
202
  @opt.parse(%w{ok -f}, :opts_before_args=>true).should == {}
@@ -345,7 +367,8 @@ module Boson
345
367
  context ":array type" do
346
368
  before(:all) {
347
369
  create :a=>:array, :b=>[1,2,3], :c=>{:type=>:array, :values=>%w{foo fa bar zebra}, :enum=>false},
348
- :d=>{:type=>:array, :split=>" ", :values=>[:ab, :bc, :cd], :enum=>false}
370
+ :d=>{:type=>:array, :split=>" ", :values=>[:ab, :bc, :cd], :enum=>false},
371
+ :e=>{:type=>:array, :values=>%w{some so silly}, :regexp=>true}
349
372
  }
350
373
 
351
374
  it "supports array defaults" do
@@ -376,6 +399,10 @@ module Boson
376
399
  parse("-c", '*')[:c].sort.should == %w{bar fa foo zebra}
377
400
  parse("-c", '*,ok')[:c].sort.should == %w{bar fa foo ok zebra}
378
401
  end
402
+
403
+ it "aliases correctly with :regexp on" do
404
+ parse("-e", 'so')[:e].sort.should == %w{so some}
405
+ end
379
406
  end
380
407
 
381
408
  context ":hash type" do
data/test/runner_test.rb CHANGED
@@ -35,7 +35,7 @@ module Boson
35
35
  start(:autoload_libraries=>true)
36
36
  Index.expects(:read)
37
37
  Index.expects(:find_library).with('blah').returns('blah')
38
- Manager.expects(:load).with('blah', :verbose=>true)
38
+ Manager.expects(:load).with('blah', :verbose=>nil)
39
39
  Boson.main_object.blah
40
40
  end
41
41
  end
@@ -25,6 +25,7 @@ module Boson
25
25
  end
26
26
  @opt_cmd = Object.new.extend Blah
27
27
  }
28
+ after(:all) { Runner.in_shell = false }
28
29
 
29
30
  def command(hash, args)
30
31
  hash = {:name=>'blah', :lib=>'bling', :options=>{:force=>:boolean, :level=>2}}.merge(hash)
@@ -131,18 +132,13 @@ module Boson
131
132
  capture_stderr { command_with_args('a1 -l') }.should =~ /Error.*level/
132
133
  end
133
134
 
134
- test "with unexpected error in translation" do
135
- Scientist.expects(:option_command).raises("unexpected")
136
- capture_stderr { command_with_args('a1') }.should =~ /Error.*unexpected/
137
- end
138
-
139
135
  test "with unexpected error in render" do
140
136
  Scientist.expects(:render?).raises("unexpected")
141
137
  capture_stderr { command_with_args('a1') }.should =~ /Error.*unexpected/
142
138
  end
143
139
 
144
140
  test "with no argument defined for options" do
145
- assert_error(ArgumentError, '2 for 1') { command({:args=>1}, 'ok') }
141
+ assert_error(OptionCommand::CommandArgumentError, '2 for 1') { command({:args=>1}, 'ok') }
146
142
  end
147
143
  end
148
144
 
@@ -170,17 +166,17 @@ module Boson
170
166
  capture_stdout { command_with_args("-p ok") } =~ /Arguments.*ok/
171
167
  end
172
168
 
173
- test "with not enough args raises ArgumentError" do
174
- args = [ArgumentError, '0 for 1']
169
+ test "with not enough args raises CommandArgumentError" do
170
+ args = [OptionCommand::CommandArgumentError, '0 for 1']
175
171
  assert_error(*args) { command_with_args }
176
172
  assert_error(*args) { command_with_args '' }
177
173
  assert_error(*args) { command_with_arg_size }
178
174
  assert_error(*args) { command_with_arg_size '' }
179
175
  end
180
176
 
181
- test "with too many args raises ArgumentError" do
177
+ test "with too many args raises CommandArgumentError" do
182
178
  args3 = [ArgumentError, '3 for 2']
183
- args4 = [ArgumentError, '4 for 2']
179
+ args4 = [OptionCommand::CommandArgumentError, '4 for 2']
184
180
  assert_error(*args3) { command_with_args 1,2,3 }
185
181
  assert_error(*args4) { command_with_args '1 2 3' }
186
182
  assert_error(*args3) { command_with_arg_size 1,2,3 }
@@ -252,7 +248,7 @@ module Boson
252
248
  end
253
249
 
254
250
  test "parses normally from cmdline" do
255
- Boson.expects(:const_defined?).returns true
251
+ Runner.expects(:in_shell?).times(2).returns true
256
252
  command(@cmd_attributes, ['--force', '--level=3']).should == {:level=>3, :force=>true}
257
253
  end
258
254
 
@@ -269,7 +265,7 @@ module Boson
269
265
  end
270
266
 
271
267
  test "prepends correctly from cmdline" do
272
- Boson.expects(:const_defined?).returns true
268
+ Runner.expects(:in_shell?).times(2).returns true
273
269
  command(@cmd_attributes, ['3','-f']).should == {:level=>3, :force=>true}
274
270
  end
275
271
  end
@@ -307,6 +303,11 @@ module Boson
307
303
  end
308
304
  end
309
305
 
306
+ test "delete_options deletes global options" do
307
+ local_and_global('--delete_options=r,p -rp -f').should ==
308
+ [{:foo=>true}, {:delete_options=>["r", "p"]}]
309
+ end
310
+
310
311
  test "global option after local one is invalid" do
311
312
  args_arr = ['-f --dude', '-f doh --dude', '-f --dude doh', [:doh, '-f --dude'] ]
312
313
  args_arr.each_with_index do |args, i|
@@ -317,12 +318,16 @@ module Boson
317
318
  end
318
319
  end
319
320
 
320
- test "--global option adds additional global options" do
321
- local_and_global('-g=d -d').should == [{:do=>true}, {:dude=>true, :global=>'d'}]
322
- local_and_global('-g "r dude" -d').should == [{:do=>true},
323
- {:global=>"r dude", :dude=>true, :render=>true}]
321
+ test "global option after local one and -" do
322
+ local_and_global("doh -r -f - --dude").should == [{:foo=>true}, {:dude=>true, :render=>true}]
324
323
  end
325
- end
326
324
 
325
+ test "no options parsed after --" do
326
+ local_and_global('doh -f -- -r').should == [{:foo=>true}, {}]
327
+ local_and_global('doh -- -r -f').should == [{}, {}]
328
+ local_and_global('-- -r -f').should == [{}, {}]
329
+ local_and_global('doh -r -- -f').should == [{}, {:render=>true}]
330
+ end
331
+ end
327
332
  end
328
333
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Horner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-19 00:00:00 -05:00
12
+ date: 2010-01-23 00:00:00 -05:00
13
13
  default_executable: boson
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -81,7 +81,6 @@ files:
81
81
  - test/argument_inspector_test.rb
82
82
  - test/bin_runner_test.rb
83
83
  - test/comment_inspector_test.rb
84
- - test/config/index.marshal
85
84
  - test/file_library_test.rb
86
85
  - test/loader_test.rb
87
86
  - test/manager_test.rb
Binary file