From b405def42411a81876c3a0ec6cc3e4954ba72211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szajbe?= Date: Tue, 19 Jul 2011 10:17:17 +0200 Subject: [PATCH 1/2] encapsulate rails 3 check for easier testing --- lib/whenever.rb | 6 +++++- lib/whenever/setup.rb | 2 +- test/functional/output_default_defined_jobs_test.rb | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/whenever.rb b/lib/whenever.rb index 8fbe7a5..fe23376 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -19,4 +19,8 @@ module Whenever Dir.pwd end -end \ No newline at end of file + def self.rails3? + File.exists?(File.join(path, 'script', 'rails')) + end + +end diff --git a/lib/whenever/setup.rb b/lib/whenever/setup.rb index 64e6f33..2e15c1a 100644 --- a/lib/whenever/setup.rb +++ b/lib/whenever/setup.rb @@ -11,7 +11,7 @@ job_type :command, ":task :output" job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output" # Create a runner job that's appropriate for the Rails version, -if File.exists?(File.join(path, 'script', 'rails')) +if Whenever.rails3? job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output" else job_type :runner, "cd :path && script/runner -e :environment ':task' :output" diff --git a/test/functional/output_default_defined_jobs_test.rb b/test/functional/output_default_defined_jobs_test.rb index 6dd389d..27691ff 100644 --- a/test/functional/output_default_defined_jobs_test.rb +++ b/test/functional/output_default_defined_jobs_test.rb @@ -110,7 +110,7 @@ class OutputDefaultDefinedJobsTest < Test::Unit::TestCase context "A runner for a Rails 3 app" do setup do Whenever.expects(:path).at_least_once.returns('/my/path') - File.expects(:exists?).with('/my/path/script/rails').returns(true) + Whenever.expects(:rails3?).returns(true) @output = Whenever.cron \ <<-file set :job_template, nil From 439a13644c9227d92fe91a49d73305edac98f853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szajbe?= Date: Tue, 19 Jul 2011 10:19:13 +0200 Subject: [PATCH 2/2] run rake through bundler if possible --- lib/whenever.rb | 4 ++++ lib/whenever/setup.rb | 8 +++++++- test/functional/output_at_test.rb | 4 ++-- .../output_default_defined_jobs_test.rb | 20 ++++++++++++++++++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/whenever.rb b/lib/whenever.rb index fe23376..8fa0a98 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -23,4 +23,8 @@ module Whenever File.exists?(File.join(path, 'script', 'rails')) end + def self.bundler? + File.exists?(File.join(path, 'Gemfile')) + end + end diff --git a/lib/whenever/setup.rb b/lib/whenever/setup.rb index 2e15c1a..68a0b17 100644 --- a/lib/whenever/setup.rb +++ b/lib/whenever/setup.rb @@ -8,7 +8,13 @@ set :path, Whenever.path set :job_template, "/bin/bash -l -c ':job'" job_type :command, ":task :output" -job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output" + +# Run rake through bundler if possible +if Whenever.bundler? + job_type :rake, "cd :path && RAILS_ENV=:environment bundle exec rake :task --silent :output" +else + job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output" +end # Create a runner job that's appropriate for the Rails version, if Whenever.rails3? diff --git a/test/functional/output_at_test.rb b/test/functional/output_at_test.rb index e30431e..2295a0e 100644 --- a/test/functional/output_at_test.rb +++ b/test/functional/output_at_test.rb @@ -114,7 +114,7 @@ class OutputAtTest < Test::Unit::TestCase end should "output the rake task using one entry because the times are aligned" do - assert_match '2 5,15 * * 1,3,5 cd /your/path && RAILS_ENV=production rake blah:blah --silent', @output + assert_match '2 5,15 * * 1,3,5 cd /your/path && RAILS_ENV=production bundle exec rake blah:blah --silent', @output end end @@ -177,7 +177,7 @@ class OutputAtTest < Test::Unit::TestCase end should "output all of the commands @daily" do - assert_match '@daily cd /your/path && RAILS_ENV=production rake blah:blah --silent', @output + assert_match '@daily cd /your/path && RAILS_ENV=production bundle exec rake blah:blah --silent', @output assert_match %(@daily cd /your/path && script/runner -e production 'runner_1'), @output assert_match '@daily command_1', @output assert_match %(@daily cd /your/path && script/runner -e production 'runner_2'), @output diff --git a/test/functional/output_default_defined_jobs_test.rb b/test/functional/output_default_defined_jobs_test.rb index 27691ff..e9d3ef4 100644 --- a/test/functional/output_default_defined_jobs_test.rb +++ b/test/functional/output_default_defined_jobs_test.rb @@ -140,6 +140,24 @@ class OutputDefaultDefinedJobsTest < Test::Unit::TestCase end should "output the rake command using that path" do + assert_match two_hours + ' cd /my/path && RAILS_ENV=production bundle exec rake blahblah --silent', @output + end + end + + context "A rake for a non-bundler app" do + setup do + Whenever.expects(:path).at_least_once.returns('/my/path') + Whenever.expects(:bundler?).returns(false) + @output = Whenever.cron \ + <<-file + set :job_template, nil + every 2.hours do + rake 'blahblah' + end + file + end + + should "not use invoke through bundler" do assert_match two_hours + ' cd /my/path && RAILS_ENV=production rake blahblah --silent', @output end end @@ -157,7 +175,7 @@ class OutputDefaultDefinedJobsTest < Test::Unit::TestCase end should "output the rake command using that path" do - assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production rake blahblah --silent', @output + assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production bundle exec rake blahblah --silent', @output end end