twig 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,10 +17,14 @@ class Twig
17
17
  opts.each do |key, value|
18
18
  case key
19
19
  when 'branch' then set_option(:branch, value)
20
- when 'except-branch' then set_option(:branch_except, value)
21
- when 'only-branch' then set_option(:branch_only, value)
22
- when 'max-days-old' then set_option(:max_days_old, value)
23
20
  when 'header-style' then set_option(:header_style, value)
21
+ when 'max-days-old' then set_option(:max_days_old, value)
22
+ when /^except-/
23
+ property_name = key.sub(/^except-/, '').to_sym
24
+ set_option(:property_except, property_name => value)
25
+ when /^only-/
26
+ property_name = key.sub(/^only-/, '').to_sym
27
+ set_option(:property_only, property_name => value)
24
28
  end
25
29
  end
26
30
  end
@@ -34,18 +38,31 @@ class Twig
34
38
  else
35
39
  abort %{The branch "#{value}" could not be found.}
36
40
  end
37
- when :branch_except
38
- options[:branch_except] = Regexp.new(value)
39
- when :branch_only
40
- options[:branch_only] = Regexp.new(value)
41
+
41
42
  when :header_style
42
43
  set_header_style_option(value)
44
+
43
45
  when :max_days_old
44
46
  if Twig::Util.numeric?(value)
45
47
  options[:max_days_old] = value.to_f
46
48
  else
47
49
  abort %{The value `--max-days-old=#{value}` is invalid.}
48
50
  end
51
+
52
+ when :property_except
53
+ property_hash = value.inject({}) do |hsh, (property, val)|
54
+ hsh.merge(property => Regexp.new(val))
55
+ end
56
+ options[:property_except] ||= {}
57
+ options[:property_except].merge!(property_hash)
58
+
59
+ when :property_only
60
+ property_hash = value.inject({}) do |hsh, (property, val)|
61
+ hsh.merge(property => Regexp.new(val))
62
+ end
63
+ options[:property_only] ||= {}
64
+ options[:property_only].merge!(property_hash)
65
+
49
66
  when :unset_property
50
67
  options[:unset_property] = value
51
68
  end
@@ -1,3 +1,3 @@
1
1
  class Twig
2
- VERSION = '1.1'
2
+ VERSION = '1.2'
3
3
  end
@@ -82,34 +82,112 @@ describe Twig::Cli do
82
82
  @twig.options[:branch].should == 'test'
83
83
  end
84
84
 
85
- it 'recognizes `--except-branch` and sets a `:branch_except` option' do
86
- @twig.options[:branch_except].should be_nil # Precondition
85
+ it 'recognizes `--max-days-old` and sets a `:max_days_old` option' do
86
+ @twig.options[:max_days_old].should be_nil # Precondition
87
+ @twig.read_cli_options!(%w[--max-days-old 30])
88
+ @twig.options[:max_days_old].should == 30
89
+ end
90
+
91
+ it 'recognizes `--except-branch` and sets a `:property_except` option' do
92
+ @twig.options[:property_except].should be_nil # Precondition
87
93
  @twig.read_cli_options!(%w[--except-branch test])
88
- @twig.options[:branch_except].should == /test/
94
+ @twig.options[:property_except].should == { :branch => /test/ }
89
95
  end
90
96
 
91
- it 'recognizes `--only-branch` and sets a `:branch_only` option' do
92
- @twig.options[:branch_only].should be_nil # Precondition
97
+ it 'recognizes `--only-branch` and sets a `:property_only` option' do
98
+ @twig.options[:property_only].should be_nil # Precondition
93
99
  @twig.read_cli_options!(%w[--only-branch test])
94
- @twig.options[:branch_only].should == /test/
100
+ @twig.options[:property_only].should == { :branch => /test/ }
95
101
  end
96
102
 
