public
Description: The scripts used to build RubyGems on GitHub
Homepage:
Clone URL: git://github.com/pjhyett/github-gem-builder.git
github-gem-builder / lazy_dir_test.rb
100644 68 lines (58 sloc) 1.63 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
    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
 
    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"
    orig = OrigDir['./**/*'].map {|f| File.expand_path(f) }
    lazy = LazyDir['../**/*'].to_a.map {|f| File.expand_path(f) }
    assert_equal orig, lazy
  end
 
  def test_lazy_dir_delegates_original_dir_methods
    assert Dir.pwd
    dir = 'asfasdfsaf'
    assert Dir.mkdir(dir)
    assert File.exist?(dir)
    assert Dir.rmdir(dir)
    assert ! File.exist?(dir)
  end
end