whenever 0.9.2 → 0.9.3

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.
@@ -0,0 +1,39 @@
1
+ module Whenever
2
+ begin
3
+ require 'minitest/autorun'
4
+ begin
5
+ # 2.0.0
6
+ class TestCase < MiniTest::Test; end
7
+ rescue NameError
8
+ # 1.9.3
9
+ class TestCase < MiniTest::Unit::TestCase; end
10
+ end
11
+ rescue LoadError
12
+ # 1.8.7
13
+ require 'test/unit'
14
+ class TestCase < Test::Unit::TestCase
15
+ def default_test; end
16
+ end
17
+ end
18
+
19
+ class TestCase
20
+ class << self
21
+ def setup(&block)
22
+ define_method(:setup) do
23
+ super()
24
+ instance_eval(&block)
25
+ end
26
+ end
27
+
28
+ def test(name, &block)
29
+ define_method("test_#{name}".to_sym, &block)
30
+ end
31
+ alias should test
32
+ end
33
+
34
+ def assert_no_match(regexp, string)
35
+ message = "<#{regexp}> expected to not match\n<#{string}>"
36
+ assert regexp !~ string, message
37
+ end
38
+ end
39
+ end
@@ -1,18 +1,37 @@
1
- # Want to test the files here, in lib, not in an installed version of the gem.
2
- $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
3
1
  require 'whenever'
4
- require 'test/unit'
5
- require 'shoulda'
6
- require 'mocha'
2
+ require 'test_case'
3
+ require 'mocha/setup'
7
4
 
8
- module TestExtensions
9
-
10
- def two_hours
11
- "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
12
- end
13
-
5
+ module Whenever::TestHelpers
6
+ protected
7
+ def new_job(options={})
8
+ Whenever::Job.new(options)
9
+ end
10
+
11
+ def parse_time(time = nil, task = nil, at = nil)
12
+ Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
13
+ end
14
+
15
+ def two_hours
16
+ "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
17
+ end
18
+
19
+ def assert_days_and_hours_and_minutes_equals(expected, time)
20
+ cron = parse_time(Whenever.seconds(2, :months), 'some task', time)
21
+ minutes, hours, days, *garbage = cron.split(' ')
22
+ assert_equal expected, [days, hours, minutes]
23
+ end
24
+
25
+ def assert_hours_and_minutes_equals(expected, time)
26
+ cron = parse_time(Whenever.seconds(2, :days), 'some task', time)
27
+ minutes, hours, *garbage = cron.split(' ')
28
+ assert_equal expected, [hours, minutes]
29
+ end
30
+
31
+ def assert_minutes_equals(expected, time)
32
+ cron = parse_time(Whenever.seconds(2, :hours), 'some task', time)
33
+ assert_equal expected, cron.split(' ')[0]
34
+ end
14
35
  end
15
36
 
16
- class Test::Unit::TestCase
17
- include TestExtensions
18
- end
37
+ Whenever::TestCase.send(:include, Whenever::TestHelpers)
@@ -1,169 +1,147 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require File.expand_path(File.dirname(__FILE__) + "/../../lib/whenever/capistrano/v2/support")
1
+ require 'test_helper'
2
+ require 'whenever/capistrano/v2/support'
3
3
 
4
4
  class CapistranoSupportTestSubject
5
5
  include Whenever::CapistranoSupport
6
6
  end
7
7
 
8
- class CapistranoSupportTest < Test::Unit::TestCase
9
- context "when using capistrano support module" do
10
- setup do
11
- @capistrano = CapistranoSupportTestSubject.new
12
- configuration = mock
13
- configuration.stubs(:load).yields(@capistrano)
14
- Whenever::CapistranoSupport.load_into(configuration)
15
- end
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
16
 
17
- context "#whenever_options" do
18
- should "return fetch(:whenever_options)" do
19
- @capistrano.expects(:fetch).with(:whenever_options)
20
- @capistrano.whenever_options
21
- end
22
- end
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
23
22
 
24
- context "#whenever_roles with one role" do
25
- setup do
26
- @capistrano.stubs(:whenever_options).returns({:roles => :role1})
27
- end
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
28
27
 
29
- should "return whenever_options[:roles] as an array" do
30
- assert_equal [:role1], @capistrano.whenever_roles
31
- end
32
- end
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
33
32
 