97
- it 'recognizes `--max-days-old` and sets a `:max_days_old` option' do
98
- @twig.options[:max_days_old].should be_nil # Precondition
99
- @twig.read_cli_options!(%w[--max-days-old 30])
100
- @twig.options[:max_days_old].should == 30
103
+ context 'with custom property "only" filtering' do
104
+ before :each do
105
+ @twig.options[:property_only].should be_nil # Precondition
106
+ end
107
+
108
+ it 'recognizes `--only-<property>` and sets a `:property_only` option' do
109
+ Twig::Branch.stub(:all_properties) { %w[foo] }
110
+ @twig.read_cli_options!(%w[--only-foo test])
111
+ @twig.options[:property_only].should == { :foo => /test/ }
112
+ end
113
+
114
+ it 'recognizes `--only-branch` and `--only-<property>` together' do
115
+ Twig::Branch.stub(:all_properties) { %w[foo] }
116
+
117
+ @twig.read_cli_options!(%w[--only-branch test --only-foo bar])
118
+
119
+ @twig.options[:property_only].should == {
120
+ :branch => /test/,
121
+ :foo => /bar/
122
+ }
123
+ end
124
+
125
+ it 'does not recognize `--only-<property>` for a missing property' do
126
+ property_name = 'foo'
127
+ Twig::Branch.all_properties.should_not include(property_name) # Precondition
128
+ @twig.stub(:puts)
129
+
130
+ begin
131
+ @twig.read_cli_options!(["--only-#{property_name}", 'test'])
132
+ rescue SystemExit => exception
133
+ expected_exception = exception
134
+ end
135
+
136
+ expected_exception.should_not be_nil
137
+ expected_exception.status.should == 0
138
+ @twig.options[:property_only].should be_nil
139
+ end
140
+ end
141
+
142
+ context 'with custom property "except" filtering' do
143
+ before :each do
144
+ @twig.options[:property_except].should be_nil # Precondition
145
+ end
146
+
147
+ it 'recognizes `--except-<property>` and sets a `:property_except` option' do
148
+ Twig::Branch.stub(:all_properties) { %w[foo] }
149
+ @twig.read_cli_options!(%w[--except-foo test])
150
+ @twig.options[:property_except].should == { :foo => /test/ }
151
+ end
152
+
153
+ it 'recognizes `--except-branch` and `--except-<property>` together' do
154
+ Twig::Branch.stub(:all_properties) { %w[foo] }
155
+
156
+ @twig.read_cli_options!(%w[--except-branch test --except-foo bar])
157
+
158
+ @twig.options[:property_except].should == {
159
+ :branch => /test/,
160
+ :foo => /bar/
161
+ }
162
+ end
163
+
164
+ it 'does not recognize `--except-<property>` for a missing property' do
165
+ property_name = 'foo'
166
+ Twig::Branch.all_properties.should_not include(property_name) # Precondition
167
+ @twig.stub(:puts)
168
+
169
+ begin
170
+ @twig.read_cli_options!(["--except-#{property_name}", 'test'])
171
+ rescue SystemExit => exception
172
+ expected_exception = exception
173
+ end
174
+
175
+ expected_exception.should_not be_nil
176
+ expected_exception.status.should == 0
177
+ @twig.options[:property_except].should be_nil
178
+ end
101
179
  end
102
180
 
103
181
  it 'recognizes `--all` and unsets other options except `:branch`' do
104
182
  @twig.set_option(:max_days_old, 30)
105
- @twig.set_option(:branch_except, /test/)
106
- @twig.set_option(:branch_only, /test/)
183
+ @twig.set_option(:property_except, :branch => /test/)
184
+ @twig.set_option(:property_only, :branch => /test/)
107
185
 
108
186
  @twig.read_cli_options!(['--all'])
109
187
 
110
188
  @twig.options[:max_days_old].should be_nil
111
- @twig.options[:branch_except].should be_nil
112
- @twig.options[:branch_only].should be_nil
189
+ @twig.options[:property_except].should be_nil
190
+ @twig.options[:property_only].should be_nil
113
191
  end
114
192
 
115
193
  it 'recognizes `--unset` and sets an `:unset_property` option' do
@@ -134,27 +212,38 @@ describe Twig::Cli do
134
212
  end
135
213
 
136
214
  it 'handles invalid options' do
137
- @twig.should_receive(:puts) do |message|
138
- message.should include('invalid option: --foo')
215
+ @twig.should_receive(:abort_for_option_exception) do |exception|
216
+ exception.should be_a(OptionParser::InvalidOption)
217
+ exception.message.should include('invalid option: --foo')
139
218
  end
140
- @twig.should_receive(:puts) do |message|
141
- message.should include('`twig --help`')
142
- end
143
- @twig.should_receive(:exit)
144
219
 
145
220
  @twig.read_cli_options!(['--foo'])
146
221
  end
147
222
 
148
223
  it 'handles missing arguments' do
149
- @twig.should_receive(:puts) do |message|
150
- message.should include('missing argument: --branch')
224
+ @twig.should_receive(:abort_for_option_exception) do |exception|
225
+ exception.should be_a(OptionParser::MissingArgument)
226
+ exception.message.should include('missing argument: --branch')
151
227
  end
