twig 1.6 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,11 +20,11 @@ describe Twig::Cli do
20
20
 
21
21
  $stdout = stdout_orig
22
22
  expect(stdout_test.string).to eq(
23
- prompt + "\n" +
24
- " 1. #{choices[0]}\n" +
25
- " 2. #{choices[1]}\n" +
26
- " 3. #{choices[2]}\n" +
27
- " 4. #{choices[3]}\n" +
23
+ prompt + "\n" \
24
+ " 1. #{choices[0]}\n" \
25
+ " 2. #{choices[1]}\n" \
26
+ " 3. #{choices[2]}\n" \
27
+ " 4. #{choices[3]}\n" \
28
28
  '> '
29
29
  )
30
30
  expect(result).to eq(choices[3])
@@ -40,148 +40,12 @@ describe Twig::Cli do
40
40
  end
41
41
  end
42
42
 
43
- describe '#help_description' do
44
- before :each do
45
- @twig = Twig.new
46
- end
47
-
48
- it 'returns short text in a single line' do
49
- text = 'The quick brown fox.'
50
- result = @twig.help_description(text, :width => 80)
51
- expect(result).to eq([text])
52
- end
53
-
54
- it 'returns long text in a string with line breaks' do
55
- text = 'The quick brown fox jumps over the lazy, lazy dog.'
56
- result = @twig.help_description(text, :width => 20)
57
- expect(result).to eq([
58
- 'The quick brown fox',
59
- 'jumps over the lazy,',
60
- 'lazy dog.'
61
- ])
62
- end
63
-
64
- it 'breaks a long word by max line length' do
65
- text = 'Thequickbrownfoxjumpsoverthelazydog.'
66
- result = @twig.help_description(text, :width => 20)
67
- expect(result).to eq([
68
- 'Thequickbrownfoxjump',
69
- 'soverthelazydog.'
70
- ])
71
- end
72
-
73
- it 'adds a separator line' do
74
- text = 'The quick brown fox.'
75
- result = @twig.help_description(text, :width => 80, :add_separator => true)
76
- expect(result).to eq([text, ' '])
77
- end
78
- end
79
-
80
- describe '#help_description_for_custom_property' do
81
- before :each do
82
- @twig = Twig.new
83
- end
84
-
85
- it 'returns a help string for a custom property' do
86
- option_parser = OptionParser.new
87
- expect(@twig).to receive(:help_separator) do |opt_parser, desc, options|
88
- expect(opt_parser).to eq(option_parser)
89
- expect(desc).to eq(" --test-option Test option description\n")
90
- expect(options).to eq(:trailing => "\n")
91
- end
92
-
93
- @twig.help_description_for_custom_property(option_parser, [
94
- ['--test-option', 'Test option description']
95
- ])
96
- end
97
-
98
- it 'supports custom trailing whitespace' do
99
- option_parser = OptionParser.new
100
- expect(@twig).to receive(:help_separator) do |opt_parser, desc, options|
101
- expect(opt_parser).to eq(option_parser)
102
- expect(desc).to eq(" --test-option Test option description\n")
103
- expect(options).to eq(:trailing => '')
104
- end
105
-
106
- @twig.help_description_for_custom_property(option_parser, [
107
- ['--test-option', 'Test option description']
108
- ], :trailing => '')
109
- end
110
- end
111
-
112
- describe '#help_paragraph' do
113
- before :each do
114
- @twig = Twig.new
115
- end
116
-
117
- it 'returns long text in a paragraph with line breaks' do
118
- text = Array.new(5) {
119
- 'The quick brown fox jumps over the lazy dog.'
120
- }.join(' ')
121
-
122
- result = @twig.help_paragraph(text)
123
-
124
- expect(result).to eq([
125
- "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the",
126
- "lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps",
127
- "over the lazy dog. The quick brown fox jumps over the lazy dog."
128
- ].join("\n"))
129
- end
130
- end
131
-
132
- describe '#help_line_for_custom_property?' do
133
- before :each do
134
- @twig = Twig.new
135
- end
136
-
137
- it 'returns true for `--except-foo`' do
138
- expect(@twig.help_line_for_custom_property?(' --except-foo ')).to be_true
139
- end
140
-
141
- it 'returns false for `--except-branch`' do
142
- expect(@twig.help_line_for_custom_property?(' --except-branch ')).to be_false
143
- end
144
-
145
- it 'returns false for `--except-property`' do
146
- expect(@twig.help_line_for_custom_property?(' --except-property ')).to be_false
147
- end
148
-
149
- it 'returns false for `--except-PROPERTY`' do
150
- expect(@twig.help_line_for_custom_property?(' --except-PROPERTY ')).to be_false
151
- end
152
-
153
- it 'returns true for `--only-foo`' do
154
- expect(@twig.help_line_for_custom_property?(' --only-foo ')).to be_true
155
- end
156
-
157
- it 'returns false for `--only-branch`' do
158
- expect(@twig.help_line_for_custom_property?(' --only-branch ')).to be_false
159
- end
160
-
161
- it 'returns false for `--only-property`' do
162
- expect(@twig.help_line_for_custom_property?(' --only-property ')).to be_false
163
- end
164
-
165
- it 'returns false for `--only-PROPERTY`' do
166
- expect(@twig.help_line_for_custom_property?(' --only-PROPERTY ')).to be_false
167
- end
168
-
169
- it 'returns true for `--foo-width`' do
170
- expect(@twig.help_line_for_custom_property?(' --foo-width ')).to be_true
171
- end
172
-
173
- it 'returns false for `--branch-width`' do
174
- expect(@twig.help_line_for_custom_property?(' --branch-width ')).to be_false
175
- end
176
-
177
- it 'returns false for `--PROPERTY-width`' do
178
- expect(@twig.help_line_for_custom_property?(' --PROPERTY-width ')).to be_false
179
- end
180
- end
181
-
182
43
  describe '#run_pager' do