34
- context "#whenever_roles with no defined roles" do
35
- setup do
36
- @capistrano.stubs(:whenever_options).returns({})
37
- end
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])
38
36
 
39
- should "return an empty array" do
40
- assert_equal [], @capistrano.whenever_roles
41
- end
42
- end
37
+ assert_equal [:server1, :server2], @capistrano.whenever_servers
38
+ end
43
39
 
44
- context "#whenever_servers" do
45
- should "return the list of servers returned by find_servers" do
46
- @capistrano.stubs(:whenever_options).returns({})
47
- @capistrano.stubs(:find_servers).returns([:server1, :server2])
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
48
45
 
49
- assert_equal [:server1, :server2], @capistrano.whenever_servers
50
- end
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 => {})
51
57
  end
58
+ end
52
59
 
53
- context "#whenever_server_roles" do
54
- setup do
55
- @mock_servers = ["foo", "bar"]
56
- @capistrano.stubs(:whenever_servers).returns(@mock_servers)
57
- end
58
-
59
- should "return a map of servers to their role(s)" do
60
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
61
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1])
62
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
63
- assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
64
- end
65
-
66
- should "exclude non-requested roles" do
67
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
68
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
69
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
70
- assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
71
- end
72
-
73
- should "include all roles for servers w/ >1 when they're requested" do
74
- @capistrano.stubs(:whenever_roles).returns([:role1, :role2, :role3])
75
- @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
76
- @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
77
- assert_equal({"foo" => [:role1, :role3], "bar" => [:role2]}, @capistrano.whenever_server_roles)
78
- end
60
+ should "#whenever_run_commands: require :path arg" do
61
+ assert_raises ArgumentError do
62
+ @capistrano.whenever_run_commands(:options => {}, :command => {}, :flags => {})
79
63
  end
64
+ end
80
65
 
81
- context "#whenever_prepare_for_rollback" do
82
- should "set path to previous_release if there is a previous release" do
83
- args = {}
84
- @capistrano.stubs(:fetch).with(:previous_release).returns("/some/path/20121221010000")
85
- assert_equal({:path => "/some/path/20121221010000"}, @capistrano.whenever_prepare_for_rollback(args))
86
- end
87
-
88
- should "set path to release_path and flags to whenever_clear_flags if there is no previous release" do
89
- args = {}
90
- @capistrano.stubs(:fetch).with(:previous_release).returns(nil)
91
- @capistrano.stubs(:fetch).with(:release_path).returns("/some/path/20121221010000")
92
- @capistrano.stubs(:fetch).with(:whenever_clear_flags).returns("--clear-crontab whenever_identifier")
93
- assert_equal({:path => "/some/path/20121221010000", :flags => "--clear-crontab whenever_identifier"}, @capistrano.whenever_prepare_for_rollback(args))
94
- end
66
+ should "#whenever_run_commands: require :flags arg" do
67
+ assert_raises ArgumentError do
68
+ @capistrano.whenever_run_commands(:options => {}, :path => {}, :command => {})
95
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)
96
77
 