228
+
229
+ @twig.read_cli_options!(['--branch'])
230
+ end
231
+ end
232
+
233
+ describe '#abort_for_option_exception' do
234
+ before :each do
235
+ @twig = Twig.new
236
+ end
237
+
238
+ it 'prints a message and exits' do
239
+ exception = Exception.new('test exception')
240
+ @twig.should_receive(:puts).with(exception.message)
152
241
  @twig.should_receive(:puts) do |message|
153
242
  message.should include('`twig --help`')
154
243
  end
155
244
  @twig.should_receive(:exit)
156
245
 
157
- @twig.read_cli_options!(['--branch'])
246
+ @twig.abort_for_option_exception(exception)
158
247
  end
159
248
  end
160
249
 
@@ -187,7 +276,6 @@ describe Twig::Cli do
187
276
  # Since we're stubbing `exec` (with an expectation), we still need it
188
277
  # to exit early like the real implementation. The following handles the
189
278
  # exit somewhat gracefully.
190
- expected_exception = nil
191
279
  begin
192
280
  @twig.read_cli_args!(['subcommand'])
193
281
  rescue SystemExit => exception
@@ -214,71 +302,22 @@ describe Twig::Cli do
214
302
  @property_value = 'bar'
215
303
  end
216
304
 
217
- context 'with the current branch' do
218
- before :each do
219
- @twig.should_receive(:current_branch_name).and_return(@branch_name)
220
- end
221
-
222
- it 'gets a property' do
223
- @twig.should_receive(:get_branch_property).
224
- with(@branch_name, @property_name).and_return(@property_value)
225
- @twig.should_receive(:puts).with(@property_value)
226
-
227
- @twig.read_cli_args!([@property_name])
228
- end
229
-
230
- it 'shows an error if getting a property that is not set' do
231
- @twig.should_receive(:get_branch_property).
232
- with(@branch_name, @property_name).and_return(nil)
233
- @twig.should_receive(:abort) do |message|
234
- message.should include(
235
- %{The branch "#{@branch_name}" does not have the property "#{@property_name}"}
236
- )
237
- end
238
-
239
- @twig.read_cli_args!([@property_name])
240
- end
241
-
242
- it 'shows an error if getting a property whose name is an empty string' do
243
- property_name = ' '
244
- error_message = 'test error'
245
- @twig.should_receive(:get_branch_property).
246
- with(@branch_name, property_name) do
247
- raise ArgumentError, error_message
248
- end
249
- @twig.should_receive(:abort).with(error_message)
305
+ it 'gets a property for the current branch' do
306
+ @twig.should_receive(:current_branch_name).and_return(@branch_name)
307
+ @twig.should_receive(:get_branch_property_for_cli).
308
+ with(@branch_name, @property_name)
250
309
 
251
- @twig.read_cli_args!([property_name])
252
- end
310
+ @twig.read_cli_args!([@property_name])
253
311
  end
254
312
 
255
- context 'with a specified branch' do
256
- before :each do
257
- @twig.should_receive(:branch_names).and_return([@branch_name])
258
- @twig.set_option(:branch, @branch_name)
259
- end
260
-
261
- it 'gets a property' do
262
- @twig.should_receive(:get_branch_property).
263
- with(@branch_name, @property_name).and_return(@property_value)
264
- @twig.should_receive(:puts).with(@property_value)
265
-
266
- @twig.read_cli_args!([@property_name])
267
- end
268
-
269
- it 'shows an error if getting a property that is not set' do
270
- @twig.should_receive(:get_branch_property).
271
- with(@branch_name, @property_name).and_return(nil)
272
- @twig.should_receive(:abort) do |message|
273
- message.should include(
274
- %{The branch "#{@branch_name}" does not have the property "#{@property_name}"}
275
- )
276
- end
313
+ it 'gets a property for a specified branch' do
314
+ @twig.should_receive(:branch_names).and_return([@branch_name])
315
+ @twig.set_option(:branch, @branch_name)
316
+ @twig.should_receive(:get_branch_property_for_cli).
317
+ with(@branch_name, @property_name)
277
318
 
278
- @twig.read_cli_args!([@property_name])
279
- end
319
+ @twig.read_cli_args!([@property_name])
280
320
  end
281
-
282
321
  end
283
322
 
284
323
  context 'setting properties' do
@@ -291,10 +330,8 @@ describe Twig::Cli do
291
330
 
292
331
  it 'sets a property for the current branch' do
