whenever 1.1.3 → 1.1.4

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.
data/test/test_case.rb DELETED
@@ -1,32 +0,0 @@
1
- module Whenever
2
- require 'minitest/autorun'
3
- begin
4
- # 2.0.0
5
- class TestCase < Minitest::Test; end
6
- rescue NameError
7
- # 1.9.3
8
- class TestCase < Minitest::Unit::TestCase; end
9
- end
10
-
11
-
12
- class TestCase
13
- class << self
14
- def setup(&block)
15
- define_method(:setup) do
16
- super()
17
- instance_eval(&block)
18
- end
19
- end
20
-
21
- def test(name, &block)
22
- define_method("test_#{name}".to_sym, &block)
23
- end
24
- alias should test
25
- end
26
-
27
- def assert_no_match(regexp, string)
28
- message = "<#{regexp}> expected to not match\n<#{string}>"
29
- assert regexp !~ string, message
30
- end
31
- end
32
- end
data/test/test_helper.rb DELETED
@@ -1,51 +0,0 @@
1
- require 'whenever'
2
- require 'test_case'
3
- require 'mocha/minitest'
4
- begin
5
- require 'active_support/all'
6
- rescue LoadError
7
- end
8
-
9
- module Whenever::TestHelpers
10
- protected
11
- def new_job(options={})
12
- Whenever::Job.new(options)
13
- end
14
-
15
- def parse_time(time = nil, task = nil, at = nil, options = {})
16
- Whenever::Output::Cron.new(time, task, at, options).time_in_cron_syntax
17
- end
18
-
19
- def two_hours
20
- "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
21
- end
22
-
23
- def assert_months_and_days_and_hours_and_minutes_equals(expected, time, options = {})
24
- cron = parse_time(Whenever.seconds(1, :year), 'some task', time, options)
25
- minutes, hours, days, months = cron.split(' ')
26
- assert_equal expected, [months, days, hours, minutes]
27
- end
28
-
29
- def assert_days_and_hours_and_minutes_equals(expected, time, options = {})
30
- cron = parse_time(Whenever.seconds(2, :months), 'some task', time, options)
31
- minutes, hours, days, _ = cron.split(' ')
32
- assert_equal expected, [days, hours, minutes]
33
- end
34
-
35
- def assert_hours_and_minutes_equals(expected, time, options = {})
36
- cron = parse_time(Whenever.seconds(2, :days), 'some task', time, options)
37
- minutes, hours, _ = cron.split(' ')
38
- assert_equal expected, [hours, minutes]
39
- end
40
-
41
- def assert_minutes_equals(expected, time, options = {})
42
- cron = parse_time(Whenever.seconds(2, :hours), 'some task', time, options)
43
- assert_equal expected, cron.split(' ')[0]
44
- end
45
-
46
- def lines_without_empty_line(lines)
47
- lines.map { |line| line.chomp }.reject { |line| line.empty? }
48
- end
49
- end
50
-
51
- Whenever::TestCase.send(:include, Whenever::TestHelpers)
@@ -1,147 +0,0 @@
1
- require 'test_helper'
2
- require 'whenever/capistrano/v2/support'
3
-
4
- class CapistranoSupportTestSubject
5
- include Whenever::CapistranoSupport
6
- end
7
-
8
- class CapistranoTestCase < Whenever::TestCase
9
- setup do
10
- @capistrano = CapistranoSupportTestSubject.new
11
- configuration = mock()
12
- configuration.stubs(:load).yields(@capistrano)
13
- Whenever::CapistranoSupport.load_into(configuration)
14
- end
15
- end
16
-
17
- class CapistranoSupportTest < CapistranoTestCase
18
- should "return fetch(:whenever_options) from #whenever_options" do
19
- @capistrano.expects(:fetch).with(:whenever_options)
20
- @capistrano.whenever_options
21
- end
22
-
23
- should "return whenever_options[:roles] as an array from #whenever_roles with one role" do
24
- @capistrano.stubs(:whenever_options).returns({:roles => :role1})
25
- assert_equal [:role1], @capistrano.whenever_roles
26
- end
27
-
28
- should "return an empty array from #whenever_roles with no defined roles" do
29
- @capistrano.stubs(:whenever_options).returns({})
30
- assert_equal [], @capistrano.whenever_roles
31
- end
32
-
33
- should "return the list of servers returned by find_servers from #whenever_servers" do
34
- @capistrano.stubs(:whenever_options).returns({})
35
- @capistrano.stubs(:find_servers).returns([:server1, :server2])
36
-
37
- assert_equal [:server1, :server2], @capistrano.whenever_servers
38
- end
39
-
40
- should "#whenever_prepare_for_rollback: set path to previous_release if there is a previous release" do
41
- args = {}
42
- @capistrano.stubs(:fetch).with(:previous_release).returns("/some/path/20121221010000")
43
- assert_equal({:path => "/some/path/20121221010000"}, @capistrano.whenever_prepare_for_rollback(args))
44
- end
45
-
46
- should "#whenever_prepare_for_rollback: set path to release_path and flags to whenever_clear_flags if there is no previous release" do
47
- args = {}
48
- @capistrano.stubs(:fetch).with(:previous_release).returns(nil)
49
- @capistrano.stubs(:fetch).with(:release_path).returns("/some/path/20121221010000")
50
- @capistrano.stubs(:fetch).with(:whenever_clear_flags).returns("--clear-crontab whenever_identifier")
51
- assert_equal({:path => "/some/path/20121221010000", :flags => "--clear-crontab whenever_identifier"}, @capistrano.whenever_prepare_for_rollback(args))
52
- end
53
-
54
- should "#whenever_run_commands: require :command arg" do
55
- assert_raises ArgumentError do
56
- @capistrano.whenever_run_commands(:options => {}, :path => {}, :flags => {})
57
- end
58
- end
59
-
60
- should "#whenever_run_commands: require :path arg" do
61
- assert_raises ArgumentError do
62
- @capistrano.whenever_run_commands(:options => {}, :command => {}, :flags => {})
63
- end
64
- end
65
-
66
- should "#whenever_run_commands: require :flags arg" do
67
- assert_raises ArgumentError do
68
- @capistrano.whenever_run_commands(:options => {}, :path => {}, :command => {})
69
- end
70
- end
71
- end
72
-
73
- class ServerRolesTest < CapistranoTestCase
74
- setup do
75
- @mock_servers = ["foo", "bar"]
76
- @capistrano.stubs(:whenever_servers).returns(@mock_servers)
77
-
78
- @mock_server1, @mock_server2, @mock_server3 = mock("Server1"), mock("Server2"), mock("Server3")
79
- @mock_server1.stubs(:host).returns("server1.foo.com")
80
- @mock_server2.stubs(:host).returns("server2.foo.com")
81
- @mock_server3.stubs(:host => "server3.foo.com", :port => 1022, :user => 'test')
82
- @mock_servers = [@mock_server1, @mock_server2]
83
- end
84
-
85
- should "return a map of servers to their role(s)" do
86
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
87
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1])
88
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
89
- assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
90
- end
91
-
92
- should "exclude non-requested roles" do
93
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
94
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
95
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
96
- assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
97
- end
98
-
99
- should "include all roles for servers w/ >1 when they're requested" do
100
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2, :role3])
101
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
102
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
103
- assert_equal({"foo" => [:role1, :role3], "bar" => [:role2]}, @capistrano.whenever_server_roles)
104
- end
105
-
106
- should "call run for each host w/ appropriate role args" do
107
- @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1])
108
- @capistrano.stubs(:role_names_for_host).with(@mock_server2).returns([:role2])
109
- @capistrano.stubs(:whenever_servers).returns(@mock_servers)
110
- roles = [:role1, :role2]
111
- @capistrano.stubs(:whenever_options).returns({:roles => roles})
112
-
113
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1', {:roles => roles, :hosts => @mock_server1})
114
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role2', {:roles => roles, :hosts => @mock_server2})
115
-
116
- @capistrano.whenever_run_commands(:command => "whenever",
117
- :path => "/foo/bar",
118
- :flags => "--flag1 --flag2")
119
- end
120
-
121
- should "call run w/ all role args for servers w/ >1 role" do
122
- @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1, :role3])
123
- @capistrano.stubs(:whenever_servers).returns([@mock_server1])
124
- roles = [:role1, :role2, :role3]
125
- @capistrano.stubs(:whenever_options).returns({:roles => roles})
126
-
127
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1,role3', {:roles => roles, :hosts => @mock_server1})
128
-
129
- @capistrano.whenever_run_commands(:command => "whenever",
130
- :path => "/foo/bar",
131
- :flags => "--flag1 --flag2")
132
- end
133
-
134
- should "call run w/ proper server options (port, user)" do
135
- @capistrano.stubs(:role_names_for_host).with(@mock_server3).returns([:role3])
136
- @capistrano.stubs(:whenever_servers).returns([@mock_server3])
137
- @capistrano.stubs(:whenever_options).returns({:roles => [:role3]})
138
-
139
- @capistrano.expects(:run).once.with do |command, options|
140
- options[:hosts].user == "test" && options[:hosts].port == 1022
141
- end
142
-
143
- @capistrano.whenever_run_commands(:command => "whenever",
144
- :path => "/foo/bar",
145
- :flags => "--flag1 --flag2")
146
- end
147
- end
@@ -1,418 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CronTest < Whenever::TestCase
4
- should "raise if less than 1 minute" do
5
- assert_raises ArgumentError do
6
- parse_time(Whenever.seconds(59, :seconds))
7
- end
8
-
9
- assert_raises ArgumentError do
10
- parse_time(Whenever.seconds(0, :minutes))
11
- end
12
- end
13
-
14
- # For sanity, do some tests on straight cron-syntax strings
15
- should "parse correctly" do
16
- assert_equal '* * * * *', parse_time(Whenever.seconds(1, :minute))
17
- assert_equal '0,5,10,15,20,25,30,35,40,45,50,55 * * * *', parse_time(Whenever.seconds(5, :minutes))
18
- assert_equal '7,14,21,28,35,42,49,56 * * * *', parse_time(Whenever.seconds(7, :minutes))
19
- assert_equal '0,30 * * * *', parse_time(Whenever.seconds(30, :minutes))
20
- assert_equal '32 * * * *', parse_time(Whenever.seconds(32, :minutes))
21
- assert '60 * * * *' != parse_time(Whenever.seconds(60, :minutes)) # 60 minutes bumps up into the hour range
22
- end
23
-
24
- # Test all minutes
25
- (2..59).each do |num|
26
- should "parse correctly for #{num} minutes" do
27
- start = 0
28
- start += num unless 60.modulo(num).zero?
29
- minutes = (start..59).step(num).to_a
30
-
31
- assert_equal "#{minutes.join(',')} * * * *", parse_time(Whenever.seconds(num, :minutes))
32
- end
33
- end
34
- end
35
-
36
- class CronParseHoursTest < Whenever::TestCase
37
- should "parse correctly" do
38
- assert_equal '0 * * * *', parse_time(Whenever.seconds(1, :hour))
39
- assert_equal '0 0,2,4,6,8,10,12,14,16,18,20,22 * * *', parse_time(Whenever.seconds(2, :hours))
40
- assert_equal '0 0,3,6,9,12,15,18,21 * * *', parse_time(Whenever.seconds(3, :hours))
41
- assert_equal '0 5,10,15,20 * * *', parse_time(Whenever.seconds(5, :hours))
42
- assert_equal '0 17 * * *', parse_time(Whenever.seconds(17, :hours))
43
- assert '0 24 * * *' != parse_time(Whenever.seconds(24, :hours)) # 24 hours bumps up into the day range
44
- end
45
-
46
- (2..23).each do |num|
47
- should "parse correctly for #{num} hours" do
48
- start = 0
49
- start += num unless 24.modulo(num).zero?
50
- hours = (start..23).step(num).to_a
51
-
52
- assert_equal "0 #{hours.join(',')} * * *", parse_time(Whenever.seconds(num, :hours))
53
- end
54
- end
55
-
56
- should "parse correctly when given an 'at' with minutes as an Integer" do
57
- assert_minutes_equals "1", 1
58
- assert_minutes_equals "14", 14
59
- assert_minutes_equals "27", 27
60
- assert_minutes_equals "55", 55
61
- end
62
-
63
- should "parse correctly when given an 'at' with minutes as a Time" do
64
- # Basically just testing that Chronic parses some times and we get the minutes out of it
65
- assert_minutes_equals "1", '3:01am'
66
- assert_minutes_equals "1", 'January 21 2:01 PM'
67
- assert_minutes_equals "0", 'midnight'
68
- assert_minutes_equals "59", '13:59'
69
- end
70
-
71
- should "parse correctly when given an 'at' with minutes as a Time and custom Chronic options are set" do
72
- assert_minutes_equals "15", '3:15'
73
- assert_minutes_equals "15", '3:15', :chronic_options => { :hours24 => true }
74
- assert_minutes_equals "15", '3:15', :chronic_options => { :hours24 => false }
75
-
76
- assert_minutes_equals "30", '6:30'
77
- assert_minutes_equals "30", '6:30', :chronic_options => { :hours24 => true }
78
- assert_minutes_equals "30", '6:30', :chronic_options => { :hours24 => false }
79
- end
80
-
81
- should "parse correctly when given an 'at' with minutes as a Range" do
82
- assert_minutes_equals "15-30", 15..30
83
- end
84
-
85
- should "raise an exception when given an 'at' with an invalid minute value" do
86
- assert_raises ArgumentError do
87
- parse_time(Whenever.seconds(1, :hour), nil, 60)
88
- end
89
-
90
- assert_raises ArgumentError do
91
- parse_time(Whenever.seconds(1, :hour), nil, -1)
92
- end
93
-
94
- assert_raises ArgumentError do
95
- parse_time(Whenever.seconds(1, :hour), nil, 0..60)
96
- end
97
-
98
- assert_raises ArgumentError do
99
- parse_time(Whenever.seconds(1, :hour), nil, -1..59)
100
- end
101
- end
102
- end
103
-
104
- class CronParseDaysTest < Whenever::TestCase
105
- should "parse correctly" do
106
- assert_equal '0 0 * * *', parse_time(Whenever.seconds(1, :days))
107
- assert_equal '0 0 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * *', parse_time(Whenever.seconds(2, :days))
108
- assert_equal '0 0 1,5,9,13,17,21,25,29 * *', parse_time(Whenever.seconds(4, :days))
109
- assert_equal '0 0 1,8,15,22 * *', parse_time(Whenever.seconds(7, :days))
110
- assert_equal '0 0 1,17 * *', parse_time(Whenever.seconds(16, :days))
111
- assert_equal '0 0 17 * *', parse_time(Whenever.seconds(17, :days))
112
- assert_equal '0 0 29 * *', parse_time(Whenever.seconds(29, :days))
113
- assert '0 0 30 * *' != parse_time(Whenever.seconds(30, :days)) # 30 days bumps into the month range
114
- end
115
-
116
- should "parse correctly when given an 'at' with hours, minutes as a Time" do
117
- # first param is an array with [hours, minutes]
118
- assert_hours_and_minutes_equals %w(3 45), '3:45am'
119
- assert_hours_and_minutes_equals %w(20 1), '8:01pm'
120
- assert_hours_and_minutes_equals %w(0 0), 'midnight'
121
- assert_hours_and_minutes_equals %w(1 23), '1:23 AM'
122
- assert_hours_and_minutes_equals %w(23 59), 'March 21 11:59 pM'
123
- end
124
-
125
- should "parse correctly when given an 'at' with hours, minutes as a Time and custom Chronic options are set" do
126
- # first param is an array with [hours, minutes]
127
- assert_hours_and_minutes_equals %w(15 15), '3:15'
128
- assert_hours_and_minutes_equals %w(3 15), '3:15', :chronic_options => { :hours24 => true }
129
- assert_hours_and_minutes_equals %w(15 15), '3:15', :chronic_options => { :hours24 => false }
130
-
131
- assert_hours_and_minutes_equals %w(6 30), '6:30'
132
- assert_hours_and_minutes_equals %w(6 30), '6:30', :chronic_options => { :hours24 => true }
133
- assert_hours_and_minutes_equals %w(6 30), '6:30', :chronic_options => { :hours24 => false }
134
- end
135
-
136
- should "parse correctly when given an 'at' with hours as an Integer" do
137
- # first param is an array with [hours, minutes]
138
- assert_hours_and_minutes_equals %w(1 0), 1
139
- assert_hours_and_minutes_equals %w(3 0), 3
140
- assert_hours_and_minutes_equals %w(15 0), 15
141
- assert_hours_and_minutes_equals %w(19 0), 19
142
- assert_hours_and_minutes_equals %w(23 0), 23
143
- end
144
-
145
- should "parse correctly when given an 'at' with hours as a Range" do
146
- assert_hours_and_minutes_equals %w(3-23 0), 3..23
147
- end
148
-
149
- should "raise an exception when given an 'at' with an invalid hour value" do
150
- assert_raises ArgumentError do
151
- parse_time(Whenever.seconds(1, :day), nil, 24)
152
- end
153
-
154
- assert_raises ArgumentError do
155
- parse_time(Whenever.seconds(1, :day), nil, -1)
156
- end
157
-
158
- assert_raises ArgumentError do
159
- parse_time(Whenever.seconds(1, :day), nil, 0..24)
160
- end
161
-
162
- assert_raises ArgumentError do
163
- parse_time(Whenever.seconds(1, :day), nil, -1..23)
164
- end
165
- end
166
- end
167
-
168
- class CronParseMonthsTest < Whenever::TestCase
169
- should "parse correctly" do
170
- assert_equal '0 0 1 * *', parse_time(Whenever.seconds(1, :month))
171
- assert_equal '0 0 1 1,3,5,7,9,11 *', parse_time(Whenever.seconds(2, :months))
172
- assert_equal '0 0 1 1,4,7,10 *', parse_time(Whenever.seconds(3, :months))
173
- assert_equal '0 0 1 1,5,9 *', parse_time(Whenever.seconds(4, :months))
174
- assert_equal '0 0 1 1,6 *', parse_time(Whenever.seconds(5, :months))
175
- assert_equal '0 0 1 7 *', parse_time(Whenever.seconds(7, :months))
176
- assert_equal '0 0 1 8 *', parse_time(Whenever.seconds(8, :months))
177
- assert_equal '0 0 1 9 *', parse_time(Whenever.seconds(9, :months))
178
- assert_equal '0 0 1 10 *', parse_time(Whenever.seconds(10, :months))
179
- assert_equal '0 0 1 11 *', parse_time(Whenever.seconds(11, :months))
180
- assert_equal '0 0 1 12 *', parse_time(Whenever.seconds(12, :months))
181
- end
182
-
183
- should "parse months with a date and/or time" do
184
- # should set the day to 1 if no date is given
185
- assert_equal '0 17 1 * *', parse_time(Whenever.seconds(1, :month), nil, "5pm")
186
- # should use the date if one is given
187
- assert_equal '0 2 23 * *', parse_time(Whenever.seconds(1, :month), nil, "February 23rd at 2am")
188
- # should use an iteger as the day
189
- assert_equal '0 0 5 * *', parse_time(Whenever.seconds(1, :month), nil, 5)
190
- end
191
-
192
- should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
193
- # first param is an array with [days, hours, minutes]
194
- assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
195
- assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
196
- assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
197
- assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
198
- end
199
-
200
- should "parse correctly when given an 'at' with days, hours, minutes as a Time and custom Chronic options are set" do
201
- # first param is an array with [days, hours, minutes]
202
- assert_days_and_hours_and_minutes_equals %w(22 15 45), 'February 22nd 3:45'
203
- assert_days_and_hours_and_minutes_equals %w(22 15 45), '02/22 3:45'
204
- assert_days_and_hours_and_minutes_equals %w(22 3 45), 'February 22nd 3:45', :chronic_options => { :hours24 => true }
205
- assert_days_and_hours_and_minutes_equals %w(22 15 45), 'February 22nd 3:45', :chronic_options => { :hours24 => false }
206
-
207
- assert_days_and_hours_and_minutes_equals %w(3 8 15), '02/03 8:15'
208
- assert_days_and_hours_and_minutes_equals %w(3 8 15), '02/03 8:15', :chronic_options => { :endian_precedence => :middle }
209
- assert_days_and_hours_and_minutes_equals %w(2 8 15), '02/03 8:15', :chronic_options => { :endian_precedence => :little }
210
-
211
- assert_days_and_hours_and_minutes_equals %w(4 4 50), '03/04 4:50', :chronic_options => { :endian_precedence => :middle, :hours24 => true }
212
- assert_days_and_hours_and_minutes_equals %w(4 16 50), '03/04 4:50', :chronic_options => { :endian_precedence => :middle, :hours24 => false }
213
- assert_days_and_hours_and_minutes_equals %w(3 4 50), '03/04 4:50', :chronic_options => { :endian_precedence => :little, :hours24 => true }
214
- assert_days_and_hours_and_minutes_equals %w(3 16 50), '03/04 4:50', :chronic_options => { :endian_precedence => :little, :hours24 => false }
215
- end
216
-
217
- should "parse correctly when given an 'at' with days as an Integer" do
218
- # first param is an array with [days, hours, minutes]
219
- assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
220
- assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
221
- assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
222
- end
223
-
224
- should "parse correctly when given an 'at' with days as a Range" do
225
- assert_days_and_hours_and_minutes_equals %w(1-7 0 0), 1..7
226
- end
227
-
228
- should "raise an exception when given an 'at' with an invalid day value" do
229
- assert_raises ArgumentError do
230
- parse_time(Whenever.seconds(1, :month), nil, 32)
231
- end
232
-
233
- assert_raises ArgumentError do
234
- parse_time(Whenever.seconds(1, :month), nil, -1)
235
- end
236
-
237
- assert_raises ArgumentError do
238
- parse_time(Whenever.seconds(1, :month), nil, 0..30)
239
- end
240
-
241
- assert_raises ArgumentError do
242
- parse_time(Whenever.seconds(1, :month), nil, 1..32)
243
- end
244
- end
245
- end
246
-
247
- class CronParseYearTest < Whenever::TestCase
248
- should "parse correctly" do
249
- assert_equal '0 0 1 1 *', parse_time(Whenever.seconds(1, :year))
250
- end
251
-
252
- should "parse year with a date and/or time" do
253
- # should set the day and month to 1 if no date is given
254
- assert_equal '0 17 1 1 *', parse_time(Whenever.seconds(1, :year), nil, "5pm")
255
- # should use the date if one is given
256
- assert_equal '0 2 23 2 *', parse_time(Whenever.seconds(1, :year), nil, "February 23rd at 2am")
257
- # should use an iteger as the month
258
- assert_equal '0 0 1 5 *', parse_time(Whenever.seconds(1, :year), nil, 5)
259
- end
260
-
261
- should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
262
- # first param is an array with [months, days, hours, minutes]
263
- assert_months_and_days_and_hours_and_minutes_equals %w(1 1 3 45), 'January 1st 3:45am'
264
- assert_months_and_days_and_hours_and_minutes_equals %w(2 11 23 0), 'Feb 11 11PM'
265
- assert_months_and_days_and_hours_and_minutes_equals %w(3 22 1 1), 'march 22nd at 1:01 am'
266
- assert_months_and_days_and_hours_and_minutes_equals %w(3 23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
267
- end
268
-
269
- should "parse correctly when given an 'at' with days, hours, minutes as a Time and custom Chronic options are set" do
270
- # first param is an array with [months, days, hours, minutes]
271
- assert_months_and_days_and_hours_and_minutes_equals %w(2 22 15 45), 'February 22nd 3:45'
272
- assert_months_and_days_and_hours_and_minutes_equals %w(2 22 15 45), '02/22 3:45'
273
- assert_months_and_days_and_hours_and_minutes_equals %w(2 22 3 45), 'February 22nd 3:45', :chronic_options => { :hours24 => true }
274
- assert_months_and_days_and_hours_and_minutes_equals %w(2 22 15 45), 'February 22nd 3:45', :chronic_options => { :hours24 => false }
275
-
276
- assert_months_and_days_and_hours_and_minutes_equals %w(2 3 8 15), '02/03 8:15'
277
- assert_months_and_days_and_hours_and_minutes_equals %w(2 3 8 15), '02/03 8:15', :chronic_options => { :endian_precedence => :middle }
278
- assert_months_and_days_and_hours_and_minutes_equals %w(3 2 8 15), '02/03 8:15', :chronic_options => { :endian_precedence => :little }
279
-
280
- assert_months_and_days_and_hours_and_minutes_equals %w(3 4 4 50), '03/04 4:50', :chronic_options => { :endian_precedence => :middle, :hours24 => true }
281
- assert_months_and_days_and_hours_and_minutes_equals %w(3 4 16 50), '03/04 4:50', :chronic_options => { :endian_precedence => :middle, :hours24 => false }
282
- assert_months_and_days_and_hours_and_minutes_equals %w(4 3 4 50), '03/04 4:50', :chronic_options => { :endian_precedence => :little, :hours24 => true }
283
- assert_months_and_days_and_hours_and_minutes_equals %w(4 3 16 50), '03/04 4:50', :chronic_options => { :endian_precedence => :little, :hours24 => false }
284
- end
285
-
286
- should "parse correctly when given an 'at' with month as an Integer" do
287
- # first param is an array with [months, days, hours, minutes]
288
- assert_months_and_days_and_hours_and_minutes_equals %w(1 1 0 0), 1
289
- assert_months_and_days_and_hours_and_minutes_equals %w(5 1 0 0), 5
290
- assert_months_and_days_and_hours_and_minutes_equals %w(12 1 0 0), 12
291
- end
292
-
293
- should "parse correctly when given an 'at' with month as a Range" do
294
- assert_months_and_days_and_hours_and_minutes_equals %w(1-3 1 0 0), 1..3
295
- end
296
-
297
- should "raise an exception when given an 'at' with an invalid month value" do
298
- assert_raises ArgumentError do
299
- parse_time(Whenever.seconds(1, :year), nil, 13)
300
- end
301
-
302
- assert_raises ArgumentError do
303
- parse_time(Whenever.seconds(1, :year), nil, -1)
304
- end
305
-
306
- assert_raises ArgumentError do
307
- parse_time(Whenever.seconds(1, :year), nil, 0..12)
308
- end
309
-
310
- assert_raises ArgumentError do
311
- parse_time(Whenever.seconds(1, :year), nil, 1..13)
312
- end
313
- end
314
- end
315
-
316
- class CronParseDaysOfWeekTest < Whenever::TestCase
317
- should "parse days of the week correctly" do
318
- {
319
- '0' => %w(sun Sunday SUNDAY SUN),
320
- '1' => %w(mon Monday MONDAY MON),
321
- '2' => %w(tue tues Tuesday TUESDAY TUE),
322
- '3' => %w(wed Wednesday WEDNESDAY WED),
323
- '4' => %w(thu thurs thur Thursday THURSDAY THU),
324
- '5' => %w(fri Friday FRIDAY FRI),
325
- '6' => %w(sat Saturday SATURDAY SAT)
326
- }.each do |day, day_tests|
327
- day_tests.each do |day_test|
328
- assert_equal "0 0 * * #{day}", parse_time(day_test)
329
- end
330
- end
331
- end
332
-
333
- should "allow additional directives" do
334
- assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
335
- assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
336
- assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
337
- end
338
-
339
- should "parse weekday correctly" do
340
- assert_equal '0 0 * * 1-5', parse_time('weekday')
341
- assert_equal '0 0 * * 1-5', parse_time('Weekdays')
342
- assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
343
- assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
344
- end
345
-
346
- should "parse weekend correctly" do
347
- assert_equal '0 0 * * 6,0', parse_time('weekend')
348
- assert_equal '0 0 * * 6,0', parse_time('Weekends')
349
- assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
350
- assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
351
- end
352
- end
353
-
354
- class CronParseShortcutsTest < Whenever::TestCase
355
- should "parse a :symbol into the correct shortcut" do
356
- assert_equal '@reboot', parse_time(:reboot)
357
- assert_equal '@annually', parse_time(:annually)
358
- assert_equal '@yearly', parse_time(:yearly)
359
- assert_equal '@daily', parse_time(:daily)
360
- assert_equal '@midnight', parse_time(:midnight)
361
- assert_equal '@monthly', parse_time(:monthly)
362
- assert_equal '@weekly', parse_time(:weekly)
363
- assert_equal '@hourly', parse_time(:hourly)
364
- end
365
-
366
- should "convert time-based shortcuts to times" do
367
- assert_equal '0 0 1 * *', parse_time(:month)
368
- assert_equal '0 0 * * *', parse_time(:day)
369
- assert_equal '0 * * * *', parse_time(:hour)
370
- assert_equal '0 0 1 1 *', parse_time(:year)
371
- assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
372
- end
373
-
374
- should "raise an exception if a valid shortcut is given but also an :at" do
375
- assert_raises ArgumentError do
376
- parse_time(:hourly, nil, "1:00 am")
377
- end
378
-
379
- assert_raises ArgumentError do
380
- parse_time(:reboot, nil, 5)
381
- end
382
-
383
- assert_raises ArgumentError do
384
- parse_time(:daily, nil, '4:20pm')
385
- end
386
- end
387
- end
388
-
389
- class CronParseRubyTimeTest < Whenever::TestCase
390
- should "process things like `1.day` correctly" do
391
- assert_equal "0 0 * * *", parse_time(1.day)
392
- end
393
- end
394
-
395
- class CronParseRawTest < Whenever::TestCase
396
- should "raise if cron-syntax string is too long" do
397
- assert_raises ArgumentError do
398
- parse_time('* * * * * *')
399
- end
400
- end
401
-
402
- should "raise if cron-syntax string is invalid" do
403
- assert_raises ArgumentError do
404
- parse_time('** * * * *')
405
- end
406
- end
407
-
408
- should "return the same cron sytax" do
409
- crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *', '*/5 6-23 * * *',
410
- "*\t*\t*\t*\t*",
411
- '7 17 * * FRI', '7 17 * * Mon-Fri', '30 12 * Jun *', '30 12 * Jun-Aug *',
412
- '@reboot', '@yearly', '@annually', '@monthly', '@weekly',
413
- '@daily', '@midnight', '@hourly']
414
- crons.each do |cron|
415
- assert_equal cron, parse_time(cron)
416
- end
417
- end
418
- end