From 9c96e7c16bb207614694bed9f3e35833b19c9657 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Fri, 24 Sep 2010 13:41:05 -0400 Subject: [PATCH] Fixed regex related bugs. Release v0.5.3 --- CHANGELOG.rdoc | 7 +++++ lib/whenever/command_line.rb | 13 +++++---- lib/whenever/version.rb | 2 +- test/functional/command_line_test.rb | 42 ++++++++++++++++++++++++++-- whenever.gemspec | 8 +++--- 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 7af24e1..2197120 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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 * Quotes automatically escaped in jobs. [Jay Adkisson] diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index 77409a5..6d6da06 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -85,25 +85,26 @@ module Whenever end end - def updated_crontab + def updated_crontab # 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}'" 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}'" exit(1) end # 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) - read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp) + if read_crontab =~ Regexp.new("^#{comment_open}$") && read_crontab =~ Regexp.new("^#{comment_close}$") + # 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 [read_crontab, whenever_cron].join("\n\n") end end - # def prepare(contents) contents.split("\n")[@options[:cut]..-1].join("\n") end diff --git a/lib/whenever/version.rb b/lib/whenever/version.rb index 237b4a6..d557b9d 100644 --- a/lib/whenever/version.rb +++ b/lib/whenever/version.rb @@ -1,3 +1,3 @@ module Whenever - VERSION = '0.5.2' + VERSION = '0.5.3' end unless defined?(Whenever::VERSION) \ No newline at end of file diff --git a/test/functional/command_line_test.rb b/test/functional/command_line_test.rb index 8611d01..3281713 100644 --- a/test/functional/command_line_test.rb +++ b/test/functional/command_line_test.rb @@ -65,8 +65,6 @@ This shouldn't get replaced # End Whenever generated tasks for: Other identifier EXISTING_CRON - @command.expects(:read_crontab).at_least_once.returns(existing) - new_cron = <<-NEW_CRON # Something @@ -79,12 +77,52 @@ This shouldn't get replaced # End Whenever generated tasks for: Other identifier NEW_CRON + @command.expects(:read_crontab).at_least_once.returns(existing) assert_equal new_cron, @command.send(:updated_crontab) @command.expects(:write_crontab).with(new_cron).returns(true) assert @command.run 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 setup do diff --git a/whenever.gemspec b/whenever.gemspec index a8fff12..2a74b2f 100644 --- a/whenever.gemspec +++ b/whenever.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| 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.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.email = %q{javan@javan.us} s.executables = ["whenever", "wheneverize"] @@ -46,7 +46,7 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/javan/whenever} s.rdoc_options = ["--charset=UTF-8"] 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.test_files = [ "test/functional/command_line_test.rb", @@ -64,7 +64,7 @@ Gem::Specification.new do |s| current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION 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, [">= 0.3.9"]) s.add_runtime_dependency(%q, [">= 2.3.4"]) s.add_development_dependency(%q, [">= 2.1.1"])