whenever 0.9.2 → 0.10.0

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +17 -7
  4. data/CHANGELOG.md +63 -0
  5. data/Gemfile +0 -5
  6. data/LICENSE +1 -1
  7. data/README.md +101 -18
  8. data/Rakefile +3 -10
  9. data/bin/whenever +3 -0
  10. data/bin/wheneverize +8 -5
  11. data/gemfiles/activesupport4.1.gemfile +5 -0
  12. data/gemfiles/activesupport4.2.gemfile +5 -0
  13. data/lib/whenever/capistrano/v2/recipes.rb +4 -3
  14. data/lib/whenever/capistrano/v3/tasks/whenever.rake +20 -7
  15. data/lib/whenever/capistrano.rb +1 -1
  16. data/lib/whenever/command_line.rb +49 -28
  17. data/lib/whenever/cron.rb +43 -21
  18. data/lib/whenever/job.rb +4 -3
  19. data/lib/whenever/job_list.rb +54 -24
  20. data/lib/whenever/numeric.rb +13 -0
  21. data/lib/whenever/numeric_seconds.rb +48 -0
  22. data/lib/whenever/os.rb +7 -0
  23. data/lib/whenever/setup.rb +5 -1
  24. data/lib/whenever/version.rb +1 -1
  25. data/lib/whenever.rb +15 -14
  26. data/test/functional/command_line_test.rb +379 -255
  27. data/test/functional/output_at_test.rb +227 -249
  28. data/test/functional/output_default_defined_jobs_test.rb +239 -288
  29. data/test/functional/output_defined_job_test.rb +65 -91
  30. data/test/functional/output_env_test.rb +22 -26
  31. data/test/functional/output_jobs_for_roles_test.rb +46 -66
  32. data/test/functional/output_jobs_with_mailto_test.rb +168 -0
  33. data/test/functional/output_redirection_test.rb +231 -309
  34. data/test/test_case.rb +32 -0
  35. data/test/test_helper.rb +44 -15
  36. data/test/unit/capistrano_support_test.rb +126 -148
  37. data/test/unit/cron_test.rb +327 -210
  38. data/test/unit/executable_test.rb +142 -0
  39. data/test/unit/job_test.rb +111 -121
  40. data/whenever.gemspec +5 -4
  41. metadata +37 -28
  42. data/lib/whenever/tasks/whenever.rake +0 -43
@@ -1,334 +1,458 @@
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
1
+ require 'test_helper'
2
+
3
+ class CommandLineWriteTest < Whenever::TestCase
4
+ setup do
5
+ Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
6
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
7
+ @command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
8
+ @task = "#{two_hours} /my/command"
9
+ Whenever.expects(:cron).returns(@task)
10
+ end
12
11
 
13
- should "output the cron job with identifier blocks" do
14
- output = <<-EXPECTED
15
- # Begin Whenever generated tasks for: My identifier
12
+ should "output the cron job with identifier blocks" do
13
+ output = <<-EXPECTED
14
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
16
15
  #{@task}
17
- # End Whenever generated tasks for: My identifier
16
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
18
17
  EXPECTED
19
18
 
20
- assert_equal output, @command.send(:whenever_cron)
21
- end
19
+ assert_equal output, @command.send(:whenever_cron)
20
+ end
22
21
 
23
- should "write the crontab when run" do
24
- @command.expects(:write_crontab).returns(true)
25
- assert @command.run
26
- end
22
+ should "write the crontab when run" do
23
+ @command.expects(:write_crontab).returns(true)
24
+ assert @command.run
27
25
  end
26
+ end
28
27
 
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
28
+ class CommandLineUpdateTest < Whenever::TestCase
29
+ setup do
30
+ Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
31
+ File.expects(:exist?).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
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)
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
40
 
41
- new_cron = <<-EXPECTED
41
+ new_cron = <<-EXPECTED
42
42
  #{existing}
43
43
 
44
- # Begin Whenever generated tasks for: My identifier
44
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
45
45
  #{@task}