97
- context "#whenever_run_commands" do
98
- should "require :command arg" do
99
- assert_raise ArgumentError do
100
- @capistrano.whenever_run_commands(:options => {}, :path => {}, :flags => {})
101
- end
102
- end
103
-
104
- should "require :path arg" do
105
- assert_raise ArgumentError do
106
- @capistrano.whenever_run_commands(:options => {}, :command => {}, :flags => {})
107
- end
108
- end
109
-
110
- should "require :flags arg" do
111
- assert_raise ArgumentError do
112
- @capistrano.whenever_run_commands(:options => {}, :path => {}, :command => {})
113
- end
114
- end
115
-
116
- context "with some servers defined" do
117
- setup do
118
- @mock_server1, @mock_server2, @mock_server3 = mock("Server1"), mock("Server2"), mock("Server3")
119
- @mock_server1.stubs(:host).returns("server1.foo.com")
120
- @mock_server2.stubs(:host).returns("server2.foo.com")
121
- @mock_server3.stubs(:host => "server3.foo.com", :port => 1022, :user => 'test')
122
- @mock_servers = [@mock_server1, @mock_server2]
123
- end
124
-
125
- should "call run for each host w/ appropriate role args" do
126
- @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1])
127
- @capistrano.stubs(:role_names_for_host).with(@mock_server2).returns([:role2])
128
- @capistrano.stubs(:whenever_servers).returns(@mock_servers)
129
- roles = [:role1, :role2]
130
- @capistrano.stubs(:whenever_options).returns({:roles => roles})
131
-
132
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1', {:roles => roles, :hosts => @mock_server1})
133
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role2', {:roles => roles, :hosts => @mock_server2})
134
-
135
- @capistrano.whenever_run_commands(:command => "whenever",
136
- :path => "/foo/bar",
137
- :flags => "--flag1 --flag2")
138
- end
139
-
140
- should "call run w/ all role args for servers w/ >1 role" do
141
- @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1, :role3])
142
- @capistrano.stubs(:whenever_servers).returns([@mock_server1])
143
- roles = [:role1, :role2, :role3]
144
- @capistrano.stubs(:whenever_options).returns({:roles => roles})
145
-
146
- @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1,role3', {:roles => roles, :hosts => @mock_server1})
147
-
148
- @capistrano.whenever_run_commands(:command => "whenever",
149
- :path => "/foo/bar",
150
- :flags => "--flag1 --flag2")
151
- end
152
-
153
- should "call run w/ proper server options (port, user)" do
154
- @capistrano.stubs(:role_names_for_host).with(@mock_server3).returns([:role3])
155
- @capistrano.stubs(:whenever_servers).returns([@mock_server3])
156
- @capistrano.stubs(:whenever_options).returns({:roles => [:role3]})
157
-
158
- @capistrano.expects(:run).once.with do |command, options|
159
- options[:hosts].user == "test" && options[:hosts].port == 1022
160
- end
161
-
162
- @capistrano.whenever_run_commands(:command => "whenever",
163
- :path => "/foo/bar",
164
- :flags => "--flag1 --flag2")
165
- end
166
- end
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
167
141
  end
142
+
143
+ @capistrano.whenever_run_commands(:command => "whenever",
144
+ :path => "/foo/bar",
145
+ :flags => "--flag1 --flag2")
168
146
  end
169
147
  end
@@ -1,253 +1,226 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
1
+ require 'test_helper'
2
2
 
3
- class CronTest < Test::Unit::TestCase
4
-
5
- context "When parsing time in minutes" do
6
- should "raise if less than 1 minute" do
7
- assert_raises ArgumentError do
8
- parse_time(59.seconds)
9
- end
10
-
11
- assert_raises ArgumentError do
12
- parse_time(0.minutes)
13
- end
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))
14
7
  end
15
8
 
16
- # For santity, do some tests on straight String
17
- should "parse correctly" do
18
- assert_equal '* * * * *', parse_time(1.minute)
19
- assert_equal '0,5,10,15,20,25,30,35,40,45,50,55 * * * *', parse_time(5.minutes)
20
- assert_equal '7,14,21,28,35,42,49,56 * * * *', parse_time(7.minutes)
21
- assert_equal '0,30 * * * *', parse_time(30.minutes)
22
- assert_equal '32 * * * *', parse_time(32.minutes)
23
- assert_not_equal '60 * * * *', parse_time(60.minutes) # 60 minutes bumps up into the hour range
24
- end
25
-
26
- # Test all minutes
27
- (2..59).each do |num|
28
- should "parse correctly for #{num} minutes" do
29
- start = 0
30
- start += num unless 60.modulo(num).zero?
31
- minutes = (start..59).step(num).to_a
32
-
33
- assert_equal "#{minutes.join(',')} * * * *", parse_time(num.minutes)
34
- end
9
+ assert_raises ArgumentError do
10
+ parse_time(Whenever.seconds(0, :minutes))
35
11
  end
36
12
  end
37
13
 
38
- context "When parsing time in hours" do
39
- should "parse correctly" do
40
- assert_equal '0 * * * *', parse_time(1.hour)
41
- assert_equal '0 0,2,4,6,8,10,12,14,16,18,20,22 * * *', parse_time(2.hours)
42
- assert_equal '0 0,3,6,9,12,15,18,21 * * *', parse_time(3.hours)
43
- assert_equal '0 5,10,15,20 * * *', parse_time(5.hours)
44
- assert_equal '0 17 * * *', parse_time(17.hours)
45
- assert_not_equal '0 24 * * *', parse_time(24.hours) # 24 hours bumps up into the day range
46
- end
14
+ # For santity, do some tests on straight String
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
47
23
 
