2008-10-31 15:24:33 +03:00
|
|
|
require 'test/unit'
|
|
|
|
require 'fileutils'
|
|
|
|
require File.dirname(__FILE__) + '/lazy_dir'
|
|
|
|
|
|
|
|
class LazyDirTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
FileUtils.mkdir('test_glob_dir')
|
|
|
|
eval %{
|
|
|
|
Object.class_eval do
|
|
|
|
remove_const :Dir
|
|
|
|
remove_const :OrigDir rescue nil
|
|
|
|
end
|
|
|
|
::Dir = LazyDir
|
|
|
|
::OrigDir = LazyDir::OrigDir
|
|
|
|
}
|
|
|
|
%w(a b c d).each {|n| File.open("test_glob_dir/#{n}", 'w'){}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
eval %{
|
|
|
|
Object.class_eval { remove_const :Dir }
|
|
|
|
::Dir = OrigDir
|
|
|
|
}
|
|
|
|
FileUtils.rm_r('test_glob_dir')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lazy_glob
|
2008-11-01 09:33:06 +03:00
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE=4
|
|
|
|
OrigDir['test_glob_dir/*']
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
|
|
|
lazy = Thread.new do
|
|
|
|
$SAFE=4
|
|
|
|
Dir['test_glob_dir/*']
|
|
|
|
end.value
|
|
|
|
|
2008-10-31 15:24:33 +03:00
|
|
|
assert_equal OrigDir['test_glob_dir/*'], lazy.to_a
|
|
|
|
assert_equal OrigDir['test_glob_dir/*'], lazy.to_ary
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lazy_glob_flags
|
|
|
|
assert LazyDir.glob('*/A').to_a.empty?
|
|
|
|
assert_equal ['test_glob_dir/a'], LazyDir.glob('*/A', File::FNM_CASEFOLD).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lazy_glob_secure
|
|
|
|
assert LazyDir['/etc/passwd'].to_a.empty?
|
|
|
|
assert LazyDir['../../*'].to_a.empty?
|
|
|
|
|
|
|
|
puts "\nbig glob test... this may take a while"
|
2008-11-03 03:25:30 +03:00
|
|
|
orig = OrigDir['./**/*'].map {|f| File.expand_path(f) }
|
|
|
|
lazy = LazyDir['../**/*'].to_a.map {|f| File.expand_path(f) }
|
2008-10-31 15:24:33 +03:00
|
|
|
assert_equal orig, lazy
|
|
|
|
end
|
2008-10-31 15:43:02 +03:00
|
|
|
|
2008-11-03 03:25:30 +03:00
|
|
|
def test_lazy_dir_delegates_original_dir_methods
|
2008-10-31 15:43:02 +03:00
|
|
|
assert Dir.pwd
|
2008-11-01 09:33:06 +03:00
|
|
|
dir = 'asfasdfsaf'
|
|
|
|
assert Dir.mkdir(dir)
|
|
|
|
assert File.exist?(dir)
|
|
|
|
assert Dir.rmdir(dir)
|
|
|
|
assert ! File.exist?(dir)
|
2008-10-31 15:43:02 +03:00
|
|
|
end
|
2008-10-31 15:24:33 +03:00
|
|
|
end
|