46
- # End Whenever generated tasks for: My identifier
46
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
47
47
  EXPECTED
48
48
 
49
- assert_equal new_cron, @command.send(:updated_crontab)
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 and the timestamp doesn't" do
56
+ existing = <<-EXISTING_CRON
57
+ # Something
58
+
59
+ # Begin Whenever generated tasks for: My identifier at: 2017-01-03 08:02:22 +0500
60
+ My whenever job that was already here
61
+ # End Whenever generated tasks for: My identifier at: 2017-01-03 08:22:22 +0500
62
+
63
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
64
+ This shouldn't get replaced
65
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
66
+ EXISTING_CRON
67
+
68
+ new_cron = <<-NEW_CRON
69
+ # Something
70
+
71
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
72
+ #{@task}
73
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
74
+
75
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
76
+ This shouldn't get replaced
77
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
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
+
87
+ should "replace an existing block if the identifier matches and the UTC timestamp doesn't" do
88
+ existing = <<-EXISTING_CRON
89
+ # Something
90
+
91
+ # Begin Whenever generated tasks for: My identifier at: 2017-01-03 08:02:22 UTC
92
+ My whenever job that was already here
93
+ # End Whenever generated tasks for: My identifier at: 2017-01-03 08:22:22 UTC
94
+
95
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
96
+ This shouldn't get replaced
97
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
98
+ EXISTING_CRON
99
+
100
+ new_cron = <<-NEW_CRON
101
+ # Something
102
+
103
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
104
+ #{@task}
105
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
106
+
107
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
108
+ This shouldn't get replaced
109
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
110
+ NEW_CRON
111
+
112
+ @command.expects(:read_crontab).at_least_once.returns(existing)
113
+ assert_equal new_cron, @command.send(:updated_crontab)
50
114
 
51
- @command.expects(:write_crontab).with(new_cron).returns(true)
52
- assert @command.run
53
- end
115
+ @command.expects(:write_crontab).with(new_cron).returns(true)
116
+ assert @command.run
117
+ end
54
118
 
55
- should "replace an existing block if the identifier matches" do
56
- existing = <<-EXISTING_CRON
119
+ should "replace an existing block if the identifier matches and it doesn't contain a timestamp" do
120
+ existing = <<-EXISTING_CRON
57
121
  # Something
58
122
 
59
123
  # Begin Whenever generated tasks for: My identifier
60
124
  My whenever job that was already here
61
125
  # End Whenever generated tasks for: My identifier
62
126
 
63
- # Begin Whenever generated tasks for: Other identifier
127
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
64
128
  This shouldn't get replaced
65
- # End Whenever generated tasks for: Other identifier
129
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
66
130
  EXISTING_CRON
67
131
 
68
- new_cron = <<-NEW_CRON
132
+ new_cron = <<-NEW_CRON
69
133
  # Something
70
134
 
71
- # Begin Whenever generated tasks for: My identifier
135
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
72
136
  #{@task}
73
- # End Whenever generated tasks for: My identifier
137
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
74
138
 
75
- # Begin Whenever generated tasks for: Other identifier
139
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
76
140
  This shouldn't get replaced
77
- # End Whenever generated tasks for: Other identifier
141
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
78
142
  NEW_CRON
79
143
 
80
- @command.expects(:read_crontab).at_least_once.returns(existing)
81
- assert_equal new_cron, @command.send(:updated_crontab)
144
+ @command.expects(:read_crontab).at_least_once.returns(existing)
145
+ assert_equal new_cron, @command.send(:updated_crontab)
82
146
 
83
- @command.expects(:write_crontab).with(new_cron).returns(true)
84
- assert @command.run
85
- end
147
+ @command.expects(:write_crontab).with(new_cron).returns(true)
148
+ assert @command.run
86
149
  end