293
332
  @twig.should_receive(:current_branch_name).and_return(@branch_name)
294
- @twig.should_receive(:set_branch_property).
295
- with(@branch_name, @property_name, @property_value).
296
- and_return(@message)
297
- @twig.should_receive(:puts).with(@message)
333
+ @twig.should_receive(:set_branch_property_for_cli).
334
+ with(@branch_name, @property_name, @property_value)
298
335
 
299
336
  @twig.read_cli_args!([@property_name, @property_value])
300
337
  end
@@ -302,37 +339,12 @@ describe Twig::Cli do
302
339
  it 'sets a property for a specified branch' do
303
340
  @twig.should_receive(:branch_names).and_return([@branch_name])
304
341
  @twig.set_option(:branch, @branch_name)
305
- @twig.should_receive(:set_branch_property).
342
+ @twig.should_receive(:set_branch_property_for_cli).
306
343
  with(@branch_name, @property_name, @property_value).
307
344
  and_return(@message)
308
- @twig.should_receive(:puts).with(@message)
309
345
 
310
346
  @twig.read_cli_args!([@property_name, @property_value])
311
347
  end
312
-
313
- it 'handles ArgumentError when setting an invalid branch property' do
314
- error_message = 'test error'
315
- @twig.should_receive(:current_branch_name).and_return(@branch_name)
316
- @twig.should_receive(:set_branch_property).
317
- with(@branch_name, @property_name, '') do
318
- raise ArgumentError, error_message
319
- end
320
- @twig.should_receive(:abort).with(error_message)
321
-
322
- @twig.read_cli_args!([@property_name, ''])
323
- end
324
-
325
- it 'handles RuntimeError when setting an invalid branch property' do
326
- error_message = 'test error'
327
- @twig.should_receive(:current_branch_name).and_return(@branch_name)
328
- @twig.should_receive(:set_branch_property).
329
- with(@branch_name, @property_name, '') do
330
- raise RuntimeError, error_message
331
- end
332
- @twig.should_receive(:abort).with(error_message)
333
-
334
- @twig.read_cli_args!([@property_name, ''])
335
- end
336
348
  end
337
349
 
338
350
  context 'unsetting properties' do
@@ -345,9 +357,8 @@ describe Twig::Cli do
345
357
 
346
358
  it 'unsets a property for the current branch' do
347
359
  @twig.should_receive(:current_branch_name).and_return(@branch_name)
348
- @twig.should_receive(:unset_branch_property).
349
- with(@branch_name, @property_name).and_return(@message)
350
- @twig.should_receive(:puts).with(@message)
360
+ @twig.should_receive(:unset_branch_property_for_cli).
361
+ with(@branch_name, @property_name)
351
362
 
352
363
  @twig.read_cli_args!([])
353
364
  end
@@ -355,36 +366,133 @@ describe Twig::Cli do
355
366
  it 'unsets a property for a specified branch' do
356
367
  @twig.should_receive(:branch_names).and_return([@branch_name])
357
368
  @twig.set_option(:branch, @branch_name)
358
- @twig.should_receive(:unset_branch_property).
359
- with(@branch_name, @property_name).and_return(@message)
360
- @twig.should_receive(:puts).with(@message)
369
+ @twig.should_receive(:unset_branch_property_for_cli).
370
+ with(@branch_name, @property_name)
361
371
 
362
372
  @twig.read_cli_args!([])
363
373
  end
374
+ end
375
+ end
364
376
 
365
- it 'handles ArgumentError when unsetting an invalid branch property' do
366
- error_message = 'test error'
367
- @twig.should_receive(:current_branch_name).and_return(@branch_name)
368
- @twig.should_receive(:unset_branch_property).
369
- with(@branch_name, @property_name) do
370
- raise ArgumentError, error_message
371
- end
372
- @twig.should_receive(:abort).with(error_message)
377
+ describe '#get_branch_property_for_cli' do
378
+ before :each do
379
+ @twig = Twig.new
380
+ @branch_name = 'test'
381
+ @property_name = 'foo'
382
+ end
373
383
 
374
- @twig.read_cli_args!([])
375
- end
384
+ it 'gets a property' do
385
+ property_value = 'bar'
386
+ @twig.should_receive(:get_branch_property).
387
+ with(@branch_name, @property_name).and_return(property_value)
388
+ @twig.should_receive(:puts).with(property_value)
376
389
 
