whenever 0.4.1 → 0.5.3

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,326 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class CommandLineTest < Test::Unit::TestCase
4
+
5
+ context "A command line write" do
6
+ setup do
7
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
8
+ @command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
9
+ @task = "#{two_hours} /my/command"
10
+ Whenever.expects(:cron).returns(@task)
11
+ end
12
+
13
+ should "output the cron job with identifier blocks" do
14
+ output = <<-EXPECTED
15
+ # Begin Whenever generated tasks for: My identifier
16
+ #{@task}
17
+ # End Whenever generated tasks for: My identifier
18
+ EXPECTED
19
+
20
+ assert_equal output, @command.send(:whenever_cron)
21
+ end
22
+
23
+ should "write the crontab when run" do
24
+ @command.expects(:write_crontab).returns(true)
25
+ assert @command.run
26
+ end
27
+ end
28
+
29
+ context "A command line update" do
30
+ setup do
31
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
32
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
33
+ @task = "#{two_hours} /my/command"
34
+ Whenever.expects(:cron).returns(@task)
35
+ end
36
+
37
+ should "add the cron to the end of the file if there is no existing identifier block" do
38
+ existing = '# Existing crontab'
39
+ @command.expects(:read_crontab).at_least_once.returns(existing)
40
+
41
+ new_cron = <<-EXPECTED
42
+ #{existing}
43
+
44
+ # Begin Whenever generated tasks for: My identifier
45
+ #{@task}
46
+ # End Whenever generated tasks for: My identifier
47
+ EXPECTED
48
+
49
+ assert_equal new_cron, @command.send(:updated_crontab)
50
+
51
+ @command.expects(:write_crontab).with(new_cron).returns(true)
52
+ assert @command.run
53
+ end
54
+
55
+ should "replace an existing block if the identifier matches" do
56
+ existing = <<-EXISTING_CRON
57
+ # Something
58
+
59
+ # Begin Whenever generated tasks for: My identifier
60
+ My whenever job that was already here
61
+ # End Whenever generated tasks for: My identifier
62
+
63
+ # Begin Whenever generated tasks for: Other identifier
64
+ This shouldn't get replaced
65
+ # End Whenever generated tasks for: Other identifier
66
+ EXISTING_CRON
67
+
68
+ new_cron = <<-NEW_CRON
69
+ # Something
70
+
71
+ # Begin Whenever generated tasks for: My identifier
72
+ #{@task}
73
+ # End Whenever generated tasks for: My identifier
74
+
75
+ # Begin Whenever generated tasks for: Other identifier
76
+ This shouldn't get replaced
77
+ # End Whenever generated tasks for: Other identifier
78
+ NEW_CRON
79
+
80
+ @command.expects(:read_crontab).at_least_once.returns(existing)
81
+ assert_equal new_cron, @command.send(:updated_crontab)
82
+
83
+ @command.expects(:write_crontab).with(new_cron).returns(true)
84
+ assert @command.run
85
+ end
86
+ end
87
+
88
+ context "A command line update that contains backslashes" do
89
+ setup do
90
+ @existing = <<-EXISTING_CRON
91
+ # Begin Whenever generated tasks for: My identifier
92
+ script/runner -e production 'puts '\\''hello'\\'''
93
+ # End Whenever generated tasks for: My identifier
94
+ EXISTING_CRON
95
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
96
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
97
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
98
+ @command.expects(:whenever_cron).returns(@existing)
99
+ end
100
+
101
+ should "replace the existing block with the backslashes in tact" do
102
+ assert_equal @existing, @command.send(:updated_crontab)
103
+ end
104
+ end
105
+
106
+ context "A command line update with an identifier similar to an existing one in the crontab already" do
107
+ setup do
108
+ @existing = <<-EXISTING_CRON
109
+ # Begin Whenever generated tasks for: WheneverExisting
110
+ # End Whenever generated tasks for: WheneverExisting
111
+ EXISTING_CRON
112
+ @new = <<-NEW_CRON
113
+ # Begin Whenever generated tasks for: Whenever
114
+ # End Whenever generated tasks for: Whenever
115
+ NEW_CRON
116
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
117
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'Whenever')
118
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
119
+ @command.expects(:whenever_cron).returns(@new)
120
+ end
121
+
122
+ should "append the similarly named command" do
123
+ assert_equal @existing + "\n\n" + @new, @command.send(:updated_crontab)
124
+ end
125
+ end
126
+
127
+ context "A command line delete" do
128
+ setup do
129
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
130
+ @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
131
+ @task = "#{two_hours} /my/command"
132
+ end
133
+
134
+ should "add an empty identifier block if there is no existing one" do
135
+ existing = '# Existing crontab'
136
+ @command.expects(:read_crontab).at_least_once.returns(existing)
137
+
138
+ new_cron = <<-EXPECTED
139
+ #{existing}
140
+
141
+ # Begin Whenever generated tasks for: My identifier
142
+ # End Whenever generated tasks for: My identifier
143
+ EXPECTED
144
+
145
+ assert_equal new_cron, @command.send(:updated_crontab)
146
+
147
+ @command.expects(:write_crontab).with(new_cron).returns(true)
148
+ assert @command.run
149
+ end
150
+
151
+ should "delete an existing block if the identifier matches" do
152
+ existing = <<-EXISTING_CRON
153
+ # Something
154
+
155
+ # Begin Whenever generated tasks for: My identifier
156
+ My whenever job that was already here
157
+ # End Whenever generated tasks for: My identifier
158
+
159
+ # Begin Whenever generated tasks for: Other identifier
160
+ This shouldn't get replaced
161
+ # End Whenever generated tasks for: Other identifier
162
+ EXISTING_CRON
163
+
164
+ @command.expects(:read_crontab).at_least_once.returns(existing)
165
+
166
+ new_cron = <<-NEW_CRON
167
+ # Something
168
+
169
+ # Begin Whenever generated tasks for: My identifier
170
+ # End Whenever generated tasks for: My identifier
171
+
172
+ # Begin Whenever generated tasks for: Other identifier
173
+ This shouldn't get replaced
174
+ # End Whenever generated tasks for: Other identifier
175
+ NEW_CRON
176
+
177
+ assert_equal new_cron, @command.send(:updated_crontab)
178
+
179
+ @command.expects(:write_crontab).with(new_cron).returns(true)
180
+ assert @command.run
181
+ end
182
+ end
183
+
184
+ context "A command line update with no identifier" do
185
+ setup do
186
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
187
+ Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
188
+ @command = Whenever::CommandLine.new(:update => true, :file => @file)
189
+ end
190
+
191
+ should "use the default identifier" do
192
+ assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
193
+ end
194
+ end
195
+
196
+ context "combined params" do
197
+ setup do
198
+ Whenever::CommandLine.any_instance.expects(:exit)
199
+ Whenever::CommandLine.any_instance.expects(:warn)
200
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
201
+ end
202
+
203
+ should "exit with write and clear" do
204
+ @command = Whenever::CommandLine.new(:write => true, :clear => true)
205
+ end
206
+
207
+ should "exit with write and update" do
208
+ @command = Whenever::CommandLine.new(:write => true, :update => true)
209
+ end
210
+
211
+ should "exit with update and clear" do
212
+ @command = Whenever::CommandLine.new(:update => true, :clear => true)
213
+ end
214
+ end
215
+
216
+ context "A runner where the environment is overridden using the :set option" do
217
+ setup do
218
+ @output = Whenever.cron :set => 'environment=serious', :string => \
219
+ <<-file
220
+ set :environment, :silly
221
+ set :path, '/my/path'
222
+ every 2.hours do
223
+ runner "blahblah"
224
+ end
225
+ file
226
+ end
227
+
228
+ should "output the runner using the override environment" do
229
+ assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output
230
+ end
231
+ end
232
+
233
+ context "A runner where the environment and path are overridden using the :set option" do
234
+ setup do
235
+ @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
236
+ <<-file
237
+ set :environment, :silly
238
+ set :path, '/silly/path'
239
+ every 2.hours do
240
+ runner "blahblah"
241
+ end
242
+ file
243
+ end
244
+
245
+ should "output the runner using the overridden path and environment" do
246
+ assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
247
+ end
248
+ end
249
+
250
+ context "A runner where the environment and path are overridden using the :set option with spaces in the string" do
251
+ setup do
252
+ @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
253
+ <<-file
254
+ set :environment, :silly
255
+ set :path, '/silly/path'
256
+ every 2.hours do
257
+ runner "blahblah"
258
+ end
259
+ file
260
+ end
261
+
262
+ should "output the runner using the overridden path and environment" do
263
+ assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
264
+ end
265
+ end
266
+
267
+ context "A runner where the environment is overridden using the :set option but no value is given" do
268
+ setup do
269
+ @output = Whenever.cron :set => ' environment=', :string => \
270
+ <<-file
271
+ set :environment, :silly
272
+ set :path, '/silly/path'
273
+ every 2.hours do
274
+ runner "blahblah"
275
+ end
276
+ file
277
+ end
278
+
279
+ should "output the runner using the original environmnet" do
280
+ assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output
281
+ end
282
+ end
283
+
284
+ context "prepare-ing the output" do
285
+ setup do
286
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
287
+ end
288
+
289
+ should "not trim off the top lines of the file" do
290
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
291
+ existing = <<-EXISTING_CRON
292
+ # Useless Comments
293
+ # at the top of the file
294
+
295
+ # Begin Whenever generated tasks for: My identifier
296
+ My whenever job that was already here
297
+ # End Whenever generated tasks for: My identifier
298
+ EXISTING_CRON
299
+
300
+ # here-doc adds an extra newline we need removed
301
+ assert_equal existing.strip, @command.send(:prepare, existing)
302
+ end
303
+
304
+ should "trim off the top lines of the file" do
305
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
306
+ existing = <<-EXISTING_CRON
307
+ # Useless Comments
308
+ # at the top of the file
309
+
310
+ # Begin Whenever generated tasks for: My identifier
311
+ My whenever job that was already here
312
+ # End Whenever generated tasks for: My identifier
313
+ EXISTING_CRON
314
+
315
+ new_cron = <<-NEW_CRON
316
+ # Begin Whenever generated tasks for: My identifier
317
+ My whenever job that was already here
318
+ # End Whenever generated tasks for: My identifier
319
+ NEW_CRON
320
+
321
+ # here-doc adds an extra newline we need removed
322
+ assert_equal new_cron.strip, @command.send(:prepare, existing)
323
+ end
324
+ end
325
+
326
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/test_helper")
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
2
 