150
+ end
87
151
 
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
152
+ class CommandLineUpdateWithBackslashesTest < Whenever::TestCase
153
+ setup do
154
+ Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
155
+ @existing = <<-EXISTING_CRON
156
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
92
157
  script/runner -e production 'puts '\\''hello'\\'''
93
- # End Whenever generated tasks for: My identifier
158
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
94
159
  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
160
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
161
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
162
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
163
+ @command.expects(:whenever_cron).returns(@existing)
164
+ end
165
+
166
+ should "replace the existing block with the backslashes in tact" do
167
+ assert_equal @existing, @command.send(:updated_crontab)
168
+ end
169
+ end
170
+
171
+ class CommandLineUpdateToSimilarCrontabTest < Whenever::TestCase
172
+ setup do
173
+ @existing = <<-EXISTING_CRON
174
+ # Begin Whenever generated tasks for: WheneverExisting at: 2017-02-24 16:21:30 +0100
175
+ # End Whenever generated tasks for: WheneverExisting at: 2017-02-24 16:21:30 +0100
111
176
  EXISTING_CRON
112
- @new = <<-NEW_CRON
113
- # Begin Whenever generated tasks for: Whenever
114
- # End Whenever generated tasks for: Whenever
177
+ @new = <<-NEW_CRON
178
+ # Begin Whenever generated tasks for: Whenever at: 2017-02-24 16:21:30 +0100
179
+ # End Whenever generated tasks for: Whenever at: 2017-02-24 16:21:30 +0100
115
180
  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" + @new, @command.send(:updated_crontab)
124
- end
125
- end
126
-
127
- context "A command line clear" 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 "clear an existing block if the identifier matches" do
135
- existing = <<-EXISTING_CRON
181
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
182
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'Whenever')
183
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
184
+ @command.expects(:whenever_cron).returns(@new)
185
+ end
186
+
187
+ should "append the similarly named command" do
188
+ assert_equal @existing + "\n" + @new, @command.send(:updated_crontab)
189
+ end
190
+ end
191
+
192
+ class CommandLineClearTest < Whenever::TestCase
193
+ setup do
194
+ Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
195
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
196
+ @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
197
+ @task = "#{two_hours} /my/command"
198
+ end
199
+
200
+ should "clear an existing block if the identifier matches and the timestamp doesn't" do
201
+ existing = <<-EXISTING_CRON
202
+ # Something
203
+
204
+ # Begin Whenever generated tasks for: My identifier at: 2017-01-03 08:20:02 +0500
205
+ My whenever job that was already here
206
+ # End Whenever generated tasks for: My identifier at: 2017-01-03 08:20:02 +0500
207
+
208
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
209
+ This shouldn't get replaced
210
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
211
+ EXISTING_CRON
212
+
213
+ @command.expects(:read_crontab).at_least_once.returns(existing)
214
+
215
+ new_cron = <<-NEW_CRON
216
+ # Something
217
+
218
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
219
+ This shouldn't get replaced
220
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
221
+ NEW_CRON
222
+
223
+ assert_equal new_cron, @command.send(:updated_crontab)
224
+
225
+ @command.expects(:write_crontab).with(new_cron).returns(true)
226
+ assert @command.run
227
+ end
228
+
229
+ should "clear an existing block if the identifier matches and the UTC timestamp doesn't" do
230
+ existing = <<-EXISTING_CRON
231
+ # Something
232
+
233
+ # Begin Whenever generated tasks for: My identifier at: 2017-01-03 08:20:02 UTC
234
+ My whenever job that was already here
235
+ # End Whenever generated tasks for: My identifier at: 2017-01-03 08:20:02 UTC
236
+
237
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
238
+ This shouldn't get replaced
239
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
240
+ EXISTING_CRON
241
+
242
+ @command.expects(:read_crontab).at_least_once.returns(existing)
243
+
244
+ new_cron = <<-NEW_CRON
245
+ # Something
246
+
247
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
248
+ This shouldn't get replaced
249
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
250
+ NEW_CRON
251
+
252
+ assert_equal new_cron, @command.send(:updated_crontab)
253
+
254
+ @command.expects(:write_crontab).with(new_cron).returns(true)
255
+ assert @command.run
256
+ end
257
+
258
+ should "clear an existing block if the identifier matches and it doesn't have a timestamp" do
259
+ existing = <<-EXISTING_CRON
136
260
  # Something