183
44
  before :each do
184
45
  @twig = Twig.new
46
+ allow(Twig::System).to receive(:windows?) { false }
47
+ allow($stdout).to receive(:tty?) { true }
48
+ allow($stderr).to receive(:tty?) { true }
185
49
  end
186
50
 
187
51
  it 'turns the current process into a `less` pager' do
@@ -200,16 +64,32 @@ describe Twig::Cli do
200
64
  @twig.run_pager
201
65
  end
202
66
 
67
+ it 'reopens original stdout and stderr if unable to fork' do
68
+ allow(Kernel).to receive(:fork) { false }
69
+ expect($stdout).to receive(:reopen)
70
+ expect($stderr).to receive(:reopen)
71
+ expect($stdin).not_to receive(:reopen)
72
+
73
+ @twig.run_pager
74
+ end
75
+
203
76
  it 'does nothing if running on Windows' do
204
77
  expect(Twig::System).to receive(:windows?).and_return(true)
205
- expect(Kernel).not_to receive(:fork)
78
+ expect(IO).not_to receive(:pipe)
206
79
 
207
80
  @twig.run_pager
208
81
  end
209
82
 
210
- it 'does nothing if not running on a terminal device' do
83
+ it 'does nothing if stdout is not running on a terminal device' do
211
84
  allow($stdout).to receive(:tty?) { false }
212
- expect(Kernel).not_to receive(:fork)
85
+ expect(IO).not_to receive(:pipe)
86
+
87
+ @twig.run_pager
88
+ end
89
+
90
+ it 'does nothing if `Kernel.fork` is not supported' do
91
+ allow(Kernel).to receive(:respond_to?).with(:fork) { false }
92
+ expect(IO).not_to receive(:pipe)
213
93
 
214
94
  @twig.run_pager
215
95
  end
@@ -413,7 +293,7 @@ describe Twig::Cli do
413
293
  it 'recognizes `--reverse`' do
414
294
  expect(@twig.options[:reverse]).to be_nil
415
295
  @twig.read_cli_options!(['--reverse'])
416
- expect(@twig.options[:reverse]).to be_true
296
+ expect(@twig.options[:reverse]).to eql(true)
417
297
  end
418
298
 
419
299
  it 'recognizes `--github-api-uri-prefix`' do
@@ -459,12 +339,12 @@ describe Twig::Cli do
459
339
  end
460
340
 
461
341
  it 'prints a message and aborts' do
462
- exception = Exception.new('test exception')
463
342
  expect(@twig).to receive(:puts) do |message|
464
- expect(message).to include('`twig --help`')
343
+ expect(message).to include('`twig help`')
465
344
  end
466
345
 
