Sha256: b13134611378239f625d24f7a346271ff0e00811b062326282a02647dbdbe08e

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'safegem/lazy_dir'

require 'test/unit'
require 'fileutils'

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
    if PLATFORM !~ /darwin/
      # this will fail on osx because of fs case insensitivity, so don't run in there
      assert LazyDir.glob('*/A').to_a.empty?
    end
    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?

    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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
github-safegem-0.1.2 test/lazy_dir_test.rb
github-safegem-0.1.3 test/lazy_dir_test.rb
github-safegem-0.2.0 test/lazy_dir_test.rb
github-safegem-0.2.10 test/lazy_dir_test.rb
github-safegem-0.2.2 test/lazy_dir_test.rb
github-safegem-0.2.3 test/lazy_dir_test.rb
github-safegem-0.2.4 test/lazy_dir_test.rb
github-safegem-0.2.5 test/lazy_dir_test.rb
github-safegem-0.2.6 test/lazy_dir_test.rb
github-safegem-0.2.7 test/lazy_dir_test.rb
github-safegem-0.2.8 test/lazy_dir_test.rb
github-safegem-0.2.9 test/lazy_dir_test.rb