137
261
 
138
262
  # Begin Whenever generated tasks for: My identifier
139
263
  My whenever job that was already here
140
264
  # End Whenever generated tasks for: My identifier
141
265
 
142
- # Begin Whenever generated tasks for: Other identifier
266
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
143
267
  This shouldn't get replaced
144
- # End Whenever generated tasks for: Other identifier
268
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
145
269
  EXISTING_CRON
146
270
 
147
- @command.expects(:read_crontab).at_least_once.returns(existing)
271
+ @command.expects(:read_crontab).at_least_once.returns(existing)
148
272
 
149
- new_cron = <<-NEW_CRON
273
+ new_cron = <<-NEW_CRON
150
274
  # Something
151
275
 
152
- # Begin Whenever generated tasks for: Other identifier
276
+ # Begin Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
153
277
  This shouldn't get replaced
154
- # End Whenever generated tasks for: Other identifier
278
+ # End Whenever generated tasks for: Other identifier at: 2017-02-24 16:21:30 +0100
155
279
  NEW_CRON
156
280
 
157
- assert_equal new_cron, @command.send(:updated_crontab)
158
-
159
- @command.expects(:write_crontab).with(new_cron).returns(true)
160
- assert @command.run
161
- end
162
- end
163
-
164
- context "A command line clear with no schedule file" do
165
- setup do
166
- File.expects(:exists?).with('config/schedule.rb').returns(false)
167
- @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
168
- end
169
-
170
- should "run successfully" do
171
- @command.expects(:write_crontab).returns(true)
172
- assert @command.run
173
- end
174
- end
175
-
176
- context "A command line update with no identifier" do
177
- setup do
178
- File.expects(:exists?).with('config/schedule.rb').returns(true)
179
- Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
180
- @command = Whenever::CommandLine.new(:update => true, :file => @file)
181
- end
182
-
183
- should "use the default identifier" do
184
- assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
185
- end
186
- end
187
-
188
- context "combined params" do
189
- setup do
190
- Whenever::CommandLine.any_instance.expects(:exit)
191
- Whenever::CommandLine.any_instance.expects(:warn)
192
- File.expects(:exists?).with('config/schedule.rb').returns(true)
193
- end
194
-
195
- should "exit with write and clear" do
196
- @command = Whenever::CommandLine.new(:write => true, :clear => true)
197
- end
198
-
199
- should "exit with write and update" do
200
- @command = Whenever::CommandLine.new(:write => true, :update => true)
201
- end
202
-
203
- should "exit with update and clear" do
204
- @command = Whenever::CommandLine.new(:update => true, :clear => true)
205
- end
206
- end
207
-
208
- context "A runner where the environment is overridden using the :set option" do
209
- setup do
210
- @output = Whenever.cron :set => 'environment=serious', :string => \
211
- <<-file
212
- set :job_template, nil
213
- set :environment, :silly
214
- set :path, '/my/path'
215
- every 2.hours do
216
- runner "blahblah"
217
- end
218
- file
219
- end
220
-
221
- should "output the runner using the override environment" do
222
- assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output
223
- end
224
- end
225
-
226
- context "A runner where the environment and path are overridden using the :set option" do
227
- setup do
228
- @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
229
- <<-file
230
- set :job_template, nil
231
- set :environment, :silly
232
- set :path, '/silly/path'
233
- every 2.hours do
234
- runner "blahblah"
235
- end
236
- file
237
- end
238
-
239
- should "output the runner using the overridden path and environment" do
240
- assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
241
- end
242
- end
243
-
244
- context "A runner where the environment and path are overridden using the :set option with spaces in the string" do
245
- setup do
246
- @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
247
- <<-file
248
- set :job_template, nil
249
- set :environment, :silly
250
- set :path, '/silly/path'
251
- every 2.hours do
252
- runner "blahblah"
253
- end
254
- file
255
- end
256
-
257
- should "output the runner using the overridden path and environment" do
258
- assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
259
- end
260
- end
261
-
262
- context "A runner where the environment is overridden using the :set option but no value is given" do
263
- setup do
264
- @output = Whenever.cron :set => ' environment=', :string => \
265
- <<-file
266
- set :job_template, nil
267
- set :environment, :silly
268
- set :path, '/silly/path'
269
- every 2.hours do
270
- runner "blahblah"
271
- end
272
- file
273
- end
274
-
275
- should "output the runner using the original environmnet" do
276
- assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output
277
- end
278
- end
279
-
280
- context "prepare-ing the output" do
281
- setup do
282
- File.expects(:exists?).with('config/schedule.rb').returns(true)
283
- end
284
-
285
- should "not trim off the top lines of the file" do
286
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
287
- existing = <<-EXISTING_CRON
281
+ assert_equal new_cron, @command.send(:updated_crontab)
282
+
283
+ @command.expects(:write_crontab).with(new_cron).returns(true)
284
+ assert @command.run
285
+ end
286
+ end
287
+
288
+ class CommandLineClearWithNoScheduleTest < Whenever::TestCase
289
+ setup do
290
+ File.expects(:exist?).with('config/schedule.rb').returns(false)
291
+ @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
292
+ end
293
+
294
+ should "run successfully" do
295
+ @command.expects(:write_crontab).returns(true)
296
+ assert @command.run
297
+ end
298
+ end
299
+
300
+ class CommandLineUpdateWithNoIdentifierTest < Whenever::TestCase
301
+ setup do
302
+ Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
303
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
304
+ Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
305
+ @command = Whenever::CommandLine.new(:update => true)
306
+ end
307
+
308
+ should "use the default identifier" do
309
+ assert_equal "Whenever generated tasks for: DEFAULT at: 2017-02-24 16:21:30 +0100", @command.send(:comment_base)
310
+ end
311
+ end
312
+
313
+ class CombinedParamsTest < Whenever::TestCase
314
+ setup do
315
+ Whenever::CommandLine.any_instance.expects(:exit)
316
+ Whenever::CommandLine.any_instance.expects(:warn)
317
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
318
+ end
319
+
320
+ should "exit with write and clear" do
321
+ @command = Whenever::CommandLine.new(:write => true, :clear => true)
322
+ end
323
+
324
+ should "exit with write and update" do
325
+ @command = Whenever::CommandLine.new(:write => true, :update => true)
326
+ end
327
+
328
+ should "exit with update and clear" do
329
+ @command = Whenever::CommandLine.new(:update => true, :clear => true)
330
+ end
331
+ end
332
+
333
+ class RunnerOverwrittenWithSetOptionTest < Whenever::TestCase
334
+ setup do
335
+ @output = Whenever.cron :set => 'environment=serious', :string => \
336
+ <<-file
337
+ set :job_template, nil
338
+ set :environment, :silly
339
+ set :path, '/my/path'
340
+ every 2.hours do
341
+ runner "blahblah"
342
+ end
343
+ file
344
+ end
345
+
346
+ should "output the runner using the override environment" do
347
+ assert_match two_hours + %( cd /my/path && bundle exec script/runner -e serious 'blahblah'), @output
348
+ end
349
+ end
350
+
351
+
352
+ class EnvironmentAndPathOverwrittenWithSetOptionTest < Whenever::TestCase
353
+ setup do
354
+ @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
355
+ <<-file
356
+ set :job_template, nil
357
+ set :environment, :silly
358
+ set :path, '/silly/path'
359
+ every 2.hours do
360
+ runner "blahblah"
361
+ end
362
+ file
363
+ end
364
+
365
+ should "output the runner using the overridden path and environment" do
366
+ assert_match two_hours + %( cd /serious/path && bundle exec script/runner -e serious 'blahblah'), @output
367
+ end
368
+ end
369
+
370
+ class EnvironmentAndPathOverwrittenWithSetOptionWithSpacesTest < Whenever::TestCase
371
+ setup do
372
+ @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
373
+ <<-file
374
+ set :job_template, nil
375
+ set :environment, :silly
376
+ set :path, '/silly/path'
377
+ every 2.hours do
378
+ runner "blahblah"
379
+ end
380
+ file
381
+ end
382
+
383
+ should "output the runner using the overridden path and environment" do
384
+ assert_match two_hours + %( cd /serious/path && bundle exec script/runner -e serious 'blahblah'), @output
385
+ end
386
+ end
387
+
388
+ class EnvironmentOverwrittenWithoutValueTest < Whenever::TestCase
389
+ setup do
390
+ @output = Whenever.cron :set => ' environment=', :string => \
391
+ <<-file
392
+ set :job_template, nil
393
+ set :environment, :silly
394
+ set :path, '/silly/path'
395
+ every 2.hours do
396
+ runner "blahblah"
397
+ end
398
+ file
399
+ end
400
+
401
+ should "output the runner using the original environmnet" do
402
+ assert_match two_hours + %( cd /silly/path && bundle exec script/runner -e silly 'blahblah'), @output
403
+ end
404
+ end
405
+
406
+ class PreparingOutputTest < Whenever::TestCase
407
+ setup do
408
+ File.expects(:exist?).with('config/schedule.rb').returns(true)
409
+ end
410
+
411
+ should "not trim off the top lines of the file" do
412
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
413
+ existing = <<-EXISTING_CRON
288
414
  # Useless Comments
