Don't leave Whenever comments when clearing

This commit is contained in:
Javan Makhmali 2010-10-25 21:03:49 -04:00
parent 2060cb95fa
commit 20faaa266d
2 changed files with 9 additions and 29 deletions

View File

@ -30,7 +30,6 @@ module Whenever
exit(1) exit(1)
end end
@options[:cut] = @options[:cut].to_i @options[:cut] = @options[:cut].to_i
end end
def run def run
@ -51,7 +50,8 @@ module Whenever
end end
def whenever_cron 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 end
def read_crontab def read_crontab
@ -102,7 +102,7 @@ module Whenever
read_crontab.gsub(Regexp.new("^#{comment_open}$.+^#{comment_close}$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\')) 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.gsub(/\n{3,}/, "\n\n") # More than two newlines becomes just two.
end end
def prepare(contents) def prepare(contents)

View File

@ -120,35 +120,18 @@ NEW_CRON
end end
should "append the similarly named command" do 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
end end
context "A command line delete" do context "A command line clear" do
setup do setup do
File.expects(:exists?).with('config/schedule.rb').returns(true) File.expects(:exists?).with('config/schedule.rb').returns(true)
@command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier') @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
@task = "#{two_hours} /my/command" @task = "#{two_hours} /my/command"
end end
should "add an empty identifier block if there is no existing one" do should "clear an existing block if the identifier matches" 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
existing = <<-EXISTING_CRON existing = <<-EXISTING_CRON
# Something # Something
@ -166,9 +149,6 @@ EXISTING_CRON
new_cron = <<-NEW_CRON new_cron = <<-NEW_CRON
# Something # Something
# Begin Whenever generated tasks for: My identifier
# End Whenever generated tasks for: My identifier
# Begin Whenever generated tasks for: Other identifier # Begin Whenever generated tasks for: Other identifier
This shouldn't get replaced This shouldn't get replaced
# End Whenever generated tasks for: Other identifier # End Whenever generated tasks for: Other identifier