3
3
  class OutputAtTest < Test::Unit::TestCase
4
4
 
@@ -91,7 +91,7 @@ class OutputAtTest < Test::Unit::TestCase
91
91
  end
92
92
 
93
93
  should "output the runner using one entry because the times are aligned" do
94
- assert_match '2 5,15 * * 1,3,5 /your/path/script/runner -e production "blahblah"', @output
94
+ assert_match %(2 5,15 * * 1,3,5 cd /your/path && script/runner -e production 'blahblah'), @output
95
95
  end
96
96
  end
97
97
 
@@ -168,11 +168,70 @@ class OutputAtTest < Test::Unit::TestCase
168
168
 
169
169
  should "output all of the commands @daily" do
170
170
  assert_match '@daily cd /your/path && RAILS_ENV=production /usr/bin/env rake blah:blah', @output
171
- assert_match '@daily /your/path/script/runner -e production "runner_1"', @output
171
+ assert_match %(@daily cd /your/path && script/runner -e production 'runner_1'), @output
172
172
  assert_match '@daily command_1', @output
173
- assert_match '@daily /your/path/script/runner -e production "runner_2"', @output
173
+ assert_match %(@daily cd /your/path && script/runner -e production 'runner_2'), @output
174
174
  assert_match '@daily command_2', @output
