Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identifier in schedule #758

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/whenever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
require 'whenever/os'

module Whenever
def self.job_list(options)
Whenever::JobList.new(options)
end

def self.cron(options)
Whenever::JobList.new(options).generate_cron_output
job_list(options).generate_cron_output
end

def self.seconds(number, units)
Expand Down
19 changes: 15 additions & 4 deletions lib/whenever/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def initialize(options={})
@options[:crontab_command] ||= 'crontab'
@options[:file] ||= 'config/schedule.rb'
@options[:cut] ||= 0
@options[:identifier] ||= default_identifier

if !File.exist?(@options[:file]) && @options[:clear].nil?
warn("[fail] Can't find file: #{@options[:file]}")
Expand Down Expand Up @@ -52,9 +51,21 @@ def default_identifier
File.expand_path(@options[:file])
end

def identifier
@options[:identifier] ||
whenever_job_list.respond_to?(:identifier) && whenever_job_list.identifier ||
default_identifier
end

def whenever_cron
return '' if @options[:clear]
@whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].compact.join("\n") + "\n"
@whenever_cron ||= [
comment_open, whenever_job_list.generate_cron_output, comment_close
].compact.join("\n") + "\n"
end

def whenever_job_list
@whenever_job_list ||= Whenever.job_list(@options)
end

def read_crontab
Expand Down Expand Up @@ -126,9 +137,9 @@ def prepare(contents)

def comment_base(include_timestamp = true)
if include_timestamp
"Whenever generated tasks for: #{@options[:identifier]} at: #{@timestamp}"
"Whenever generated tasks for: #{identifier} at: #{@timestamp}"
else
"Whenever generated tasks for: #{@options[:identifier]}"
"Whenever generated tasks for: #{identifier}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def method_missing(name, *args, &block)
@set_variables.has_key?(name) ? @set_variables[name] : super
end

def self.respond_to?(name, include_private = false)
def respond_to_missing?(name, include_private = false)
@set_variables.has_key?(name) || super
end

Expand Down
49 changes: 47 additions & 2 deletions test/functional/command_line_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
require 'test_helper'

class WheneverJobListMock
def initialize(task)
@task = task
end

def generate_cron_output
@task
end
end

class CommandLineWriteTest < Whenever::TestCase
setup do
Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
File.expects(:exist?).with('config/schedule.rb').returns(true)
@command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
@task = "#{two_hours} /my/command"
Whenever.expects(:cron).returns(@task)
job_list_mock = WheneverJobListMock.new(@task)
Whenever.expects(:job_list).returns(job_list_mock)
end

should "output the cron job with identifier blocks" do
Expand All @@ -31,7 +42,8 @@ class CommandLineUpdateTest < Whenever::TestCase
File.expects(:exist?).with('config/schedule.rb').returns(true)
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
@task = "#{two_hours} /my/command"
Whenever.expects(:cron).returns(@task)
job_list_mock = WheneverJobListMock.new(@task)
Whenever.expects(:job_list).returns(job_list_mock)
end

should "add the cron to the end of the file if there is no existing identifier block" do
Expand Down Expand Up @@ -301,6 +313,7 @@ class CommandLineUpdateWithNoIdentifierTest < Whenever::TestCase
setup do
Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
File.expects(:exist?).with('config/schedule.rb').returns(true)
Whenever.expects(:job_list).returns(Object.new)
Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
@command = Whenever::CommandLine.new(:update => true)
end
Expand Down Expand Up @@ -456,3 +469,35 @@ class PreparingOutputTest < Whenever::TestCase
assert_equal existing, @command.send(:prepare, existing)
end
end

class IdentifierInSchedule < Whenever::TestCase
setup do
Time.stubs(:now).returns(Time.new(2017, 2, 24, 16, 21, 30, '+01:00'))
@test_schedule_path = File.expand_path('../../test-schedules/schedule.rb', __FILE__)
end

should "use schedule.rb's identifier" do
command = Whenever::CommandLine.new(update: true, file: @test_schedule_path)
expected = <<-EXPECTED
# Begin Whenever generated tasks for: Identifier Defined In Schedule.rb at: 2017-02-24 16:21:30 +0100

# End Whenever generated tasks for: Identifier Defined In Schedule.rb at: 2017-02-24 16:21:30 +0100
EXPECTED

assert_equal expected, command.send(:whenever_cron)
end

should "user commandline option over in-schedule option" do
command = Whenever::CommandLine.new(
update: true,
file: @test_schedule_path,
identifier: 'Identifier From CommandLine'
)
expected = <<-EXPECTED
# Begin Whenever generated tasks for: Identifier From CommandLine at: 2017-02-24 16:21:30 +0100

# End Whenever generated tasks for: Identifier From CommandLine at: 2017-02-24 16:21:30 +0100
EXPECTED
assert_equal expected, command.send(:whenever_cron)
end
end
2 changes: 2 additions & 0 deletions test/test-schedules/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

set :identifier, 'Identifier Defined In Schedule.rb'