diff --git a/lib/whenever.rb b/lib/whenever.rb index b22281ea..af2ea293 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -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) diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index b75ea7e3..d7d0702d 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -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]}") @@ -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 @@ -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 diff --git a/lib/whenever/job_list.rb b/lib/whenever/job_list.rb index 7df39c68..14d5c555 100644 --- a/lib/whenever/job_list.rb +++ b/lib/whenever/job_list.rb @@ -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 diff --git a/test/functional/command_line_test.rb b/test/functional/command_line_test.rb index 866a5690..80ed548d 100644 --- a/test/functional/command_line_test.rb +++ b/test/functional/command_line_test.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/test-schedules/schedule.rb b/test/test-schedules/schedule.rb new file mode 100755 index 00000000..755357eb --- /dev/null +++ b/test/test-schedules/schedule.rb @@ -0,0 +1,2 @@ + +set :identifier, 'Identifier Defined In Schedule.rb'