48
- (2..23).each do |num|
49
- should "parse correctly for #{num} hours" do
50
- start = 0
51
- start += num unless 24.modulo(num).zero?
52
- hours = (start..23).step(num).to_a
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
53
30
 
54
- assert_equal "0 #{hours.join(',')} * * *", parse_time(num.hours)
55
- end
31
+ assert_equal "#{minutes.join(',')} * * * *", parse_time(Whenever.seconds(num, :minutes))
56
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
57
45
 
58
- should "parse correctly when given an 'at' with minutes as an Integer" do
59
- assert_minutes_equals "1", 1
60
- assert_minutes_equals "14", 14
61
- assert_minutes_equals "27", 27
62
- assert_minutes_equals "55", 55
63
- end
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
64
51
 
65
- should "parse correctly when given an 'at' with minutes as a Time" do
66
- # Basically just testing that Chronic parses some times and we get the minutes out of it
67
- assert_minutes_equals "1", '3:01am'
68
- assert_minutes_equals "1", 'January 21 2:01 PM'
69
- assert_minutes_equals "0", 'midnight'
70
- assert_minutes_equals "59", '13:59'
52
+ assert_equal "0 #{hours.join(',')} * * *", parse_time(Whenever.seconds(num, :hours))
71
53
  end
72
54
  end
73
55
 
74
- context "When parsing time in days (of month)" do
75
- should "parse correctly" do
76
- assert_equal '0 0 * * *', parse_time(1.days)
77
- assert_equal '0 0 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * *', parse_time(2.days)
78
- assert_equal '0 0 1,5,9,13,17,21,25,29 * *', parse_time(4.days)
79
- assert_equal '0 0 1,8,15,22 * *', parse_time(7.days)
80
- assert_equal '0 0 1,17 * *', parse_time(16.days)
81
- assert_equal '0 0 17 * *', parse_time(17.days)
82
- assert_equal '0 0 29 * *', parse_time(29.days)
83
- assert_not_equal '0 0 30 * *', parse_time(30.days) # 30 days bumps into the month range
84
- end
85
-
86
- should "parse correctly when given an 'at' with hours, minutes as a Time" do
87
- # first param is an array with [hours, minutes]
88
- assert_hours_and_minutes_equals %w(3 45), '3:45am'
89
- assert_hours_and_minutes_equals %w(20 1), '8:01pm'
90
- assert_hours_and_minutes_equals %w(0 0), 'midnight'
91
- assert_hours_and_minutes_equals %w(1 23), '1:23 AM'
92
- assert_hours_and_minutes_equals %w(23 59), 'March 21 11:59 pM'
93
- end
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
94
62
 
95
- should "parse correctly when given an 'at' with hours as an Integer" do
96
- # first param is an array with [hours, minutes]
97
- assert_hours_and_minutes_equals %w(1 0), 1
98
- assert_hours_and_minutes_equals %w(3 0), 3
99
- assert_hours_and_minutes_equals %w(15 0), 15
100
- assert_hours_and_minutes_equals %w(19 0), 19
101
- assert_hours_and_minutes_equals %w(23 0), 23
102
- end
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
+ end
71
+
72
+ class CronParseDaysTest < Whenever::TestCase
73
+ should "parse correctly" do
74
+ assert_equal '0 0 * * *', parse_time(Whenever.seconds(1, :days))
75
+ 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))
76
+ assert_equal '0 0 1,5,9,13,17,21,25,29 * *', parse_time(Whenever.seconds(4, :days))
77
+ assert_equal '0 0 1,8,15,22 * *', parse_time(Whenever.seconds(7, :days))
78
+ assert_equal '0 0 1,17 * *', parse_time(Whenever.seconds(16, :days))
79
+ assert_equal '0 0 17 * *', parse_time(Whenever.seconds(17, :days))
80
+ assert_equal '0 0 29 * *', parse_time(Whenever.seconds(29, :days))
81
+ assert '0 0 30 * *' != parse_time(Whenever.seconds(30, :days)) # 30 days bumps into the month range
103
82
  end
