seisuke-whenever 0.6.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,20 @@
1
+ require 'rubygems'
2
+
3
+ # Want to test the files here, in lib, not in an installed version of the gem.
4
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
5
+ require 'whenever'
6
+
7
+ require 'shoulda'
8
+ require 'mocha'
9
+
10
+ module TestExtensions
11
+
12
+ def two_hours
13
+ "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
14
+ end
15
+
16
+ end
17
+
18
+ class Test::Unit::TestCase
19
+ include TestExtensions
20
+ end
@@ -0,0 +1,226 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
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
14
+ end
15
+
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
35
+ end
36
+ end
37
+
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
47
+
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
53
+
54
+ assert_equal "0 #{hours.join(',')} * * *", parse_time(num.hours)
55
+ end
56
+ end
57
+
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
64
+
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'
71
+ end
72
+ end
73
+
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
94
+
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
103
+ end
104
+
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
119
+
120
+ should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
121
+ # first param is an array with [days, hours, minutes]
122
+ assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
123
+ assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
124
+ assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
125
+ assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
126
+ end
127
+
128
+ should "parse correctly when given an 'at' with days as an Integer" do
129
+ # first param is an array with [days, hours, minutes]
130
+ assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
131
+ assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
132
+ assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
133
+ end
134
+ end
135
+
136
+ context "When parsing time in days (of week)" do
137
+ should "parse days of the week correctly" do
138
+ {
139
+ '0' => %w(sun Sunday SUNDAY SUN),
140
+ '1' => %w(mon Monday MONDAY MON),
141
+ '2' => %w(tue tues Tuesday TUESDAY TUE),
142
+ '3' => %w(wed Wednesday WEDNESDAY WED),
143
+ '4' => %w(thu thurs thur Thursday THURSDAY THU),
144
+ '5' => %w(fri Friday FRIDAY FRI),
145
+ '6' => %w(sat Saturday SATURDAY SAT)
146
+ }.each do |day, day_tests|
147
+ day_tests.each do |day_test|
148
+ assert_equal "0 0 * * #{day}", parse_time(day_test)
149
+ end
150
+ end
151
+ end
152
+
153
+ should "allow additional directives" do
154
+ assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
155
+ assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
156
+ assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
157
+ end
158
+
159
+ should "parse weekday correctly" do
160
+ assert_equal '0 0 * * 1-5', parse_time('weekday')
161
+ assert_equal '0 0 * * 1-5', parse_time('Weekdays')
162
+ assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
163
+ assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
164
+ end
165
+
166
+ should "parse weekend correctly" do
167
+ assert_equal '0 0 * * 6,0', parse_time('weekend')
168
+ assert_equal '0 0 * * 6,0', parse_time('Weekends')
169
+ assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
170
+ assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
171
+ end
172
+ end
173
+
174
+ context "When parsing time using the cron shortcuts" do
175
+ should "parse a :symbol into the correct shortcut" do
176
+ assert_equal '@reboot', parse_time(:reboot)
177
+ assert_equal '@annually', parse_time(:year)
178
+ assert_equal '@annually', parse_time(:yearly)
179
+ assert_equal '@daily', parse_time(:day)
180
+ assert_equal '@daily', parse_time(:daily)
181
+ assert_equal '@midnight', parse_time(:midnight)
182
+ assert_equal '@monthly', parse_time(:month)
183
+ assert_equal '@monthly', parse_time(:monthly)
184
+ assert_equal '@hourly', parse_time(:hour)
185
+ assert_equal '@hourly', parse_time(:hourly)
186
+ end
187
+
188
+ should "raise an exception if a valid shortcut is given but also an :at" do
189
+ assert_raises ArgumentError do
190
+ parse_time(:hour, nil, "1:00 am")
191
+ end
192
+
193
+ assert_raises ArgumentError do
194
+ parse_time(:reboot, nil, 5)
195
+ end
196
+
197
+ assert_raises ArgumentError do
198
+ parse_time(:day, nil, '4:20pm')
199
+ end
200
+ end
201
+ end
202
+
203
+ private
204
+
205
+ def assert_days_and_hours_and_minutes_equals(expected, time)
206
+ cron = parse_time(2.months, 'some task', time)
207
+ minutes, hours, days, *garbage = cron.split(' ')
208
+ assert_equal expected, [days, hours, minutes]
209
+ end
210
+
211
+ def assert_hours_and_minutes_equals(expected, time)
212
+ cron = parse_time(2.days, 'some task', time)
213
+ minutes, hours, *garbage = cron.split(' ')
214
+ assert_equal expected, [hours, minutes]
215
+ end
216
+
217
+ def assert_minutes_equals(expected, time)
218
+ cron = parse_time(2.hours, 'some task', time)
219
+ assert_equal expected, cron.split(' ')[0]
220
+ end
221
+
222
+ def parse_time(time = nil, task = nil, at = nil)
223
+ Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
224
+ end
225
+
226
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class JobTest < Test::Unit::TestCase
4
+
5
+ context "A Job" do
6
+ should "return the :at set when #at is called" do
7
+ assert_equal 'foo', new_job(:at => 'foo').at
8
+ end
9
+
10
+ should "substitute the :task when #output is called" do
11
+ job = new_job(:template => ":task", :task => 'abc123')
12
+ assert_equal 'abc123', job.output
13
+ end
14
+
15
+ should "substitute the :path when #output is called" do
16
+ assert_equal 'foo', new_job(:template => ':path', :path => 'foo').output
17
+ end
18
+
19
+ should "substitute the :path with the default Whenever.path if none is provided when #output is called" do
20
+ Whenever.expects(:path).returns('/my/path')
21
+ assert_equal '/my/path', new_job(:template => ':path').output
22
+ end
23
+ end
24
+
25
+
26
+ context "A Job with quotes" do
27
+ should "output the :task if it's in single quotes" do
28
+ job = new_job(:template => "':task'", :task => 'abc123')
29
+ assert_equal %q('abc123'), job.output
30
+ end
31
+
32
+ should "output the :task if it's in double quotes" do
33
+ job = new_job(:template => '":task"', :task => 'abc123')
34
+ assert_equal %q("abc123"), job.output
35
+ end
36
+
37
+ should "output escaped single quotes in when it's wrapped in them" do
38
+ job = new_job(
39
+ :template => "before ':foo' after",
40
+ :foo => "quote -> ' <- quote"
41
+ )
42
+ assert_equal %q(before 'quote -> '\'' <- quote' after), job.output
43
+ end
44
+
45
+ should "output escaped double quotes when it's wrapped in them" do
46
+ job = new_job(
47
+ :template => 'before ":foo" after',
48
+ :foo => 'quote -> " <- quote'
49
+ )
50
+ assert_equal %q(before "quote -> \" <- quote" after), job.output
51
+ end
52
+ end
53
+
54
+ context "A Job with a job_template" do
55
+ should "use the job template" do
56
+ job = new_job(:template => ':task', :task => 'abc123', :job_template => 'left :job right')
57
+ assert_equal 'left abc123 right', job.output
58
+ end
59
+
60
+ should "escape single quotes" do
61
+ job = new_job(:template => "before ':task' after", :task => "quote -> ' <- quote", :job_template => "left ':job' right")
62
+ assert_equal %q(left 'before '\''quote -> '\\''\\'\\'''\\'' <- quote'\'' after' right), job.output
63
+ end
64
+
65
+ should "escape double quotes" do
66
+ job = new_job(:template => 'before ":task" after', :task => 'quote -> " <- quote', :job_template => 'left ":job" right')
67
+ assert_equal %q(left "before \"quote -> \\\" <- quote\" after" right), job.output
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def new_job(options={})
74
+ Whenever::Job.new(options)
75
+ end
76
+
77
+ end
data/whenever.gemspec ADDED
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{whenever}
8
+ s.version = "0.6.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Javan Makhmali"]
12
+ s.date = %q{2010-12-02}
13
+ s.description = %q{Clean ruby syntax for writing and deploying cron jobs.}
14
+ s.email = %q{javan@javan.us}
15
+ s.executables = ["wheneverize", "whenever"]
16
+ s.extra_rdoc_files = [
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "CHANGELOG.md",
21
+ "README.md",
22
+ "Rakefile",
23
+ "bin/whenever",
24
+ "bin/wheneverize",
25
+ "lib/whenever.rb",
26
+ "lib/whenever/capistrano.rb",
27
+ "lib/whenever/command_line.rb",
28
+ "lib/whenever/cron.rb",
29
+ "lib/whenever/job.rb",
30
+ "lib/whenever/job_list.rb",
31
+ "lib/whenever/output_redirection.rb",
32
+ "lib/whenever/setup.rb",
33
+ "lib/whenever/version.rb",
34
+ "test/functional/command_line_test.rb",
35
+ "test/functional/output_at_test.rb",
36
+ "test/functional/output_default_defined_jobs_test.rb",
37
+ "test/functional/output_defined_job_test.rb",
38
+ "test/functional/output_env_test.rb",
39
+ "test/functional/output_redirection_test.rb",
40
+ "test/test_helper.rb",
41
+ "test/unit/cron_test.rb",
42
+ "test/unit/job_test.rb",
43
+ "whenever.gemspec"
44
+ ]
45
+ s.homepage = %q{http://github.com/javan/whenever}
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.7}
48
+ s.summary = %q{Write your cron jobs in ruby.}
49
+ s.test_files = [
50
+ "test/functional/command_line_test.rb",
51
+ "test/functional/output_at_test.rb",
52
+ "test/functional/output_default_defined_jobs_test.rb",
53
+ "test/functional/output_defined_job_test.rb",
54
+ "test/functional/output_env_test.rb",
55
+ "test/functional/output_redirection_test.rb",
56
+ "test/test_helper.rb",
57
+ "test/unit/cron_test.rb",
58
+ "test/unit/job_test.rb"
59
+ ]
60
+
61
+ if s.respond_to? :specification_version then
62
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
+ s.specification_version = 3
64
+
65
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
66
+ s.add_runtime_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
67
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
68
+ s.add_development_dependency(%q<shoulda>, [">= 2.1.1"])
69
+ s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
70
+ else
71
+ s.add_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
72
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
73
+ s.add_dependency(%q<shoulda>, [">= 2.1.1"])
74
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
75
+ end
76
+ else
77
+ s.add_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
78
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
79
+ s.add_dependency(%q<shoulda>, [">= 2.1.1"])
80
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
81
+ end
82
+ end
83
+
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seisuke-whenever
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 3
10
+ version: 0.6.3
11
+ platform: ruby
12
+ authors:
13
+ - Javan Makhmali
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-02 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: aaronh-chronic
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 9
34
+ version: 0.3.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 4
50
+ version: 2.3.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: shoulda
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 9
62
+ segments:
63
+ - 2
64
+ - 1
65
+ - 1
66
+ version: 2.1.1
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 49
78
+ segments:
79
+ - 0
80
+ - 9
81
+ - 5
82
+ version: 0.9.5
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: Clean ruby syntax for writing and deploying cron jobs.
86
+ email: javan@javan.us
87
+ executables:
88
+ - wheneverize
89
+ - whenever
90
+ extensions: []
91
+
92
+ extra_rdoc_files:
93
+ - README.md
94
+ files:
95
+ - CHANGELOG.md
96
+ - README.md
97
+ - Rakefile
98
+ - bin/whenever
99
+ - bin/wheneverize
100
+ - lib/whenever.rb
101
+ - lib/whenever/capistrano.rb
102
+ - lib/whenever/command_line.rb
103
+ - lib/whenever/cron.rb
104
+ - lib/whenever/job.rb
105
+ - lib/whenever/job_list.rb
106
+ - lib/whenever/output_redirection.rb
107
+ - lib/whenever/setup.rb
108
+ - lib/whenever/version.rb
109
+ - test/functional/command_line_test.rb
110
+ - test/functional/output_at_test.rb
111
+ - test/functional/output_default_defined_jobs_test.rb
112
+ - test/functional/output_defined_job_test.rb
113
+ - test/functional/output_env_test.rb
114
+ - test/functional/output_redirection_test.rb
115
+ - test/test_helper.rb
116
+ - test/unit/cron_test.rb
117
+ - test/unit/job_test.rb
118
+ - whenever.gemspec
119
+ has_rdoc: true
120
+ homepage: http://github.com/javan/whenever
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Write your cron jobs in ruby.
153
+ test_files:
154
+ - test/functional/command_line_test.rb
155
+ - test/functional/output_at_test.rb
156
+ - test/functional/output_default_defined_jobs_test.rb
157
+ - test/functional/output_defined_job_test.rb
158
+ - test/functional/output_env_test.rb
159
+ - test/functional/output_redirection_test.rb
160
+ - test/test_helper.rb
161
+ - test/unit/cron_test.rb
162
+ - test/unit/job_test.rb