Skip to content

Commit

Permalink
merged new implementation of the system dir stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Aug 31, 2008
2 parents 53ab09f + bc08e77 commit d1241fb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TAGS
temp_*
pkg
x
*.patch
.#*
x
61 changes: 36 additions & 25 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# as a library via a require statement, but it can be distributed
# independently as an application.

RAKEVERSION = '0.8.1.7'
RAKEVERSION = '0.8.1.6'

require 'rbconfig'
require 'getoptlong'
Expand Down Expand Up @@ -1888,6 +1888,7 @@ def initialize
@default_loader = Rake::DefaultLoader.new
@original_dir = Dir.pwd
@top_level_tasks = []
add_loader('rb', DefaultLoader.new)
add_loader('rf', DefaultLoader.new)
add_loader('rake', DefaultLoader.new)
@tty_output = STDOUT.tty?
Expand Down Expand Up @@ -1995,14 +1996,13 @@ def standard_exception_handling

# True if one of the files in RAKEFILES is in the current directory.
# If a match is found, it is copied into @rakefile.
def have_project_rakefile
def have_rakefile
@rakefiles.each do |fn|
if File.exist?(fn) || fn == ''
@rakefile = fn
return true
return fn
end
end
return false
return nil
end

# True if we are outputting to TTY, false otherwise
Expand Down Expand Up @@ -2075,7 +2075,7 @@ def unix?
def windows?
Config::CONFIG['host_os'] =~ /mswin/
end

def truncate(string, width)
if string.length <= width
string
Expand Down Expand Up @@ -2190,6 +2190,14 @@ def handle_options
options.silent = true
}
],
['--system', '-g',
"Using system wide (global) rakefiles (usually '~/.rake/*.rake').",
lambda { |value| options.load_system = true }
],
['--no-system', '-G',
"Use standard project Rakefile search paths, ignore system wide rakefiles.",
lambda { |value| options.ignore_system = true }
],
['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
lambda { |value|
options.show_tasks = true
Expand All @@ -2216,9 +2224,6 @@ def handle_options

options.rakelib = ['rakelib']

# opts = GetoptLong.new(*command_line_options)
# opts.each { |opt, value| do_option(opt, value) }

parsed_argv = nil
opts = OptionParser.new do |opts|
opts.banner = "rake [-f rakefile] {options} targets..."
Expand Down Expand Up @@ -2264,34 +2269,40 @@ def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
fail LoadError, "Can't find #{file_name}"
end

def raw_load_rakefile # :nodoc:
def find_rakefile_location
here = Dir.pwd
if (options.load_system || ! have_project_rakefile) && ! options.ignore_system && have_system_rakefiles
while ! (fn = have_rakefile)
Dir.chdir("..")
if Dir.pwd == here || options.nosearch
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})"
end
here = Dir.pwd
end
[fn, here]
ensure
Dir.chdir(Rake.original_dir)
end

def raw_load_rakefile # :nodoc:
rakefile, location = find_rakefile_location
if (! options.ignore_system) && (options.load_system || rakefile.nil?)
puts "(in #{Dir.pwd})" unless options.silent
Dir["#{system_dir}/*.rake"].each do |name|
add_import name
end
else
while ! have_project_rakefile
Dir.chdir("..")
if Dir.pwd == here || options.nosearch
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})"
end
here = Dir.pwd
end
@rakefile = rakefile
Dir.chdir(location)
puts "(in #{Dir.pwd})" unless options.silent
$rakefile = @rakefile
load File.expand_path(@rakefile) if @rakefile && @rakefile != ''
end
puts "(in #{Dir.pwd})" unless options.silent
$rakefile = @rakefile
load File.expand_path(@rakefile) if @rakefile && @rakefile != ''
options.rakelib.each do |rlib|
Dir["#{rlib}/*.rake"].each do |name| add_import name end
end
load_imports
end

def have_system_rakefiles
Dir[File.join(system_dir, '*.rake')].size > 0
end

# The directory path containing the system wide rakefiles.
def system_dir
if ENV['RAKE_SYSTEM']
Expand Down
3 changes: 3 additions & 0 deletions test/data/sys/sys1.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
task "sys1" do
puts "SYS1"
end
21 changes: 20 additions & 1 deletion test/session_functional.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env ruby

require 'rubygems'
begin
require 'rubygems'
rescue LoadError => ex
end
require 'test/unit'
require 'fileutils'
require 'session'
Expand Down Expand Up @@ -86,6 +89,22 @@ def test_rbext
assert_match %r{^OK$}, @out
end

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

def test_no_system
ENV['RAKE_SYSTEM'] = 'test/data/sys'
rake '-G', "sys1"
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
Expand Down
114 changes: 49 additions & 65 deletions test/test_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,27 @@ def test_display_tasks
end

def test_display_tasks_with_long_comments
ENV['RAKE_COLUMNS'] = '80'
@app.options.show_task_pattern = //
@app.last_description = "1234567890" * 8
@app.define_task(Rake::Task, "t")
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
assert_match(/^rake t/, out)
assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
ensure
ENV['RAKE_COLUMNS'] = nil
in_environment('RAKE_COLUMNS' => '80') do
@app.options.show_task_pattern = //
@app.last_description = "1234567890" * 8
@app.define_task(Rake::Task, "t")
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
assert_match(/^rake t/, out)
assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
end
end