289
415
  # at the top of the file
290
416
 
291
- # Begin Whenever generated tasks for: My identifier
417
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
292
418
  My whenever job that was already here
293
- # End Whenever generated tasks for: My identifier
419
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
294
420
  EXISTING_CRON
295
421
 
296
- assert_equal existing, @command.send(:prepare, existing)
297
- end
422
+ assert_equal existing, @command.send(:prepare, existing)
423
+ end
298
424
 
299
- should "trim off the top lines of the file" do
300
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
301
- existing = <<-EXISTING_CRON
425
+ should "trim off the top lines of the file" do
426
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
427
+ existing = <<-EXISTING_CRON
302
428
  # Useless Comments
303
429
  # at the top of the file
304
430
 
305
- # Begin Whenever generated tasks for: My identifier
431
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
306
432
  My whenever job that was already here
307
- # End Whenever generated tasks for: My identifier
433
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
308
434
  EXISTING_CRON
309
435
 
310
- new_cron = <<-NEW_CRON
311
- # Begin Whenever generated tasks for: My identifier
436
+ new_cron = <<-NEW_CRON
437
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
312
438
  My whenever job that was already here
313
- # End Whenever generated tasks for: My identifier
439
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
314
440
  NEW_CRON
315
441
 
316
- assert_equal new_cron, @command.send(:prepare, existing)
317
- end
442
+ assert_equal new_cron, @command.send(:prepare, existing)
443
+ end
318
444
 
319
- should "preserve terminating newlines in files" do
320
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
321
- existing = <<-EXISTING_CRON
322
- # Begin Whenever generated tasks for: My identifier
445
+ should "preserve terminating newlines in files" do
446
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
447
+ existing = <<-EXISTING_CRON
448
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
323
449
  My whenever job that was already here
324
- # End Whenever generated tasks for: My identifier
450
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
325
451
 
326
452
  # A non-Whenever task
327
453
  My non-whenever job that was already here
328
454
  EXISTING_CRON
329
455
 
330
- assert_equal existing, @command.send(:prepare, existing)
331
- end
456
+ assert_equal existing, @command.send(:prepare, existing)
332
457
  end
333
-
334
458
  end