1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

envutil.rb: assert_file and assert_file_not

* test/ruby/envutil.rb (assert_file, assert_file_not): more
  descriptive assertions for File predicates.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37127 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-10-09 14:18:05 +00:00
parent e3efce6df1
commit 428ec4ec05
8 changed files with 33 additions and 18 deletions

View file

@ -1,3 +1,8 @@
Tue Oct 9 23:18:03 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* test/ruby/envutil.rb (assert_file, assert_file_not): more
descriptive assertions for File predicates.
Tue Oct 9 18:01:37 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_sample): use rb_random_ulong_limited, since

View file

@ -224,6 +224,14 @@ module Test
def assert_is_minus_zero(f)
assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0")
end
def assert_file(predicate, path)
assert(File.__send__(predicate, path), "Expected file #{path.inspect} to be #{predicate}")
end
def assert_file_not(predicate, path)
assert(!File.__send__(predicate, path), "Expected file #{path.inspect} not to be #{predicate}")
end
end
end
end

View file

@ -44,7 +44,7 @@ class TestFile < Test::Unit::TestCase
bug6487 = '[ruby-core:45203]'
f = Tempfile.new(__method__.to_s)
f.close
assert File.exist? f.path
assert_file(:exist?, f.path)
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:utf-8')}
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:bom|utf-8')}
f.close(true)

View file

@ -1,6 +1,7 @@
require "test/unit"
require "fileutils"
require "tmpdir"
require_relative "envutil"
class TestFileExhaustive < Test::Unit::TestCase
DRIVE = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
@ -119,7 +120,7 @@ class TestFileExhaustive < Test::Unit::TestCase
Dir.mktmpdir do |dir|
prefix = File.join(dir, "...a")
Dir.mkdir(prefix)
assert File.exist?(prefix)
assert_file(:exist?, prefix)
assert_nothing_raised { File.stat(prefix) }
@ -169,9 +170,9 @@ class TestFileExhaustive < Test::Unit::TestCase
end
def test_exist_p
assert(File.exist?(@dir))
assert(File.exist?(@file))
assert(!(File.exist?(@nofile)))
assert_file(:exist?, @dir)
assert_file(:exist?, @file)
assert_file_not(:exist?, @nofile)
end
def test_readable_p
@ -398,8 +399,8 @@ class TestFileExhaustive < Test::Unit::TestCase
def test_rename
assert_equal(0, File.rename(@file, @nofile))
assert(!(File.exist?(@file)))
assert(File.exist?(@nofile))
assert_file_not(:exist?, @file)
assert_file(:exist?, @nofile)
assert_equal(0, File.rename(@nofile, @file))
assert_raise(Errno::ENOENT) { File.rename(@nofile, @file) }
end
@ -786,21 +787,21 @@ class TestFileExhaustive < Test::Unit::TestCase
def test_truncate
assert_equal(0, File.truncate(@file, 1))
assert(File.exist?(@file))
assert_file(:exist?, @file)
assert_equal(1, File.size(@file))
assert_equal(0, File.truncate(@file, 0))
assert(File.exist?(@file))
assert(File.zero?(@file))
assert_file(:exist?, @file)
assert_file(:zero?, @file)
make_file("foo", @file)
assert_raise(Errno::ENOENT) { File.truncate(@nofile, 0) }
f = File.new(@file, "w")
assert_equal(0, f.truncate(2))
assert(File.exist?(@file))
assert_file(:exist?, @file)
assert_equal(2, File.size(@file))
assert_equal(0, f.truncate(0))
assert(File.exist?(@file))
assert(File.zero?(@file))
assert_file(:exist?, @file)
assert_file(:zero?, @file)
f.close
make_file("foo", @file)

View file

@ -403,8 +403,8 @@ class TestProcess < Test::Unit::TestCase
with_tmpchdir {|d|
Dir.mkdir "foo"
system(*PWD, :chdir => "foo", :out => "open_chdir_test")
assert(File.exist?("open_chdir_test"))
assert(!File.exist?("foo/open_chdir_test"))
assert_file(:exist?, "open_chdir_test")
assert_file_not(:exist?, "foo/open_chdir_test")
assert_equal("#{d}/foo", File.read("open_chdir_test").chomp)
}
end

View file

@ -109,7 +109,7 @@ class TestRequire < Test::Unit::TestCase
Dir.mktmpdir do |tmp|
req = File.join(tmp, "very_long_file_name.rb")
File.write(req, "p :ok\n")
assert(File.exist?(req))
assert_file(:exist?, req)
req[/.rb$/i] = ""
assert_in_out_err(['--disable-gems'], <<-INPUT, %w(:ok), [])
require "#{req}"

View file

@ -388,7 +388,7 @@ class TestRubyOptions < Test::Unit::TestCase
rubybin = Regexp.quote(EnvUtil.rubybin)
pat = Regexp.quote(notexist)
bug1573 = '[ruby-core:23717]'
assert_equal(false, File.exist?(notexist))
assert_file_not(:exist?, notexist)
assert_in_out_err(["-r", notexist, "-ep"], "", [], /.* -- #{pat} \(LoadError\)/, bug1573)
assert_in_out_err([notexist], "", [], /#{rubybin}:.* -- #{pat} \(LoadError\)/, bug1573)
end

View file

@ -1,5 +1,6 @@
require 'test/unit'
require 'tmpdir'
require_relative 'envutil'
class TestWhileuntil < Test::Unit::TestCase
def test_while
@ -68,7 +69,7 @@ class TestWhileuntil < Test::Unit::TestCase
tmp.close
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
assert(!File.exist?(tmpfilename))
assert_file_not(:exist?, tmpfilename)
}
end