def test_display_tasks_with_task_name_wider_than_tty_display
ENV['RAKE_COLUMNS'] = '80'
@app.options.show_task_pattern = //
description = "something short"
task_name = "task name" * 80
@app.last_description = "something short"
@app.define_task(Rake::Task, task_name )
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
# Ensure the entire task name is output and we end up showing no description
assert_match(/rake #{task_name} # .../, out)
ensure
ENV['RAKE_COLUMNS'] = nil
in_environment('RAKE_COLUMNS' => '80') do
@app.options.show_task_pattern = //
description = "something short"
task_name = "task name" * 80
@app.last_description = "something short"
@app.define_task(Rake::Task, task_name )
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
# Ensure the entire task name is output and we end up showing no description
assert_match(/rake #{task_name} # .../, out)
end
end

def test_display_tasks_with_very_long_task_name_to_a_non_tty_shows_name_and_comment
Expand All @@ -87,16 +85,15 @@ def test_display_tasks_with_long_comments_to_a_non_tty_shows_entire_comment
end

def test_display_tasks_with_long_comments_to_a_non_tty_with_columns_set_truncates_comments
ENV['RAKE_COLUMNS'] = '80'
@app.options.show_task_pattern = //
@app.tty_output = false
@app.last_description = "1234567890" * 8
@app.define_task(Rake::Task, "t")
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
assert_match(/^rake t/, out)
assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
ensure
ENV['RAKE_COLUMNS'] = nil
in_environment("RAKE_COLUMNS" => '80') do
@app.options.show_task_pattern = //
@app.tty_output = false
@app.last_description = "1234567890" * 8
@app.define_task(Rake::Task, "t")
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
assert_match(/^rake t/, out)
assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
end
end

def test_display_tasks_with_full_descriptions
Expand All @@ -110,51 +107,44 @@ def test_display_tasks_with_full_descriptions
end

def test_finding_rakefile
assert @app.instance_eval { have_project_rakefile }
assert_equal "rakefile", @app.rakefile.downcase
assert_equal "rakefile", @app.instance_eval { have_rakefile }
end

def test_not_finding_rakefile
@app.instance_eval { @rakefiles = ['NEVER_FOUND'] }
assert( ! @app.instance_eval do have_project_rakefile end )
assert( ! @app.instance_eval do have_rakefile end )
assert_nil @app.rakefile
end

def test_load_rakefile
original_dir = Dir.pwd
Dir.chdir("test/data/unittest")
@app.instance_eval do
handle_options
options.silent = true
options.ignore_system = true
load_rakefile
end
assert_equal "rakefile", @app.rakefile.downcase
assert_match(%r(unittest$), Dir.pwd)
ensure
Dir.chdir(original_dir)
in_environment("PWD" => "test/data/unittest") do
@app.instance_eval do
handle_options
options.silent = true
load_rakefile
end
assert_equal "rakefile", @app.rakefile.downcase
assert_match(%r(unittest$), Dir.pwd)
end
end

def test_load_rakefile_from_subdir
original_dir = Dir.pwd
Dir.chdir("test/data/unittest/subdir")
@app.instance_eval do
handle_options
options.silent = true
load_rakefile
in_environment("PWD" => "test/data/unittest/subdir") do
@app.instance_eval do
handle_options
options.silent = true
load_rakefile
end
assert_equal "rakefile", @app.rakefile.downcase
assert_match(%r(unittest$), Dir.pwd)
end
assert_equal "rakefile", @app.rakefile.downcase
assert_match(%r(unittest$), Dir.pwd)
ensure
Dir.chdir(original_dir)
end

def test_load_rakefile_not_found
in_environment("PWD" => "/") do
@app.instance_eval do
handle_options
options.silent = true
options.ignore_system = true
end
ex = assert_raise(RuntimeError) do
@app.instance_eval do raw_load_rakefile end
Expand All @@ -164,25 +154,19 @@ def test_load_rakefile_not_found
end

def test_load_from_system_rakefile
in_environment('RAKE_SYSTEM' => 'test') do
in_environment('RAKE_SYSTEM' => 'test/data/sys') do
@app.options.rakelib = []
@app.instance_eval do
handle_options
options.silent = true
options.load_system = true
load_rakefile
end
assert_equal "test", @app.system_dir
assert_equal "test/data/sys", @app.system_dir
assert_nil @app.rakefile
end
end

def test_not_caring_about_finding_rakefile
@app.instance_eval do @rakefiles = [''] end
assert(@app.instance_eval do have_project_rakefile end)
assert_equal '', @app.rakefile
end

def test_loading_imports
mock = flexmock("loader")
mock.should_receive(:load).with("x.dummy").once
Expand Down Expand Up @@ -283,7 +267,7 @@ def test_run_with_bad_options
end

private

def set_env(settings)
result = {}
settings.each do |k, v|
Expand Down

0 comments on commit d1241fb

Please sign in to comment.