From 20faaa266d8adb85fbea9ad4f6c6c7aacafabfa8 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Mon, 25 Oct 2010 21:03:49 -0400 Subject: [PATCH] Don't leave Whenever comments when clearing --- lib/whenever/command_line.rb | 6 +++--- test/functional/command_line_test.rb | 32 ++++++---------------------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index 6d6da06..92e2b0f 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -30,7 +30,6 @@ module Whenever exit(1) end @options[:cut] = @options[:cut].to_i - end def run @@ -51,7 +50,8 @@ module Whenever end def whenever_cron - @whenever_cron ||= [comment_open, (Whenever.cron(@options) unless @options[:clear]), comment_close].compact.join("\n") + "\n" + return '' if @options[:clear] + @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].compact.join("\n") + "\n" end def read_crontab @@ -102,7 +102,7 @@ module Whenever 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.gsub(/\n{3,}/, "\n\n") # More than two newlines becomes just two. end def prepare(contents) diff --git a/test/functional/command_line_test.rb b/test/functional/command_line_test.rb index 627424c..654d2c4 100644 --- a/test/functional/command_line_test.rb +++ b/test/functional/command_line_test.rb @@ -120,35 +120,18 @@ NEW_CRON end should "append the similarly named command" do - assert_equal @existing + "\n\n" + @new, @command.send(:updated_crontab) + assert_equal @existing + "\n" + @new, @command.send(:updated_crontab) end end - context "A command line delete" do + context "A command line clear" do setup do File.expects(:exists?).with('config/schedule.rb').returns(true) @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier') @task = "#{two_hours} /my/command" end - should "add an empty identifier block if there is no existing one" do - existing = '# Existing crontab' - @command.expects(:read_crontab).at_least_once.returns(existing) - - new_cron = <<-EXPECTED -#{existing} - -# Begin Whenever generated tasks for: My identifier -# End Whenever generated tasks for: My identifier -EXPECTED - - assert_equal new_cron, @command.send(:updated_crontab) - - @command.expects(:write_crontab).with(new_cron).returns(true) - assert @command.run - end - - should "delete an existing block if the identifier matches" do + should "clear an existing block if the identifier matches" do existing = <<-EXISTING_CRON # Something @@ -162,20 +145,17 @@ This shouldn't get replaced EXISTING_CRON @command.expects(:read_crontab).at_least_once.returns(existing) - + new_cron = <<-NEW_CRON # Something -# Begin Whenever generated tasks for: My identifier -# End Whenever generated tasks for: My identifier - # Begin Whenever generated tasks for: Other identifier This shouldn't get replaced # End Whenever generated tasks for: Other identifier NEW_CRON - + assert_equal new_cron, @command.send(:updated_crontab) - + @command.expects(:write_crontab).with(new_cron).returns(true) assert @command.run end