Fixed regex related bugs. Release v0.5.3

This commit is contained in:
Javan Makhmali 2010-09-24 13:41:05 -04:00
parent a3d2da903d
commit 9c96e7c16b
5 changed files with 59 additions and 13 deletions

View File

@ -1,3 +1,10 @@
== 0.5.3 / September 24th, 2010
* Better regexes for replacing Whenever blocks in the crontab. #45 [Javan Makhmali]
* Preserving backslashes when updating existing crontab. #82 [Javan Makhmali]
== 0.5.2 / September 15th, 2010 == 0.5.2 / September 15th, 2010
* Quotes automatically escaped in jobs. [Jay Adkisson] * Quotes automatically escaped in jobs. [Jay Adkisson]

View File

@ -87,23 +87,24 @@ module Whenever
def updated_crontab def updated_crontab
# Check for unopened or unclosed identifier blocks # Check for unopened or unclosed identifier blocks
if read_crontab.index(comment_open) && !read_crontab.index(comment_close) if read_crontab =~ Regexp.new("^#{comment_open}$") && (read_crontab =~ Regexp.new("^#{comment_close}$")).nil?
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'" warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
exit(1) exit(1)
elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close) elsif (read_crontab =~ Regexp.new("^#{comment_open}$")).nil? && read_crontab =~ Regexp.new("^#{comment_close}$")
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'" warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
exit(1) exit(1)
end end
# If an existing identier block is found, replace it with the new cron entries # If an existing identier block is found, replace it with the new cron entries
if read_crontab.index(comment_open) && read_crontab.index(comment_close) if read_crontab =~ Regexp.new("^#{comment_open}$") && read_crontab =~ Regexp.new("^#{comment_close}$")
read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp) # If the existing crontab file contains backslashes they get lost going through gsub.
# .gsub('\\', '\\\\\\') preserves them. Go figure.
read_crontab.gsub(Regexp.new("^#{comment_open}$.+^#{comment_close}$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\'))
else # Otherwise, append the new cron entries after any existing ones else # Otherwise, append the new cron entries after any existing ones
[read_crontab, whenever_cron].join("\n\n") [read_crontab, whenever_cron].join("\n\n")
end end
end end
#
def prepare(contents) def prepare(contents)
contents.split("\n")[@options[:cut]..-1].join("\n") contents.split("\n")[@options[:cut]..-1].join("\n")
end end

View File

@ -1,3 +1,3 @@
module Whenever module Whenever
VERSION = '0.5.2' VERSION = '0.5.3'
end unless defined?(Whenever::VERSION) end unless defined?(Whenever::VERSION)

View File

@ -65,8 +65,6 @@ This shouldn't get replaced
# End Whenever generated tasks for: Other identifier # End Whenever generated tasks for: Other identifier
EXISTING_CRON EXISTING_CRON
@command.expects(:read_crontab).at_least_once.returns(existing)
new_cron = <<-NEW_CRON new_cron = <<-NEW_CRON
# Something # Something
@ -79,6 +77,7 @@ This shouldn't get replaced
# End Whenever generated tasks for: Other identifier # End Whenever generated tasks for: Other identifier
NEW_CRON NEW_CRON
@command.expects(:read_crontab).at_least_once.returns(existing)
assert_equal new_cron, @command.send(:updated_crontab) assert_equal new_cron, @command.send(:updated_crontab)
@command.expects(:write_crontab).with(new_cron).returns(true) @command.expects(:write_crontab).with(new_cron).returns(true)
@ -86,6 +85,45 @@ NEW_CRON
end end
end end
context "A command line update that contains backslashes" do
setup do
@existing = <<-EXISTING_CRON
# Begin Whenever generated tasks for: My identifier
script/runner -e production 'puts '\\''hello'\\'''
# End Whenever generated tasks for: My identifier
EXISTING_CRON
File.expects(:exists?).with('config/schedule.rb').returns(true)
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
@command.expects(:read_crontab).at_least_once.returns(@existing)
@command.expects(:whenever_cron).returns(@existing)
end
should "replace the existing block with the backslashes in tact" do
assert_equal @existing, @command.send(:updated_crontab)
end
end
context "A command line update with an identifier similar to an existing one in the crontab already" do
setup do
@existing = <<-EXISTING_CRON
# Begin Whenever generated tasks for: WheneverExisting
# End Whenever generated tasks for: WheneverExisting
EXISTING_CRON
@new = <<-NEW_CRON
# Begin Whenever generated tasks for: Whenever
# End Whenever generated tasks for: Whenever
NEW_CRON
File.expects(:exists?).with('config/schedule.rb').returns(true)
@command = Whenever::CommandLine.new(:update => true, :identifier => 'Whenever')
@command.expects(:read_crontab).at_least_once.returns(@existing)
@command.expects(:whenever_cron).returns(@new)
end
should "append the similarly named command" do
assert_equal @existing + "\n\n" + @new, @command.send(:updated_crontab)
end
end
context "A command line delete" do context "A command line delete" do
setup do setup do
File.expects(:exists?).with('config/schedule.rb').returns(true) File.expects(:exists?).with('config/schedule.rb').returns(true)

View File

@ -5,11 +5,11 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{whenever} s.name = %q{whenever}
s.version = "0.5.2" s.version = "0.5.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Javan Makhmali"] s.authors = ["Javan Makhmali"]
s.date = %q{2010-09-15} s.date = %q{2010-09-24}
s.description = %q{Clean ruby syntax for defining and deploying messy cron jobs.} s.description = %q{Clean ruby syntax for defining and deploying messy cron jobs.}
s.email = %q{javan@javan.us} s.email = %q{javan@javan.us}
s.executables = ["whenever", "wheneverize"] s.executables = ["whenever", "wheneverize"]
@ -46,7 +46,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/javan/whenever} s.homepage = %q{http://github.com/javan/whenever}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6} s.rubygems_version = %q{1.3.7}
s.summary = %q{Clean ruby syntax for defining and deploying messy cron jobs.} s.summary = %q{Clean ruby syntax for defining and deploying messy cron jobs.}
s.test_files = [ s.test_files = [
"test/functional/command_line_test.rb", "test/functional/command_line_test.rb",
@ -64,7 +64,7 @@ Gem::Specification.new do |s|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3 s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<aaronh-chronic>, [">= 0.3.9"]) s.add_runtime_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"]) s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
s.add_development_dependency(%q<shoulda>, [">= 2.1.1"]) s.add_development_dependency(%q<shoulda>, [">= 2.1.1"])