whenever 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +0 -5
- data/README.md +3 -1
- data/lib/whenever.rb +10 -11
- data/lib/whenever/capistrano/v3/tasks/whenever.rake +2 -2
- data/lib/whenever/command_line.rb +9 -9
- data/lib/whenever/cron.rb +11 -11
- data/lib/whenever/job.rb +2 -2
- data/lib/whenever/job_list.rb +18 -7
- data/lib/whenever/numeric_seconds.rb +54 -0
- data/lib/whenever/tasks/whenever.rake +2 -2
- data/lib/whenever/version.rb +1 -1
- data/test/functional/command_line_test.rb +221 -224
- data/test/functional/output_at_test.rb +188 -249
- data/test/functional/output_default_defined_jobs_test.rb +214 -290
- data/test/functional/output_defined_job_test.rb +65 -91
- data/test/functional/output_env_test.rb +22 -26
- data/test/functional/output_jobs_for_roles_test.rb +46 -66
- data/test/functional/output_redirection_test.rb +231 -309
- data/test/test_case.rb +39 -0
- data/test/test_helper.rb +33 -14
- data/test/unit/capistrano_support_test.rb +126 -148
- data/test/unit/cron_test.rb +188 -215
- data/test/unit/job_test.rb +111 -121
- data/whenever.gemspec +0 -2
- metadata +15 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1b180d6b22aaf3199ec47903e36483b327aeafd
|
4
|
+
data.tar.gz: 91102f27ed51d7821ff1888ea73e92a65e402bb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7044dac57adb8271c293e67efca30307731b7107415daa144e7dbd86cd273631df06d4cfda5142a592b936c0c06dcb44b2902969be837a304a57d86e2bf4022
|
7
|
+
data.tar.gz: d8f94fb6114a737eb9b5d17c9884deba827057763b0c45af7e04e6ed5971101142f2df3bfc7bf37b75015f8d844e08f0ff3fd985f3214f61fc4e0d5748a1b929
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
### 0.9.3 / October 5, 2014
|
2
|
+
|
3
|
+
* Drop ActiveSupport dependency [James Healy, Javan Makhmali]
|
4
|
+
|
5
|
+
* Drop shoulda for tests
|
6
|
+
|
7
|
+
* Fix `whenever:clear_crontab` Cap 3 task [Javan Makhmali]
|
8
|
+
|
9
|
+
* Avoid using tempfiles [ahoward]
|
10
|
+
|
11
|
+
|
1
12
|
### 0.9.2 / March 4, 2014
|
2
13
|
|
3
14
|
* Fix issues generating arguments for `execute` in Capistrano 3 tasks. [Javan Makhmali]
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -139,7 +139,9 @@ In your "Capfile" file:
|
|
139
139
|
require "whenever/capistrano"
|
140
140
|
```
|
141
141
|
|
142
|
-
Take a look at the load:defaults (bottom of file) task for options you can set. <
|
142
|
+
Take a look at the load:defaults (bottom of file) task for options you can set. <https://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v3/tasks/whenever.rake>. For example, to namespace the crontab entries by application and stage do this.
|
143
|
+
|
144
|
+
In your in "config/deploy.rb" file:
|
143
145
|
|
144
146
|
```ruby
|
145
147
|
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
|
data/lib/whenever.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'whenever/numeric_seconds'
|
2
|
+
require 'whenever/job_list'
|
3
|
+
require 'whenever/job'
|
4
|
+
require 'whenever/command_line'
|
5
|
+
require 'whenever/cron'
|
6
|
+
require 'whenever/output_redirection'
|
3
7
|
|
4
8
|
module Whenever
|
5
|
-
autoload :JobList, 'whenever/job_list'
|
6
|
-
autoload :Job, 'whenever/job'
|
7
|
-
autoload :CommandLine, 'whenever/command_line'
|
8
|
-
|
9
|
-
module Output
|
10
|
-
autoload :Cron, 'whenever/cron'
|
11
|
-
autoload :Redirection, 'whenever/output_redirection'
|
12
|
-
end
|
13
|
-
|
14
9
|
def self.cron(options)
|
15
10
|
Whenever::JobList.new(options).generate_cron_output
|
16
11
|
end
|
17
12
|
|
13
|
+
def self.seconds(number, units)
|
14
|
+
Whenever::NumericSeconds.seconds(number, units)
|
15
|
+
end
|
16
|
+
|
18
17
|
def self.path
|
19
18
|
Dir.pwd
|
20
19
|
end
|
@@ -3,10 +3,10 @@ namespace :whenever do
|
|
3
3
|
args = Array(fetch(:whenever_command)) + args
|
4
4
|
|
5
5
|
on roles fetch(:whenever_roles) do |host|
|
6
|
-
|
6
|
+
args = args + Array(yield(host)) if block_given?
|
7
7
|
within release_path do
|
8
8
|
with fetch(:whenever_command_environment_variables) do
|
9
|
-
execute *
|
9
|
+
execute *args
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'fileutils'
|
2
|
-
require 'tempfile'
|
3
2
|
|
4
3
|
module Whenever
|
5
4
|
class CommandLine
|
@@ -66,23 +65,24 @@ module Whenever
|
|
66
65
|
end
|
67
66
|
|
68
67
|
def write_crontab(contents)
|
69
|
-
tmp_cron_file = Tempfile.open('whenever_tmp_cron')
|
70
|
-
tmp_cron_file << contents
|
71
|
-
tmp_cron_file.fsync
|
72
|
-
|
73
68
|
command = ['crontab']
|
74
69
|
command << "-u #{@options[:user]}" if @options[:user]
|
75
|
-
command <<
|
70
|
+
command << "-"
|
71
|
+
|
72
|
+
IO.popen(command.join(' '), 'r+') do |crontab|
|
73
|
+
crontab.write(contents)
|
74
|
+
crontab.close_write
|
75
|
+
end
|
76
|
+
|
77
|
+
success = $?.exitstatus.zero?
|
76
78
|
|
77
|
-
if
|
79
|
+
if success
|
78
80
|
action = 'written' if @options[:write]
|
79
81
|
action = 'updated' if @options[:update]
|
80
82
|
puts "[write] crontab file #{action}"
|
81
|
-
tmp_cron_file.close!
|
82
83
|
exit(0)
|
83
84
|
else
|
84
85
|
warn "[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid."
|
85
|
-
tmp_cron_file.close!
|
86
86
|
exit(1)
|
87
87
|
end
|
88
88
|
end
|
data/lib/whenever/cron.rb
CHANGED
@@ -60,11 +60,11 @@ module Whenever
|
|
60
60
|
def parse_symbol
|
61
61
|
shortcut = case @time
|
62
62
|
when *KEYWORDS then "@#{@time}" # :reboot => '@reboot'
|
63
|
-
when :year then 12
|
64
|
-
when :day then 1
|
65
|
-
when :month then 1
|
66
|
-
when :week then 1
|
67
|
-
when :hour then 1
|
63
|
+
when :year then Whenever.seconds(12, :months)
|
64
|
+
when :day then Whenever.seconds(1, :day)
|
65
|
+
when :month then Whenever.seconds(1, :month)
|
66
|
+
when :week then Whenever.seconds(1, :week)
|
67
|
+
when :hour then Whenever.seconds(1, :hour)
|
68
68
|
end
|
69
69
|
|
70
70
|
if shortcut.is_a?(Numeric)
|
@@ -84,21 +84,21 @@ module Whenever
|
|
84
84
|
def parse_time
|
85
85
|
timing = Array.new(5, '*')
|
86
86
|
case @time
|
87
|
-
when 0.seconds
|
87
|
+
when Whenever.seconds(0, :seconds)...Whenever.seconds(1, :minute)
|
88
88
|
raise ArgumentError, "Time must be in minutes or higher"
|
89
|
-
when 1
|
89
|
+
when Whenever.seconds(1, :minute)...Whenever.seconds(1, :hour)
|
90
90
|
minute_frequency = @time / 60
|
91
91
|
timing[0] = comma_separated_timing(minute_frequency, 59, @at || 0)
|
92
|
-
when 1
|
92
|
+
when Whenever.seconds(1, :hour)...Whenever.seconds(1, :day)
|
93
93
|
hour_frequency = (@time / 60 / 60).round
|
94
94
|
timing[0] = @at.is_a?(Time) ? @at.min : @at
|
95
95
|
timing[1] = comma_separated_timing(hour_frequency, 23)
|
96
|
-
when 1
|
96
|
+
when Whenever.seconds(1, :day)...Whenever.seconds(1, :month)
|
97
97
|
day_frequency = (@time / 24 / 60 / 60).round
|
98
98
|
timing[0] = @at.is_a?(Time) ? @at.min : 0
|
99
99
|
timing[1] = @at.is_a?(Time) ? @at.hour : @at
|
100
100
|
timing[2] = comma_separated_timing(day_frequency, 31, 1)
|
101
|
-
when 1
|
101
|
+
when Whenever.seconds(1, :month)..Whenever.seconds(12, :months)
|
102
102
|
month_frequency = (@time / 30 / 24 / 60 / 60).round
|
103
103
|
timing[0] = @at.is_a?(Time) ? @at.min : 0
|
104
104
|
timing[1] = @at.is_a?(Time) ? @at.hour : 0
|
@@ -133,7 +133,7 @@ module Whenever
|
|
133
133
|
end
|
134
134
|
|
135
135
|
def comma_separated_timing(frequency, max, start = 0)
|
136
|
-
return start if frequency.
|
136
|
+
return start if frequency.nil? || frequency == "" || frequency.zero?
|
137
137
|
return '*' if frequency == 1
|
138
138
|
return frequency if frequency > (max * 0.5).ceil
|
139
139
|
|
data/lib/whenever/job.rb
CHANGED
@@ -9,7 +9,7 @@ module Whenever
|
|
9
9
|
@at = options.delete(:at)
|
10
10
|
@template = options.delete(:template)
|
11
11
|
@job_template = options.delete(:job_template) || ":job"
|
12
|
-
@roles = Array
|
12
|
+
@roles = Array(options.delete(:roles))
|
13
13
|
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
|
14
14
|
@options[:environment_variable] ||= "RAILS_ENV"
|
15
15
|
@options[:environment] ||= :production
|
@@ -40,7 +40,7 @@ module Whenever
|
|
40
40
|
else
|
41
41
|
option
|
42
42
|
end
|
43
|
-
end.
|
43
|
+
end.gsub(/\s+/m, " ").strip
|
44
44
|
end
|
45
45
|
|
46
46
|
def escape_single_quotes(str)
|
data/lib/whenever/job_list.rb
CHANGED
@@ -21,8 +21,8 @@ module Whenever
|
|
21
21
|
File.read(options[:file])
|
22
22
|
end
|
23
23
|
|
24
|
-
instance_eval(setup, setup_file)
|
25
|
-
instance_eval(schedule, options[:file] || '<eval>')
|
24
|
+
instance_eval(Whenever::NumericSeconds.process_string(setup), setup_file)
|
25
|
+
instance_eval(Whenever::NumericSeconds.process_string(schedule), options[:file] || '<eval>')
|
26
26
|
end
|
27
27
|
|
28
28
|
def set(variable, value)
|
@@ -45,7 +45,7 @@ module Whenever
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def job_type(name, template)
|
48
|
-
class_eval do
|
48
|
+
singleton_class_shim.class_eval do
|
49
49
|
define_method(name) do |task, *args|
|
50
50
|
options = { :task => task, :template => template }
|
51
51
|
options.merge!(args[0]) if args[0].is_a? Hash
|
@@ -67,19 +67,30 @@ module Whenever
|
|
67
67
|
|
68
68
|
private
|
69
69
|
|
70
|
+
# The Object#singleton_class method only became available in MRI 1.9.2, so
|
71
|
+
# we need this to maintain 1.8 compatibility. Once 1.8 support is dropped,
|
72
|
+
# this can be removed
|
73
|
+
def singleton_class_shim
|
74
|
+
if self.respond_to?(:singleton_clas)
|
75
|
+
singleton_class
|
76
|
+
else
|
77
|
+
class << self; self; end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
70
81
|
#
|
71
82
|
# Takes a string like: "variable1=something&variable2=somethingelse"
|
72
83
|
# and breaks it into variable/value pairs. Used for setting variables at runtime from the command line.
|
73
84
|
# Only works for setting values as strings.
|
74
85
|
#
|
75
86
|
def pre_set(variable_string = nil)
|
76
|
-
return if variable_string.
|
87
|
+
return if variable_string.nil? || variable_string == ""
|
77
88
|
|
78
89
|
pairs = variable_string.split('&')
|
79
90
|
pairs.each do |pair|
|
80
91
|
next unless pair.index('=')
|
81
92
|
variable, value = *pair.split('=')
|
82
|
-
unless variable.
|
93
|
+
unless variable.nil? || variable == "" || value.nil? || value == ""
|
83
94
|
variable = variable.strip.to_sym
|
84
95
|
set(variable, value.strip)
|
85
96
|
@pre_set_variables[variable] = value
|
@@ -92,7 +103,7 @@ module Whenever
|
|
92
103
|
|
93
104
|
output = []
|
94
105
|
@env.each do |key, val|
|
95
|
-
output << "#{key}=#{val.
|
106
|
+
output << "#{key}=#{val.nil? || val == "" ? '""' : val}\n"
|
96
107
|
end
|
97
108
|
output << "\n"
|
98
109
|
|
@@ -140,7 +151,7 @@ module Whenever
|
|
140
151
|
Whenever::Output::Cron.output(time, job) do |cron|
|
141
152
|
cron << "\n\n"
|
142
153
|
|
143
|
-
if cron
|
154
|
+
if cron[0,1] == "@"
|
144
155
|
shortcut_jobs << cron
|
145
156
|
else
|
146
157
|
regular_jobs << cron
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Whenever
|
2
|
+
class NumericSeconds
|
3
|
+
PATTERN = /(\d+)\.(seconds?|minutes?|hours?|days?|weeks?|months?|years?)/
|
4
|
+
|
5
|
+
attr_reader :number
|
6
|
+
|
7
|
+
def self.seconds(number, units)
|
8
|
+
new(number).send(units)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.process_string(string)
|
12
|
+
string.gsub(PATTERN) { Whenever.seconds($1, $2) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(number)
|
16
|
+
@number = number.to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def seconds
|
20
|
+
number
|
21
|
+
end
|
22
|
+
alias :second :seconds
|
23
|
+
|
24
|
+
def minutes
|
25
|
+
number * 60
|
26
|
+
end
|
27
|
+
alias :minute :minutes
|
28
|
+
|
29
|
+
def hours
|
30
|
+
number * 3_600
|
31
|
+
end
|
32
|
+
alias :hour :hours
|
33
|
+
|
34
|
+
def days
|
35
|
+
number * 86_400
|
36
|
+
end
|
37
|
+
alias :day :days
|
38
|
+
|
39
|
+
def weeks
|
40
|
+
number * 604_800
|
41
|
+
end
|
42
|
+
alias :week :weeks
|
43
|
+
|
44
|
+
def months
|
45
|
+
number * 2_592_000
|
46
|
+
end
|
47
|
+
alias :month :months
|
48
|
+
|
49
|
+
def years
|
50
|
+
number * 31_557_600
|
51
|
+
end
|
52
|
+
alias :year :years
|
53
|
+
end
|
54
|
+
end
|
@@ -3,10 +3,10 @@ namespace :whenever do
|
|
3
3
|
args = Array(fetch(:whenever_command)) + args
|
4
4
|
|
5
5
|
on roles fetch(:whenever_roles) do |host|
|
6
|
-
|
6
|
+
args = args + Array(yield(host)) if block_given?
|
7
7
|
within release_path do
|
8
8
|
with fetch(:whenever_command_environment_variables) do
|
9
|
-
execute *
|
9
|
+
execute *args
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
data/lib/whenever/version.rb
CHANGED
@@ -1,44 +1,42 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
Whenever.expects(:cron).returns(@task)
|
11
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CommandLineWriteTest < Whenever::TestCase
|
4
|
+
setup do
|
5
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
6
|
+
@command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
|
7
|
+
@task = "#{two_hours} /my/command"
|
8
|
+
Whenever.expects(:cron).returns(@task)
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
11
|
+
should "output the cron job with identifier blocks" do
|
12
|
+
output = <<-EXPECTED
|
15
13
|
# Begin Whenever generated tasks for: My identifier
|
16
14
|
#{@task}
|
17
15
|
# End Whenever generated tasks for: My identifier
|
18
16
|
EXPECTED
|
19
17
|
|
20
|
-
|
21
|
-
|
18
|
+
assert_equal output, @command.send(:whenever_cron)
|
19
|
+
end
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
21
|
+
should "write the crontab when run" do
|
22
|
+
@command.expects(:write_crontab).returns(true)
|
23
|
+
assert @command.run
|
27
24
|
end
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
class CommandLineUpdateTest < Whenever::TestCase
|
28
|
+
setup do
|
29
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
30
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
|
31
|
+
@task = "#{two_hours} /my/command"
|
32
|
+
Whenever.expects(:cron).returns(@task)
|
33
|
+
end
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
should "add the cron to the end of the file if there is no existing identifier block" do
|
36
|
+
existing = '# Existing crontab'
|
37
|
+
@command.expects(:read_crontab).at_least_once.returns(existing)
|
40
38
|
|
41
|
-
|
39
|
+
new_cron = <<-EXPECTED
|
42
40
|
#{existing}
|
43
41
|
|
44
42
|
# Begin Whenever generated tasks for: My identifier
|
@@ -46,14 +44,14 @@ EXPECTED
|
|
46
44
|
# End Whenever generated tasks for: My identifier
|
47
45
|
EXPECTED
|
48
46
|
|
49
|
-
|
47
|
+
assert_equal new_cron, @command.send(:updated_crontab)
|
50
48
|
|
51
|
-
|
52
|
-
|
53
|
-
|
49
|
+
@command.expects(:write_crontab).with(new_cron).returns(true)
|
50
|
+
assert @command.run
|
51
|
+
end
|
54
52
|
|
55
|
-
|
56
|
-
|
53
|
+
should "replace an existing block if the identifier matches" do
|
54
|
+
existing = <<-EXISTING_CRON
|
57
55
|
# Something
|
58
56
|
|
59
57
|
# Begin Whenever generated tasks for: My identifier
|
@@ -65,7 +63,7 @@ This shouldn't get replaced
|
|
65
63
|
# End Whenever generated tasks for: Other identifier
|
66
64
|
EXISTING_CRON
|
67
65
|
|
68
|
-
|
66
|
+
new_cron = <<-NEW_CRON
|
69
67
|
# Something
|
70
68
|
|
71
69
|
# Begin Whenever generated tasks for: My identifier
|
@@ -77,62 +75,62 @@ This shouldn't get replaced
|
|
77
75
|
# End Whenever generated tasks for: Other identifier
|
78
76
|
NEW_CRON
|
79
77
|
|
80
|
-
|
81
|
-
|
78
|
+
@command.expects(:read_crontab).at_least_once.returns(existing)
|
79
|
+
assert_equal new_cron, @command.send(:updated_crontab)
|
82
80
|
|
83
|
-
|
84
|
-
|
85
|
-
end
|
81
|
+
@command.expects(:write_crontab).with(new_cron).returns(true)
|
82
|
+
assert @command.run
|
86
83
|
end
|
84
|
+
end
|
87
85
|
|
88
|
-
|
89
|
-
|
90
|
-
|
86
|
+
class CommandLineUpdateWithBackslashesTest < Whenever::TestCase
|
87
|
+
setup do
|
88
|
+
@existing = <<-EXISTING_CRON
|
91
89
|
# Begin Whenever generated tasks for: My identifier
|
92
90
|
script/runner -e production 'puts '\\''hello'\\'''
|
93
91
|
# End Whenever generated tasks for: My identifier
|
94
92
|
EXISTING_CRON
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
93
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
94
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
|
95
|
+
@command.expects(:read_crontab).at_least_once.returns(@existing)
|
96
|
+
@command.expects(:whenever_cron).returns(@existing)
|
97
|
+
end
|
100
98
|
|
101
|
-
|
102
|
-
|
103
|
-
end
|
99
|
+
should "replace the existing block with the backslashes in tact" do
|
100
|
+
assert_equal @existing, @command.send(:updated_crontab)
|
104
101
|
end
|
102
|
+
end
|
105
103
|
|
106
|
-
|
107
|
-
|
108
|
-
|
104
|
+
class CommandLineUpdateToSimilarCrontabTest < Whenever::TestCase
|
105
|
+
setup do
|
106
|
+
@existing = <<-EXISTING_CRON
|
109
107
|
# Begin Whenever generated tasks for: WheneverExisting
|
110
108
|
# End Whenever generated tasks for: WheneverExisting
|
111
109
|
EXISTING_CRON
|
112
|
-
|
110
|
+
@new = <<-NEW_CRON
|
113
111
|
# Begin Whenever generated tasks for: Whenever
|
114
112
|
# End Whenever generated tasks for: Whenever
|
115
113
|
NEW_CRON
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
114
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
115
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'Whenever')
|
116
|
+
@command.expects(:read_crontab).at_least_once.returns(@existing)
|
117
|
+
@command.expects(:whenever_cron).returns(@new)
|
118
|
+
end
|
119
|
+
|
120
|
+
should "append the similarly named command" do
|
121
|
+
assert_equal @existing + "\n" + @new, @command.send(:updated_crontab)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class CommandLineClearTest < Whenever::TestCase
|
126
|
+
setup do
|
127
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
128
|
+
@command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
|
129
|
+
@task = "#{two_hours} /my/command"
|
130
|
+
end
|
131
|
+
|
132
|
+
should "clear an existing block if the identifier matches" do
|
133
|
+
existing = <<-EXISTING_CRON
|
136
134
|
# Something
|
137
135
|
|
138
136
|
# Begin Whenever generated tasks for: My identifier
|
@@ -144,9 +142,9 @@ This shouldn't get replaced
|
|
144
142
|
# End Whenever generated tasks for: Other identifier
|
145
143
|
EXISTING_CRON
|
146
144
|
|
147
|
-
|
145
|
+
@command.expects(:read_crontab).at_least_once.returns(existing)
|
148
146
|
|
149
|
-
|
147
|
+
new_cron = <<-NEW_CRON
|
150
148
|
# Something
|
151
149
|
|
152
150
|
# Begin Whenever generated tasks for: Other identifier
|
@@ -154,137 +152,138 @@ This shouldn't get replaced
|
|
154
152
|
# End Whenever generated tasks for: Other identifier
|
155
153
|
NEW_CRON
|
156
154
|
|
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
|
-
end
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
end
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
end
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
155
|
+
assert_equal new_cron, @command.send(:updated_crontab)
|
156
|
+
|
157
|
+
@command.expects(:write_crontab).with(new_cron).returns(true)
|
158
|
+
assert @command.run
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class CommandLineClearWithNoScheduleTest < Whenever::TestCase
|
163
|
+
setup do
|
164
|
+
File.expects(:exists?).with('config/schedule.rb').returns(false)
|
165
|
+
@command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
|
166
|
+
end
|
167
|
+
|
168
|
+
should "run successfully" do
|
169
|
+
@command.expects(:write_crontab).returns(true)
|
170
|
+
assert @command.run
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
class CommandLineUpdateWithNoIdentifierTest < Whenever::TestCase
|
175
|
+
setup do
|
176
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
177
|
+
Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
|
178
|
+
@command = Whenever::CommandLine.new(:update => true, :file => @file)
|
179
|
+
end
|
180
|
+
|
181
|
+
should "use the default identifier" do
|
182
|
+
assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
class CombinedParamsTest < Whenever::TestCase
|
187
|
+
setup do
|
188
|
+
Whenever::CommandLine.any_instance.expects(:exit)
|
189
|
+
Whenever::CommandLine.any_instance.expects(:warn)
|
190
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
191
|
+
end
|
192
|
+
|
193
|
+
should "exit with write and clear" do
|
194
|
+
@command = Whenever::CommandLine.new(:write => true, :clear => true)
|
195
|
+
end
|
196
|
+
|
197
|
+
should "exit with write and update" do
|
198
|
+
@command = Whenever::CommandLine.new(:write => true, :update => true)
|
199
|
+
end
|
200
|
+
|
201
|
+
should "exit with update and clear" do
|
202
|
+
@command = Whenever::CommandLine.new(:update => true, :clear => true)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
class RunnerOverwrittenWithSetOptionTest < Whenever::TestCase
|
207
|
+
setup do
|
208
|
+
@output = Whenever.cron :set => 'environment=serious', :string => \
|
209
|
+
<<-file
|
210
|
+
set :job_template, nil
|
211
|
+
set :environment, :silly
|
212
|
+
set :path, '/my/path'
|
213
|
+
every 2.hours do
|
214
|
+
runner "blahblah"
|
215
|
+
end
|
216
|
+
file
|
217
|
+
end
|
218
|
+
|
219
|
+
should "output the runner using the override environment" do
|
220
|
+
assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
class EnvironmentAndPathOverwrittenWithSetOptionTest < Whenever::TestCase
|
226
|
+
setup do
|
227
|
+
@output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
|
228
|
+
<<-file
|
229
|
+
set :job_template, nil
|
230
|
+
set :environment, :silly
|
231
|
+
set :path, '/silly/path'
|
232
|
+
every 2.hours do
|
233
|
+
runner "blahblah"
|
234
|
+
end
|
235
|
+
file
|
236
|
+
end
|
237
|
+
|
238
|
+
should "output the runner using the overridden path and environment" do
|
239
|
+
assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
class EnvironmentAndPathOverwrittenWithSetOptionWithSpacesTest < Whenever::TestCase
|
244
|
+
setup do
|
245
|
+
@output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
|
246
|
+
<<-file
|
247
|
+
set :job_template, nil
|
248
|
+
set :environment, :silly
|
249
|
+
set :path, '/silly/path'
|
250
|
+
every 2.hours do
|
251
|
+
runner "blahblah"
|
252
|
+
end
|
253
|
+
file
|
254
|
+
end
|
255
|
+
|
256
|
+
should "output the runner using the overridden path and environment" do
|
257
|
+
assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
class EnvironmentOverwrittenWithoutValueTest < Whenever::TestCase
|
262
|
+
setup do
|
263
|
+
@output = Whenever.cron :set => ' environment=', :string => \
|
264
|
+
<<-file
|
265
|
+
set :job_template, nil
|
266
|
+
set :environment, :silly
|
267
|
+
set :path, '/silly/path'
|
268
|
+
every 2.hours do
|
269
|
+
runner "blahblah"
|
270
|
+
end
|
271
|
+
file
|
272
|
+
end
|
273
|
+
|
274
|
+
should "output the runner using the original environmnet" do
|
275
|
+
assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
class PreparingOutputTest < Whenever::TestCase
|
280
|
+
setup do
|
281
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
282
|
+
end
|
283
|
+
|
284
|
+
should "not trim off the top lines of the file" do
|
285
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
|
286
|
+
existing = <<-EXISTING_CRON
|
288
287
|
# Useless Comments
|
289
288
|
# at the top of the file
|
290
289
|
|
@@ -293,12 +292,12 @@ My whenever job that was already here
|
|
293
292
|
# End Whenever generated tasks for: My identifier
|
294
293
|
EXISTING_CRON
|
295
294
|
|
296
|
-
|
297
|
-
|
295
|
+
assert_equal existing, @command.send(:prepare, existing)
|
296
|
+
end
|
298
297
|
|
299
|
-
|
300
|
-
|
301
|
-
|
298
|
+
should "trim off the top lines of the file" do
|
299
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
|
300
|
+
existing = <<-EXISTING_CRON
|
302
301
|
# Useless Comments
|
303
302
|
# at the top of the file
|
304
303
|
|
@@ -307,18 +306,18 @@ My whenever job that was already here
|
|
307
306
|
# End Whenever generated tasks for: My identifier
|
308
307
|
EXISTING_CRON
|
309
308
|
|
310
|
-
|
309
|
+
new_cron = <<-NEW_CRON
|
311
310
|
# Begin Whenever generated tasks for: My identifier
|
312
311
|
My whenever job that was already here
|
313
312
|
# End Whenever generated tasks for: My identifier
|
314
313
|
NEW_CRON
|
315
314
|
|
316
|
-
|
317
|
-
|
315
|
+
assert_equal new_cron, @command.send(:prepare, existing)
|
316
|
+
end
|
318
317
|
|
319
|
-
|
320
|
-
|
321
|
-
|
318
|
+
should "preserve terminating newlines in files" do
|
319
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
|
320
|
+
existing = <<-EXISTING_CRON
|
322
321
|
# Begin Whenever generated tasks for: My identifier
|
323
322
|
My whenever job that was already here
|
324
323
|
# End Whenever generated tasks for: My identifier
|
@@ -327,8 +326,6 @@ My whenever job that was already here
|
|
327
326
|
My non-whenever job that was already here
|
328
327
|
EXISTING_CRON
|
329
328
|
|
330
|
-
|
331
|
-
end
|
329
|
+
assert_equal existing, @command.send(:prepare, existing)
|
332
330
|
end
|
333
|
-
|
334
331
|
end
|