104
83
 
105
- context "When parsing time in months" do
106
- should "parse correctly" do
107
- assert_equal '0 0 1 * *', parse_time(1.month)
108
- assert_equal '0 0 1 1,3,5,7,9,11 *', parse_time(2.months)
109
- assert_equal '0 0 1 1,4,7,10 *', parse_time(3.months)
110
- assert_equal '0 0 1 1,5,9 *', parse_time(4.months)
111
- assert_equal '0 0 1 1,6 *', parse_time(5.months)
112
- assert_equal '0 0 1 7 *', parse_time(7.months)
113
- assert_equal '0 0 1 8 *', parse_time(8.months)
114
- assert_equal '0 0 1 9 *', parse_time(9.months)
115
- assert_equal '0 0 1 10 *', parse_time(10.months)
116
- assert_equal '0 0 1 11 *', parse_time(11.months)
117
- assert_equal '0 0 1 12 *', parse_time(12.months)
118
- end
84
+ should "parse correctly when given an 'at' with hours, minutes as a Time" do
85
+ # first param is an array with [hours, minutes]
86
+ assert_hours_and_minutes_equals %w(3 45), '3:45am'
87
+ assert_hours_and_minutes_equals %w(20 1), '8:01pm'
88
+ assert_hours_and_minutes_equals %w(0 0), 'midnight'
89
+ assert_hours_and_minutes_equals %w(1 23), '1:23 AM'
90
+ assert_hours_and_minutes_equals %w(23 59), 'March 21 11:59 pM'
91
+ end
119
92
 
120
- should "parse months with a date and/or time" do
121
- # should set the day to 1 if no date is given
122
- assert_equal '0 17 1 * *', parse_time(1.month, nil, "5pm")
123
- # should use the date if one is given
124
- assert_equal '0 2 23 * *', parse_time(1.month, nil, "February 23rd at 2am")
125
- # should use an iteger as the day
126
- assert_equal '0 0 5 * *', parse_time(1.month, nil, 5)
127
- end
93
+ should "parse correctly when given an 'at' with hours as an Integer" do
94
+ # first param is an array with [hours, minutes]
95
+ assert_hours_and_minutes_equals %w(1 0), 1
96
+ assert_hours_and_minutes_equals %w(3 0), 3
97
+ assert_hours_and_minutes_equals %w(15 0), 15
98
+ assert_hours_and_minutes_equals %w(19 0), 19
99
+ assert_hours_and_minutes_equals %w(23 0), 23
100
+ end
101
+ end
102
+
103
+ class CronParseMonthsTest < Whenever::TestCase
104
+ should "parse correctly" do
105
+ assert_equal '0 0 1 * *', parse_time(Whenever.seconds(1, :month))
106
+ assert_equal '0 0 1 1,3,5,7,9,11 *', parse_time(Whenever.seconds(2, :months))
107
+ assert_equal '0 0 1 1,4,7,10 *', parse_time(Whenever.seconds(3, :months))
108
+ assert_equal '0 0 1 1,5,9 *', parse_time(Whenever.seconds(4, :months))
109
+ assert_equal '0 0 1 1,6 *', parse_time(Whenever.seconds(5, :months))
110
+ assert_equal '0 0 1 7 *', parse_time(Whenever.seconds(7, :months))
111
+ assert_equal '0 0 1 8 *', parse_time(Whenever.seconds(8, :months))
112
+ assert_equal '0 0 1 9 *', parse_time(Whenever.seconds(9, :months))
113
+ assert_equal '0 0 1 10 *', parse_time(Whenever.seconds(10, :months))
114
+ assert_equal '0 0 1 11 *', parse_time(Whenever.seconds(11, :months))
115
+ assert_equal '0 0 1 12 *', parse_time(Whenever.seconds(12, :months))
116
+ end
128
117
 
