From f17c8d8cd2830a035cb5df2dfb48f2031bd1bec8 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Mon, 6 Sep 2010 23:38:11 -0400 Subject: [PATCH] consolidated tests - moving away from testing on runners, rakes, etc. now that jobs are definable. added more units tests for Jobs. --- lib/whenever/job.rb | 2 +- test/functional/command_line_test.rb | 69 +++++++ test/functional/output_command_test.rb | 37 ---- ...rb => output_default_defined_jobs_test.rb} | 90 +++++---- test/functional/output_defined_job_test.rb | 18 ++ test/functional/output_runner_test.rb | 175 ------------------ test/unit/job_test.rb | 33 +++- 7 files changed, 170 insertions(+), 254 deletions(-) delete mode 100644 test/functional/output_command_test.rb rename test/functional/{output_rake_test.rb => output_default_defined_jobs_test.rb} (59%) delete mode 100644 test/functional/output_runner_test.rb diff --git a/lib/whenever/job.rb b/lib/whenever/job.rb index 5d23501..b15897c 100644 --- a/lib/whenever/job.rb +++ b/lib/whenever/job.rb @@ -1,7 +1,7 @@ module Whenever class Job - attr_accessor :at, :output_redirection + attr_reader :at, :output_redirection def initialize(options = {}) @options = options diff --git a/test/functional/command_line_test.rb b/test/functional/command_line_test.rb index 04811a4..0267036 100644 --- a/test/functional/command_line_test.rb +++ b/test/functional/command_line_test.rb @@ -174,4 +174,73 @@ NEW_CRON @command = Whenever::CommandLine.new(:update => true, :clear => true) end end + + context "A runner 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' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the override environment" do + assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output + end + end + + context "A runner 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' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the overridden path and environment" do + assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output + end + end + + context "A runner where the environment and path are overridden using the :set option with spaces in the string" do + setup do + @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the overridden path and environment" do + assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output + end + end + + context "A runner where the environment is overridden using the :set option but no value is given" do + setup do + @output = Whenever.cron :set => ' environment=', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the original environmnet" do + assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output + end + end + end diff --git a/test/functional/output_command_test.rb b/test/functional/output_command_test.rb deleted file mode 100644 index beb3d5c..0000000 --- a/test/functional/output_command_test.rb +++ /dev/null @@ -1,37 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/../test_helper") - -class OutputCommandTest < Test::Unit::TestCase - - context "A plain command" do - setup do - @output = Whenever.cron \ - <<-file - every 2.hours do - command "blahblah" - end - file - end - - should "output the command" do - assert_match /^.+ .+ .+ .+ blahblah$/, @output - end - end - - context "An every statement with two commands in it" do - setup do - @output = Whenever.cron \ - <<-file - every 1.hour do - command "first" - command "second" - end - file - end - - should "output both commands" do - assert_match "0 * * * * first", @output - assert_match "0 * * * * second", @output - end - end - -end \ No newline at end of file diff --git a/test/functional/output_rake_test.rb b/test/functional/output_default_defined_jobs_test.rb similarity index 59% rename from test/functional/output_rake_test.rb rename to test/functional/output_default_defined_jobs_test.rb index d5104c9..c8d7c8d 100644 --- a/test/functional/output_rake_test.rb +++ b/test/functional/output_default_defined_jobs_test.rb @@ -1,9 +1,59 @@ require File.expand_path(File.dirname(__FILE__) + "/../test_helper") -class OutputRakeTest < Test::Unit::TestCase +class OutputDefaultDefinedJobsTest < Test::Unit::TestCase - # Rake are generated in an almost identical way to runners so we - # only need some basic tests to ensure they are output correctly + # command + + context "A plain command" do + setup do + @output = Whenever.cron \ + <<-file + every 2.hours do + command "blahblah" + end + file + end + + should "output the command" do + assert_match /^.+ .+ .+ .+ blahblah$/, @output + end + end + + # runner + + context "A runner with path set" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + every 2.hours do + runner 'blahblah' + end + file + end + + should "output the runner using that path" do + assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), @output + end + end + + context "A runner that overrides the path set" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + every 2.hours do + runner "blahblah", :path => '/some/other/path' + end + file + end + + should "output the runner using that path" do + assert_match two_hours + %( cd /some/other/path && script/runner -e production 'blahblah'), @output + end + end + + # rake context "A rake command with path set" do setup do @@ -37,38 +87,4 @@ class OutputRakeTest < Test::Unit::TestCase end end - context "A rake command with environment set" do - setup do - @output = Whenever.cron \ - <<-file - set :environment, :silly - set :path, '/my/path' - every 2.hours do - rake "blahblah" - end - file - end - - should "output the rake command using that environment" do - assert_match two_hours + ' cd /my/path && RAILS_ENV=silly /usr/bin/env rake blahblah', @output - end - end - - context "A rake command that overrides the environment set" do - setup do - @output = Whenever.cron \ - <<-file - set :environment, :silly - set :path, '/my/path' - every 2.hours do - rake "blahblah", :environment => :serious - end - file - end - - should "output the rake command using that environment" do - assert_match two_hours + ' cd /my/path && RAILS_ENV=serious /usr/bin/env rake blahblah', @output - end - end - end \ No newline at end of file diff --git a/test/functional/output_defined_job_test.rb b/test/functional/output_defined_job_test.rb index 6081a5a..322dd37 100644 --- a/test/functional/output_defined_job_test.rb +++ b/test/functional/output_defined_job_test.rb @@ -83,5 +83,23 @@ class OutputDefinedJobTest < Test::Unit::TestCase assert_match /^.+ .+ .+ .+ before during after$/, @output end end + + context "A defined job that uses a :path where none is explicitly set" do + setup do + Whenever.expects(:path).returns('/my/path') + + @output = Whenever.cron \ + <<-file + job_type :some_job, "cd :path && :task" + every 2.hours do + some_job 'blahblah' + end + file + end + + should "default to using the Whenever.path" do + assert_match two_hours + %( cd /my/path && blahblah), @output + end + end end \ No newline at end of file diff --git a/test/functional/output_runner_test.rb b/test/functional/output_runner_test.rb deleted file mode 100644 index b344861..0000000 --- a/test/functional/output_runner_test.rb +++ /dev/null @@ -1,175 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/../test_helper") - -class OutputRunnerTest < Test::Unit::TestCase - - context "A runner with path set" do - setup do - @output = Whenever.cron \ - <<-file - set :path, '/my/path' - every 2.hours do - runner 'blahblah' - end - file - end - - should "output the runner using that path" do - assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), @output - end - end - - context "A runner that overrides the path set" do - setup do - @output = Whenever.cron \ - <<-file - set :path, '/my/path' - every 2.hours do - runner "blahblah", :path => '/some/other/path' - end - file - end - - should "output the runner using that path" do - assert_match two_hours + %( cd /some/other/path && script/runner -e production 'blahblah'), @output - end - end - - context "A runner with no path set and RAILS_ROOT defined" do - setup do - Whenever.stubs(:path).returns('/my/path') - - @output = Whenever.cron \ - <<-file - every 2.hours do - runner 'blahblah' - end - file - end - - should "output the runner using that path" do - assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), @output - end - end - - context "A runner with path set AND RAILS_ROOT defined" do - setup do - Whenever.stubs(:path).returns('/my/rails/path') - - @output = Whenever.cron \ - <<-file - set :path, '/my/path' - every 2.hours do - runner "blahblah" - end - file - end - - should "use the path" do - assert_match two_hours + %( cd /my/path && script/runner -e production 'blahblah'), @output - assert_no_match /\/rails\/path/, @output - end - end - - context "A runner with an environment set" do - setup do - @output = Whenever.cron \ - <<-file - set :environment, :silly - set :path, '/my/path' - every 2.hours do - runner "blahblah" - end - file - end - - should "output the runner using that environment" do - assert_match two_hours + %( cd /my/path && script/runner -e silly 'blahblah'), @output - end - end - - context "A runner that overrides the environment set" do - setup do - @output = Whenever.cron \ - <<-file - set :environment, :silly - set :path, '/my/path' - every 2.hours do - runner "blahblah", :environment => :serious - end - file - end - - should "output the runner using that environment" do - assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output - end - end - - context "A runner 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' - every 2.hours do - runner "blahblah" - end - file - end - - should "output the runner using the override environment" do - assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output - end - end - - context "A runner 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' - every 2.hours do - runner "blahblah" - end - file - end - - should "output the runner using the overridden path and environment" do - assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output - end - end - - context "A runner where the environment and path are overridden using the :set option with spaces in the string" do - setup do - @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \ - <<-file - set :environment, :silly - set :path, '/silly/path' - every 2.hours do - runner "blahblah" - end - file - end - - should "output the runner using the overridden path and environment" do - assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output - end - end - - context "A runner where the environment is overridden using the :set option but no value is given" do - setup do - @output = Whenever.cron :set => ' environment=', :string => \ - <<-file - set :environment, :silly - set :path, '/silly/path' - every 2.hours do - runner "blahblah" - end - file - end - - should "output the runner using the original environmnet" do - assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output - end - end - -end diff --git a/test/unit/job_test.rb b/test/unit/job_test.rb index 9cb2210..ad448a0 100644 --- a/test/unit/job_test.rb +++ b/test/unit/job_test.rb @@ -3,11 +3,36 @@ require File.expand_path(File.dirname(__FILE__) + "/../test_helper") class JobTest < Test::Unit::TestCase context "A Job" do - should "output the :task" do - job = new_job(:template => ":task", :task => 'abc123') - assert_equal %q(abc123), job.output + should "return the :at set when #at is called" do + assert_equal 'foo', new_job(:at => 'foo').at end + should "return :output when #output_redirection is called" do + assert_equal 'foo', new_job(:output => 'foo').output_redirection + end + + should "return :not_set when #output_redirection is called and no :output is set" do + assert_equal :not_set, new_job.output_redirection + end + + should "substitute the :task when #output is called" do + job = new_job(:template => ":task", :task => 'abc123') + assert_equal 'abc123', job.output + end + + should "substitute the :path when #output is called" do + assert_equal 'foo', new_job(:template => ':path', :path => 'foo').output + end + + should "substitute the :path with the default Whenever.path if none is provided when #output is called" do + Whenever.expects(:path).returns('/my/path') + assert_equal '/my/path', new_job(:template => ':path').output + end + end + + + context "A Job with quotes" do + should "output the :task if it's in single quotes" do job = new_job(:template => "':task'", :task => 'abc123') assert_equal %q('abc123'), job.output @@ -37,7 +62,7 @@ class JobTest < Test::Unit::TestCase private - def new_job(options) + def new_job(options={}) Whenever::Job.new(options) end