diff --git a/bin/whenever b/bin/whenever index d5e4099..d109ed4 100644 --- a/bin/whenever +++ b/bin/whenever @@ -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 diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index f2efd83..c101c1a 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -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 diff --git a/test/command_line_test.rb b/test/command_line_test.rb index 9722462..e6d39f7 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -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 @@ -97,5 +154,24 @@ NEW_CRON assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base) end end - -end \ No newline at end of file + + 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