Adds new line after closing Whenever comment. Prevents accumulation of new lines with each --update-crontab. Closes #18

This commit is contained in:
Javan Makhmali 2009-07-13 17:23:58 -04:00
parent 2bd1660386
commit 434423f22d
3 changed files with 43 additions and 85 deletions

View File

@ -40,7 +40,7 @@ module Whenever
end end
def whenever_cron 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 end
def read_crontab def read_crontab
@ -86,7 +86,7 @@ module Whenever
# If an existing identier block is found, replace it with the new cron entries # 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) 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 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

View File

@ -11,13 +11,13 @@ class CommandLineTest < Test::Unit::TestCase
end end
should "output the cron job with identifier blocks" do should "output the cron job with identifier blocks" do
output = <<-expected output = <<-EXPECTED
# Begin Whenever generated tasks for: My identifier # Begin Whenever generated tasks for: My identifier
#{@task} #{@task}
# End Whenever generated tasks for: My identifier # End Whenever generated tasks for: My identifier
expected EXPECTED
assert_equal unindent(output).chomp, @command.send(:whenever_cron).chomp assert_equal output, @command.send(:whenever_cron)
end end
should "write the crontab when run" do should "write the crontab when run" do
@ -38,49 +38,50 @@ class CommandLineTest < Test::Unit::TestCase
existing = '# Existing crontab' existing = '# Existing crontab'
@command.expects(:read_crontab).at_least_once.returns(existing) @command.expects(:read_crontab).at_least_once.returns(existing)
new_cron = <<-expected new_cron = <<-EXPECTED
#{existing} #{existing}
# Begin Whenever generated tasks for: My identifier
#{@task}
# End Whenever generated tasks for: My identifier
EXPECTED
# Begin Whenever generated tasks for: My identifier assert_equal new_cron, @command.send(:updated_crontab)
#{@task}
# End Whenever generated tasks for: My identifier
expected
assert_equal unindent(new_cron).chomp, @command.send(:updated_crontab).chomp @command.expects(:write_crontab).with(new_cron).returns(true)
@command.expects(:write_crontab).with(unindent(new_cron)).returns(true)
assert @command.run assert @command.run
end end
should "replace an existing block if the identifier matches" do should "replace an existing block if the identifier matches" do
existing = <<-existing existing = <<-EXISTING_CRON
# Something # 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 new_cron = <<-NEW_CRON
My whenever job that was already here # Something
# End Whenever generated tasks for: My identifier
# 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 assert_equal new_cron, @command.send(:updated_crontab)
This shouldn't get replaced
# End Whenever generated tasks for: Other identifier
existing
@command.expects(:read_crontab).at_least_once.returns(unindent(existing))
new_cron = <<-new_cron @command.expects(:write_crontab).with(new_cron).returns(true)
# 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)
assert @command.run assert @command.run
end end
end end
@ -97,11 +98,4 @@ class CommandLineTest < Test::Unit::TestCase
end end
end end
private
def unindent(string)
indentation = string[/\A\s*/]
string.strip.gsub(/^#{indentation}/, "")
end
end end

View File

@ -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<chronic>, [">= 0.2.3"])
else
s.add_dependency(%q<chronic>, [">= 0.2.3"])
end
else
s.add_dependency(%q<chronic>, [">= 0.2.3"])
end
end