From 7e6a199b8bb7eca8207b394d04eac517da34e9c9 Mon Sep 17 00:00:00 2001 From: Damien Date: Sat, 29 May 2010 21:27:07 +0800 Subject: [PATCH] Writing some tests for user defined jobs and changing job.environment option to job.no_environment so it plays nicely with the environment settings from the parent class --- README.rdoc | 2 +- lib/whenever/job_list.rb | 3 +- lib/whenever/job_types/user_defined.rb | 8 +- test/output_user_defined_test.rb | 163 +++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 test/output_user_defined_test.rb diff --git a/README.rdoc b/README.rdoc index dab2fe7..70e5b1e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -85,7 +85,7 @@ This would create a job with the command "bundle exec rails runner". By defining jobs like this, you can also make reusable system commands such as: job_type :my_great_command do |job| - job.environment = false # this stops whenever appending '-e production' to the task + job.no_environment = true # this stops whenever appending '-e production' to the task job.command = "/usr/bin/my_great_command" end diff --git a/lib/whenever/job_list.rb b/lib/whenever/job_list.rb index d6acd0d..d9c2f3b 100644 --- a/lib/whenever/job_list.rb +++ b/lib/whenever/job_list.rb @@ -49,10 +49,11 @@ module Whenever def job_type(name, options = {}, &block) (class << self; self; end).class_eval do - define_method(name) do |task| + define_method(name) do |task,*args| new_job = Whenever::Job::UserDefined.new block.call(new_job) options = new_job.to_options + options.merge!(args[0]) if args[0].is_a?(Hash) options.reverse_merge!(:environment => @environment, :path => @path) options[:class] = Whenever::Job::UserDefined command(task, options) diff --git a/lib/whenever/job_types/user_defined.rb b/lib/whenever/job_types/user_defined.rb index e516fb2..c11019e 100644 --- a/lib/whenever/job_types/user_defined.rb +++ b/lib/whenever/job_types/user_defined.rb @@ -1,17 +1,17 @@ module Whenever module Job class UserDefined < Whenever::Job::Default - attr_accessor :uses_bundler, :command, :environment + attr_accessor :uses_bundler, :command, :no_environment def initialize(options={}) super @uses_bundler = options[:uses_bundler] @command = options[:command] - @env = options[:environment] + @no_environment = options[:no_environment] end def to_options - { :uses_bundler => uses_bundler, :command => command, :environment => environment } + { :uses_bundler => uses_bundler, :command => command, :no_environment => no_environment } end def output @@ -19,7 +19,7 @@ module Whenever out << "cd #{File.join(@path)} &&" out << "bundle exec" if uses_bundler out << command - out << "-e #{@environment}" if @env + out << "-e #{@environment}" unless no_environment out << task.inspect out.join(" ") end diff --git a/test/output_user_defined_test.rb b/test/output_user_defined_test.rb new file mode 100644 index 0000000..3d19dfd --- /dev/null +++ b/test/output_user_defined_test.rb @@ -0,0 +1,163 @@ +require File.expand_path(File.dirname(__FILE__) + "/test_helper") + +class OutputUserDefinedTest < Test::Unit::TestCase + + context "A simple custom job type" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the command using simple_job" do + assert_match two_hours + ' cd /my/path && hello -e production "blahblah"', @output + end + end + + context "A custom job which uses bunder" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + job.uses_bundler = true + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the command using simple_job with bundler" do + assert_match two_hours + ' cd /my/path && bundle exec hello -e production "blahblah"', @output + end + end + + context "A custom job which doesn't use an environment" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + job.no_environment = true + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the command using simple_job without environment" do + assert_match two_hours + ' cd /my/path && hello "blahblah"', @output + end + end + + context "A custom job that overrides the path set" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah", :path => '/some/other/path' + end + file + end + + should "output the runner using that path" do + assert_match two_hours + ' cd /some/other/path && hello -e production "blahblah"', @output + end + end + + context "A custom job with an environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the runner using that environment" do + assert_match two_hours + ' cd /my/path && hello -e silly "blahblah"', @output + end + end + + context "A custom job that overrides the environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah", :environment => :serious + end + file + end + + should "output the runner using that environment" do + assert_match two_hours + ' cd /my/path && hello -e serious "blahblah"', @output + end + end + + context "A custom job where the environment is overridden using the :set option" do + setup do + @output = Whenever.cron :set => 'environment=serious', :string => \ + <<-file + set :environment, :silly + set :path, '/my/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the runner using the override environment" do + assert_match two_hours + ' cd /my/path && hello -e serious "blahblah"', @output + end + end + + context "A custom job where the environment and path are overridden using the :set option" do + setup do + @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + job_type :simple_job do |job| + job.command = "hello" + end + every 2.hours do + simple_job "blahblah" + end + file + end + + should "output the runner using the overridden path and environment" do + assert_match two_hours + ' cd /serious/path && hello -e serious "blahblah"', @output + end + end + +end