175
175
  end
176
176
  end
177
177
 
178
+ context "every 5 minutes but but starting at 1" do
179
+ setup do
180
+ @output = Whenever.cron \
181
+ <<-file
182
+ every 5.minutes, :at => 1 do
183
+ command "blahblah"
184
+ end
185
+ file
186
+ end
187
+
188
+ should "output the command using that time" do
189
+ assert_match '1,6,11,16,21,26,31,36,41,46,51,56 * * * * blahblah', @output
190
+ end
191
+ end
192
+
193
+ context "every 4 minutes but starting at 2" do
194
+ setup do
195
+ @output = Whenever.cron \
196
+ <<-file
197
+ every 4.minutes, :at => 2 do
198
+ command "blahblah"
199
+ end
200
+ file
201
+ end
202
+
203
+ should "output the command using that time" do
204
+ assert_match '2,6,10,14,18,22,26,30,34,38,42,46,50,54,58 * * * * blahblah', @output
205
+ end
206
+ end
207
+
208
+ context "every 3 minutes but starting at 7" do
209
+ setup do
210
+ @output = Whenever.cron \
211
+ <<-file
212
+ every 3.minutes, :at => 7 do
213
+ command "blahblah"
214
+ end
215
+ file
216
+ end
217
+
218
+ should "output the command using that time" do
219
+ assert_match '7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58 * * * * blahblah', @output
220
+ end
221
+ end
222
+
223
+ context "every 2 minutes but starting at 27" do
224
+ setup do
225
+ @output = Whenever.cron \
226
+ <<-file
227
+ every 2.minutes, :at => 27 do
228
+ command "blahblah"
229
+ end
230
+ file
231
+ end
232
+
233
+ should "output the command using that time" do
234
+ assert_match '27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * * blahblah', @output
235
+ end
236
+ end
178
237
  end