377
- it 'handles MissingPropertyError when unsetting a missing branch property' do
378
- error_message = 'test error'
379
- @twig.should_receive(:current_branch_name).and_return(@branch_name)
380
- @twig.should_receive(:unset_branch_property).
381
- with(@branch_name, @property_name) do
382
- raise Twig::Branch::MissingPropertyError, error_message
383
- end
384
- @twig.should_receive(:abort).with(error_message)
390
+ @twig.get_branch_property_for_cli(@branch_name, @property_name)
391
+ end
385
392
 
386
- @twig.read_cli_args!([])
387
- end
393
+ it 'shows an error when getting a property that is not set' do
394
+ error_message = 'test error'
395
+ @twig.should_receive(:get_branch_property).
396
+ with(@branch_name, @property_name).and_return(nil)
397
+ Twig::Branch::MissingPropertyError.any_instance.
398
+ stub(:message) { error_message }
399
+ @twig.should_receive(:abort).with(error_message)
400
+
401
+ @twig.get_branch_property_for_cli(@branch_name, @property_name)
402
+ end
403
+
404
+ it 'handles ArgumentError when getting an invalid branch property name' do
405
+ bad_property_name = ''
406
+ error_message = 'test error'
407
+ @twig.should_receive(:get_branch_property).
408
+ with(@branch_name, bad_property_name) do
409
+ raise ArgumentError, error_message
410
+ end
411
+ @twig.should_receive(:abort).with(error_message)
412
+
413
+ @twig.get_branch_property_for_cli(@branch_name, bad_property_name)
414
+ end
415
+ end
416
+
417
+ describe '#set_branch_property_for_cli' do
418
+ before :each do
419
+ @twig = Twig.new
420
+ @branch_name = 'test'
421
+ @property_name = 'foo'
422
+ end
423
+
424
+ it 'sets a property for the specified branch' do
425
+ success_message = 'test success'
426
+ property_value = 'bar'
427
+ @twig.should_receive(:set_branch_property).
428
+ with(@branch_name, @property_name, property_value).
429
+ and_return(success_message)
430
+ @twig.should_receive(:puts).with(success_message)
431
+
432
+ @twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
433
+ end
434
+
435
+ it 'handles ArgumentError when unsetting an invalid branch property name' do
436
+ error_message = 'test error'
437
+ property_value = ''
438
+ @twig.should_receive(:set_branch_property).
439
+ with(@branch_name, @property_name, property_value) do
440
+ raise ArgumentError, error_message
441
+ end
442
+ @twig.should_receive(:abort).with(error_message)
443
+
444
+ @twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
445
+ end
446
+
447
+ it 'handles RuntimeError when Git is unable to set a branch property' do
448
+ error_message = 'test error'
449
+ property_value = ''
450
+ @twig.should_receive(:set_branch_property).
451
+ with(@branch_name, @property_name, property_value) do
452
+ raise RuntimeError, error_message
453
+ end
454
+ @twig.should_receive(:abort).with(error_message)
455
+
456
+ @twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
457
+ end
458
+ end
459
+
460
+ describe '#unset_branch_property_for_cli' do
461
+ before :each do
462
+ @twig = Twig.new
463
+ @branch_name = 'test'
464
+ @property_name = 'foo'
465
+ end
466
+
467
+ it 'unsets a property for the specified branch' do
468
+ success_message = 'test success'
469
+ @twig.should_receive(:unset_branch_property).
470
+ with(@branch_name, @property_name).and_return(success_message)
471
+ @twig.should_receive(:puts).with(success_message)
472
+
473
+ @twig.unset_branch_property_for_cli(@branch_name, @property_name)
474
+ end
475
+
476
+ it 'handles ArgumentError when unsetting an invalid branch property name' do
477
+ error_message = 'test error'
478
+ @twig.should_receive(:unset_branch_property).
479
+ with(@branch_name, @property_name) do
480
+ raise ArgumentError, error_message
481
+ end
482
+ @twig.should_receive(:abort).with(error_message)
483
+
484
+ @twig.unset_branch_property_for_cli(@branch_name, @property_name)
485
+ end
486
+
487
+ it 'handles MissingPropertyError when unsetting a branch property that is not set' do
488
+ error_message = 'test error'
489
+ @twig.should_receive(:unset_branch_property).
490
+ with(@branch_name, @property_name) do
491
+ raise Twig::Branch::MissingPropertyError, error_message
492
+ end
493
+ @twig.should_receive(:abort).with(error_message)
494
+
495
+ @twig.unset_branch_property_for_cli(@branch_name, @property_name)
388
496
  end
389
497
  end
390
498