whenever 0.9.2 → 0.9.3

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