@@ -1,47 +1,64 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/test_helper")
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
2
 
3
- class OutputRakeTest < Test::Unit::TestCase
3
+ class OutputDefaultDefinedJobsTest < Test::Unit::TestCase
4
4
 
5
- # Rake are generated in an almost identical way to runners so we
6
- # only need some basic tests to ensure they are output correctly
5
+ # command
7
6
 
8
- context "A rake command with path set" do
7
+ context "A plain command" do
8
+ setup do
9
+ @output = Whenever.cron \
10
+ <<-file
11
+ every 2.hours do
12
+ command "blahblah"
13
+ end
14
+ file
15
+ end
16
+
17
+ should "output the command" do
18
+ assert_match /^.+ .+ .+ .+ blahblah$/, @output
19
+ end
20
+ end
21
+
22
+ # runner
23
+
24
+ context "A runner with path set" do
9
25
  setup do
10
26
  @output = Whenever.cron \
11
27
  <<-file
12
28
  set :path, '/my/path'
13
29
  every 2.hours do
14
- rake "blahblah"
30
+ runner 'blahblah'
15
31
  end
16
32
  file
17
33
  end
18
34
 
19
- should "output the rake command using that path" do
20
- assert_match two_hours + ' cd /my/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
35
+ should "output the runner using that path" do
36
+ assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), @output
21
37
  end
22
38
  end
23
39
 
24
- context "A rake command that overrides the path set" do
40
+ context "A runner that overrides the path set" do
25
41
  setup do
26
42
  @output = Whenever.cron \
27
43
  <<-file
28
44
  set :path, '/my/path'
29
45
  every 2.hours do
30
- rake "blahblah", :path => '/some/other/path'
46
+ runner "blahblah", :path => '/some/other/path'
31
47
  end
32
48
  file
33
49
  end
34
50
 
35
- should "output the rake command using that path" do
36
- assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
51
+ should "output the runner using that path" do
52
+ assert_match two_hours + %( cd /some/other/path && script/runner -e production 'blahblah'), @output
37
53
  end
38
54
  end
39
55
 
40
- context "A rake command with environment set" do
56
+ # rake
57
+
58
+ context "A rake command with path set" do
41
59
  setup do
42
60
  @output = Whenever.cron \
43
61
  <<-file
44
- set :environment, :silly
45
62
  set :path, '/my/path'
46
63
  every 2.hours do
47
64
  rake "blahblah"
@@ -49,25 +66,24 @@ class OutputRakeTest < Test::Unit::TestCase
49
66
  file
50
67
  end
51
68
 
52
- should "output the rake command using that environment" do
53
- assert_match two_hours + ' cd /my/path && RAILS_ENV=silly /usr/bin/env rake blahblah', @output
69
+ should "output the rake command using that path" do
70
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
54
71
  end