129
- should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
130
- # first param is an array with [days, hours, minutes]
131
- assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
132
- assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
133
- assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
134
- assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
135
- end
118
+ should "parse months with a date and/or time" do
119
+ # should set the day to 1 if no date is given
120
+ assert_equal '0 17 1 * *', parse_time(Whenever.seconds(1, :month), nil, "5pm")
121
+ # should use the date if one is given
122
+ assert_equal '0 2 23 * *', parse_time(Whenever.seconds(1, :month), nil, "February 23rd at 2am")
123
+ # should use an iteger as the day
124
+ assert_equal '0 0 5 * *', parse_time(Whenever.seconds(1, :month), nil, 5)
125
+ end
136
126
 
137
- should "parse correctly when given an 'at' with days as an Integer" do
138
- # first param is an array with [days, hours, minutes]
139
- assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
140
- assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
141
- assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
142
- end
127
+ should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
128
+ # first param is an array with [days, hours, minutes]
129
+ assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
130
+ assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
131
+ assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
132
+ assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
143
133
  end
144
134
 
145
- context "When parsing time in days (of week)" do
146
- should "parse days of the week correctly" do
147
- {
148
- '0' => %w(sun Sunday SUNDAY SUN),
149
- '1' => %w(mon Monday MONDAY MON),
150
- '2' => %w(tue tues Tuesday TUESDAY TUE),
151
- '3' => %w(wed Wednesday WEDNESDAY WED),
152
- '4' => %w(thu thurs thur Thursday THURSDAY THU),
153
- '5' => %w(fri Friday FRIDAY FRI),
154
- '6' => %w(sat Saturday SATURDAY SAT)
155
- }.each do |day, day_tests|
156
- day_tests.each do |day_test|
157
- assert_equal "0 0 * * #{day}", parse_time(day_test)
158
- end
135
+ should "parse correctly when given an 'at' with days as an Integer" do
136
+ # first param is an array with [days, hours, minutes]
137
+ assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
138
+ assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
139
+ assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
140
+ end
141
+ end
142
+
143
+ class CronParseDaysOfWeekTest < Whenever::TestCase
144
+ should "parse days of the week correctly" do
145
+ {
146
+ '0' => %w(sun Sunday SUNDAY SUN),
147
+ '1' => %w(mon Monday MONDAY MON),
148
+ '2' => %w(tue tues Tuesday TUESDAY TUE),
149
+ '3' => %w(wed Wednesday WEDNESDAY WED),
150
+ '4' => %w(thu thurs thur Thursday THURSDAY THU),
151
+ '5' => %w(fri Friday FRIDAY FRI),
152
+ '6' => %w(sat Saturday SATURDAY SAT)
153
+ }.each do |day, day_tests|
154
+ day_tests.each do |day_test|
155
+ assert_equal "0 0 * * #{day}", parse_time(day_test)
159
156
  end
160
157
  end
158
+ end
161
159
 
162
- should "allow additional directives" do
163
- assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
164
- assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
165
- assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
166
- end
160
+ should "allow additional directives" do
161
+ assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
162
+ assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
163
+ assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
164
+ end
167
165
 
168
- should "parse weekday correctly" do
169
- assert_equal '0 0 * * 1-5', parse_time('weekday')
170
- assert_equal '0 0 * * 1-5', parse_time('Weekdays')
171
- assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
172
- assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
173
- end
166
+ should "parse weekday correctly" do
167
+ assert_equal '0 0 * * 1-5', parse_time('weekday')
168
+ assert_equal '0 0 * * 1-5', parse_time('Weekdays')
169
+ assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
170
+ assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
171
+ end
174
172
 
175
- should "parse weekend correctly" do
176
- assert_equal '0 0 * * 6,0', parse_time('weekend')
177
- assert_equal '0 0 * * 6,0', parse_time('Weekends')
178
- assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
179
- assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
180
- end
173
+ should "parse weekend correctly" do
174
+ assert_equal '0 0 * * 6,0', parse_time('weekend')
175
+ assert_equal '0 0 * * 6,0', parse_time('Weekends')
176
+ assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
177
+ assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
178
+ end
179
+ end
180
+
181
+ class CronParseShortcutsTest < Whenever::TestCase
182
+ should "parse a :symbol into the correct shortcut" do
183
+ assert_equal '@reboot', parse_time(:reboot)
184
+ assert_equal '@annually', parse_time(:annually)
185
+ assert_equal '@yearly', parse_time(:yearly)
186
+ assert_equal '@daily', parse_time(:daily)
187
+ assert_equal '@midnight', parse_time(:midnight)
188
+ assert_equal '@monthly', parse_time(:monthly)
189
+ assert_equal '@weekly', parse_time(:weekly)
190
+ assert_equal '@hourly', parse_time(:hourly)
181
191
  end
