mguymon-whenever 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class OutputEnvTest < Test::Unit::TestCase
4
+
5
+ context "The output from Whenever with environment variables set" do
6
+ setup do
7
+ @output = Whenever.cron \
8
+ <<-file
9
+ env :MYVAR, 'blah'
10
+ env 'MAILTO', "someone@example.com"
11
+ file
12
+ end
13
+
14
+ should "output MYVAR environment variable" do
15
+ assert_match "MYVAR=blah", @output
16
+ end
17
+
18
+ should "output MAILTO environment variable" do
19
+ assert_match "MAILTO=someone@example.com", @output
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,307 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class OutputRedirectionTest < Test::Unit::TestCase
4
+
5
+ context "A command when the output is set to nil" do
6
+ setup do
7
+ @output = Whenever.cron \
8
+ <<-file
9
+ set :job_template, nil
10
+ set :output, nil
11
+ every 2.hours do
12
+ command "blahblah"
13
+ end
14
+ file
15
+ end
16
+
17
+ should "output the command with the log syntax appended" do
18
+ assert_match /^.+ .+ .+ .+ blahblah >> \/dev\/null 2>&1$/, @output
19
+ end
20
+ end
21
+
22
+
23
+ context "A command when the output is set" do
24
+ setup do
25
+ @output = Whenever.cron \
26
+ <<-file
27
+ set :job_template, nil
28
+ set :output, 'logfile.log'
29
+ every 2.hours do
30
+ command "blahblah"
31
+ end
32
+ file
33
+ end
34
+
35
+ should "output the command with the log syntax appended" do
36
+ assert_match /^.+ .+ .+ .+ blahblah >> logfile.log 2>&1$/, @output
37
+ end
38
+ end
39
+
40
+ context "A command when the error and standard output is set by the command" do
41
+ setup do
42
+ @output = Whenever.cron \
43
+ <<-file
44
+ set :job_template, nil
45
+ every 2.hours do
46
+ command "blahblah", :output => {:standard => 'dev_null', :error => 'dev_err'}
47
+ end
48
+ file
49
+ end
50
+
51
+ should "output the command without the log syntax appended" do
52
+ assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
53
+ end
54
+ end
55
+
56
+ context "A command when the output is set and the comand overrides it" do
57
+ setup do
58
+ @output = Whenever.cron \
59
+ <<-file
60
+ set :job_template, nil
61
+ set :output, 'logfile.log'
62
+ every 2.hours do
63
+ command "blahblah", :output => 'otherlog.log'
64
+ end
65
+ file
66
+ end
67
+
68
+ should "output the command with the command syntax appended" do
69
+ assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
70
+ assert_match /^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1$/, @output
71
+ end
72
+ end
73
+
74
+ context "A command when the output is set and the comand overrides with standard and error" do
75
+ setup do
76
+ @output = Whenever.cron \
77
+ <<-file
78
+ set :job_template, nil
79
+ set :output, 'logfile.log'
80
+ every 2.hours do
81
+ command "blahblah", :output => {:error => 'dev_err', :standard => 'dev_null' }
82
+ end
83
+ file
84
+ end
85
+
86
+ should "output the command with the overridden redirection syntax appended" do
87
+ assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
88
+ assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
89
+ end
90
+ end
91
+
92
+ context "A command when the output is set and the comand rejects it" do
93
+ setup do
94
+ @output = Whenever.cron \
95
+ <<-file
96
+ set :job_template, nil
97
+ set :output, 'logfile.log'
98
+ every 2.hours do
99
+ command "blahblah", :output => false
100
+ end
101
+ file
102
+ end
103
+
104
+ should "output the command without the log syntax appended" do
105
+ assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
106
+ assert_match /^.+ .+ .+ .+ blahblah$/, @output
107
+ end
108
+ end
109
+
110
+ context "A command when the output is set and is overridden by the :set option" do
111
+ setup do
112
+ @output = Whenever.cron :set => 'output=otherlog.log', :string => \
113
+ <<-file
114
+ set :job_template, nil
115
+ set :output, 'logfile.log'
116
+ every 2.hours do
117
+ command "blahblah"
118
+ end
119
+ file
120
+ end
121
+
122
+ should "output the otherlog.log as the log file" do
123
+ assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
124
+ assert_match /^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1/, @output
125
+ end
126
+ end
127
+
128
+ context "A command when the error and standard output is set" do
129
+ setup do
130
+ @output = Whenever.cron \
131
+ <<-file
132
+ set :job_template, nil
133
+ set :output, {:error => 'dev_err', :standard => 'dev_null' }
134
+ every 2.hours do
135
+ command "blahblah"
136
+ end
137
+ file
138
+ end
139
+
140
+ should "output the command without the redirection syntax appended" do
141
+ assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
142
+ end
143
+ end
144
+
145
+ context "A command when error output is set" do
146
+ setup do
147
+ @output = Whenever.cron \
148
+ <<-file
149
+ set :job_template, nil
150
+ set :output, {:error => 'dev_null'}
151
+ every 2.hours do
152
+ command "blahblah"
153
+ end
154
+ file
155
+ end
156
+
157
+ should "output the command without the standard error syntax appended" do
158
+ assert_match /^.+ .+ .+ .+ blahblah 2>> dev_null$/, @output
159
+ end
160
+ end
161
+
162
+ context "A command when the standard output is set" do
163
+ setup do
164
+ @output = Whenever.cron \
165
+ <<-file
166
+ set :job_template, nil
167
+ set :output, {:standard => 'dev_out'}
168
+ every 2.hours do
169
+ command "blahblah"
170
+ end
171
+ file
172
+ end
173
+
174
+ should "output the command with standard output syntax appended" do
175
+ assert_match /^.+ .+ .+ .+ blahblah >> dev_out$/, @output
176
+ end
177
+ end
178
+
179
+ context "A command when error output is set by the command" do
180
+ setup do
181
+ @output = Whenever.cron \
182
+ <<-file
183
+ set :job_template, nil
184
+ every 2.hours do
185
+ command "blahblah", :output => {:error => 'dev_err'}
186
+ end
187
+ file
188
+ end
189
+
190
+ should "output the command without the log syntax appended" do
191
+ assert_match /^.+ .+ .+ .+ blahblah 2>> dev_err$/, @output
192
+ end
193
+ end
194
+
195
+ context "A command when standard output is set by the command" do
196
+ setup do
197
+ @output = Whenever.cron \
198
+ <<-file
199
+ set :job_template, nil
200
+ every 2.hours do
201
+ command "blahblah", :output => {:standard => 'dev_out'}
202
+ end
203
+ file
204
+ end
205
+
206
+ should "output the command without the log syntax appended" do
207
+ assert_match /^.+ .+ .+ .+ blahblah >> dev_out$/, @output
208
+ end
209
+ end
210
+
211
+ context "A command when standard output is set to nil" do
212
+ setup do
213
+ @output = Whenever.cron \
214
+ <<-file
215
+ set :job_template, nil
216
+ every 2.hours do
217
+ command "blahblah", :output => {:standard => nil}
218
+ end
219
+ file
220
+ end
221
+
222
+ should "output the command with stdout directed to /dev/null" do
223
+ assert_match /^.+ .+ .+ .+ blahblah > \/dev\/null$/, @output
224
+ end
225
+ end
226
+
227
+ context "A command when standard error is set to nil" do
228
+ setup do
229
+ @output = Whenever.cron \
230
+ <<-file
231
+ set :job_template, nil
232
+ every 2.hours do
233
+ command "blahblah", :output => {:error => nil}
234
+ end
235
+ file
236
+ end
237
+
238
+ should "output the command with stderr directed to /dev/null" do
239
+ assert_match /^.+ .+ .+ .+ blahblah 2> \/dev\/null$/, @output
240
+ end
241
+ end
242
+
243
+ context "A command when standard output and standard error is set to nil" do
244
+ setup do
245
+ @output = Whenever.cron \
246
+ <<-file
247
+ set :job_template, nil
248
+ every 2.hours do
249
+ command "blahblah", :output => {:error => nil, :standard => nil}
250
+ end
251
+ file
252
+ end
253
+
254
+ should "output the command with stderr directed to /dev/null" do
255
+ assert_match /^.+ .+ .+ .+ blahblah > \/dev\/null 2>&1$/, @output
256
+ end
257
+ end
258
+
259
+ context "A command when standard output is set and standard error is set to nil" do
260
+ setup do
261
+ @output = Whenever.cron \
262
+ <<-file
263
+ set :job_template, nil
264
+ every 2.hours do
265
+ command "blahblah", :output => {:error => nil, :standard => 'my.log'}
266
+ end
267
+ file
268
+ end
269
+
270
+ should "output the command with stderr directed to /dev/null" do
271
+ assert_match /^.+ .+ .+ .+ blahblah >> my.log 2> \/dev\/null$/, @output
272
+ end
273
+ end
274
+
275
+ context "A command when standard output is nil and standard error is set" do
276
+ setup do
277
+ @output = Whenever.cron \
278
+ <<-file
279
+ set :job_template, nil
280
+ every 2.hours do
281
+ command "blahblah", :output => {:error => 'my_error.log', :standard => nil}
282
+ end
283
+ file
284
+ end
285
+
286
+ should "output the command with stderr directed to /dev/null" do
287
+ assert_match /^.+ .+ .+ .+ blahblah >> \/dev\/null 2>> my_error.log$/, @output
288
+ end
289
+ end
290
+
291
+ context "A command when the deprecated :cron_log is set" do
292
+ setup do
293
+ @output = Whenever.cron \
294
+ <<-file
295
+ set :job_template, nil
296
+ set :cron_log, "cron.log"
297
+ every 2.hours do
298
+ command "blahblah"
299
+ end
300
+ file
301
+ end
302
+
303
+ should "output the command with with the stdout and stderr going to the log" do
304
+ assert_match /^.+ .+ .+ .+ blahblah >> cron.log 2>&1$/, @output
305
+ end
306
+ end
307
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ # Want to test the files here, in lib, not in an installed version of the gem.
4
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
5
+ require 'whenever'
6
+
7
+ require 'shoulda'
8
+ require 'mocha'
9
+
10
+ module TestExtensions
11
+
12
+ def two_hours
13
+ "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
14
+ end
15
+
16
+ end
17
+
18
+ class Test::Unit::TestCase
19
+ include TestExtensions
20
+ end
@@ -0,0 +1,226 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class CronTest < Test::Unit::TestCase
4
+
5
+ context "When parsing time in minutes" do
6
+ should "raise if less than 1 minute" do
7
+ assert_raises ArgumentError do
8
+ parse_time(59.seconds)
9
+ end
10
+
11
+ assert_raises ArgumentError do
12
+ parse_time(0.minutes)
13
+ end
14
+ end
15
+
16
+ # For santity, do some tests on straight String
17
+ should "parse correctly" do
18
+ assert_equal '* * * * *', parse_time(1.minute)
19
+ assert_equal '0,5,10,15,20,25,30,35,40,45,50,55 * * * *', parse_time(5.minutes)
20
+ assert_equal '7,14,21,28,35,42,49,56 * * * *', parse_time(7.minutes)
21
+ assert_equal '0,30 * * * *', parse_time(30.minutes)
22
+ assert_equal '32 * * * *', parse_time(32.minutes)
23
+ assert_not_equal '60 * * * *', parse_time(60.minutes) # 60 minutes bumps up into the hour range
24
+ end
25
+
26
+ # Test all minutes
27
+ (2..59).each do |num|
28
+ should "parse correctly for #{num} minutes" do
29
+ start = 0
30
+ start += num unless 60.modulo(num).zero?
31
+ minutes = (start..59).step(num).to_a
32
+
33
+ assert_equal "#{minutes.join(',')} * * * *", parse_time(num.minutes)
34
+ end
35
+ end
36
+ end
37
+
38
+ context "When parsing time in hours" do
39
+ should "parse correctly" do
40
+ assert_equal '0 * * * *', parse_time(1.hour)
41
+ assert_equal '0 0,2,4,6,8,10,12,14,16,18,20,22 * * *', parse_time(2.hours)
42
+ assert_equal '0 0,3,6,9,12,15,18,21 * * *', parse_time(3.hours)
43
+ assert_equal '0 5,10,15,20 * * *', parse_time(5.hours)
44
+ assert_equal '0 17 * * *', parse_time(17.hours)
45
+ assert_not_equal '0 24 * * *', parse_time(24.hours) # 24 hours bumps up into the day range
46
+ end
47
+
48
+ (2..23).each do |num|
49
+ should "parse correctly for #{num} hours" do
50
+ start = 0
51
+ start += num unless 24.modulo(num).zero?
52
+ hours = (start..23).step(num).to_a
53
+
54
+ assert_equal "0 #{hours.join(',')} * * *", parse_time(num.hours)
55
+ end
56
+ end
57
+
58
+ should "parse correctly when given an 'at' with minutes as an Integer" do
59
+ assert_minutes_equals "1", 1
60
+ assert_minutes_equals "14", 14
61
+ assert_minutes_equals "27", 27
62
+ assert_minutes_equals "55", 55
63
+ end
64
+
65
+ should "parse correctly when given an 'at' with minutes as a Time" do
66
+ # Basically just testing that Chronic parses some times and we get the minutes out of it
67
+ assert_minutes_equals "1", '3:01am'
68
+ assert_minutes_equals "1", 'January 21 2:01 PM'
69
+ assert_minutes_equals "0", 'midnight'
70
+ assert_minutes_equals "59", '13:59'
71
+ end
72
+ end
73
+
74
+ context "When parsing time in days (of month)" do
75
+ should "parse correctly" do
76
+ assert_equal '0 0 * * *', parse_time(1.days)
77
+ assert_equal '0 0 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * *', parse_time(2.days)
78
+ assert_equal '0 0 1,5,9,13,17,21,25,29 * *', parse_time(4.days)
79
+ assert_equal '0 0 1,8,15,22 * *', parse_time(7.days)
80
+ assert_equal '0 0 1,17 * *', parse_time(16.days)
81
+ assert_equal '0 0 17 * *', parse_time(17.days)
82
+ assert_equal '0 0 29 * *', parse_time(29.days)
83
+ assert_not_equal '0 0 30 * *', parse_time(30.days) # 30 days bumps into the month range
84
+ end
85
+
86
+ should "parse correctly when given an 'at' with hours, minutes as a Time" do
87
+ # first param is an array with [hours, minutes]
88
+ assert_hours_and_minutes_equals %w(3 45), '3:45am'
89
+ assert_hours_and_minutes_equals %w(20 1), '8:01pm'
90
+ assert_hours_and_minutes_equals %w(0 0), 'midnight'
91
+ assert_hours_and_minutes_equals %w(1 23), '1:23 AM'
92
+ assert_hours_and_minutes_equals %w(23 59), 'March 21 11:59 pM'
93
+ end
94
+
95
+ should "parse correctly when given an 'at' with hours as an Integer" do
96
+ # first param is an array with [hours, minutes]
97
+ assert_hours_and_minutes_equals %w(1 0), 1
98
+ assert_hours_and_minutes_equals %w(3 0), 3
99
+ assert_hours_and_minutes_equals %w(15 0), 15
100
+ assert_hours_and_minutes_equals %w(19 0), 19
101
+ assert_hours_and_minutes_equals %w(23 0), 23
102
+ end
103
+ end
104
+
105
+ context "When parsing time in months" do
106
+ should "parse correctly" do
107
+ assert_equal '0 0 1 * *', parse_time(1.month)
108
+ assert_equal '0 0 1 1,3,5,7,9,11 *', parse_time(2.months)
109
+ assert_equal '0 0 1 1,4,7,10 *', parse_time(3.months)
110
+ assert_equal '0 0 1 1,5,9 *', parse_time(4.months)
111
+ assert_equal '0 0 1 1,6 *', parse_time(5.months)
112
+ assert_equal '0 0 1 7 *', parse_time(7.months)
113
+ assert_equal '0 0 1 8 *', parse_time(8.months)
114
+ assert_equal '0 0 1 9 *', parse_time(9.months)
115
+ assert_equal '0 0 1 10 *', parse_time(10.months)
116
+ assert_equal '0 0 1 11 *', parse_time(11.months)
117
+ assert_equal '0 0 1 12 *', parse_time(12.months)
118
+ end
119
+
120
+ should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
121
+ # first param is an array with [days, hours, minutes]
122
+ assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
123
+ assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
124
+ assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
125
+ assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
126
+ end
127
+
128
+ should "parse correctly when given an 'at' with days as an Integer" do
129
+ # first param is an array with [days, hours, minutes]
130
+ assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
131
+ assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
132
+ assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
133
+ end
134
+ end
135
+
136
+ context "When parsing time in days (of week)" do
137
+ should "parse days of the week correctly" do
138
+ {
139
+ '0' => %w(sun Sunday SUNDAY SUN),
140
+ '1' => %w(mon Monday MONDAY MON),
141
+ '2' => %w(tue tues Tuesday TUESDAY TUE),
142
+ '3' => %w(wed Wednesday WEDNESDAY WED),
143
+ '4' => %w(thu thurs thur Thursday THURSDAY THU),
144
+ '5' => %w(fri Friday FRIDAY FRI),
145
+ '6' => %w(sat Saturday SATURDAY SAT)
146
+ }.each do |day, day_tests|
147
+ day_tests.each do |day_test|
148
+ assert_equal "0 0 * * #{day}", parse_time(day_test)
149
+ end
150
+ end
151
+ end
152
+
153
+ should "allow additional directives" do
154
+ assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
155
+ assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
156
+ assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
157
+ end
158
+
159
+ should "parse weekday correctly" do
160
+ assert_equal '0 0 * * 1-5', parse_time('weekday')
161
+ assert_equal '0 0 * * 1-5', parse_time('Weekdays')
162
+ assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
163
+ assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
164
+ end
165
+
166
+ should "parse weekend correctly" do
167
+ assert_equal '0 0 * * 6,0', parse_time('weekend')
168
+ assert_equal '0 0 * * 6,0', parse_time('Weekends')
169
+ assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
170
+ assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
171
+ end
172
+ end
173
+
174
+ context "When parsing time using the cron shortcuts" do
175
+ should "parse a :symbol into the correct shortcut" do
176
+ assert_equal '@reboot', parse_time(:reboot)
177
+ assert_equal '@annually', parse_time(:year)
178
+ assert_equal '@annually', parse_time(:yearly)
179
+ assert_equal '@daily', parse_time(:day)
180
+ assert_equal '@daily', parse_time(:daily)
181
+ assert_equal '@midnight', parse_time(:midnight)
182
+ assert_equal '@monthly', parse_time(:month)
183
+ assert_equal '@monthly', parse_time(:monthly)
184
+ assert_equal '@hourly', parse_time(:hour)
185
+ assert_equal '@hourly', parse_time(:hourly)
186
+ end
187
+
188
+ should "raise an exception if a valid shortcut is given but also an :at" do
189
+ assert_raises ArgumentError do
190
+ parse_time(:hour, nil, "1:00 am")
191
+ end
192
+
193
+ assert_raises ArgumentError do
194
+ parse_time(:reboot, nil, 5)
195
+ end
196
+
197
+ assert_raises ArgumentError do
198
+ parse_time(:day, nil, '4:20pm')
199
+ end
200
+ end
201
+ end
202
+
203
+ private
204
+
205
+ def assert_days_and_hours_and_minutes_equals(expected, time)
206
+ cron = parse_time(2.months, 'some task', time)
207
+ minutes, hours, days, *garbage = cron.split(' ')
208
+ assert_equal expected, [days, hours, minutes]
209
+ end
210
+
211
+ def assert_hours_and_minutes_equals(expected, time)
212
+ cron = parse_time(2.days, 'some task', time)
213
+ minutes, hours, *garbage = cron.split(' ')
214
+ assert_equal expected, [hours, minutes]
215
+ end
216
+
217
+ def assert_minutes_equals(expected, time)
218
+ cron = parse_time(2.hours, 'some task', time)
219
+ assert_equal expected, cron.split(' ')[0]
220
+ end
221
+
222
+ def parse_time(time = nil, task = nil, at = nil)
223
+ Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
224
+ end
225
+
226
+ end