467
346
  expect {
347
+ exception = Exception.new('test exception')
468
348
  @twig.abort_for_option_exception(exception)
469
349
  }.to raise_exception { |exception|
470
350
  expect(exception).to be_a(SystemExit)
@@ -473,48 +353,13 @@ describe Twig::Cli do
473
353
  end
474
354
  end
475
355
 
476
- describe '#exec_subcommand_if_any' do
477
- before :each do
478
- @twig = Twig.new
479
- @branch_name = 'test'
480
- allow(Twig).to receive(:run)
481
- allow(@twig).to receive(:current_branch_name) { @branch_name }
482
- end
483
-
484
- it 'recognizes a subcommand' do
485
- command_path = '/path/to/bin/twig-subcommand'
486
- expect(Twig).to receive(:run).with('which twig-subcommand 2>/dev/null').
487
- and_return(command_path)
488
- expect(@twig).to receive(:exec).with(command_path) { exit }
489
-
490
- # Since we're stubbing `exec` (with an expectation), we still need it
491
- # to exit early like the real implementation. The following handles the
492
- # exit somewhat gracefully.
493
- expect {
494
- @twig.read_cli_args!(['subcommand'])
495
- }.to raise_exception { |exception|
496
- expect(exception).to be_a(SystemExit)
497
- expect(exception.status).to eq(0)
498
- }
499
- end
500
-
501
- it 'does not recognize a subcommand' do
502
- expect(Twig).to receive(:run).
503
- with('which twig-subcommand 2>/dev/null').and_return('')
504
- expect(@twig).not_to receive(:exec)
505
- allow(@twig).to receive(:abort)
506
-
507
- @twig.read_cli_args!(['subcommand'])
508
- end
509
- end
510
-
511
356
  describe '#read_cli_args!' do
512
357
  before :each do
513
358
  @twig = Twig.new
514
359
  end
515
360
 
516
361
  it 'checks for and executes a subcommand if there are any args' do
517
- expect(@twig).to receive(:exec_subcommand_if_any).with(['foo']) { exit }
362
+ expect(Twig::Subcommands).to receive(:exec_subcommand_if_any).with(['foo']) { exit }
518
363
 
519
364
  expect {
520
365
  @twig.read_cli_args!(['foo'])
@@ -525,8 +370,8 @@ describe Twig::Cli do
525
370
  end
526
371
 
527
372
  it 'does not check for a subcommand if there are no args' do
528
- branch_list = %[foo bar]
529
- expect(@twig).not_to receive(:exec_subcommand_if_any)
373
+ branch_list = %w[foo bar]
374
+ expect(Twig::Subcommands).not_to receive(:exec_subcommand_if_any)
530
375
  allow(@twig).to receive(:list_branches).and_return(branch_list)
531
376
  allow(@twig).to receive(:puts).with(branch_list)
532
377
 
@@ -534,7 +379,7 @@ describe Twig::Cli do
534
379
  end
535
380
 
536
381
  it 'lists branches' do
537
- branch_list = %[foo bar]
382
+ branch_list = %w[foo bar]
538
383
  expect(@twig).to receive(:list_branches).and_return(branch_list)
539
384
  expect(@twig).to receive(:puts).with(branch_list)
540
385
 
@@ -3,51 +3,220 @@ require 'spec_helper'
3
3
  describe Twig::CommitTime do
4
4
  before :each do
5
5
  @time = Time.utc(2000, 12, 1, 12, 00, 00)
6
+ allow(Twig::CommitTime).to receive(:now) { @time }
6
7
  end
7
8
 
8
9
  describe '#initialize' do
9
10
  it 'stores a Time object' do
10
- commit_time = Twig::CommitTime.new(@time, '99 days ago')
11
+ commit_time = Twig::CommitTime.new(@time)
11
12
  expect(commit_time.instance_variable_get(:@time)).to eq(@time)
12
13
  end
13
14
 
14
15
  it 'stores a "time ago" string as its shortened version' do
15
- expect(Twig::CommitTime.new(@time, '1 year, 1 month ago').
16
- instance_variable_get(:@time_ago)).to eq('1y ago')
17
- expect(Twig::CommitTime.new(@time, '2 years, 2 months ago').
16
+ seconds_in_a_year = 60 * 60 * 24 * 365
17
+ seconds_in_a_week = 60 * 60 * 24 * 7
18
+ seconds_in_a_day = 60 * 60 * 24
19
+
20
+ expect(Twig::CommitTime.new(@time - (seconds_in_a_year * 2)).
18
21
  instance_variable_get(:@time_ago)).to eq('2y ago')
19
- expect(Twig::CommitTime.new(@time, '1 year ago').
22
+ expect(Twig::CommitTime.new(@time - (seconds_in_a_year * 1)).
20
23
  instance_variable_get(:@time_ago)).to eq('1y ago')
21
- expect(Twig::CommitTime.new(@time, '2 years ago').
22
- instance_variable_get(:@time_ago)).to eq('2y ago')
23
- expect(Twig::CommitTime.new(@time, '2 months ago').
24
+
25
+ two_months_ago = @time - (7 * seconds_in_a_week)
26
+ expect(Twig::CommitTime.new(two_months_ago).
24
27
  instance_variable_get(:@time_ago)).to eq('2mo ago')
25
- expect(Twig::CommitTime.new(@time, '2 weeks ago').
28
+
29
+ two_weeks_ago = @time - (2 * seconds_in_a_week)
30
+ expect(Twig::CommitTime.new(two_weeks_ago).
26
31
  instance_variable_get(:@time_ago)).to eq('2w ago')
27
- expect(Twig::CommitTime.new(@time, '2 days ago').
32
+
33
+ two_days_ago = @time - (2 * seconds_in_a_day)
34
+ expect(Twig::CommitTime.new(two_days_ago).
28
35
  instance_variable_get(:@time_ago)).to eq('2d ago')
29
- expect(Twig::CommitTime.new(@time, '2 hours ago').
36
+
37
+ two_hours_ago = @time - (60 * 120)
38
+ expect(Twig::CommitTime.new(two_hours_ago).
30
39
  instance_variable_get(:@time_ago)).to eq('2h ago')
31
- expect(Twig::CommitTime.new(@time, '2 minutes ago').
40
+
41
+ expect(Twig::CommitTime.new(@time - 120).
32
42
  instance_variable_get(:@time_ago)).to eq('2m ago')
33
- expect(Twig::CommitTime.new(@time, '2 seconds ago').
43
+
44
+ expect(Twig::CommitTime.new(@time - 2).
34
45
  instance_variable_get(:@time_ago)).to eq('2s ago')
46
+
47
+ expect(Twig::CommitTime.new(@time).
48
+ instance_variable_get(:@time_ago)).to eq('0s ago')
49
+ end
50
+ end
51
+
52
+ describe '#count_years_ago' do
53
+ it 'returns 0 for the actual date' do
54
+ ref_time = @time
55
+ commit_time = Twig::CommitTime.new(ref_time)
56
+ expect(commit_time.count_years_ago).to eq(0)
57
+ end
58
+
59
+ it 'returns 1 for one year ago' do
60
+ ref_time = @time - (60 * 60 * 24 * 365)
61
+ commit_time = Twig::CommitTime.new(ref_time)
62
+ expect(commit_time.count_years_ago).to eq(1)
63
+ end
64
+
65
+ it 'returns 2 for 21 months ago' do
66
+ ref_time = @time - (60 * 60 * 24 * 30 * 21)
67
+ commit_time = Twig::CommitTime.new(ref_time)
68
+ expect(commit_time.count_years_ago).to eq(2)
69
+ end
70
+ end
71
+
72
+ describe '#count_months_ago' do
73
+ it 'returns 0 for the actual date' do
74
+ ref_time = @time
75
+ commit_time = Twig::CommitTime.new(ref_time)
76
+ expect(commit_time.count_months_ago).to eq(0)
77
+ end
78
+
79
+ it 'returns 1 for 20 days ago' do
80
+ ref_time = @time - (60 * 60 * 24 * 20)
81
+ commit_time = Twig::CommitTime.new(ref_time)
82
+ expect(commit_time.count_months_ago).to eq(1)
83
+ end
84
+
85
+ it 'returns 2 for 50 days ago' do
86
+ ref_time = @time - (60 * 60 * 24 * 50)
87
+ commit_time = Twig::CommitTime.new(ref_time)
88
+ expect(commit_time.count_months_ago).to eq(2)
89
+ end
90
+ end
91
+
92
+ describe '#count_weeks_ago' do
93
+ it 'returns 0 for the actual date' do
94
+ ref_time = @time
95
+ commit_time = Twig::CommitTime.new(ref_time)
96
+ expect(commit_time.count_weeks_ago).to eq(0)
97
+ end
98
+
99
+ it 'returns 1 for 8 days ago' do
100
+ ref_time = @time - (60 * 60 * 24 * 8)
101
+ commit_time = Twig::CommitTime.new(ref_time)
102
+ expect(commit_time.count_weeks_ago).to eq(1)
103
+ end
104
+
105
+ it 'returns 2 for 14 days ago' do
106
+ ref_time = @time - (60 * 60 * 24 * 14)
107
+ commit_time = Twig::CommitTime.new(ref_time)
108
+ expect(commit_time.count_weeks_ago).to eq(2)
109
+ end
110
+
111
+ it 'returns 3 for 2.5 weeks ago' do
112
+ ref_time = @time - (60 * 60 * 24 * 7 * 2.5)
113
+ commit_time = Twig::CommitTime.new(ref_time)
114
+ expect(commit_time.count_weeks_ago).to eq(3)
115
+ end
116
+ end
117
+
118
+ describe '#count_days_ago' do
119
+ it 'returns 0 for the actual date' do
120
+ ref_time = @time
121
+ commit_time = Twig::CommitTime.new(ref_time)
122
+ expect(commit_time.count_days_ago).to eq(0)
123
+ end
124
+
125
+ it 'returns 1 for one day ago' do
126
+ ref_time = @time - (60 * 60 * 24)
127
+ commit_time = Twig::CommitTime.new(ref_time)
128
+ expect(commit_time.count_days_ago).to eq(1)
129
+ end
130
+
131
+ it 'returns 2 for 1.5 days ago' do
132
+ ref_time = @time - (60 * 60 * 24 * 1.5)
133
+ commit_time = Twig::CommitTime.new(ref_time)
134
+ expect(commit_time.count_days_ago).to eq(2)
135
+ end
136
+ end
137
+
138
+ describe '#count_hours_ago' do
139
+ it 'returns 0 for the actual date' do
140
+ ref_time = @time
141
+ commit_time = Twig::CommitTime.new(ref_time)
142
+ expect(commit_time.count_days_ago).to eq(0)
143
+ end
144
+
145
+ it 'returns 1 for one hour ago' do
146
+ ref_time = @time - (60 * 60)
147
+ commit_time = Twig::CommitTime.new(ref_time)
148
+ expect(commit_time.count_hours_ago).to eq(1)
149
+ end
150
+
151
+ it 'returns 2 for 1.5 hours ago' do
152
+ ref_time = @time - (60 * 60 * 1.5)
153
+ commit_time = Twig::CommitTime.new(ref_time)
154
+ expect(commit_time.count_hours_ago).to eq(2)
155
+ end
156
+ end
157
+
158
+ describe '#count_minutes_ago' do
159
+ it 'returns 0 for the actual date' do
160
+ ref_time = @time
161
+ commit_time = Twig::CommitTime.new(ref_time)
162
+ expect(commit_time.count_minutes_ago).to eq(0)
163
+ end
164
+
165
+ it 'returns 1 for one minute ago' do
166
+ ref_time = @time - 60
167
+ commit_time = Twig::CommitTime.new(ref_time)
168
+ expect(commit_time.count_minutes_ago).to eq(1)
169
+ end
170
+
171
+ it 'returns 20 for 20 minutes ago' do
172
+ ref_time = @time - (60 * 20)
173
+ commit_time = Twig::CommitTime.new(ref_time)
174
+ expect(commit_time.count_minutes_ago).to eq(20)
175
+ end
176
+
177
+ it 'returns 21 for 20.5 minutes ago' do
178
+ ref_time = @time - (60 * 20.5)
179
+ commit_time = Twig::CommitTime.new(ref_time)
180
+ expect(commit_time.count_minutes_ago).to eq(21)
181
+ end
182
+ end
183
+
184
+ describe '#count_seconds_ago' do
185
+ it 'returns 0 for the actual date' do
186
+ ref_time = @time
187
+ commit_time = Twig::CommitTime.new(ref_time)
188
+ expect(commit_time.count_seconds_ago).to eq(0)
189
+ end
190
+
191
+ it 'returns 1 for one second ago' do
192
+ ref_time = @time - 1
193
+ commit_time = Twig::CommitTime.new(ref_time)
194
+ expect(commit_time.count_seconds_ago).to eq(1)
195
+ end
196
+
197
+ it 'returns 70 for one minute and 10 second ago' do
198
+ ref_time = @time - 70
199
+ commit_time = Twig::CommitTime.new(ref_time)
200
+ expect(commit_time.count_seconds_ago).to eq(70)
35
201
  end
36
202
  end
37
203
 
38
204
  describe '#to_i' do
39
205
  it 'returns the time as an integer' do
40
- commit_time = Twig::CommitTime.new(@time, '99 days ago')
206
+ commit_time = Twig::CommitTime.new(@time)
41
207
  expect(commit_time.to_i).to eq(@time.to_i)
42
208
  end
43
209
  end
44
210
 
45
211
  describe '#to_s' do
46
212
  it 'returns a formatted string, including time ago' do
47
- commit_time = Twig::CommitTime.new(@time, '99 days ago')
213
+ ref_time = @time - (60 * 60)
214
+ commit_time = Twig::CommitTime.new(ref_time)
215
+
48
216
  result = commit_time.to_s
217
+
49
218
  expect(result).to include('2000-12-01')
50
- expect(result).to include('(99d ago)')
219
+ expect(result).to include('(1h ago)')
51
220
  end
52
221
  end
53
222
  end