182
192
 
183
- context "When parsing time using the cron shortcuts" do
184
- should "parse a :symbol into the correct shortcut" do
185
- assert_equal '@reboot', parse_time(:reboot)
186
- assert_equal '@annually', parse_time(:annually)
187
- assert_equal '@yearly', parse_time(:yearly)
188
- assert_equal '@daily', parse_time(:daily)
189
- assert_equal '@midnight', parse_time(:midnight)
190
- assert_equal '@monthly', parse_time(:monthly)
191
- assert_equal '@weekly', parse_time(:weekly)
192
- assert_equal '@hourly', parse_time(:hourly)
193
- end
193
+ should "convert time-based shortcuts to times" do
194
+ assert_equal '0 0 1 * *', parse_time(:month)
195
+ assert_equal '0 0 * * *', parse_time(:day)
196
+ assert_equal '0 * * * *', parse_time(:hour)
197
+ assert_equal '0 0 1 12 *', parse_time(:year)
198
+ assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
199
+ end
194
200
 
195
- should "convert time-based shortcuts to times" do
196
- assert_equal '0 0 1 * *', parse_time(:month)
197
- assert_equal '0 0 * * *', parse_time(:day)
198
- assert_equal '0 * * * *', parse_time(:hour)
199
- assert_equal '0 0 1 12 *', parse_time(:year)
200
- assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
201
+ should "raise an exception if a valid shortcut is given but also an :at" do
202
+ assert_raises ArgumentError do
203
+ parse_time(:hourly, nil, "1:00 am")
201
204
  end
202
205
 
203
- should "raise an exception if a valid shortcut is given but also an :at" do
204
- assert_raises ArgumentError do
205
- parse_time(:hourly, nil, "1:00 am")
206
- end
207
-
208
- assert_raises ArgumentError do
209
- parse_time(:reboot, nil, 5)
210
- end
211
-
212
- assert_raises ArgumentError do
213
- parse_time(:daily, nil, '4:20pm')
214
- end
206
+ assert_raises ArgumentError do
207
+ parse_time(:reboot, nil, 5)
215
208
  end
216
- end
217
209
 
218
- context "When given raw cron sytax" do
219
- should "return the same cron sytax" do
220
- crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *',
221
- "*\t*\t*\t*\t*",
222
- '@reboot', '@yearly', '@annually', '@monthly', '@weekly',
223
- '@daily', '@midnight', '@hourly']
224
- crons.each do |cron|
225
- assert_equal cron, parse_time(cron)
226
- end
210
+ assert_raises ArgumentError do
211
+ parse_time(:daily, nil, '4:20pm')
227
212
  end
228
213
  end
214
+ end
229
215
 
230
- private
231
-
232
- def assert_days_and_hours_and_minutes_equals(expected, time)
233
- cron = parse_time(2.months, 'some task', time)
234
- minutes, hours, days, *garbage = cron.split(' ')
235
- assert_equal expected, [days, hours, minutes]
236
- end
237
-
238
- def assert_hours_and_minutes_equals(expected, time)
239
- cron = parse_time(2.days, 'some task', time)
240
- minutes, hours, *garbage = cron.split(' ')
241
- assert_equal expected, [hours, minutes]
242
- end
243
-
244
- def assert_minutes_equals(expected, time)
245
- cron = parse_time(2.hours, 'some task', time)
246
- assert_equal expected, cron.split(' ')[0]
247
- end
248
-
249
- def parse_time(time = nil, task = nil, at = nil)
250
- Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
216
+ class CronParseRawTest < Whenever::TestCase
217
+ should "return the same cron sytax" do
218
+ crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *',
219
+ "*\t*\t*\t*\t*",
220
+ '@reboot', '@yearly', '@annually', '@monthly', '@weekly',
221
+ '@daily', '@midnight', '@hourly']
222
+ crons.each do |cron|
223
+ assert_equal cron, parse_time(cron)
224
+ end
251
225
  end
252
-
253
- end
226
+ end