talentbox-whenever 0.7.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,18 @@
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
+ require 'whenever'
4
+
5
+ require 'shoulda'
6
+ require 'mocha'
7
+
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
+
14
+ end
15
+
16
+ class Test::Unit::TestCase
17
+ include TestExtensions
18
+ end
@@ -0,0 +1,252 @@
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 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
128
+
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
136
+
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
143
+ end
144
+
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
159
+ end
160
+ end
161
+
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
167
+
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
174
+
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
181
+ end
182
+
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
194
+
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
+ end
202
+
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
215
+ end
216
+ end
217
+
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
+ '@reboot', '@yearly', '@annually', '@monthly', '@weekly',
222
+ '@daily', '@midnight', '@hourly']
223
+ crons.each do |cron|
224
+ assert_equal cron, parse_time(cron)
225
+ end
226
+ end
227
+ end
228
+
229
+ private
230
+
231
+ def assert_days_and_hours_and_minutes_equals(expected, time)
232
+ cron = parse_time(2.months, 'some task', time)
233
+ minutes, hours, days, *garbage = cron.split(' ')
234
+ assert_equal expected, [days, hours, minutes]
235
+ end
236
+
237
+ def assert_hours_and_minutes_equals(expected, time)
238
+ cron = parse_time(2.days, 'some task', time)
239
+ minutes, hours, *garbage = cron.split(' ')
240
+ assert_equal expected, [hours, minutes]
241
+ end
242
+
243
+ def assert_minutes_equals(expected, time)
244
+ cron = parse_time(2.hours, 'some task', time)
245
+ assert_equal expected, cron.split(' ')[0]
246
+ end
247
+
248
+ def parse_time(time = nil, task = nil, at = nil)
249
+ Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
250
+ end
251
+
252
+ end
@@ -0,0 +1,107 @@
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
+
24
+ should "escape the :path" do
25
+ assert_equal '/my/spacey\ path', new_job(:template => ':path', :path => '/my/spacey path').output
26
+ end
27
+
28
+ should "escape percent signs" do
29
+ job = new_job(
30
+ :template => "before :foo after",
31
+ :foo => "percent -> % <- percent"
32
+ )
33
+ assert_equal %q(before percent -> \% <- percent after), job.output
34
+ end
35
+
36
+ should "assume percent signs are not already escaped" do
37
+ job = new_job(
38
+ :template => "before :foo after",
39
+ :foo => %q(percent preceded by a backslash -> \% <-)
40
+ )
41
+ assert_equal %q(before percent preceded by a backslash -> \\\% <- after), job.output
42
+ end
43
+
44
+ should "reject newlines" do
45
+ job = new_job(
46
+ :template => "before :foo after",
47
+ :foo => "newline -> \n <- newline"
48
+ )
49
+ assert_raise ArgumentError do
50
+ job.output
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ context "A Job with quotes" do
57
+ should "output the :task if it's in single quotes" do
58
+ job = new_job(:template => "':task'", :task => 'abc123')
59
+ assert_equal %q('abc123'), job.output
60
+ end
61
+
62
+ should "output the :task if it's in double quotes" do
63
+ job = new_job(:template => '":task"', :task => 'abc123')
64
+ assert_equal %q("abc123"), job.output
65
+ end
66
+
67
+ should "output escaped single quotes in when it's wrapped in them" do
68
+ job = new_job(
69
+ :template => "before ':foo' after",
70
+ :foo => "quote -> ' <- quote"
71
+ )
72
+ assert_equal %q(before 'quote -> '\'' <- quote' after), job.output
73
+ end
74
+
75
+ should "output escaped double quotes when it's wrapped in them" do
76
+ job = new_job(
77
+ :template => 'before ":foo" after',
78
+ :foo => 'quote -> " <- quote'
79
+ )
80
+ assert_equal %q(before "quote -> \" <- quote" after), job.output
81
+ end
82
+ end
83
+
84
+ context "A Job with a job_template" do
85
+ should "use the job template" do
86
+ job = new_job(:template => ':task', :task => 'abc123', :job_template => 'left :job right')
87
+ assert_equal 'left abc123 right', job.output
88
+ end
89
+
90
+ should "escape single quotes" do
91
+ job = new_job(:template => "before ':task' after", :task => "quote -> ' <- quote", :job_template => "left ':job' right")
92
+ assert_equal %q(left 'before '\''quote -> '\\''\\'\\'''\\'' <- quote'\'' after' right), job.output
93
+ end
94
+
95
+ should "escape double quotes" do
96
+ job = new_job(:template => 'before ":task" after', :task => 'quote -> " <- quote', :job_template => 'left ":job" right')
97
+ assert_equal %q(left "before \"quote -> \\\" <- quote\" after" right), job.output
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def new_job(options={})
104
+ Whenever::Job.new(options)
105
+ end
106
+
107
+ end
data/whenever.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "whenever/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "talentbox-whenever"
7
+ s.version = Whenever::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Javan Makhmali"]
10
+ s.email = ["javan@javan.us"]
11
+ s.homepage = ""
12
+ s.summary = %q{Cron jobs in ruby.}
13
+ s.description = %q{Clean ruby syntax for writing and deploying cron jobs.}
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- test/{functional,unit}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "chronic", "~> 0.6.3"
20
+ s.add_dependency "activesupport", ">= 2.3.2"
21
+
22
+ s.add_development_dependency "shoulda", ">= 2.1.1"
23
+ s.add_development_dependency "mocha", ">= 0.9.5"
24
+ s.add_development_dependency "rake"
25
+
26
+ # I'm not sure why this isn't installed along with activesupport,
27
+ # but for whatever reason running `bundle install` doesn't install
28
+ # i18n so I'm adding it here for now.
29
+ # https://github.com/rails/rails/blob/master/activesupport/activesupport.gemspec#L19 ?
30
+ s.add_development_dependency "i18n"
31
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talentbox-whenever
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Javan Makhmali
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chronic
16
+ requirement: &2165732340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2165732340
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &2165731420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2165731420
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &2165730920 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.1.1
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2165730920
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: &2165730420 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.5
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2165730420
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &2165730000 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2165730000
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ requirement: &2165729520 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2165729520
80
+ description: Clean ruby syntax for writing and deploying cron jobs.
81
+ email:
82
+ - javan@javan.us
83
+ executables:
84
+ - whenever
85
+ - wheneverize
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - .gitignore
90
+ - .travis.yml
91
+ - CHANGELOG.md
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - bin/whenever
97
+ - bin/wheneverize
98
+ - ci/Gemfile-AS-2_3_2
99
+ - ci/Gemfile-AS-2_3_4
100
+ - ci/Gemfile-AS-3_0_0
101
+ - ci/Gemfile-AS-3_1_0
102
+ - ci/Gemfile-AS-3_2_0
103
+ - lib/whenever.rb
104
+ - lib/whenever/capistrano.rb
105
+ - lib/whenever/command_line.rb
106
+ - lib/whenever/cron.rb
107
+ - lib/whenever/job.rb
108
+ - lib/whenever/job_list.rb
109
+ - lib/whenever/output_redirection.rb
110
+ - lib/whenever/setup.rb
111
+ - lib/whenever/version.rb
112
+ - test/functional/command_line_test.rb
113
+ - test/functional/output_at_test.rb
114
+ - test/functional/output_default_defined_jobs_test.rb
115
+ - test/functional/output_defined_job_test.rb
116
+ - test/functional/output_env_test.rb
117
+ - test/functional/output_redirection_test.rb
118
+ - test/test_helper.rb
119
+ - test/unit/cron_test.rb
120
+ - test/unit/job_test.rb
121
+ - whenever.gemspec
122
+ homepage: ''
123
+ licenses: []
124
+ post_install_message:
125
+ rdoc_options: []
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
+ version: '0'
134
+ segments:
135
+ - 0
136
+ hash: -968715254992877630
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: -968715254992877630
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.15
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Cron jobs in ruby.
152
+ test_files:
153
+ - test/functional/command_line_test.rb
154
+ - test/functional/output_at_test.rb
155
+ - test/functional/output_default_defined_jobs_test.rb
156
+ - test/functional/output_defined_job_test.rb
157
+ - test/functional/output_env_test.rb
158
+ - test/functional/output_redirection_test.rb
159
+ - test/unit/cron_test.rb
160
+ - test/unit/job_test.rb