Skip to content

Commit

Permalink
Adds new line after closing Whenever comment. Prevents accumulation o…
Browse files Browse the repository at this point in the history
…f new lines with each --update-crontab. Closes javan#18
  • Loading branch information
javan committed Jul 13, 2009
1 parent 2bd1660 commit 434423f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 85 deletions.
4 changes: 2 additions & 2 deletions lib/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def default_identifier
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
Expand Down Expand Up @@ -86,7 +86,7 @@ def updated_crontab

# 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
Expand Down
88 changes: 41 additions & 47 deletions test/command_line_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
# Begin Whenever generated tasks for: My identifier
#{@task}
# End Whenever generated tasks for: My identifier
expected
new_cron = <<-EXPECTED
#{existing}
# Begin Whenever generated tasks for: My identifier
#{@task}
# End Whenever generated tasks for: My identifier
EXPECTED

assert_equal unindent(new_cron).chomp, @command.send(:updated_crontab).chomp
assert_equal new_cron, @command.send(:updated_crontab)

@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
# 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
@command.expects(:read_crontab).at_least_once.returns(unindent(existing))

new_cron = <<-new_cron
# Something
# Begin Whenever generated tasks for: My identifier
#{@task}
# End Whenever generated tasks for: My identifier
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: Other identifier
This shouldn't get replaced
# End Whenever generated tasks for: Other identifier
new_cron
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
assert_equal new_cron, @command.send(:updated_crontab)

@command.expects(:write_crontab).with(unindent(new_cron)).returns(true)
@command.expects(:write_crontab).with(new_cron).returns(true)
assert @command.run
end
end
Expand All @@ -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
36 changes: 0 additions & 36 deletions whenever.gemspec

This file was deleted.

0 comments on commit 434423f

Please sign in to comment.