andrew-whenever 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG.rdoc +93 -0
- data/README.rdoc +105 -0
- data/Rakefile +32 -0
- data/andrew-whenever.gemspec +77 -0
- data/bin/whenever +28 -0
- data/bin/wheneverize +69 -0
- data/lib/whenever.rb +31 -0
- data/lib/whenever/base.rb +15 -0
- data/lib/whenever/command_line.rb +111 -0
- data/lib/whenever/job_list.rb +161 -0
- data/lib/whenever/job_types/default.rb +27 -0
- data/lib/whenever/job_types/rake_task.rb +12 -0
- data/lib/whenever/job_types/runner.rb +12 -0
- data/lib/whenever/outputs/cron.rb +140 -0
- data/lib/whenever/outputs/cron/output_redirection.rb +60 -0
- data/lib/whenever/version.rb +3 -0
- data/test/command_line_test.rb +101 -0
- data/test/cron_test.rb +230 -0
- data/test/output_at_test.rb +178 -0
- data/test/output_command_test.rb +37 -0
- data/test/output_env_test.rb +56 -0
- data/test/output_rake_test.rb +74 -0
- data/test/output_redirection_test.rb +289 -0
- data/test/output_runner_test.rb +209 -0
- data/test/test_helper.rb +33 -0
- metadata +117 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class OutputCommandTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "A plain command" do
|
6
|
+
setup do
|
7
|
+
@output = Whenever.cron \
|
8
|
+
<<-file
|
9
|
+
every 2.hours do
|
10
|
+
command "blahblah"
|
11
|
+
end
|
12
|
+
file
|
13
|
+
end
|
14
|
+
|
15
|
+
should "output the command" do
|
16
|
+
assert_match /^.+ .+ .+ .+ blahblah$/, @output
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "An every statement with two commands in it" do
|
21
|
+
setup do
|
22
|
+
@output = Whenever.cron \
|
23
|
+
<<-file
|
24
|
+
every 1.hour do
|
25
|
+
command "first"
|
26
|
+
command "second"
|
27
|
+
end
|
28
|
+
file
|
29
|
+
end
|
30
|
+
|
31
|
+
should "output both commands" do
|
32
|
+
assert_match "0 * * * * first", @output
|
33
|
+
assert_match "0 * * * * second", @output
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class OutputEnvTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "The output from Whenever with environment variables set" do
|
6
|
+
setup do
|
7
|
+
@output = Whenever.cron \
|
8
|
+
<<-file
|
9
|
+
env :MYVAR, 'blah'
|
10
|
+
env 'MAILTO', "someone@example.com"
|
11
|
+
file
|
12
|
+
end
|
13
|
+
|
14
|
+
should "output MYVAR environment variable" do
|
15
|
+
assert_match "MYVAR=blah", @output
|
16
|
+
end
|
17
|
+
|
18
|
+
should "output MAILTO environment variable" do
|
19
|
+
assert_match "MAILTO=someone@example.com", @output
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "No PATH environment variable set" do
|
24
|
+
setup do
|
25
|
+
Whenever::JobList.any_instance.expects(:read_path).at_least_once.returns('/usr/local/bin')
|
26
|
+
@output = Whenever.cron ""
|
27
|
+
end
|
28
|
+
|
29
|
+
should "add a PATH variable based on the user's PATH" do
|
30
|
+
assert_match "PATH=/usr/local/bin", @output
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "A PATH environment variable set" do
|
35
|
+
setup do
|
36
|
+
Whenever::JobList.stubs(:read_path).returns('/usr/local/bin')
|
37
|
+
@output = Whenever.cron "env :PATH, '/my/path'"
|
38
|
+
end
|
39
|
+
|
40
|
+
should "use that path and the user's PATH" do
|
41
|
+
assert_match "PATH=/my/path", @output
|
42
|
+
assert_no_match /local/, @output
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "No PATH set and instructed not to automatically load the user's path" do
|
47
|
+
setup do
|
48
|
+
@output = Whenever.cron "set :set_path_automatically, false"
|
49
|
+
end
|
50
|
+
|
51
|
+
should "not have a PATH set" do
|
52
|
+
assert_no_match /PATH/, @output
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class OutputRakeTest < Test::Unit::TestCase
|
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
|
7
|
+
|
8
|
+
context "A rake command with path set" do
|
9
|
+
setup do
|
10
|
+
@output = Whenever.cron \
|
11
|
+
<<-file
|
12
|
+
set :path, '/my/path'
|
13
|
+
every 2.hours do
|
14
|
+
rake "blahblah"
|
15
|
+
end
|
16
|
+
file
|
17
|
+
end
|
18
|
+
|
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
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "A rake command that overrides the path set" do
|
25
|
+
setup do
|
26
|
+
@output = Whenever.cron \
|
27
|
+
<<-file
|
28
|
+
set :path, '/my/path'
|
29
|
+
every 2.hours do
|
30
|
+
rake "blahblah", :path => '/some/other/path'
|
31
|
+
end
|
32
|
+
file
|
33
|
+
end
|
34
|
+
|
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
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "A rake command with environment set" do
|
41
|
+
setup do
|
42
|
+
@output = Whenever.cron \
|
43
|
+
<<-file
|
44
|
+
set :environment, :silly
|
45
|
+
set :path, '/my/path'
|
46
|
+
every 2.hours do
|
47
|
+
rake "blahblah"
|
48
|
+
end
|
49
|
+
file
|
50
|
+
end
|
51
|
+
|
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
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "A rake command that overrides the environment set" do
|
58
|
+
setup do
|
59
|
+
@output = Whenever.cron \
|
60
|
+
<<-file
|
61
|
+
set :environment, :silly
|
62
|
+
set :path, '/my/path'
|
63
|
+
every 2.hours do
|
64
|
+
rake "blahblah", :environment => :serious
|
65
|
+
end
|
66
|
+
file
|
67
|
+
end
|
68
|
+
|
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
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class OutputRedirectionTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "A command when the output is set to nil" do
|
6
|
+
setup do
|
7
|
+
@output = Whenever.cron \
|
8
|
+
<<-file
|
9
|
+
set :output, nil
|
10
|
+
every 2.hours do
|
11
|
+
command "blahblah"
|
12
|
+
end
|
13
|
+
file
|
14
|
+
end
|
15
|
+
|
16
|
+
should "output the command with the log syntax appended" do
|
17
|
+
assert_match /^.+ .+ .+ .+ blahblah >> \/dev\/null 2>&1$/, @output
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
context "A command when the output is set" do
|
23
|
+
setup do
|
24
|
+
@output = Whenever.cron \
|
25
|
+
<<-file
|
26
|
+
set :output, 'logfile.log'
|
27
|
+
every 2.hours do
|
28
|
+
command "blahblah"
|
29
|
+
end
|
30
|
+
file
|
31
|
+
end
|
32
|
+
|
33
|
+
should "output the command with the log syntax appended" do
|
34
|
+
assert_match /^.+ .+ .+ .+ blahblah >> logfile.log 2>&1$/, @output
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "A command when the error and standard output is set by the command" do
|
39
|
+
setup do
|
40
|
+
@output = Whenever.cron \
|
41
|
+
<<-file
|
42
|
+
every 2.hours do
|
43
|
+
command "blahblah", :output => {:standard => 'dev_null', :error => 'dev_err'}
|
44
|
+
end
|
45
|
+
file
|
46
|
+
end
|
47
|
+
|
48
|
+
should "output the command without the log syntax appended" do
|
49
|
+
assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "A command when the output is set and the comand overrides it" do
|
54
|
+
setup do
|
55
|
+
@output = Whenever.cron \
|
56
|
+
<<-file
|
57
|
+
set :output, 'logfile.log'
|
58
|
+
every 2.hours do
|
59
|
+
command "blahblah", :output => 'otherlog.log'
|
60
|
+
end
|
61
|
+
file
|
62
|
+
end
|
63
|
+
|
64
|
+
should "output the command with the command syntax appended" do
|
65
|
+
assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
|
66
|
+
assert_match /^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1$/, @output
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "A command when the output is set and the comand overrides with standard and error" do
|
71
|
+
setup do
|
72
|
+
@output = Whenever.cron \
|
73
|
+
<<-file
|
74
|
+
set :output, 'logfile.log'
|
75
|
+
every 2.hours do
|
76
|
+
command "blahblah", :output => {:error => 'dev_err', :standard => 'dev_null' }
|
77
|
+
end
|
78
|
+
file
|
79
|
+
end
|
80
|
+
|
81
|
+
should "output the command with the overridden redirection syntax appended" do
|
82
|
+
assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
|
83
|
+
assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "A command when the output is set and the comand rejects it" do
|
88
|
+
setup do
|
89
|
+
@output = Whenever.cron \
|
90
|
+
<<-file
|
91
|
+
set :output, 'logfile.log'
|
92
|
+
every 2.hours do
|
93
|
+
command "blahblah", :output => false
|
94
|
+
end
|
95
|
+
file
|
96
|
+
end
|
97
|
+
|
98
|
+
should "output the command without the log syntax appended" do
|
99
|
+
assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
|
100
|
+
assert_match /^.+ .+ .+ .+ blahblah$/, @output
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "A command when the output is set and is overridden by the :set option" do
|
105
|
+
setup do
|
106
|
+
@output = Whenever.cron :set => 'output=otherlog.log', :string => \
|
107
|
+
<<-file
|
108
|
+
set :output, 'logfile.log'
|
109
|
+
every 2.hours do
|
110
|
+
command "blahblah"
|
111
|
+
end
|
112
|
+
file
|
113
|
+
end
|
114
|
+
|
115
|
+
should "output the otherlog.log as the log file" do
|
116
|
+
assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output
|
117
|
+
assert_match /^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1/, @output
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "A command when the error and standard output is set" do
|
122
|
+
setup do
|
123
|
+
@output = Whenever.cron \
|
124
|
+
<<-file
|
125
|
+
set :output, {:error => 'dev_err', :standard => 'dev_null' }
|
126
|
+
every 2.hours do
|
127
|
+
command "blahblah"
|
128
|
+
end
|
129
|
+
file
|
130
|
+
end
|
131
|
+
|
132
|
+
should "output the command without the redirection syntax appended" do
|
133
|
+
assert_match /^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, @output
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "A command when error output is set" do
|
138
|
+
setup do
|
139
|
+
@output = Whenever.cron \
|
140
|
+
<<-file
|
141
|
+
set :output, {:error => 'dev_null'}
|
142
|
+
every 2.hours do
|
143
|
+
command "blahblah"
|
144
|
+
end
|
145
|
+
file
|
146
|
+
end
|
147
|
+
|
148
|
+
should "output the command without the standard error syntax appended" do
|
149
|
+
assert_match /^.+ .+ .+ .+ blahblah 2>> dev_null$/, @output
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context "A command when the standard output is set" do
|
154
|
+
setup do
|
155
|
+
@output = Whenever.cron \
|
156
|
+
<<-file
|
157
|
+
set :output, {:standard => 'dev_out'}
|
158
|
+
every 2.hours do
|
159
|
+
command "blahblah"
|
160
|
+
end
|
161
|
+
file
|
162
|
+
end
|
163
|
+
|
164
|
+
should "output the command with standard output syntax appended" do
|
165
|
+
assert_match /^.+ .+ .+ .+ blahblah >> dev_out$/, @output
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context "A command when error output is set by the command" do
|
170
|
+
setup do
|
171
|
+
@output = Whenever.cron \
|
172
|
+
<<-file
|
173
|
+
every 2.hours do
|
174
|
+
command "blahblah", :output => {:error => 'dev_err'}
|
175
|
+
end
|
176
|
+
file
|
177
|
+
end
|
178
|
+
|
179
|
+
should "output the command without the log syntax appended" do
|
180
|
+
assert_match /^.+ .+ .+ .+ blahblah 2>> dev_err$/, @output
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context "A command when standard output is set by the command" do
|
185
|
+
setup do
|
186
|
+
@output = Whenever.cron \
|
187
|
+
<<-file
|
188
|
+
every 2.hours do
|
189
|
+
command "blahblah", :output => {:standard => 'dev_out'}
|
190
|
+
end
|
191
|
+
file
|
192
|
+
end
|
193
|
+
|
194
|
+
should "output the command without the log syntax appended" do
|
195
|
+
assert_match /^.+ .+ .+ .+ blahblah >> dev_out$/, @output
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context "A command when standard output is set to nil" do
|
200
|
+
setup do
|
201
|
+
@output = Whenever.cron \
|
202
|
+
<<-file
|
203
|
+
every 2.hours do
|
204
|
+
command "blahblah", :output => {:standard => nil}
|
205
|
+
end
|
206
|
+
file
|
207
|
+
end
|
208
|
+
|
209
|
+
should "output the command with stdout directed to /dev/null" do
|
210
|
+
assert_match /^.+ .+ .+ .+ blahblah > \/dev\/null$/, @output
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context "A command when standard error is set to nil" do
|
215
|
+
setup do
|
216
|
+
@output = Whenever.cron \
|
217
|
+
<<-file
|
218
|
+
every 2.hours do
|
219
|
+
command "blahblah", :output => {:error => nil}
|
220
|
+
end
|
221
|
+
file
|
222
|
+
end
|
223
|
+
|
224
|
+
should "output the command with stderr directed to /dev/null" do
|
225
|
+
assert_match /^.+ .+ .+ .+ blahblah 2> \/dev\/null$/, @output
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context "A command when standard output and standard error is set to nil" do
|
230
|
+
setup do
|
231
|
+
@output = Whenever.cron \
|
232
|
+
<<-file
|
233
|
+
every 2.hours do
|
234
|
+
command "blahblah", :output => {:error => nil, :standard => nil}
|
235
|
+
end
|
236
|
+
file
|
237
|
+
end
|
238
|
+
|
239
|
+
should "output the command with stderr directed to /dev/null" do
|
240
|
+
assert_match /^.+ .+ .+ .+ blahblah > \/dev\/null 2>&1$/, @output
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
context "A command when standard output is set and standard error is set to nil" do
|
245
|
+
setup do
|
246
|
+
@output = Whenever.cron \
|
247
|
+
<<-file
|
248
|
+
every 2.hours do
|
249
|
+
command "blahblah", :output => {:error => nil, :standard => 'my.log'}
|
250
|
+
end
|
251
|
+
file
|
252
|
+
end
|
253
|
+
|
254
|
+
should "output the command with stderr directed to /dev/null" do
|
255
|
+
assert_match /^.+ .+ .+ .+ blahblah >> my.log 2> \/dev\/null$/, @output
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
context "A command when standard output is nil and standard error is set" do
|
260
|
+
setup do
|
261
|
+
@output = Whenever.cron \
|
262
|
+
<<-file
|
263
|
+
every 2.hours do
|
264
|
+
command "blahblah", :output => {:error => 'my_error.log', :standard => nil}
|
265
|
+
end
|
266
|
+
file
|
267
|
+
end
|
268
|
+
|
269
|
+
should "output the command with stderr directed to /dev/null" do
|
270
|
+
assert_match /^.+ .+ .+ .+ blahblah >> \/dev\/null 2>> my_error.log$/, @output
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context "A command when the deprecated :cron_log is set" do
|
275
|
+
setup do
|
276
|
+
@output = Whenever.cron \
|
277
|
+
<<-file
|
278
|
+
set :cron_log, "cron.log"
|
279
|
+
every 2.hours do
|
280
|
+
command "blahblah"
|
281
|
+
end
|
282
|
+
file
|
283
|
+
end
|
284
|
+
|
285
|
+
should "output the command with with the stdout and stderr going to the log" do
|
286
|
+
assert_match /^.+ .+ .+ .+ blahblah >> cron.log 2>&1$/, @output
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|