whenever-benlangfeld 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +18 -0
  4. data/CHANGELOG.md +333 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +260 -0
  8. data/Rakefile +10 -0
  9. data/bin/whenever +41 -0
  10. data/bin/wheneverize +68 -0
  11. data/gemfiles/activesupport4.1.gemfile +5 -0
  12. data/gemfiles/activesupport4.2.gemfile +5 -0
  13. data/lib/whenever.rb +34 -0
  14. data/lib/whenever/capistrano.rb +7 -0
  15. data/lib/whenever/capistrano/v2/hooks.rb +8 -0
  16. data/lib/whenever/capistrano/v2/recipes.rb +48 -0
  17. data/lib/whenever/capistrano/v2/support.rb +53 -0
  18. data/lib/whenever/capistrano/v3/tasks/whenever.rake +45 -0
  19. data/lib/whenever/command_line.rb +135 -0
  20. data/lib/whenever/cron.rb +153 -0
  21. data/lib/whenever/job.rb +54 -0
  22. data/lib/whenever/job_list.rb +155 -0
  23. data/lib/whenever/numeric.rb +13 -0
  24. data/lib/whenever/numeric_seconds.rb +48 -0
  25. data/lib/whenever/os.rb +7 -0
  26. data/lib/whenever/output_redirection.rb +57 -0
  27. data/lib/whenever/setup.rb +26 -0
  28. data/lib/whenever/tasks/whenever.rake +1 -0
  29. data/lib/whenever/version.rb +3 -0
  30. data/test/functional/command_line_test.rb +331 -0
  31. data/test/functional/output_at_test.rb +207 -0
  32. data/test/functional/output_default_defined_jobs_test.rb +296 -0
  33. data/test/functional/output_defined_job_test.rb +85 -0
  34. data/test/functional/output_env_test.rb +29 -0
  35. data/test/functional/output_jobs_for_roles_test.rb +65 -0
  36. data/test/functional/output_redirection_test.rb +248 -0
  37. data/test/test_case.rb +32 -0
  38. data/test/test_helper.rb +37 -0
  39. data/test/unit/capistrano_support_test.rb +147 -0
  40. data/test/unit/cron_test.rb +244 -0
  41. data/test/unit/job_test.rb +114 -0
  42. data/whenever.gemspec +27 -0
  43. metadata +167 -0