55
72
  end
56
73
 
57
- context "A rake command that overrides the environment set" do
74
+ context "A rake command that overrides the path set" do
58
75
  setup do
59
76
  @output = Whenever.cron \
60
77
  <<-file
61
- set :environment, :silly
62
78
  set :path, '/my/path'
63
79
  every 2.hours do
64
- rake "blahblah", :environment => :serious
80
+ rake "blahblah", :path => '/some/other/path'
65
81
  end
66
82
  file
67
83
  end
68
84
 
69
- should "output the rake command using that environment" do
70
- assert_match two_hours + ' cd /my/path && RAILS_ENV=serious /usr/bin/env rake blahblah', @output
85
+ should "output the rake command using that path" do
86
+ assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
71
87
  end
72
88
  end
73
89
 
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class OutputDefinedJobTest < Test::Unit::TestCase
4
+
5
+ context "A defined job with a :task" do
6
+ setup do
7
+ @output = Whenever.cron \
8
+ <<-file
9
+ job_type :some_job, "before :task after"
10
+ every 2.hours do
11
+ some_job "during"
12
+ end
13
+ file
14
+ end
15
+
16
+ should "output the defined job with the task" do
17
+ assert_match /^.+ .+ .+ .+ before during after$/, @output
18
+ end
19
+ end
20
+
21
+ context "A defined job with a :task and some options" do
22
+ setup do
23
+ @output = Whenever.cron \
24
+ <<-file
25
+ job_type :some_job, "before :task after :option1 :option2"
26
+ every 2.hours do
27
+ some_job "during", :option1 => 'happy', :option2 => 'birthday'
28
+ end
29
+ file
30
+ end
31
+
32
+ should "output the defined job with the task and options" do
33
+ assert_match /^.+ .+ .+ .+ before during after happy birthday$/, @output
34
+ end
35
+ end
36
+
37
+ context "A defined job with a :task and an option where the option is set globally" do
38
+ setup do
39
+ @output = Whenever.cron \
40
+ <<-file
41
+ job_type :some_job, "before :task after :option1"
42
+ set :option1, 'happy'
43
+ every 2.hours do
44
+ some_job "during"
45
+ end
46
+ file
47
+ end
48
+
49
+ should "output the defined job with the task and options" do
50
+ assert_match /^.+ .+ .+ .+ before during after happy$/, @output
51
+ end
52
+ end
53
+
54
+ context "A defined job with a :task and an option where the option is set globally and locally" do
55
+ setup do
56
+ @output = Whenever.cron \
57
+ <<-file
58
+ job_type :some_job, "before :task after :option1"
59
+ set :option1, 'global'
60
+ every 2.hours do
61
+ some_job "during", :option1 => 'local'
62
+ end
63
+ file
64
+ end
65
+
66
+ should "output the defined job using the local option" do
67
+ assert_match /^.+ .+ .+ .+ before during after local$/, @output
68
+ end
69
+ end
70
+
71
+ context "A defined job with a :task and an option that is not set" do
72
+ setup do
73
+ @output = Whenever.cron \
74
+ <<-file
75
+ job_type :some_job, "before :task after :option1"
76
+ every 2.hours do
77
+ some_job "during", :option2 => 'happy'
78
+ end
79
+ file
80
+ end
81
+
82
+ should "output the defined job with that option removed" do
83
+ assert_match /^.+ .+ .+ .+ before during after$/, @output
84
+ end
85
+ end
86
+
87
+ context "A defined job that uses a :path where none is explicitly set" do
88
+ setup do
89
+ Whenever.expects(:path).returns('/my/path')
90
+
91
+ @output = Whenever.cron \
92
+ <<-file
93
+ job_type :some_job, "cd :path && :task"
94
+ every 2.hours do
95
+ some_job 'blahblah'
96
+ end
97
+ file
98
+ end
99
+
100
+ should "default to using the Whenever.path" do
101
+ assert_match two_hours + %( cd /my/path && blahblah), @output
102
+ end
103
+ end
104
+
105
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/test_helper")
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
2
 
3
3
  class OutputEnvTest < Test::Unit::TestCase
4
4