Skip to content

Commit

Permalink
fixed finding global rakefiles when no project available. Fixed nosea…
Browse files Browse the repository at this point in the history
…rch / global search issues.
  • Loading branch information
jimweirich committed Sep 1, 2008
1 parent ca502bb commit f29ae0a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 64 deletions.
8 changes: 6 additions & 2 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ def find_rakefile_location
while ! (fn = have_rakefile)
Dir.chdir("..")
if Dir.pwd == here || options.nosearch
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})"
return nil
end
here = Dir.pwd
end
Expand All @@ -2285,12 +2285,16 @@ def find_rakefile_location

def raw_load_rakefile # :nodoc:
rakefile, location = find_rakefile_location
if (! options.ignore_system) && (options.load_system || rakefile.nil?)
if (! options.ignore_system) &&
(options.load_system || rakefile.nil?) &&
File.directory?(system_dir)
puts "(in #{Dir.pwd})" unless options.silent
Dir["#{system_dir}/*.rake"].each do |name|
add_import name
end
else
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})" if
rakefile.nil?
@rakefile = rakefile
Dir.chdir(location)
puts "(in #{Dir.pwd})" unless options.silent
Expand Down
1 change: 1 addition & 0 deletions test/data/nosearch/dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy file to give the directory some content.
28 changes: 28 additions & 0 deletions test/in_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module InEnvironment
private

# Create an environment for a test. At the completion of the yielded
# block, the environment is restored to its original conditions.
def in_environment(settings)
original_dir = Dir.pwd
original_settings = set_env(settings)
yield
ensure
set_env(original_settings)
end

# Set the environment according to the settings hash.
def set_env(settings) # :nodoc:
result = {}
settings.each do |k, v|
result[k] = ENV[k]
if k == 'PWD'
Dir.chdir(v)
else
ENV[k] = v
end
end
result
end

end
117 changes: 80 additions & 37 deletions test/session_functional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'test/unit'
require 'fileutils'
require 'session'
require 'test/in_environment'

# Version 2.1.9 of session has a bug where the @debug instance
# variable is not initialized, causing warning messages. This snippet
Expand All @@ -22,6 +23,7 @@ def initialize(*args)
end

class FunctionalTest < Test::Unit::TestCase
include InEnvironment

RUBY_COMMAND = 'ruby'

Expand Down Expand Up @@ -66,53 +68,82 @@ def test_env_availabe_at_task_scope
end

