File: output_default_defined_jobs_test.rb

package info (click to toggle)
ruby-whenever 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,722; makefile: 2
file content (296 lines) | stat: -rw-r--r-- 7,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
require 'test_helper'

class OutputDefaultDefinedJobsTest < Whenever::TestCase

  # command

  test "A plain command with the job template set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah$/, output)
  end

  test "A plain command with no job template set" do
    output = Whenever.cron \
    <<-file
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ \/bin\/bash -l -c 'blahblah'$/, output)
  end

  test "A plain command with a job_template using a normal parameter" do
    output = Whenever.cron \
    <<-file
      set :job_template, "/bin/bash -l -c 'cd :path && :job'"
      every 2.hours do
        set :path, "/tmp"
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ \/bin\/bash -l -c 'cd \/tmp \&\& blahblah'$/, output)
  end

  test "A plain command that overrides the job_template set" do
    output = Whenever.cron \
    <<-file
      set :job_template, "/bin/bash -l -c ':job'"
      every 2.hours do
        command "blahblah", :job_template => "/bin/sh -l -c ':job'"
      end
    file


    assert_match(/^.+ .+ .+ .+ \/bin\/sh -l -c 'blahblah'$/, output)
    assert_no_match(/bash/, output)
  end

  test "A plain command that overrides the job_template set using a parameter" do
    output = Whenever.cron \
    <<-file
      set :job_template, "/bin/bash -l -c 'cd :path && :job'"
      every 2.hours do
        set :path, "/tmp"
        command "blahblah", :job_template => "/bin/sh -l -c 'cd :path && :job'"
      end
    file


    assert_match(/^.+ .+ .+ .+ \/bin\/sh -l -c 'cd \/tmp && blahblah'$/, output)
    assert_no_match(/bash/, output)
  end

  test "A plain command that is conditional on default environent and path" do
    Whenever.expects(:path).at_least_once.returns('/what/you/want')
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      if environment == 'production' && path == '/what/you/want'
        every 2.hours do
          command "blahblah"
        end
      end
    file

    assert_match(/blahblah/, output)
  end

  # runner

  test "A runner with path set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        runner 'blahblah'
      end
    file

    assert_match two_hours + %( cd /my/path && bundle exec script/runner -e production 'blahblah'), output
  end

  test "A runner that overrides the path set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        runner "blahblah", :path => '/some/other/path'
      end
    file

    assert_match two_hours + %( cd /some/other/path && bundle exec script/runner -e production 'blahblah'), output
  end

  test "A runner for a non-bundler app" do
    Whenever.expects(:bundler?).returns(false)
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        runner 'blahblah'
      end
    file

    assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), output
  end

  test "A runner for an app with bin/rails" do
    Whenever.expects(:path).at_least_once.returns('/my/path')
    Whenever.expects(:bin_rails?).returns(true)
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        runner 'blahblah'
      end
    file

    assert_match two_hours + %( cd /my/path && bin/rails runner -e production 'blahblah'), output
  end

  test "A runner for an app with script/rails" do
    Whenever.expects(:path).at_least_once.returns('/my/path')
    Whenever.expects(:script_rails?).returns(true)
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        runner 'blahblah'
      end
    file

    assert_match two_hours + %( cd /my/path && script/rails runner -e production 'blahblah'), output
  end

  # rake

  test "A rake command with path set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        rake "blahblah"
      end
    file

    assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec rake blahblah --silent', output
  end

  test "A rake command with arguments" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        rake "blahblah[foobar]"
      end
    file

    assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec rake blahblah[foobar] --silent', output
  end

  test "A rake for a non-bundler app" do
    Whenever.expects(:path).at_least_once.returns('/my/path')
    Whenever.expects(:bundler?).returns(false)
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        rake 'blahblah'
      end
    file

    assert_match two_hours + ' cd /my/path && RAILS_ENV=production rake blahblah --silent', output
  end

  test "A rake command that overrides the path set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        rake "blahblah", :path => '/some/other/path'
      end
    file

    assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production bundle exec rake blahblah --silent', output
  end

  test "A rake command that sets the environment variable" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      set :environment_variable, 'RAKE_ENV'
      every 2.hours do
        rake "blahblah"
      end
    file

    assert_match two_hours + ' cd /my/path && RAKE_ENV=production bundle exec rake blahblah --silent', output
  end

  test "A rake command that overrides the environment variable" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      set :environment_variable, 'RAKE_ENV'
      every 2.hours do
        rake "blahblah", :environment_variable => 'SOME_ENV'
      end
    file

    assert_match two_hours + ' cd /my/path && SOME_ENV=production bundle exec rake blahblah --silent', output
  end

    # script

  test "A script command with path set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :path, '/my/path'
      every 2.hours do
        script "blahblah"
      end
    file

    assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec script/blahblah', output
  end

  test "A script command for a non-bundler app" do
    Whenever.expects(:path).at_least_once.returns('/my/path')
    Whenever.expects(:bundler?).returns(false)
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        script 'blahblah'
      end
    file

    assert_match two_hours + ' cd /my/path && RAILS_ENV=production script/blahblah', output
  end

  test "A script command that uses output" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, '/log/file'
      set :path, '/my/path'
      every 2.hours do
        script "blahblah", :path => '/some/other/path'
      end
    file

    assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production bundle exec script/blahblah >> /log/file 2>&1', output
  end

  test "A script command that uses an environment variable" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :environment_variable, 'RAKE_ENV'
      set :path, '/my/path'
      every 2.hours do
        script "blahblah"
      end
    file

    assert_match two_hours + ' cd /my/path && RAKE_ENV=production bundle exec script/blahblah', output
  end
end