twig 1.4 → 1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +15 -0
- data/README.md +17 -4
- data/bin/twig-gh-open +1 -1
- data/bin/twig-gh-open-issue +1 -1
- data/bin/twig-gh-update +1 -1
- data/bin/twig-help +1 -1
- data/bin/twig-init-completion-bash +22 -3
- data/bin/twig-rebase +1 -1
- data/lib/twig.rb +23 -13
- data/lib/twig/branch.rb +24 -9
- data/lib/twig/cli.rb +84 -25
- data/lib/twig/commit_time.rb +10 -0
- data/lib/twig/display.rb +9 -0
- data/lib/twig/options.rb +88 -46
- data/lib/twig/subcommands.rb +26 -0
- data/lib/twig/system.rb +9 -0
- data/lib/twig/version.rb +1 -1
- data/spec/spec_helper.rb +7 -0
- data/spec/twig/branch_spec.rb +134 -60
- data/spec/twig/cli_spec.rb +247 -156
- data/spec/twig/commit_time_spec.rb +20 -18
- data/spec/twig/display_spec.rb +108 -57
- data/spec/twig/github_spec.rb +90 -73
- data/spec/twig/options_spec.rb +311 -152
- data/spec/twig/subcommands_spec.rb +29 -0
- data/spec/twig/system_spec.rb +36 -0
- data/spec/twig/util_spec.rb +20 -20
- data/spec/twig_spec.rb +103 -76
- data/twig.gemspec +9 -6
- metadata +17 -11
data/spec/twig/cli_spec.rb
CHANGED
@@ -10,32 +10,32 @@ describe Twig::Cli do
|
|
10
10
|
it 'returns short text in a single line' do
|
11
11
|
text = 'The quick brown fox.'
|
12
12
|
result = @twig.help_description(text, :width => 80)
|
13
|
-
result.
|
13
|
+
expect(result).to eq([text])
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns long text in a string with line breaks' do
|
17
17
|
text = 'The quick brown fox jumps over the lazy, lazy dog.'
|
18
18
|
result = @twig.help_description(text, :width => 20)
|
19
|
-
result.
|
19
|
+
expect(result).to eq([
|
20
20
|
'The quick brown fox',
|
21
21
|
'jumps over the lazy,',
|
22
22
|
'lazy dog.'
|
23
|
-
]
|
23
|
+
])
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'breaks a long word by max line length' do
|
27
27
|
text = 'Thequickbrownfoxjumpsoverthelazydog.'
|
28
28
|
result = @twig.help_description(text, :width => 20)
|
29
|
-
result.
|
29
|
+
expect(result).to eq([
|
30
30
|
'Thequickbrownfoxjump',
|
31
31
|
'soverthelazydog.'
|
32
|
-
]
|
32
|
+
])
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'adds a separator line' do
|
36
36
|
text = 'The quick brown fox.'
|
37
37
|
result = @twig.help_description(text, :width => 80, :add_separator => true)
|
38
|
-
result.
|
38
|
+
expect(result).to eq([text, ' '])
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -46,16 +46,29 @@ describe Twig::Cli do
|
|
46
46
|
|
47
47
|
it 'returns a help string for a custom property' do
|
48
48
|
option_parser = OptionParser.new
|
49
|
-
@twig.
|
50
|
-
opt_parser.
|
51
|
-
desc.
|
52
|
-
options.
|
49
|
+
expect(@twig).to receive(:help_separator) do |opt_parser, desc, options|
|
50
|
+
expect(opt_parser).to eq(option_parser)
|
51
|
+
expect(desc).to eq(" --test-option Test option description\n")
|
52
|
+
expect(options).to eq(:trailing => "\n")
|
53
53
|
end
|
54
54
|
|
55
55
|
@twig.help_description_for_custom_property(option_parser, [
|
56
56
|
['--test-option', 'Test option description']
|
57
57
|
])
|
58
58
|
end
|
59
|
+
|
60
|
+
it 'supports custom trailing whitespace' do
|
61
|
+
option_parser = OptionParser.new
|
62
|
+
expect(@twig).to receive(:help_separator) do |opt_parser, desc, options|
|
63
|
+
expect(opt_parser).to eq(option_parser)
|
64
|
+
expect(desc).to eq(" --test-option Test option description\n")
|
65
|
+
expect(options).to eq(:trailing => '')
|
66
|
+
end
|
67
|
+
|
68
|
+
@twig.help_description_for_custom_property(option_parser, [
|
69
|
+
['--test-option', 'Test option description']
|
70
|
+
], :trailing => '')
|
71
|
+
end
|
59
72
|
end
|
60
73
|
|
61
74
|
describe '#help_paragraph' do
|
@@ -70,11 +83,11 @@ describe Twig::Cli do
|
|
70
83
|
|
71
84
|
result = @twig.help_paragraph(text)
|
72
85
|
|
73
|
-
result.
|
86
|
+
expect(result).to eq([
|
74
87
|
"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the",
|
75
88
|
"lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps",
|
76
89
|
"over the lazy dog. The quick brown fox jumps over the lazy dog."
|
77
|
-
].join("\n")
|
90
|
+
].join("\n"))
|
78
91
|
end
|
79
92
|
end
|
80
93
|
|
@@ -84,133 +97,170 @@ describe Twig::Cli do
|
|
84
97
|
end
|
85
98
|
|
86
99
|
it 'returns true for `--except-foo`' do
|
87
|
-
@twig.help_line_for_custom_property?(' --except-foo ').
|
100
|
+
expect(@twig.help_line_for_custom_property?(' --except-foo ')).to be_true
|
88
101
|
end
|
89
102
|
|
90
103
|
it 'returns false for `--except-branch`' do
|
91
|
-
@twig.help_line_for_custom_property?(' --except-branch ').
|
104
|
+
expect(@twig.help_line_for_custom_property?(' --except-branch ')).to be_false
|
92
105
|
end
|
93
106
|
|
94
107
|
it 'returns false for `--except-PROPERTY`' do
|
95
|
-
@twig.help_line_for_custom_property?(' --except-PROPERTY ').
|
108
|
+
expect(@twig.help_line_for_custom_property?(' --except-PROPERTY ')).to be_false
|
96
109
|
end
|
97
110
|
|
98
111
|
it 'returns true for `--only-foo`' do
|
99
|
-
@twig.help_line_for_custom_property?(' --only-foo ').
|
112
|
+
expect(@twig.help_line_for_custom_property?(' --only-foo ')).to be_true
|
100
113
|
end
|
101
114
|
|
102
115
|
it 'returns false for `--only-branch`' do
|
103
|
-
@twig.help_line_for_custom_property?(' --only-branch ').
|
116
|
+
expect(@twig.help_line_for_custom_property?(' --only-branch ')).to be_false
|
104
117
|
end
|
105
118
|
|
106
119
|
it 'returns false for `--only-PROPERTY`' do
|
107
|
-
@twig.help_line_for_custom_property?(' --only-PROPERTY ').
|
120
|
+
expect(@twig.help_line_for_custom_property?(' --only-PROPERTY ')).to be_false
|
108
121
|
end
|
109
122
|
|
110
123
|
it 'returns true for `--foo-width`' do
|
111
|
-
@twig.help_line_for_custom_property?(' --foo-width ').
|
124
|
+
expect(@twig.help_line_for_custom_property?(' --foo-width ')).to be_true
|
112
125
|
end
|
113
126
|
|
114
127
|
it 'returns false for `--branch-width`' do
|
115
|
-
@twig.help_line_for_custom_property?(' --branch-width ').
|
128
|
+
expect(@twig.help_line_for_custom_property?(' --branch-width ')).to be_false
|
116
129
|
end
|
117
130
|
|
118
131
|
it 'returns false for `--PROPERTY-width`' do
|
119
|
-
@twig.help_line_for_custom_property?(' --PROPERTY-width ').
|
132
|
+
expect(@twig.help_line_for_custom_property?(' --PROPERTY-width ')).to be_false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe '#run_pager' do
|
137
|
+
before :each do
|
138
|
+
@twig = Twig.new
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'turns the current process into a `less` pager' do
|
142
|
+
allow(Kernel).to receive(:fork) { true }
|
143
|
+
expect(@twig).to receive(:exec).with('less')
|
144
|
+
|
145
|
+
@twig.run_pager
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'turns the current process into a custom pager' do
|
149
|
+
allow(Kernel).to receive(:fork) { true }
|
150
|
+
pager = 'arbitrary'
|
151
|
+
expect(ENV).to receive(:[]).with('PAGER').and_return(pager)
|
152
|
+
expect(@twig).to receive(:exec).with(pager)
|
153
|
+
|
154
|
+
@twig.run_pager
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'does nothing if running on Windows' do
|
158
|
+
expect(Twig::System).to receive(:windows?).and_return(true)
|
159
|
+
expect(Kernel).not_to receive(:fork)
|
160
|
+
|
161
|
+
@twig.run_pager
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'does nothing if not running on a terminal device' do
|
165
|
+
allow($stdout).to receive(:tty?) { false }
|
166
|
+
expect(Kernel).not_to receive(:fork)
|
167
|
+
|
168
|
+
@twig.run_pager
|
120
169
|
end
|
121
170
|
end
|
122
171
|
|
123
172
|
describe '#read_cli_options!' do
|
124
173
|
before :each do
|
125
174
|
@twig = Twig.new
|
175
|
+
allow(@twig).to receive(:run_pager)
|
126
176
|
end
|
127
177
|
|
128
178
|
it 'recognizes `--unset` and sets an `:unset_property` option' do
|
129
|
-
@twig.options[:unset_property].
|
179
|
+
expect(@twig.options[:unset_property]).to be_nil
|
130
180
|
@twig.read_cli_options!(%w[--unset test])
|
131
|
-
@twig.options[:unset_property].
|
181
|
+
expect(@twig.options[:unset_property]).to eq('test')
|
132
182
|
end
|
133
183
|
|
134
184
|
it 'recognizes `--help` and prints the help content' do
|
135
185
|
help_lines = []
|
136
|
-
@twig.
|
137
|
-
@twig.
|
186
|
+
allow(@twig).to receive(:puts) { |message| help_lines << message.strip }
|
187
|
+
expect(@twig).to receive(:exit)
|
138
188
|
|
139
189
|
@twig.read_cli_options!(['--help'])
|
140
190
|
|
141
|
-
help_lines.
|
142
|
-
help_lines.
|
191
|
+
expect(help_lines).to include("Twig v#{Twig::VERSION}")
|
192
|
+
expect(help_lines).to include('http://rondevera.github.io/twig/')
|
143
193
|
end
|
144
194
|
|
145
195
|
it 'recognizes `--version` and prints the current version' do
|
146
|
-
@twig.
|
147
|
-
@twig.
|
196
|
+
expect(@twig).to receive(:puts).with(Twig::VERSION)
|
197
|
+
expect(@twig).to receive(:exit)
|
148
198
|
|
149
199
|
@twig.read_cli_options!(['--version'])
|
150
200
|
end
|
151
201
|
|
152
202
|
it 'recognizes `-b` and sets a `:branch` option' do
|
153
|
-
@twig.
|
154
|
-
@twig.options[:branch].
|
203
|
+
expect(@twig).to receive(:all_branch_names).and_return(['test'])
|
204
|
+
expect(@twig.options[:branch]).to be_nil
|
155
205
|
|
156
206
|
@twig.read_cli_options!(%w[-b test])
|
157
207
|
|
158
|
-
@twig.options[:branch].
|
208
|
+
expect(@twig.options[:branch]).to eq('test')
|
159
209
|
end
|
160
210
|
|
161
211
|
it 'recognizes `--branch` and sets a `:branch` option' do
|
162
|
-
@twig.
|
163
|
-
@twig.options[:branch].
|
212
|
+
expect(@twig).to receive(:all_branch_names).and_return(['test'])
|
213
|
+
expect(@twig.options[:branch]).to be_nil
|
164
214
|
|
165
215
|
@twig.read_cli_options!(%w[--branch test])
|
166
216
|
|
167
|
-
@twig.options[:branch].
|
217
|
+
expect(@twig.options[:branch]).to eq('test')
|
168
218
|
end
|
169
219
|
|
170
220
|
it 'recognizes `--max-days-old` and sets a `:max_days_old` option' do
|
171
|
-
@twig.options[:max_days_old].
|
221
|
+
expect(@twig.options[:max_days_old]).to be_nil
|
172
222
|
@twig.read_cli_options!(%w[--max-days-old 30])
|
173
|
-
@twig.options[:max_days_old].
|
223
|
+
expect(@twig.options[:max_days_old]).to eq(30)
|
174
224
|
end
|
175
225
|
|
176
226
|
it 'recognizes `--except-branch` and sets a `:property_except` option' do
|
177
|
-
@twig.options[:property_except].
|
227
|
+
expect(@twig.options[:property_except]).to be_nil
|
178
228
|
@twig.read_cli_options!(%w[--except-branch test])
|
179
|
-
@twig.options[:property_except].
|
229
|
+
expect(@twig.options[:property_except]).to eq(:branch => /test/)
|
180
230
|
end
|
181
231
|
|
182
232
|
it 'recognizes `--only-branch` and sets a `:property_only` option' do
|
183
|
-
@twig.options[:property_only].
|
233
|
+
expect(@twig.options[:property_only]).to be_nil
|
184
234
|
@twig.read_cli_options!(%w[--only-branch test])
|
185
|
-
@twig.options[:property_only].
|
235
|
+
expect(@twig.options[:property_only]).to eq(:branch => /test/)
|
186
236
|
end
|
187
237
|
|
188
238
|
context 'with custom property "only" filtering' do
|
189
239
|
before :each do
|
190
|
-
@twig.options[:property_only].
|
240
|
+
expect(@twig.options[:property_only]).to be_nil
|
191
241
|
end
|
192
242
|
|
193
243
|
it 'recognizes `--only-<property>` and sets a `:property_only` option' do
|
194
|
-
Twig::Branch.
|
244
|
+
allow(Twig::Branch).to receive(:all_property_names) { %w[foo] }
|
195
245
|
@twig.read_cli_options!(%w[--only-foo test])
|
196
|
-
@twig.options[:property_only].
|
246
|
+
expect(@twig.options[:property_only]).to eq(:foo => /test/)
|
197
247
|
end
|
198
248
|
|
199
249
|
it 'recognizes `--only-branch` and `--only-<property>` together' do
|
200
|
-
Twig::Branch.
|
250
|
+
allow(Twig::Branch).to receive(:all_property_names) { %w[foo] }
|
201
251
|
|
202
252
|
@twig.read_cli_options!(%w[--only-branch test --only-foo bar])
|
203
253
|
|
204
|
-
@twig.options[:property_only].
|
254
|
+
expect(@twig.options[:property_only]).to eq(
|
205
255
|
:branch => /test/,
|
206
256
|
:foo => /bar/
|
207
|
-
|
257
|
+
)
|
208
258
|
end
|
209
259
|
|
210
260
|
it 'does not recognize `--only-<property>` for a missing property' do
|
211
261
|
property_name = 'foo'
|
212
|
-
Twig::Branch.all_property_names.
|
213
|
-
@twig.
|
262
|
+
expect(Twig::Branch.all_property_names).not_to include(property_name)
|
263
|
+
allow(@twig).to receive(:puts)
|
214
264
|
|
215
265
|
begin
|
216
266
|
@twig.read_cli_options!(["--only-#{property_name}", 'test'])
|
@@ -218,38 +268,38 @@ describe Twig::Cli do
|
|
218
268
|
expected_exception = exception
|
219
269
|
end
|
220
270
|
|
221
|
-
expected_exception.
|
222
|
-
expected_exception.status.
|
223
|
-
@twig.options[:property_only].
|
271
|
+
expect(expected_exception).not_to be_nil
|
272
|
+
expect(expected_exception.status).to eq(0)
|
273
|
+
expect(@twig.options[:property_only]).to be_nil
|
224
274
|
end
|
225
275
|
end
|
226
276
|
|
227
277
|
context 'with custom property "except" filtering' do
|
228
278
|
before :each do
|
229
|
-
@twig.options[:property_except].
|
279
|
+
expect(@twig.options[:property_except]).to be_nil
|
230
280
|
end
|
231
281
|
|
232
282
|
it 'recognizes `--except-<property>` and sets a `:property_except` option' do
|
233
|
-
Twig::Branch.
|
283
|
+
allow(Twig::Branch).to receive(:all_property_names) { %w[foo] }
|
234
284
|
@twig.read_cli_options!(%w[--except-foo test])
|
235
|
-
@twig.options[:property_except].
|
285
|
+
expect(@twig.options[:property_except]).to eq(:foo => /test/)
|
236
286
|
end
|
237
287
|
|
238
288
|
it 'recognizes `--except-branch` and `--except-<property>` together' do
|
239
|
-
Twig::Branch.
|
289
|
+
allow(Twig::Branch).to receive(:all_property_names) { %w[foo] }
|
240
290
|
|
241
291
|
@twig.read_cli_options!(%w[--except-branch test --except-foo bar])
|
242
292
|
|
243
|
-
@twig.options[:property_except].
|
293
|
+
expect(@twig.options[:property_except]).to eq(
|
244
294
|
:branch => /test/,
|
245
295
|
:foo => /bar/
|
246
|
-
|
296
|
+
)
|
247
297
|
end
|
248
298
|
|
249
299
|
it 'does not recognize `--except-<property>` for a missing property' do
|
250
300
|
property_name = 'foo'
|
251
|
-
Twig::Branch.all_property_names.
|
252
|
-
@twig.
|
301
|
+
expect(Twig::Branch.all_property_names).not_to include(property_name)
|
302
|
+
allow(@twig).to receive(:puts)
|
253
303
|
|
254
304
|
begin
|
255
305
|
@twig.read_cli_options!(["--except-#{property_name}", 'test'])
|
@@ -257,12 +307,18 @@ describe Twig::Cli do
|
|
257
307
|
expected_exception = exception
|
258
308
|
end
|
259
309
|
|
260
|
-
expected_exception.
|
261
|
-
expected_exception.status.
|
262
|
-
@twig.options[:property_except].
|
310
|
+
expect(expected_exception).not_to be_nil
|
311
|
+
expect(expected_exception.status).to eq(0)
|
312
|
+
expect(@twig.options[:property_except]).to be_nil
|
263
313
|
end
|
264
314
|
end
|
265
315
|
|
316
|
+
it 'recognizes `--format` and sets a `:format` option' do
|
317
|
+
expect(@twig.options[:format]).to be_nil
|
318
|
+
@twig.read_cli_options!(%w[--format json])
|
319
|
+
expect(@twig.options[:format]).to eq(:json)
|
320
|
+
end
|
321
|
+
|
266
322
|
it 'recognizes `--all` and unsets other options except `:branch`' do
|
267
323
|
@twig.set_option(:max_days_old, 30)
|
268
324
|
@twig.set_option(:property_except, :branch => /test/)
|
@@ -270,71 +326,71 @@ describe Twig::Cli do
|
|
270
326
|
|
271
327
|
@twig.read_cli_options!(['--all'])
|
272
328
|
|
273
|
-
@twig.options[:max_days_old].
|
274
|
-
@twig.options[:property_except].
|
275
|
-
@twig.options[:property_only].
|
329
|
+
expect(@twig.options[:max_days_old]).to be_nil
|
330
|
+
expect(@twig.options[:property_except]).to be_nil
|
331
|
+
expect(@twig.options[:property_only]).to be_nil
|
276
332
|
end
|
277
333
|
|
278
334
|
it 'recognizes `--branch-width`' do
|
279
|
-
@twig.options[:property_width].
|
280
|
-
@twig.
|
335
|
+
expect(@twig.options[:property_width]).to be_nil
|
336
|
+
expect(@twig).to receive(:set_option).with(:property_width, :branch => '10')
|
281
337
|
|
282
338
|
@twig.read_cli_options!(%w[--branch-width 10])
|
283
339
|
end
|
284
340
|
|
285
341
|
it 'recognizes `--<property>-width`' do
|
286
|
-
Twig::Branch.
|
287
|
-
@twig.options[:property_width].
|
288
|
-
@twig.
|
342
|
+
allow(Twig::Branch).to receive(:all_property_names) { %w[foo] }
|
343
|
+
expect(@twig.options[:property_width]).to be_nil
|
344
|
+
expect(@twig).to receive(:set_option).with(:property_width, :foo => '10')
|
289
345
|
|
290
346
|
@twig.read_cli_options!(%w[--foo-width 10])
|
291
347
|
end
|
292
348
|
|
293
349
|
it 'recognizes `--header-style`' do
|
294
|
-
@twig.options[:header_color].
|
295
|
-
@twig.options[:header_weight].
|
350
|
+
expect(@twig.options[:header_color]).to eq(Twig::DEFAULT_HEADER_COLOR)
|
351
|
+
expect(@twig.options[:header_weight]).to be_nil
|
296
352
|
@twig.read_cli_options!(['--header-style', 'green bold'])
|
297
|
-
@twig.options[:header_color].
|
298
|
-
@twig.options[:header_weight].
|
353
|
+
expect(@twig.options[:header_color]).to eq(:green)
|
354
|
+
expect(@twig.options[:header_weight]).to eq(:bold)
|
299
355
|
end
|
300
356
|
|
301
357
|
it 'recognizes `--reverse`' do
|
302
|
-
@twig.options[:reverse].
|
358
|
+
expect(@twig.options[:reverse]).to be_nil
|
303
359
|
@twig.read_cli_options!(['--reverse'])
|
304
|
-
@twig.options[:reverse].
|
360
|
+
expect(@twig.options[:reverse]).to be_true
|
305
361
|
end
|
306
362
|
|
307
363
|
it 'recognizes `--github-api-uri-prefix`' do
|
308
|
-
@twig.options[:github_api_uri_prefix].
|
364
|
+
expect(@twig.options[:github_api_uri_prefix]).to eq(Twig::DEFAULT_GITHUB_API_URI_PREFIX)
|
309
365
|
prefix = 'https://github-enterprise.example.com/api/v3'
|
310
366
|
|
311
367
|
@twig.read_cli_options!(['--github-api-uri-prefix', prefix])
|
312
368
|
|
313
|
-
@twig.options[:github_api_uri_prefix].
|
369
|
+
expect(@twig.options[:github_api_uri_prefix]).to eq(prefix)
|
314
370
|
end
|
315
371
|
|
316
372
|
it 'recognizes `--github-uri-prefix`' do
|
317
|
-
@twig.options[:github_uri_prefix].
|
373
|
+
expect(@twig.options[:github_uri_prefix]).to eq(Twig::DEFAULT_GITHUB_URI_PREFIX)
|
318
374
|
prefix = 'https://github-enterprise.example.com'
|
319
375
|
|
320
376
|
@twig.read_cli_options!(['--github-uri-prefix', prefix])
|
321
377
|
|
322
|
-
@twig.options[:github_uri_prefix].
|
378
|
+
expect(@twig.options[:github_uri_prefix]).to eq(prefix)
|
323
379
|
end
|
324
380
|
|
325
381
|
it 'handles invalid options' do
|
326
|
-
@twig.
|
327
|
-
exception.
|
328
|
-
exception.message.
|
382
|
+
expect(@twig).to receive(:abort_for_option_exception) do |exception|
|
383
|
+
expect(exception).to be_a(OptionParser::InvalidOption)
|
384
|
+
expect(exception.message).to include('invalid option: --foo')
|
329
385
|
end
|
330
386
|
|
331
387
|
@twig.read_cli_options!(['--foo'])
|
332
388
|
end
|
333
389
|
|
334
390
|
it 'handles missing arguments' do
|
335
|
-
@twig.
|
336
|
-
exception.
|
337
|
-
exception.message.
|
391
|
+
expect(@twig).to receive(:abort_for_option_exception) do |exception|
|
392
|
+
expect(exception).to be_a(OptionParser::MissingArgument)
|
393
|
+
expect(exception.message).to include('missing argument: --branch')
|
338
394
|
end
|
339
395
|
|
340
396
|
@twig.read_cli_options!(['--branch'])
|
@@ -348,62 +404,97 @@ describe Twig::Cli do
|
|
348
404
|
|
349
405
|
it 'prints a message and exits' do
|
350
406
|
exception = Exception.new('test exception')
|
351
|
-
@twig.
|
352
|
-
@twig.
|
353
|
-
message.
|
407
|
+
expect(@twig).to receive(:puts).with(exception.message)
|
408
|
+
expect(@twig).to receive(:puts) do |message|
|
409
|
+
expect(message).to include('`twig --help`')
|
354
410
|
end
|
355
|
-
@twig.
|
411
|
+
expect(@twig).to receive(:exit)
|
356
412
|
|
357
413
|
@twig.abort_for_option_exception(exception)
|
358
414
|
end
|
359
415
|
end
|
360
416
|
|
417
|
+
describe '#exec_subcommand_if_any' do
|
418
|
+
before :each do
|
419
|
+
@twig = Twig.new
|
420
|
+
@branch_name = 'test'
|
421
|
+
allow(Twig).to receive(:run)
|
422
|
+
allow(@twig).to receive(:current_branch_name) { @branch_name }
|
423
|
+
end
|
424
|
+
|
425
|
+
it 'recognizes a subcommand' do
|
426
|
+
command_path = '/path/to/bin/twig-subcommand'
|
427
|
+
expect(Twig).to receive(:run).with('which twig-subcommand 2>/dev/null').
|
428
|
+
and_return(command_path)
|
429
|
+
expect(@twig).to receive(:exec).with(command_path) { exit }
|
430
|
+
|
431
|
+
# Since we're stubbing `exec` (with an expectation), we still need it
|
432
|
+
# to exit early like the real implementation. The following handles the
|
433
|
+
# exit somewhat gracefully.
|
434
|
+
begin
|
435
|
+
@twig.read_cli_args!(['subcommand'])
|
436
|
+
rescue SystemExit => exception
|
437
|
+
expected_exception = exception
|
438
|
+
end
|
439
|
+
|
440
|
+
expect(expected_exception).not_to be_nil
|
441
|
+
expect(expected_exception.status).to eq(0)
|
442
|
+
end
|
443
|
+
|
444
|
+
it 'does not recognize a subcommand' do
|
445
|
+
expect(Twig).to receive(:run).
|
446
|
+
with('which twig-subcommand 2>/dev/null').and_return('')
|
447
|
+
expect(@twig).not_to receive(:exec)
|
448
|
+
allow(@twig).to receive(:abort)
|
449
|
+
|
450
|
+
@twig.read_cli_args!(['subcommand'])
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
361
454
|
describe '#read_cli_args!' do
|
362
455
|
before :each do
|
363
456
|
@twig = Twig.new
|
364
457
|
end
|
365
458
|
|
366
|
-
it '
|
367
|
-
|
368
|
-
@twig.should_receive(:list_branches).and_return(branch_list)
|
369
|
-
@twig.should_receive(:puts).with(branch_list)
|
459
|
+
it 'checks for and executes a subcommand if there are any args' do
|
460
|
+
expect(@twig).to receive(:exec_subcommand_if_any).with(['foo']) { exit }
|
370
461
|
|
371
|
-
|
462
|
+
begin
|
463
|
+
@twig.read_cli_args!(['foo'])
|
464
|
+
rescue SystemExit => exception
|
465
|
+
expected_exception = exception
|
466
|
+
end
|
467
|
+
|
468
|
+
expect(expected_exception).not_to be_nil
|
469
|
+
expect(expected_exception.status).to eq(0)
|
372
470
|
end
|
373
471
|
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
end
|
472
|
+
it 'does not check for a subcommand if there are no args' do
|
473
|
+
branch_list = %[foo bar]
|
474
|
+
expect(@twig).not_to receive(:exec_subcommand_if_any)
|
475
|
+
allow(@twig).to receive(:list_branches).and_return(branch_list)
|
476
|
+
allow(@twig).to receive(:puts).with(branch_list)
|
380
477
|
|
381
|
-
|
382
|
-
|
383
|
-
Twig.should_receive(:run).with('which twig-subcommand 2>/dev/null').
|
384
|
-
and_return(command_path)
|
385
|
-
@twig.should_receive(:exec).with(command_path) { exit }
|
478
|
+
@twig.read_cli_args!([])
|
479
|
+
end
|
386
480
|
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
@twig.read_cli_args!(['subcommand'])
|
392
|
-
rescue SystemExit => exception
|
393
|
-
expected_exception = exception
|
394
|
-
end
|
481
|
+
it 'lists branches' do
|
482
|
+
branch_list = %[foo bar]
|
483
|
+
expect(@twig).to receive(:list_branches).and_return(branch_list)
|
484
|
+
expect(@twig).to receive(:puts).with(branch_list)
|
395
485
|
|
396
|
-
|
397
|
-
|
398
|
-
end
|
486
|
+
@twig.read_cli_args!([])
|
487
|
+
end
|
399
488
|
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
489
|
+
it 'prints branch properties as JSON' do
|
490
|
+
branches_json = {
|
491
|
+
'branch1' => { 'last-commit-time' => '2000-01-01' },
|
492
|
+
'branch2' => { 'last-commit-time' => '2000-01-01' }
|
493
|
+
}.to_json
|
494
|
+
expect(@twig).to receive(:branches_json).and_return(branches_json)
|
495
|
+
expect(@twig).to receive(:puts).with(branches_json)
|
404
496
|
|
405
|
-
|
406
|
-
end
|
497
|
+
@twig.read_cli_args!(%w[--format json])
|
407
498
|
end
|
408
499
|
|
409
500
|
context 'getting properties' do
|
@@ -414,17 +505,17 @@ describe Twig::Cli do
|
|
414
505
|
end
|
415
506
|
|
416
507
|
it 'gets a property for the current branch' do
|
417
|
-
@twig.
|
418
|
-
@twig.
|
508
|
+
expect(@twig).to receive(:current_branch_name).and_return(@branch_name)
|
509
|
+
expect(@twig).to receive(:get_branch_property_for_cli).
|
419
510
|
with(@branch_name, @property_name)
|
420
511
|
|
421
512
|
@twig.read_cli_args!([@property_name])
|
422
513
|
end
|
423
514
|
|
424
515
|
it 'gets a property for a specified branch' do
|
425
|
-
@twig.
|
516
|
+
expect(@twig).to receive(:all_branch_names).and_return([@branch_name])
|
426
517
|
@twig.set_option(:branch, @branch_name)
|
427
|
-
@twig.
|
518
|
+
expect(@twig).to receive(:get_branch_property_for_cli).
|
428
519
|
with(@branch_name, @property_name)
|
429
520
|
|
430
521
|
@twig.read_cli_args!([@property_name])
|
@@ -440,17 +531,17 @@ describe Twig::Cli do
|
|
440
531
|
end
|
441
532
|
|
442
533
|
it 'sets a property for the current branch' do
|
443
|
-
@twig.
|
444
|
-
@twig.
|
534
|
+
expect(@twig).to receive(:current_branch_name).and_return(@branch_name)
|
535
|
+
expect(@twig).to receive(:set_branch_property_for_cli).
|
445
536
|
with(@branch_name, @property_name, @property_value)
|
446
537
|
|
447
538
|
@twig.read_cli_args!([@property_name, @property_value])
|
448
539
|
end
|
449
540
|
|
450
541
|
it 'sets a property for a specified branch' do
|
451
|
-
@twig.
|
542
|
+
expect(@twig).to receive(:all_branch_names).and_return([@branch_name])
|
452
543
|
@twig.set_option(:branch, @branch_name)
|
453
|
-
@twig.
|
544
|
+
expect(@twig).to receive(:set_branch_property_for_cli).
|
454
545
|
with(@branch_name, @property_name, @property_value).
|
455
546
|
and_return(@message)
|
456
547
|
|
@@ -467,17 +558,17 @@ describe Twig::Cli do
|
|
467
558
|
end
|
468
559
|
|
469
560
|
it 'unsets a property for the current branch' do
|
470
|
-
@twig.
|
471
|
-
@twig.
|
561
|
+
expect(@twig).to receive(:current_branch_name).and_return(@branch_name)
|
562
|
+
expect(@twig).to receive(:unset_branch_property_for_cli).
|
472
563
|
with(@branch_name, @property_name)
|
473
564
|
|
474
565
|
@twig.read_cli_args!([])
|
475
566
|
end
|
476
567
|
|
477
568
|
it 'unsets a property for a specified branch' do
|
478
|
-
@twig.
|
569
|
+
expect(@twig).to receive(:all_branch_names).and_return([@branch_name])
|
479
570
|
@twig.set_option(:branch, @branch_name)
|
480
|
-
@twig.
|
571
|
+
expect(@twig).to receive(:unset_branch_property_for_cli).
|
481
572
|
with(@branch_name, @property_name)
|
482
573
|
|
483
574
|
@twig.read_cli_args!([])
|
@@ -494,20 +585,20 @@ describe Twig::Cli do
|
|
494
585
|
|
495
586
|
it 'gets a property' do
|
496
587
|
property_value = 'bar'
|
497
|
-
@twig.
|
588
|
+
expect(@twig).to receive(:get_branch_property).
|
498
589
|
with(@branch_name, @property_name).and_return(property_value)
|
499
|
-
@twig.
|
590
|
+
expect(@twig).to receive(:puts).with(property_value)
|
500
591
|
|
501
592
|
@twig.get_branch_property_for_cli(@branch_name, @property_name)
|
502
593
|
end
|
503
594
|
|
504
595
|
it 'shows an error when getting a property that is not set' do
|
505
596
|
error_message = 'test error'
|
506
|
-
@twig.
|
597
|
+
expect(@twig).to receive(:get_branch_property).
|
507
598
|
with(@branch_name, @property_name).and_return(nil)
|
508
|
-
Twig::Branch::MissingPropertyError.
|
509
|
-
|
510
|
-
@twig.
|
599
|
+
allow_any_instance_of(Twig::Branch::MissingPropertyError).
|
600
|
+
to receive(:message) { error_message }
|
601
|
+
expect(@twig).to receive(:abort).with(error_message)
|
511
602
|
|
512
603
|
@twig.get_branch_property_for_cli(@branch_name, @property_name)
|
513
604
|
end
|
@@ -515,11 +606,11 @@ describe Twig::Cli do
|
|
515
606
|
it 'handles ArgumentError when getting an invalid branch property name' do
|
516
607
|
bad_property_name = ''
|
517
608
|
error_message = 'test error'
|
518
|
-
@twig.
|
609
|
+
expect(@twig).to receive(:get_branch_property).
|
519
610
|
with(@branch_name, bad_property_name) do
|
520
611
|
raise ArgumentError, error_message
|
521
612
|
end
|
522
|
-
@twig.
|
613
|
+
expect(@twig).to receive(:abort).with(error_message)
|
523
614
|
|
524
615
|
@twig.get_branch_property_for_cli(@branch_name, bad_property_name)
|
525
616
|
end
|
@@ -535,10 +626,10 @@ describe Twig::Cli do
|
|
535
626
|
it 'sets a property for the specified branch' do
|
536
627
|
success_message = 'test success'
|
537
628
|
property_value = 'bar'
|
538
|
-
@twig.
|
629
|
+
expect(@twig).to receive(:set_branch_property).
|
539
630
|
with(@branch_name, @property_name, property_value).
|
540
631
|
and_return(success_message)
|
541
|
-
@twig.
|
632
|
+
expect(@twig).to receive(:puts).with(success_message)
|
542
633
|
|
543
634
|
@twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
|
544
635
|
end
|
@@ -546,11 +637,11 @@ describe Twig::Cli do
|
|
546
637
|
it 'handles ArgumentError when unsetting an invalid branch property name' do
|
547
638
|
error_message = 'test error'
|
548
639
|
property_value = ''
|
549
|
-
@twig.
|
640
|
+
expect(@twig).to receive(:set_branch_property).
|
550
641
|
with(@branch_name, @property_name, property_value) do
|
551
642
|
raise ArgumentError, error_message
|
552
643
|
end
|
553
|
-
@twig.
|
644
|
+
expect(@twig).to receive(:abort).with(error_message)
|
554
645
|
|
555
646
|
@twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
|
556
647
|
end
|
@@ -558,11 +649,11 @@ describe Twig::Cli do
|
|
558
649
|
it 'handles RuntimeError when Git is unable to set a branch property' do
|
559
650
|
error_message = 'test error'
|
560
651
|
property_value = ''
|
561
|
-
@twig.
|
652
|
+
expect(@twig).to receive(:set_branch_property).
|
562
653
|
with(@branch_name, @property_name, property_value) do
|
563
654
|
raise RuntimeError, error_message
|
564
655
|
end
|
565
|
-
@twig.
|
656
|
+
expect(@twig).to receive(:abort).with(error_message)
|
566
657
|
|
567
658
|
@twig.set_branch_property_for_cli(@branch_name, @property_name, property_value)
|
568
659
|
end
|
@@ -577,31 +668,31 @@ describe Twig::Cli do
|
|
577
668
|
|
578
669
|
it 'unsets a property for the specified branch' do
|
579
670
|
success_message = 'test success'
|
580
|
-
@twig.
|
671
|
+
expect(@twig).to receive(:unset_branch_property).
|
581
672
|
with(@branch_name, @property_name).and_return(success_message)
|
582
|
-
@twig.
|
673
|
+
expect(@twig).to receive(:puts).with(success_message)
|
583
674
|
|
584
675
|
@twig.unset_branch_property_for_cli(@branch_name, @property_name)
|
585
676
|
end
|
586
677
|
|
587
678
|
it 'handles ArgumentError when unsetting an invalid branch property name' do
|
588
679
|
error_message = 'test error'
|
589
|
-
@twig.
|
680
|
+
expect(@twig).to receive(:unset_branch_property).
|
590
681
|
with(@branch_name, @property_name) do
|
591
682
|
raise ArgumentError, error_message
|
592
683
|
end
|
593
|
-
@twig.
|
684
|
+
expect(@twig).to receive(:abort).with(error_message)
|
594
685
|
|
595
686
|
@twig.unset_branch_property_for_cli(@branch_name, @property_name)
|
596
687
|
end
|
597
688
|
|
598
689
|
it 'handles MissingPropertyError when unsetting a branch property that is not set' do
|
599
690
|
error_message = 'test error'
|
600
|
-
@twig.
|
691
|
+
expect(@twig).to receive(:unset_branch_property).
|
601
692
|
with(@branch_name, @property_name) do
|
602
693
|
raise Twig::Branch::MissingPropertyError, error_message
|
603
694
|
end
|
604
|
-
@twig.
|
695
|
+
expect(@twig).to receive(:abort).with(error_message)
|
605
696
|
|
606
697
|
@twig.unset_branch_property_for_cli(@branch_name, @property_name)
|
607
698
|
end
|