clear the crontab

This commit is contained in:
Michael Raidel 2010-05-28 16:48:41 +08:00 committed by Javan Makhmali
parent 087119ce2d
commit 8faf291369
3 changed files with 86 additions and 6 deletions

View File

@ -14,6 +14,10 @@ OptionParser.new do |opts|
options[:update] = true
options[:identifier] = identifier if identifier
end
opts.on('-c', '--clear-crontab [identifier]') do |identifier|
options[:clear] = true
options[:identifier] = identifier if identifier
end
opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
options[:file] = file if file
end

View File

@ -19,14 +19,14 @@ module Whenever
exit(1)
end
if @options[:update] && @options[:write]
warn("[fail] Can't update AND write. choose one.")
if [@options[:update], @options[:write], @options[:clear]].compact.length > 1
warn("[fail] Can only update, write or delete. choose one.")
exit(1)
end
end
def run
if @options[:update]
if @options[:update] || @options[:clear]
write_crontab(updated_crontab)
elsif @options[:write]
write_crontab(whenever_cron)
@ -43,7 +43,7 @@ module Whenever
end
def whenever_cron
@whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + "\n"
@whenever_cron ||= [comment_open, (Whenever.cron(@options) unless @options[:clear]), comment_close].compact.join("\n") + "\n"
end
def read_crontab

View File

@ -74,6 +74,63 @@ EXISTING_CRON
#{@task}
# 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
end
context "A command line delete" 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
existing = <<-EXISTING_CRON
# Something
# Begin Whenever generated tasks for: My identifier
My whenever job that was already here
# 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
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
@ -98,4 +155,23 @@ NEW_CRON
end
end
context "combined params" do
setup do
Whenever::CommandLine.any_instance.expects(:exit)
Whenever::CommandLine.any_instance.expects(:warn)
File.expects(:exists?).with('config/schedule.rb').returns(true)
end
should "exit with write and clear" do
@command = Whenever::CommandLine.new(:write => true, :clear => true)
end
should "exit with write and update" do
@command = Whenever::CommandLine.new(:write => true, :update => true)
end
should "exit with update and clear" do
@command = Whenever::CommandLine.new(:update => true, :clear => true)
end
end
end