def test_multi_desc
ENV['RAKE_COLUMNS'] = '80'
Dir.chdir("test/data/multidesc") do rake "-T" end
in_environment(
'RAKE_COLUMNS' => "80",
"PWD" => "test/data/multidesc"
) do
rake "-T"
end
assert_match %r{^rake a *# A / A2 *$}, @out
assert_match %r{^rake b *# B *$}, @out
assert_no_match %r{^rake c}, @out
assert_match %r{^rake d *# x{65}\.\.\.$}, @out
ensure
ENV['RAKE_COLUMNS'] = nil
end

def test_long_description
Dir.chdir("test/data/multidesc") do rake "--describe" end
in_environment("PWD" => "test/data/multidesc") do
rake "--describe"
end
assert_match %r{^rake a\n *A / A2 *$}m, @out
assert_match %r{^rake b\n *B *$}m, @out
assert_match %r{^rake d\n *x{80}}m, @out
assert_no_match %r{^rake c\n}m, @out
end

def test_rbext
Dir.chdir("test/data/rbext") do rake "-N" end
in_environment("PWD" => "test/data/rbext") do
rake "-N"
end
assert_match %r{^OK$}, @out
end

def test_system
ENV['RAKE_SYSTEM'] = 'test/data/sys'
rake '-g', "sys1"
in_environment('RAKE_SYSTEM' => 'test/data/sys') do
rake '-g', "sys1"
end
assert_match %r{^SYS1}, @out
end

def test_implicit_system
in_environment('RAKE_SYSTEM' => File.expand_path('test/data/sys'), "PWD" => "/") do
rake "sys1", "--trace"
end
assert_match %r{^SYS1}, @out
ensure
ENV['RAKE_SYSTEM'] = nil
end

def test_no_system
ENV['RAKE_SYSTEM'] = 'test/data/sys'
rake '-G', "sys1"
in_environment('RAKE_SYSTEM' => 'test/data/sys') do
rake '-G', "sys1"
end
assert_match %r{^Don't know how to build task}, @err # emacs wart: '
ensure
ENV['RAKE_SYSTEM'] = nil
end

def test_nosearch
mkdir_p "test/data/nosearch", :verbose => false rescue nil
Dir.chdir("test/data/nosearch") do rake "-N" end
def test_nosearch_with_rakefile_uses_local_rakefile
in_environment("PWD" => "test/data/default") do
rake "--nosearch"
end
assert_match %r{^DEFAULT}, @out
end

def test_nosearch_without_rakefile_finds_system
in_environment(
"PWD" => "test/data/nosearch",
"RAKE_SYSTEM" => File.expand_path("test/data/sys")
) do
rake "--nosearch", "sys1"
end
assert_match %r{^SYS1}, @out
end

def test_nosearch_without_rakefile_and_no_system_fails
in_environment("PWD" => "test/data/nosearch", "RAKE_SYSTEM" => "not_exist") do
rake "--nosearch"
end
assert_match %r{^No Rakefile found}, @err
end

def test_dry_run
Dir.chdir("test/data/default") do rake "-n", "other" end
in_environment("PWD" => "test/data/default") do rake "-n", "other" end
assert_match %r{Execute \(dry run\) default}, @out
assert_match %r{Execute \(dry run\) other}, @out
assert_no_match %r{DEFAULT}, @out
Expand All @@ -121,18 +152,26 @@ def test_dry_run

# Test for the trace/dry_run bug found by Brian Chandler
def test_dry_run_bug
Dir.chdir("test/data/dryrun") do rake end
in_environment("PWD" => "test/data/dryrun") do
rake
end
FileUtils.rm_f "test/data/dryrun/temp_one"
Dir.chdir("test/data/dryrun") do rake "--dry-run" end
in_environment("PWD" => "test/data/dryrun") do
rake "--dry-run"
end
assert_no_match(/No such file/, @out)
assert_status
end

# Test for the trace/dry_run bug found by Brian Chandler
def test_trace_bug
Dir.chdir("test/data/dryrun") do rake end
in_environment("PWD" => "test/data/dryrun") do
rake
end
FileUtils.rm_f "test/data/dryrun/temp_one"
Dir.chdir("test/data/dryrun") do rake "--trace" end
in_environment("PWD" => "test/data/dryrun") do
rake "--trace"
end
assert_no_match(/No such file/, @out)
assert_status
end
Expand All @@ -142,7 +181,9 @@ def test_imports
f.puts 'puts "STATIC"'
end
FileUtils.rm_f "test/data/imports/dynamic_deps"
Dir.chdir("test/data/imports") do rake end
in_environment("PWD" => "test/data/imports") do
rake
end
assert File.exist?("test/data/imports/dynamic_deps"),
"'dynamic_deps' file should exist"
assert_match(/^FIRST$\s+^DYNAMIC$\s+^STATIC$\s+^OTHER$/, @out)
Expand All @@ -153,20 +194,22 @@ def test_imports

def test_rules_chaining_to_file_task
remove_chaining_files
Dir.chdir("test/data/chains") do rake end
in_environment("PWD" => "test/data/chains") do
rake
end
assert File.exist?("test/data/chains/play.app"),
"'play.app' file should exist"
assert_status
remove_chaining_files
end

def test_file_creation_task
Dir.chdir("test/data/file_creation_task") do
in_environment("PWD" => "test/data/file_creation_task") do
rake "prep"
rake "run"
rake "run"
assert(@err !~ /^cp src/, "Should not recopy data")
end
assert(@err !~ /^cp src/, "Should not recopy data")
end

def test_dash_f_with_no_arg_foils_rakefile_lookup
Expand All @@ -180,63 +223,63 @@ def test_dot_rake_files_can_be_loaded_with_dash_r
end

def test_can_invoke_task_in_toplevel_namespace
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "copy"
assert_match(/^COPY$/, @out)
end
assert_match(/^COPY$/, @out)
end

def test_can_invoke_task_in_nested_namespace
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "nest:copy"
assert_match(/^NEST COPY$/, @out)
end
end

def test_tasks_can_reference_task_in_same_namespace
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "nest:xx"
assert_match(/^NEST COPY$/m, @out)
end
end

def test_tasks_can_reference_task_in_other_namespaces
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "b:run"
assert_match(/^IN A\nIN B$/m, @out)
end
end

def test_anonymous_tasks_can_be_invoked_indirectly
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "anon"
assert_match(/^ANON COPY$/m, @out)
end
end

def test_rake_namespace_refers_to_toplevel
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "very:nested:run"
assert_match(/^COPY$/m, @out)
end
end

def test_file_task_are_not_scoped_by_namespaces
Dir.chdir("test/data/namespace") do
in_environment("PWD" => "test/data/namespace") do
rake "xyz.rb"
assert_match(/^XYZ1\nXYZ2$/m, @out)
end
end

def test_rake_returns_status_error_values
Dir.chdir("test/data/statusreturn") do
in_environment("PWD" => "test/data/statusreturn") do
rake "exit5"
assert_status(5)
end
end

def test_rake_returns_no_status_error_on_normal_exit
Dir.chdir("test/data/statusreturn") do
in_environment("PWD" => "test/data/statusreturn") do
rake "normal"
assert_status(0)
end
Expand Down
28 changes: 3 additions & 25 deletions test/test_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
require 'rake'
require 'test/rake_test_setup'
require 'test/capture_stdout'
require 'test/in_environment'

TESTING_REQUIRE = [ ]

######################################################################
class TestApplication < Test::Unit::TestCase
include CaptureStdout
include InEnvironment

def setup
@app = Rake::Application.new
Expand Down Expand Up @@ -141,7 +143,7 @@ def test_load_rakefile_from_subdir
end

def test_load_rakefile_not_found
in_environment("PWD" => "/") do
in_environment("PWD" => "/", "RAKE_SYSTEM" => 'not_exist') do
@app.instance_eval do
handle_options
options.silent = true
Expand Down Expand Up @@ -265,30 +267,6 @@ def test_run_with_bad_options
ensure
ARGV.clear
end

private

def set_env(settings)
result = {}
settings.each do |k, v|
result[k] = ENV[k]
if k == 'PWD'
Dir.chdir(v)
else
ENV[k] = v
end
end
result
end

def in_environment(settings)
original_dir = Dir.pwd
original_settings = set_env(settings)
yield
ensure
set_env(original_settings)
end

end


Expand Down

0 comments on commit f29ae0a

Please sign in to comment.