From 434423f22d525955dbb30bcb24cac62e89f90426 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Mon, 13 Jul 2009 17:23:58 -0400 Subject: [PATCH] Adds new line after closing Whenever comment. Prevents accumulation of new lines with each --update-crontab. Closes #18 --- lib/command_line.rb | 4 +- test/command_line_test.rb | 88 ++++++++++++++++++--------------------- whenever.gemspec | 36 ---------------- 3 files changed, 43 insertions(+), 85 deletions(-) delete mode 100644 whenever.gemspec diff --git a/lib/command_line.rb b/lib/command_line.rb index b1bad31..63a3ba3 100644 --- a/lib/command_line.rb +++ b/lib/command_line.rb @@ -40,7 +40,7 @@ module Whenever end def whenever_cron - @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + "\n" end def read_crontab @@ -86,7 +86,7 @@ module Whenever # 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) + read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp) else # Otherwise, append the new cron entries after any existing ones [read_crontab, whenever_cron].join("\n\n") end diff --git a/test/command_line_test.rb b/test/command_line_test.rb index 55e1d3b..9722462 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -11,13 +11,13 @@ class CommandLineTest < Test::Unit::TestCase end should "output the cron job with identifier blocks" do - output = <<-expected - # Begin Whenever generated tasks for: My identifier - #{@task} - # End Whenever generated tasks for: My identifier - expected + output = <<-EXPECTED +# Begin Whenever generated tasks for: My identifier +#{@task} +# End Whenever generated tasks for: My identifier +EXPECTED - assert_equal unindent(output).chomp, @command.send(:whenever_cron).chomp + assert_equal output, @command.send(:whenever_cron) end should "write the crontab when run" do @@ -38,49 +38,50 @@ class CommandLineTest < Test::Unit::TestCase existing = '# Existing crontab' @command.expects(:read_crontab).at_least_once.returns(existing) - new_cron = <<-expected - #{existing} + new_cron = <<-EXPECTED +#{existing} + +# Begin Whenever generated tasks for: My identifier +#{@task} +# End Whenever generated tasks for: My identifier +EXPECTED - # Begin Whenever generated tasks for: My identifier - #{@task} - # End Whenever generated tasks for: My identifier - expected + assert_equal new_cron, @command.send(:updated_crontab) - assert_equal unindent(new_cron).chomp, @command.send(:updated_crontab).chomp - - @command.expects(:write_crontab).with(unindent(new_cron)).returns(true) + @command.expects(:write_crontab).with(new_cron).returns(true) assert @command.run end should "replace an existing block if the identifier matches" do - existing = <<-existing - # Something + 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) - # Begin Whenever generated tasks for: My identifier - My whenever job that was already here - # End Whenever generated tasks for: My identifier + new_cron = <<-NEW_CRON +# Something + +# Begin Whenever generated tasks for: My identifier +#{@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 - # Begin Whenever generated tasks for: Other identifier - This shouldn't get replaced - # End Whenever generated tasks for: Other identifier - existing - @command.expects(:read_crontab).at_least_once.returns(unindent(existing)) + assert_equal new_cron, @command.send(:updated_crontab) - new_cron = <<-new_cron - # Something - - # Begin Whenever generated tasks for: My identifier - #{@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 unindent(new_cron).chomp, @command.send(:updated_crontab).chomp - - @command.expects(:write_crontab).with(unindent(new_cron)).returns(true) + @command.expects(:write_crontab).with(new_cron).returns(true) assert @command.run end end @@ -97,11 +98,4 @@ class CommandLineTest < Test::Unit::TestCase end end -private - - def unindent(string) - indentation = string[/\A\s*/] - string.strip.gsub(/^#{indentation}/, "") - end - end \ No newline at end of file diff --git a/whenever.gemspec b/whenever.gemspec deleted file mode 100644 index cba6b90..0000000 --- a/whenever.gemspec +++ /dev/null @@ -1,36 +0,0 @@ -# -*- encoding: utf-8 -*- - -Gem::Specification.new do |s| - s.name = %q{whenever} - s.version = "0.3.1" - - s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= - s.authors = ["Javan Makhmali"] - s.date = %q{2009-06-25} - s.description = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.} - s.email = %q{javan@javan.us} - s.executables = ["whenever", "wheneverize"] - s.extra_rdoc_files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "README.rdoc"] - s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec"] - s.has_rdoc = true - s.homepage = %q{http://github.com/javan/whenever} - s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Whenever", "--main", "README.rdoc"] - s.require_paths = ["lib"] - s.rubyforge_project = %q{whenever} - s.rubygems_version = %q{1.3.1} - s.summary = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.} - s.test_files = ["test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb"] - - if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 2 - - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - s.add_runtime_dependency(%q, [">= 0.2.3"]) - else - s.add_dependency(%q, [">= 0.2.3"]) - end - else - s.add_dependency(%q, [">= 0.2.3"]) - end -end