whenever 0.8.2 → 1.0.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 (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.travis.yml +20 -7
  4. data/Appraisals +19 -0
  5. data/CHANGELOG.md +116 -3
  6. data/Gemfile +3 -3
  7. data/LICENSE +2 -2
  8. data/README.md +133 -32
  9. data/Rakefile +3 -10
  10. data/bin/whenever +3 -0
  11. data/bin/wheneverize +8 -5
  12. data/gemfiles/activesupport4.1.gemfile +7 -0
  13. data/gemfiles/activesupport4.2.gemfile +7 -0
  14. data/gemfiles/activesupport5.0.gemfile +7 -0
  15. data/gemfiles/activesupport5.1.gemfile +7 -0
  16. data/gemfiles/activesupport5.2.gemfile +7 -0
  17. data/lib/whenever/capistrano/v2/hooks.rb +8 -0
  18. data/lib/whenever/capistrano/{recipes.rb → v2/recipes.rb} +7 -13
  19. data/lib/whenever/capistrano/{support.rb → v2/support.rb} +12 -0
  20. data/lib/whenever/capistrano/v3/tasks/whenever.rake +56 -0
  21. data/lib/whenever/capistrano.rb +5 -6
  22. data/lib/whenever/command_line.rb +69 -48
  23. data/lib/whenever/cron.rb +54 -25
  24. data/lib/whenever/job.rb +13 -14
  25. data/lib/whenever/job_list.rb +54 -24
  26. data/lib/whenever/numeric.rb +13 -0
  27. data/lib/whenever/numeric_seconds.rb +48 -0
  28. data/lib/whenever/os.rb +7 -0
  29. data/lib/whenever/output_redirection.rb +1 -0
  30. data/lib/whenever/setup.rb +19 -15
  31. data/lib/whenever/version.rb +2 -2
  32. data/lib/whenever.rb +19 -14
  33. data/test/functional/command_line_test.rb +379 -243
  34. data/test/functional/output_at_test.rb +227 -249
  35. data/test/functional/output_default_defined_jobs_test.rb +251 -193
  36. data/test/functional/output_defined_job_test.rb +65 -91
  37. data/test/functional/output_env_test.rb +22 -26
  38. data/test/functional/output_jobs_for_roles_test.rb +46 -65
  39. data/test/functional/output_jobs_with_mailto_test.rb +168 -0
  40. data/test/functional/output_redirection_test.rb +232 -291
  41. data/test/test_case.rb +32 -0
  42. data/test/test_helper.rb +44 -15
  43. data/test/unit/capistrano_support_test.rb +128 -134
  44. data/test/unit/cron_test.rb +373 -208
  45. data/test/unit/executable_test.rb +142 -0
  46. data/test/unit/job_test.rb +111 -117
  47. data/whenever.gemspec +7 -4
  48. metadata +63 -44
@@ -1,322 +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 update with no identifier" do
165
- setup do
166
- File.expects(:exists?).with('config/schedule.rb').returns(true)
167
- Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
168
- @command = Whenever::CommandLine.new(:update => true, :file => @file)
169
- end
170
-
171
- should "use the default identifier" do
172
- assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
173
- end
174
- end
175
-
176
- context "combined params" do
177
- setup do
178
- Whenever::CommandLine.any_instance.expects(:exit)
179
- Whenever::CommandLine.any_instance.expects(:warn)
180
- File.expects(:exists?).with('config/schedule.rb').returns(true)
181
- end
182
-
183
- should "exit with write and clear" do
184
- @command = Whenever::CommandLine.new(:write => true, :clear => true)
185
- end
186
-
187
- should "exit with write and update" do
188
- @command = Whenever::CommandLine.new(:write => true, :update => true)
189
- end
190
-
191
- should "exit with update and clear" do
192
- @command = Whenever::CommandLine.new(:update => true, :clear => true)
193
- end
194
- end
195
-
196
- context "A runner where the environment is overridden using the :set option" do
197
- setup do
198
- @output = Whenever.cron :set => 'environment=serious', :string => \
199
- <<-file
200
- set :job_template, nil
201
- set :environment, :silly
202
- set :path, '/my/path'
203
- every 2.hours do
204
- runner "blahblah"
205
- end
206
- file
207
- end
208
-
209
- should "output the runner using the override environment" do
210
- assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output
211
- end
212
- end
213
-
214
- context "A runner where the environment and path are overridden using the :set option" do
215
- setup do
216
- @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
217
- <<-file
218
- set :job_template, nil
219
- set :environment, :silly
220
- set :path, '/silly/path'
221
- every 2.hours do
222
- runner "blahblah"
223
- end
224
- file
225
- end
226
-
227
- should "output the runner using the overridden path and environment" do
228
- assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
229
- end
230
- end
231
-
232
- context "A runner where the environment and path are overridden using the :set option with spaces in the string" do
233
- setup do
234
- @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
235
- <<-file
236
- set :job_template, nil
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 is overridden using the :set option but no value is given" do
251
- setup do
252
- @output = Whenever.cron :set => ' environment=', :string => \
253
- <<-file
254
- set :job_template, nil
255
- set :environment, :silly
256
- set :path, '/silly/path'
257
- every 2.hours do
258
- runner "blahblah"
259
- end
260
- file
261
- end
262
-
263
- should "output the runner using the original environmnet" do
264
- assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output
265
- end
266
- end
267
-
268
- context "prepare-ing the output" do
269
- setup do
270
- File.expects(:exists?).with('config/schedule.rb').returns(true)
271
- end
272
-
273
- should "not trim off the top lines of the file" do
274
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
275
- 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
276
414
  # Useless Comments
277
415
  # at the top of the file
278
416
 
279
- # Begin Whenever generated tasks for: My identifier
417
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
280
418
  My whenever job that was already here
281
- # End Whenever generated tasks for: My identifier
419
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
282
420
  EXISTING_CRON
283
421
 
284
- assert_equal existing, @command.send(:prepare, existing)
285
- end
422
+ assert_equal existing, @command.send(:prepare, existing)
423
+ end
286
424
 
287
- should "trim off the top lines of the file" do
288
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
289
- 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
290
428
  # Useless Comments
291
429
  # at the top of the file
292
430
 
293
- # Begin Whenever generated tasks for: My identifier
431
+ # Begin Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
294
432
  My whenever job that was already here
295
- # End Whenever generated tasks for: My identifier
433
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
296
434
  EXISTING_CRON
297
435
 
298
- new_cron = <<-NEW_CRON
299
- # 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
300
438
  My whenever job that was already here
301
- # End Whenever generated tasks for: My identifier
439
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
302
440
  NEW_CRON
303
441
 
304
- assert_equal new_cron, @command.send(:prepare, existing)
305
- end
442
+ assert_equal new_cron, @command.send(:prepare, existing)
443
+ end
306
444
 
307
- should "preserve terminating newlines in files" do
308
- @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
309
- existing = <<-EXISTING_CRON
310
- # 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
311
449
  My whenever job that was already here
312
- # End Whenever generated tasks for: My identifier
450
+ # End Whenever generated tasks for: My identifier at: 2017-02-24 16:21:30 +0100
313
451
 
314
452
  # A non-Whenever task
315
453
  My non-whenever job that was already here
316
454
  EXISTING_CRON
317
455
 
318
- assert_equal existing, @command.send(:prepare, existing)
319
- end
456
+ assert_equal existing, @command.send(:prepare, existing)
320
457
  end
321
-
322
458
  end