2011-06-23 18:11:55 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2009-10-02 15:07:55 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'stringio'
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
class TestRakeFileUtils < Rake::TestCase
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def teardown
|
|
|
|
FileUtils::LN_SUPPORTED[0] = true
|
2011-06-27 22:45:29 -04:00
|
|
|
RakeFileUtils.verbose_flag = Rake::FileUtilsExt::DEFAULT
|
2011-06-23 18:11:55 -04:00
|
|
|
|
|
|
|
super
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def test_rm_one_file
|
2011-06-23 18:11:55 -04:00
|
|
|
create_file("a")
|
|
|
|
FileUtils.rm_rf "a"
|
|
|
|
refute File.exist?("a")
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rm_two_files
|
2011-06-23 18:11:55 -04:00
|
|
|
create_file("a")
|
|
|
|
create_file("b")
|
|
|
|
FileUtils.rm_rf ["a", "b"]
|
|
|
|
refute File.exist?("a")
|
|
|
|
refute File.exist?("b")
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rm_filelist
|
2011-06-23 18:11:55 -04:00
|
|
|
list = Rake::FileList.new << "a" << "b"
|
2009-10-02 15:07:55 -04:00
|
|
|
list.each { |fn| create_file(fn) }
|
|
|
|
FileUtils.rm_r list
|
2011-06-23 18:11:55 -04:00
|
|
|
refute File.exist?("a")
|
|
|
|
refute File.exist?("b")
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ln
|
2011-06-23 18:11:55 -04:00
|
|
|
open("a", "w") { |f| f.puts "TEST_LN" }
|
|
|
|
|
|
|
|
Rake::FileUtilsExt.safe_ln("a", "b", :verbose => false)
|
|
|
|
|
|
|
|
assert_equal "TEST_LN\n", File.read('b')
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class BadLink
|
2011-06-23 18:11:55 -04:00
|
|
|
include Rake::FileUtilsExt
|
2009-10-02 15:07:55 -04:00
|
|
|
attr_reader :cp_args
|
2013-10-11 17:35:01 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def initialize(klass)
|
|
|
|
@failure_class = klass
|
|
|
|
end
|
2013-10-11 17:35:01 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def cp(*args)
|
|
|
|
@cp_args = args
|
|
|
|
end
|
2013-10-11 17:35:01 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def ln(*args)
|
|
|
|
fail @failure_class, "ln not supported"
|
|
|
|
end
|
2013-10-11 17:35:01 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
public :safe_ln
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_ln_failover_to_cp_on_standard_error
|
|
|
|
FileUtils::LN_SUPPORTED[0] = true
|
|
|
|
c = BadLink.new(StandardError)
|
|
|
|
c.safe_ln "a", "b"
|
|
|
|
assert_equal ['a', 'b'], c.cp_args
|
|
|
|
c.safe_ln "x", "y"
|
|
|
|
assert_equal ['x', 'y'], c.cp_args
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_ln_failover_to_cp_on_not_implemented_error
|
|
|
|
FileUtils::LN_SUPPORTED[0] = true
|
|
|
|
c = BadLink.new(NotImplementedError)
|
|
|
|
c.safe_ln "a", "b"
|
|
|
|
assert_equal ['a', 'b'], c.cp_args
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_ln_fails_on_script_error
|
|
|
|
FileUtils::LN_SUPPORTED[0] = true
|
|
|
|
c = BadLink.new(ScriptError)
|
2011-06-23 18:11:55 -04:00
|
|
|
assert_raises(ScriptError) do c.safe_ln "a", "b" end
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verbose
|
|
|
|
verbose true
|
|
|
|
assert_equal true, verbose
|
|
|
|
verbose false
|
|
|
|
assert_equal false, verbose
|
|
|
|
verbose(true) {
|
|
|
|
assert_equal true, verbose
|
|
|
|
}
|
|
|
|
assert_equal false, verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nowrite
|
|
|
|
nowrite true
|
|
|
|
assert_equal true, nowrite
|
|
|
|
nowrite false
|
|
|
|
assert_equal false, nowrite
|
2013-10-11 17:35:01 -04:00
|
|
|
nowrite(true) {
|
2009-10-02 15:07:55 -04:00
|
|
|
assert_equal true, nowrite
|
|
|
|
}
|
|
|
|
assert_equal false, nowrite
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_utils_methods_are_available_at_top_level
|
2011-06-23 18:11:55 -04:00
|
|
|
create_file("a")
|
2011-06-27 22:45:29 -04:00
|
|
|
|
|
|
|
capture_io do
|
|
|
|
rm_rf "a"
|
|
|
|
end
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
refute File.exist?("a")
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fileutils_methods_dont_leak
|
|
|
|
obj = Object.new
|
2011-06-23 18:11:55 -04:00
|
|
|
assert_raises(NoMethodError) { obj.copy } # from FileUtils
|
|
|
|
assert_raises(NoMethodError) { obj.ruby "-v" } # from RubyFileUtils
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
2011-07-23 01:38:50 -04:00
|
|
|
verbose(false) { sh %{#{Rake::TestCase::RUBY} shellcommand.rb} }
|
2009-10-02 15:07:55 -04:00
|
|
|
assert true, "should not fail"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_with_a_single_string_argument
|
2011-06-23 18:11:55 -04:00
|
|
|
check_expansion
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
ENV['RAKE_TEST_SH'] = 'someval'
|
|
|
|
verbose(false) {
|
2011-07-23 01:38:50 -04:00
|
|
|
sh %{#{RUBY} check_expansion.rb #{env_var} someval}
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_with_multiple_arguments
|
2011-06-23 18:11:55 -04:00
|
|
|
check_no_expansion
|
2009-10-02 15:07:55 -04:00
|
|
|
ENV['RAKE_TEST_SH'] = 'someval'
|
2011-06-23 18:11:55 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
verbose(false) {
|
2011-07-23 01:38:50 -04:00
|
|
|
sh RUBY, 'check_no_expansion.rb', env_var, 'someval'
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_failure
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
|
|
|
assert_raises(RuntimeError) {
|
2011-07-23 01:38:50 -04:00
|
|
|
verbose(false) { sh %{#{RUBY} shellcommand.rb 1} }
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_special_handling
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
count = 0
|
|
|
|
verbose(false) {
|
2011-07-23 01:38:50 -04:00
|
|
|
sh(%{#{RUBY} shellcommand.rb}) do |ok, res|
|
2009-10-02 15:07:55 -04:00
|
|
|
assert(ok)
|
|
|
|
assert_equal 0, res.exitstatus
|
|
|
|
count += 1
|
|
|
|
end
|
2011-07-23 01:38:50 -04:00
|
|
|
sh(%{#{RUBY} shellcommand.rb 1}) do |ok, res|
|
2009-10-02 15:07:55 -04:00
|
|
|
assert(!ok)
|
|
|
|
assert_equal 1, res.exitstatus
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal 2, count, "Block count should be 2"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_noop
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
|
|
|
verbose(false) { sh %{shellcommand.rb 1}, :noop=>true }
|
2009-10-02 15:07:55 -04:00
|
|
|
assert true, "should not fail"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_bad_option
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
|
|
|
ex = assert_raises(ArgumentError) {
|
|
|
|
verbose(false) { sh %{shellcommand.rb}, :bad_option=>true }
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
assert_match(/bad_option/, ex.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sh_verbose
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
|
|
|
_, err = capture_io do
|
2009-10-02 15:07:55 -04:00
|
|
|
verbose(true) {
|
2011-06-23 18:11:55 -04:00
|
|
|
sh %{shellcommand.rb}, :noop=>true
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
2011-06-23 18:11:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "shellcommand.rb\n", err
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
2011-06-27 22:45:29 -04:00
|
|
|
def test_sh_verbose_false
|
2011-06-23 18:11:55 -04:00
|
|
|
shellcommand
|
|
|
|
|
|
|
|
_, err = capture_io do
|
2009-10-02 15:07:55 -04:00
|
|
|
verbose(false) {
|
2011-06-23 18:11:55 -04:00
|
|
|
sh %{shellcommand.rb}, :noop=>true
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
2011-06-23 18:11:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal '', err
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
2011-06-27 22:45:29 -04:00
|
|
|
def test_sh_verbose_flag_nil
|
|
|
|
shellcommand
|
|
|
|
|
|
|
|
RakeFileUtils.verbose_flag = nil
|
|
|
|
|
|
|
|
assert_silent do
|
|
|
|
sh %{shellcommand.rb}, :noop=>true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def test_ruby_with_a_single_string_argument
|
2011-06-23 18:11:55 -04:00
|
|
|
check_expansion
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
ENV['RAKE_TEST_SH'] = 'someval'
|
2011-06-23 18:11:55 -04:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
verbose(false) {
|
2011-07-23 01:38:50 -04:00
|
|
|
replace_ruby {
|
|
|
|
ruby %{check_expansion.rb #{env_var} someval}
|
|
|
|
}
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ruby_with_multiple_arguments
|
2011-06-23 18:11:55 -04:00
|
|
|
check_no_expansion
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
ENV['RAKE_TEST_SH'] = 'someval'
|
|
|
|
verbose(false) {
|
2011-07-23 01:38:50 -04:00
|
|
|
replace_ruby {
|
|
|
|
ruby 'check_no_expansion.rb', env_var, 'someval'
|
|
|
|
}
|
2009-10-02 15:07:55 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_all
|
2011-06-23 18:11:55 -04:00
|
|
|
assert_equal ['a'], Rake::FileUtilsExt.split_all('a')
|
|
|
|
assert_equal ['..'], Rake::FileUtilsExt.split_all('..')
|
|
|
|
assert_equal ['/'], Rake::FileUtilsExt.split_all('/')
|
|
|
|
assert_equal ['a', 'b'], Rake::FileUtilsExt.split_all('a/b')
|
|
|
|
assert_equal ['/', 'a', 'b'], Rake::FileUtilsExt.split_all('/a/b')
|
|
|
|
assert_equal ['..', 'a', 'b'], Rake::FileUtilsExt.split_all('../a/b')
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
2013-10-11 17:35:01 -04:00
|
|
|
def command(name, text)
|
2011-06-23 18:11:55 -04:00
|
|
|
open name, 'w', 0750 do |io|
|
|
|
|
io << text
|
|
|
|
end
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
def check_no_expansion
|
|
|
|
command 'check_no_expansion.rb', <<-CHECK_EXPANSION
|
|
|
|
if ARGV[0] != ARGV[1]
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
CHECK_EXPANSION
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
def check_expansion
|
|
|
|
command 'check_expansion.rb', <<-CHECK_EXPANSION
|
|
|
|
if ARGV[0] != ARGV[1]
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
CHECK_EXPANSION
|
|
|
|
end
|
|
|
|
|
2011-07-23 01:38:50 -04:00
|
|
|
def replace_ruby
|
|
|
|
ruby = FileUtils::RUBY
|
|
|
|
FileUtils.send :remove_const, :RUBY
|
|
|
|
FileUtils.const_set :RUBY, RUBY
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
FileUtils.send :remove_const, :RUBY
|
|
|
|
FileUtils.const_set:RUBY, ruby
|
|
|
|
end
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
def shellcommand
|
|
|
|
command 'shellcommand.rb', <<-SHELLCOMMAND
|
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
exit((ARGV[0] || "0").to_i)
|
|
|
|
SHELLCOMMAND
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def env_var
|
|
|
|
windows? ? '%RAKE_TEST_SH%' : '$RAKE_TEST_SH'
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
def windows?
|
|
|
|
! File::ALT_SEPARATOR.nil?
|
|
|
|
end
|
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|