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

This commit is contained in:
Damien 2010-05-29 21:27:07 +08:00 committed by Javan Makhmali
parent b0ce52fa55
commit 7e6a199b8b
4 changed files with 170 additions and 6 deletions

View File

@ -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: By defining jobs like this, you can also make reusable system commands such as:
job_type :my_great_command do |job| 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" job.command = "/usr/bin/my_great_command"
end end

View File

@ -49,10 +49,11 @@ module Whenever
def job_type(name, options = {}, &block) def job_type(name, options = {}, &block)
(class << self; self; end).class_eval do (class << self; self; end).class_eval do
define_method(name) do |task| define_method(name) do |task,*args|
new_job = Whenever::Job::UserDefined.new new_job = Whenever::Job::UserDefined.new
block.call(new_job) block.call(new_job)
options = new_job.to_options options = new_job.to_options
options.merge!(args[0]) if args[0].is_a?(Hash)
options.reverse_merge!(:environment => @environment, :path => @path) options.reverse_merge!(:environment => @environment, :path => @path)
options[:class] = Whenever::Job::UserDefined options[:class] = Whenever::Job::UserDefined
command(task, options) command(task, options)

View File

@ -1,17 +1,17 @@
module Whenever module Whenever
module Job module Job
class UserDefined < Whenever::Job::Default class UserDefined < Whenever::Job::Default
attr_accessor :uses_bundler, :command, :environment attr_accessor :uses_bundler, :command, :no_environment
def initialize(options={}) def initialize(options={})
super super
@uses_bundler = options[:uses_bundler] @uses_bundler = options[:uses_bundler]
@command = options[:command] @command = options[:command]
@env = options[:environment] @no_environment = options[:no_environment]
end end
def to_options def to_options
{ :uses_bundler => uses_bundler, :command => command, :environment => environment } { :uses_bundler => uses_bundler, :command => command, :no_environment => no_environment }
end end
def output def output
@ -19,7 +19,7 @@ module Whenever
out << "cd #{File.join(@path)} &&" out << "cd #{File.join(@path)} &&"
out << "bundle exec" if uses_bundler out << "bundle exec" if uses_bundler
out << command out << command
out << "-e #{@environment}" if @env out << "-e #{@environment}" unless no_environment
out << task.inspect out << task.inspect
out.join(" ") out.join(" ")
end end

View File

@ -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