@@ -0,0 +1,296 @@
1
+ require 'test_helper'
2
+
3
+ class OutputDefaultDefinedJobsTest < Whenever::TestCase
4
+
5
+ # command
6
+
7
+ test "A plain command with the job template set to nil" do
8
+ output = Whenever.cron \
9
+ <<-file
10
+ set :job_template, nil
11
+ every 2.hours do
12
+ command "blahblah"
13
+ end
14
+ file
15
+
16
+ assert_match(/^.+ .+ .+ .+ blahblah$/, output)
17
+ end
18
+
19
+ test "A plain command with no job template set" do
20
+ output = Whenever.cron \
21
+ <<-file
22
+ every 2.hours do
23
+ command "blahblah"
24
+ end
25
+ file
26
+
27
+ assert_match(/^.+ .+ .+ .+ \/bin\/bash -l -c 'blahblah'$/, output)
28
+ end
29
+
30
+ test "A plain command with a job_template using a normal parameter" do
31
+ output = Whenever.cron \
32
+ <<-file
33
+ set :job_template, "/bin/bash -l -c 'cd :path && :job'"
34
+ every 2.hours do
35
+ set :path, "/tmp"
36
+ command "blahblah"
37
+ end
38
+ file
39
+
40
+ assert_match(/^.+ .+ .+ .+ \/bin\/bash -l -c 'cd \/tmp \&\& blahblah'$/, output)
41
+ end
42
+
43
+ test "A plain command that overrides the job_template set" do
44
+ output = Whenever.cron \
45
+ <<-file
46
+ set :job_template, "/bin/bash -l -c ':job'"
47
+ every 2.hours do
48
+ command "blahblah", :job_template => "/bin/sh -l -c ':job'"
49
+ end
50
+ file
51
+
52
+
53
+ assert_match(/^.+ .+ .+ .+ \/bin\/sh -l -c 'blahblah'$/, output)
54
+ assert_no_match(/bash/, output)
55
+ end
56
+
57
+ test "A plain command that overrides the job_template set using a parameter" do
58
+ output = Whenever.cron \
59
+ <<-file
60
+ set :job_template, "/bin/bash -l -c 'cd :path && :job'"
61
+ every 2.hours do
62
+ set :path, "/tmp"
63
+ command "blahblah", :job_template => "/bin/sh -l -c 'cd :path && :job'"
64
+ end
65
+ file
66
+
67
+
68
+ assert_match(/^.+ .+ .+ .+ \/bin\/sh -l -c 'cd \/tmp && blahblah'$/, output)
69
+ assert_no_match(/bash/, output)
70
+ end
71
+
72
+ test "A plain command that is conditional on default environent and path" do
73
+ Whenever.expects(:path).at_least_once.returns('/what/you/want')
74
+ output = Whenever.cron \
75
+ <<-file
76
+ set :job_template, nil
77
+ if environment == 'production' && path == '/what/you/want'
78
+ every 2.hours do
79
+ command "blahblah"
80
+ end
81
+ end
82
+ file
83
+
84
+ assert_match(/blahblah/, output)
85
+ end
86
+
87
+ # runner
88
+
89
+ test "A runner with path set" do
90
+ output = Whenever.cron \
91
+ <<-file
92
+ set :job_template, nil
93
+ set :path, '/my/path'
94
+ every 2.hours do
95
+ runner 'blahblah'
96
+ end
97
+ file
98
+
99
+ assert_match two_hours + %( cd /my/path && bundle exec script/runner -e production 'blahblah'), output
100
+ end
101
+
102
+ test "A runner that overrides the path set" do
103
+ output = Whenever.cron \
104
+ <<-file
105
+ set :job_template, nil
106
+ set :path, '/my/path'
107
+ every 2.hours do
108
+ runner "blahblah", :path => '/some/other/path'
109
+ end
110
+ file
111
+
112
+ assert_match two_hours + %( cd /some/other/path && bundle exec script/runner -e production 'blahblah'), output
113
+ end
114
+
115
+ test "A runner for a non-bundler app" do
116
+ Whenever.expects(:bundler?).returns(false)
117
+ output = Whenever.cron \
118
+ <<-file
119
+ set :job_template, nil
120
+ set :path, '/my/path'
121
+ every 2.hours do
122
+ runner 'blahblah'
123
+ end
124
+ file
125
+
126
+ assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), output
127
+ end
128
+
129
+ test "A runner for an app with bin/rails" do
130
+ Whenever.expects(:path).at_least_once.returns('/my/path')
131
+ Whenever.expects(:bin_rails?).returns(true)
132
+ output = Whenever.cron \
133
+ <<-file
134
+ set :job_template, nil
135
+ every 2.hours do
136
+ runner 'blahblah'
137
+ end
138
+ file
139
+
140
+ assert_match two_hours + %( cd /my/path && bin/rails runner -e production 'blahblah'), output
141
+ end
142
+
143
+ test "A runner for an app with script/rails" do
144
+ Whenever.expects(:path).at_least_once.returns('/my/path')
145
+ Whenever.expects(:script_rails?).returns(true)
146
+ output = Whenever.cron \
147
+ <<-file
148
+ set :job_template, nil
149
+ every 2.hours do
150
+ runner 'blahblah'
151
+ end
152
+ file
153
+
154
+ assert_match two_hours + %( cd /my/path && script/rails runner -e production 'blahblah'), output
155
+ end
156
+
157
+ # rake
158
+
159
+ test "A rake command with path set" do
160
+ output = Whenever.cron \
161
+ <<-file
162
+ set :job_template, nil
163
+ set :path, '/my/path'
164
+ every 2.hours do
165
+ rake "blahblah"
166
+ end
167
+ file
168
+
169
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec rake blahblah --silent', output
170
+ end
171
+
172
+ test "A rake command with arguments" do
173
+ output = Whenever.cron \
174
+ <<-file
175
+ set :job_template, nil
176
+ set :path, '/my/path'
177
+ every 2.hours do
178
+ rake "blahblah[foobar]"
179
+ end
180
+ file
181
+
182
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec rake blahblah[foobar] --silent', output
183
+ end
184
+
185
+ test "A rake for a non-bundler app" do
186
+ Whenever.expects(:path).at_least_once.returns('/my/path')
187
+ Whenever.expects(:bundler?).returns(false)
188
+ output = Whenever.cron \
189
+ <<-file
190
+ set :job_template, nil
191
+ every 2.hours do
192
+ rake 'blahblah'
193
+ end
194
+ file
195
+
196
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production rake blahblah --silent', output
197
+ end
198
+
199
+ test "A rake command that overrides the path set" do
200
+ output = Whenever.cron \
201
+ <<-file
202
+ set :job_template, nil
203
+ set :path, '/my/path'
204
+ every 2.hours do
205
+ rake "blahblah", :path => '/some/other/path'
206
+ end
207
+ file
208
+
209
+ assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production bundle exec rake blahblah --silent', output
210
+ end
211
+
212
+ test "A rake command that sets the environment variable" do
213
+ output = Whenever.cron \
214
+ <<-file
215
+ set :job_template, nil
216
+ set :path, '/my/path'
217
+ set :environment_variable, 'RAKE_ENV'
218
+ every 2.hours do
219
+ rake "blahblah"
220
+ end
221
+ file
222
+
223
+ assert_match two_hours + ' cd /my/path && RAKE_ENV=production bundle exec rake blahblah --silent', output
224
+ end
225
+
226
+ test "A rake command that overrides the environment variable" do
227
+ output = Whenever.cron \
228
+ <<-file
229
+ set :job_template, nil
230
+ set :path, '/my/path'
231
+ set :environment_variable, 'RAKE_ENV'
232
+ every 2.hours do
233
+ rake "blahblah", :environment_variable => 'SOME_ENV'
234
+ end
235
+ file
236
+
237
+ assert_match two_hours + ' cd /my/path && SOME_ENV=production bundle exec rake blahblah --silent', output
238
+ end
239
+
240
+ # script
241
+
242
+ test "A script command with path set" do
243
+ output = Whenever.cron \
244
+ <<-file
245
+ set :job_template, nil
246
+ set :path, '/my/path'
247
+ every 2.hours do
248
+ script "blahblah"
249
+ end
250
+ file
251
+
252
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec script/blahblah', output
253
+ end
254
+
255
+ test "A script command for a non-bundler app" do
256
+ Whenever.expects(:path).at_least_once.returns('/my/path')
257
+ Whenever.expects(:bundler?).returns(false)
258
+ output = Whenever.cron \
259
+ <<-file
260
+ set :job_template, nil
261
+ every 2.hours do
262
+ script 'blahblah'
263
+ end
264
+ file
265
+
266
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production script/blahblah', output
267
+ end
268
+
269
+ test "A script command that uses output" do
270
+ output = Whenever.cron \
271
+ <<-file
272
+ set :job_template, nil
273
+ set :output, '/log/file'
274
+ set :path, '/my/path'
275
+ every 2.hours do
276
+ script "blahblah", :path => '/some/other/path'
277
+ end
278
+ file
279
+
280
+ assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production bundle exec script/blahblah >> /log/file 2>&1', output
281
+ end
282
+
283
+ test "A script command that uses an environment variable" do
284
+ output = Whenever.cron \
285
+ <<-file
286
+ set :job_template, nil
287
+ set :environment_variable, 'RAKE_ENV'
288
+ set :path, '/my/path'
289
+ every 2.hours do
290
+ script "blahblah"
291
+ end
292
+ file
293
+
294
+ assert_match two_hours + ' cd /my/path && RAKE_ENV=production bundle exec script/blahblah', output
295
+ end
296
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class OutputDefinedJobTest < Whenever::TestCase
4
+ test "defined job with a :task" do
5
+ output = Whenever.cron \
6
+ <<-file
7
+ set :job_template, nil
8
+ job_type :some_job, "before :task after"
9
+ every 2.hours do
10
+ some_job "during"
11
+ end
12
+ file
13
+
14
+ assert_match(/^.+ .+ .+ .+ before during after$/, output)
15
+ end
16
+
17
+ test "defined job with a :task and some options" do
18
+ output = Whenever.cron \
19
+ <<-file
20
+ set :job_template, nil
21
+ job_type :some_job, "before :task after :option1 :option2"
22
+ every 2.hours do
23
+ some_job "during", :option1 => 'happy', :option2 => 'birthday'
24
+ end
25
+ file
26
+
27
+ assert_match(/^.+ .+ .+ .+ before during after happy birthday$/, output)
28
+ end
29
+
30
+ test "defined job with a :task and an option where the option is set globally" do
31
+ output = Whenever.cron \
32
+ <<-file
33
+ set :job_template, nil
34
+ job_type :some_job, "before :task after :option1"
35
+ set :option1, 'happy'
36
+ every 2.hours do
37
+ some_job "during"
38
+ end
39
+ file
40
+
41
+ assert_match(/^.+ .+ .+ .+ before during after happy$/, output)
42
+ end
43
+
44
+ test "defined job with a :task and an option where the option is set globally and locally" do
45
+ output = Whenever.cron \
46
+ <<-file
47
+ set :job_template, nil
48
+ job_type :some_job, "before :task after :option1"
49
+ set :option1, 'global'
50
+ every 2.hours do
51
+ some_job "during", :option1 => 'local'
52
+ end
53
+ file
54
+
55
+ assert_match(/^.+ .+ .+ .+ before during after local$/, output)
56
+ end
57
+
58
+ test "defined job with a :task and an option that is not set" do
59
+ output = Whenever.cron \
60
+ <<-file
61
+ set :job_template, nil
62
+ job_type :some_job, "before :task after :option1"
63
+ every 2.hours do
64
+ some_job "during", :option2 => 'happy'
65
+ end
66
+ file
67
+
68
+ assert_match(/^.+ .+ .+ .+ before during after :option1$/, output)
69
+ end
70
+
71
+ test "defined job that uses a :path where none is explicitly set" do
72
+ Whenever.stubs(:path).returns('/my/path')
73
+
74
+ output = Whenever.cron \
75
+ <<-file
76
+ set :job_template, nil
77
+ job_type :some_job, "cd :path && :task"
78
+ every 2.hours do
79
+ some_job 'blahblah'
80
+ end
81
+ file
82
+
83
+ assert_match two_hours + %( cd /my/path && blahblah), output
84
+ end
85
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class OutputEnvTest < Whenever::TestCase
4
+ setup do
5
+ @output = Whenever.cron \
6
+ <<-file
7
+ env :MYVAR, 'blah'
8
+ env 'MAILTO', "someone@example.com"
9
+ env :BLANKVAR, ''
10
+ env :NILVAR, nil
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
+
22
+ should "output BLANKVAR environment variable" do
23
+ assert_match "BLANKVAR=\"\"", @output
24
+ end
25
+
26
+ should "output NILVAR environment variable" do
27
+ assert_match "NILVAR=\"\"", @output
28
+ end
29
+ end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class OutputJobsForRolesTest < Whenever::TestCase
4
+ test "one role requested and specified on the job" do
5
+ output = Whenever.cron :roles => [:role1], :string => \
6
+ <<-file
7
+ every 2.hours, :roles => [:role1] do
8
+ command "blahblah"
9
+ end
10
+ file
11
+
12
+ assert_equal two_hours + " /bin/bash -l -c 'blahblah'\n\n", output
13
+ end
14
+
15
+ test "one role requested but none specified on the job" do
16
+ output = Whenever.cron :roles => [:role1], :string => \
17
+ <<-file
18
+ every 2.hours do
19
+ command "blahblah"
20
+ end
21
+ file
22
+
23
+ # this should output the job because not specifying a role means "all roles"
24
+ assert_equal two_hours + " /bin/bash -l -c 'blahblah'\n\n", output
25
+ end
26
+
27
+ test "no roles requested but one specified on the job" do
28
+ output = Whenever.cron \
29
+ <<-file
30
+ every 2.hours, :roles => [:role1] do
31
+ command "blahblah"
32
+ end
33
+ file
34
+
35
+ # this should output the job because not requesting roles means "all roles"
36
+ assert_equal two_hours + " /bin/bash -l -c 'blahblah'\n\n", output
37
+ end
38
+
39
+ test "a different role requested than the one specified on the job" do
40
+ output = Whenever.cron :roles => [:role1], :string => \
41
+ <<-file
42
+ every 2.hours, :roles => [:role2] do
43
+ command "blahblah"
44
+ end
45
+ file
46
+
47
+ assert_equal "", output
48
+ end
49
+
50
+ test "with 2 roles requested and a job defined for each" do
51
+ output = Whenever.cron :roles => [:role1, :role2], :string => \
52
+ <<-file
53
+ every 2.hours, :roles => [:role1] do
54
+ command "role1_cmd"
55
+ end
56
+
57
+ every :hour, :roles => [:role2] do
58
+ command "role2_cmd"
59
+ end
60
+ file
61
+
62
+ assert_match two_hours + " /bin/bash -l -c 'role1_cmd'", output
63
+ assert_match "0 * * * * /bin/bash -l -c 'role2_cmd'", output
64
+ end
65
+ end