ruby-changes:38145
From: akr <ko1@a...>
Date: Sat, 11 Apr 2015 15:57:58 +0900 (JST)
Subject: [ruby-changes:38145] akr:r50226 (trunk): * test/ruby/test_file_exhaustive.rb: Create sample files lazily.
akr 2015-04-11 15:57:37 +0900 (Sat, 11 Apr 2015) New Revision: 50226 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50226 Log: * test/ruby/test_file_exhaustive.rb: Create sample files lazily. Modified files: trunk/ChangeLog trunk/test/ruby/test_file_exhaustive.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50225) +++ ChangeLog (revision 50226) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Apr 11 15:56:58 2015 Tanaka Akira <akr@f...> + + * test/ruby/test_file_exhaustive.rb: Create sample files lazily. + Sat Apr 11 14:03:47 2015 Tanaka Akira <akr@f...> * test/ruby/test_file_exhaustive.rb: Test character device using Index: test/ruby/test_file_exhaustive.rb =================================================================== --- test/ruby/test_file_exhaustive.rb (revision 50225) +++ test/ruby/test_file_exhaustive.rb (revision 50226) @@ -15,51 +15,102 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L15 def setup @dir = Dir.mktmpdir("rubytest-file") - @rootdir = "#{DRIVE}/" File.chown(-1, Process.gid, @dir) - @file = make_tmp_filename("file") - @zerofile = make_tmp_filename("zerofile") + end + + def teardown + GC.start + FileUtils.remove_entry_secure @dir + end + + def make_tmp_filename(prefix) + "#{@dir}/#{prefix}.test" + end + + def rootdir + return @rootdir if defined? @rootdir + @rootdir = "#{DRIVE}/" + @rootdir + end + + def nofile + return @nofile if defined? @nofile @nofile = make_tmp_filename("nofile") - @symlinkfile = make_tmp_filename("symlinkfile") - @hardlinkfile = make_tmp_filename("hardlinkfile") - make_file("foo", @file) + @nofile + end + + def make_file(content, file) + open(file, "w") {|fh| fh << content } + end + + def zerofile + return @zerofile if defined? @zerofile + @zerofile = make_tmp_filename("zerofile") make_file("", @zerofile) - @chardev = File::NULL == "/dev/null" ? "/dev/null" : nil - @time = Time.now + @zerofile + end + + def regular_file + return @file if defined? @file + @file = make_tmp_filename("file") + make_file("foo", @file) + @file + end + + def symlinkfile + return @symlinkfile if @symlinkfile + @symlinkfile = make_tmp_filename("symlinkfile") begin - File.symlink(@file, @symlinkfile) + File.symlink(regular_file, @symlinkfile) rescue NotImplementedError @symlinkfile = nil end + @symlinkfile + end + + def hardlinkfile + return @hardlinkfile if defined? @hardlinkfile + @hardlinkfile = make_tmp_filename("hardlinkfile") begin - File.link(@file, @hardlinkfile) + File.link(regular_file, @hardlinkfile) rescue NotImplementedError, Errno::EINVAL # EINVAL for Windows Vista @hardlinkfile = nil end + @hardlinkfile end - def teardown - GC.start - FileUtils.remove_entry_secure @dir - end - - def make_file(content, file = @file) - open(file, "w") {|fh| fh << content } + def fifo + return @fifo if defined? @fifo + if /mswin|mingw|bccwin/ !~ RUBY_PLATFORM + fn = make_tmp_filename("fifo") + system("mkfifo", fn) + assert $?.success?, "mkfifo fails" + @fifo = fn + else + @fifo = nil + end + @fifo end - if /mswin|mingw|bccwin/ !~ RUBY_PLATFORM - def make_fifo(file) - system("mkfifo", file) - assert $?.success?, "mkfifo fails" + def socket + return @socket if defined? @socket + if defined? UNIXServer + socket = make_tmp_filename("s") + UNIXServer.open(socket).close + @socket = socket + else + @socket = nil end end - def make_tmp_filename(prefix) - "#{@dir}/#{prefix}.test" + def chardev + return @chardev if defined? @chardev + @chardev = File::NULL == "/dev/null" ? "/dev/null" : nil + @chardev end def test_path - file = @file + file = regular_file assert_equal(file, File.open(file) {|f| f.path}) assert_equal(file, File.path(file)) @@ -84,9 +135,12 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L135 end def test_stat - sleep(@time - Time.now + 1.1) - make_file("foo", @file + "2") - fs1, fs2 = File.stat(@file), File.stat(@file + "2") + hardlinkfile + fn1 = regular_file + sleep(1.1) + fn2 = fn1 + "2" + make_file("foo", fn2) + fs1, fs2 = File.stat(fn1), File.stat(fn2) assert_nothing_raised do assert_equal(0, fs1 <=> fs1) assert_equal(-1, fs1 <=> fs2) @@ -103,7 +157,7 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L157 unless /emx|mswin|mingw/ =~ RUBY_PLATFORM # on Windows, nlink is always 1. but this behavior will be changed # in the future. - assert_equal(@hardlinkfile ? 2 : 1, fs1.nlink) + assert_equal(hardlinkfile ? 2 : 1, fs1.nlink) end assert_integer(fs1.uid) assert_integer(fs1.gid) @@ -115,10 +169,10 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L169 assert_kind_of(Time, fs1.ctime) assert_kind_of(String, fs1.inspect) end - assert_raise(Errno::ENOENT) { File.stat(@nofile) } - assert_kind_of(File::Stat, File.open(@file) {|f| f.stat}) - assert_raise(Errno::ENOENT) { File.lstat(@nofile) } - assert_kind_of(File::Stat, File.open(@file) {|f| f.lstat}) + assert_raise(Errno::ENOENT) { File.stat(nofile) } + assert_kind_of(File::Stat, File.open(fn1) {|f| f.stat}) + assert_raise(Errno::ENOENT) { File.lstat(nofile) } + assert_kind_of(File::Stat, File.open(fn1) {|f| f.lstat}) end def test_stat_drive_root @@ -146,254 +200,245 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L200 def test_directory_p assert_file.directory?(@dir) assert_file.not_directory?(@dir+"/...") - assert_file.not_directory?(@file) - assert_file.not_directory?(@nofile) + assert_file.not_directory?(regular_file) + assert_file.not_directory?(nofile) end def test_pipe_p assert_file.not_pipe?(@dir) - assert_file.not_pipe?(@file) - assert_file.not_pipe?(@nofile) - if self.respond_to? :make_fifo - fifo = make_tmp_filename("fifo") - make_fifo fifo - assert_file.pipe?(fifo) - end + assert_file.not_pipe?(regular_file) + assert_file.not_pipe?(nofile) + assert_file.pipe?(fifo) if fifo end def test_symlink_p assert_file.not_symlink?(@dir) - assert_file.not_symlink?(@file) - assert_file.symlink?(@symlinkfile) if @symlinkfile - assert_file.not_symlink?(@hardlinkfile) if @hardlinkfile - assert_file.not_symlink?(@nofile) + assert_file.not_symlink?(regular_file) + assert_file.symlink?(symlinkfile) if symlinkfile + assert_file.not_symlink?(hardlinkfile) if hardlinkfile + assert_file.not_symlink?(nofile) end def test_socket_p assert_file.not_socket?(@dir) - assert_file.not_socket?(@file) - assert_file.not_socket?(@nofile) - if defined? UNIXServer - socket = make_tmp_filename("s") - UNIXServer.open(socket) {|sock| - assert_file.socket?(socket) - } - end + assert_file.not_socket?(regular_file) + assert_file.not_socket?(nofile) + assert_file.socket?(socket) if socket end def test_blockdev_p ## xxx assert_file.not_blockdev?(@dir) - assert_file.not_blockdev?(@file) - assert_file.not_blockdev?(@nofile) + assert_file.not_blockdev?(regular_file) + assert_file.not_blockdev?(nofile) end def test_chardev_p assert_file.not_chardev?(@dir) - assert_file.not_chardev?(@file) - assert_file.not_chardev?(@nofile) - assert_file.chardev?(@chardev) if @chardev + assert_file.not_chardev?(regular_file) + assert_file.not_chardev?(nofile) + assert_file.chardev?(chardev) if chardev end def test_exist_p assert_file.exist?(@dir) - assert_file.exist?(@file) - assert_file.not_exist?(@nofile) + assert_file.exist?(regular_file) + assert_file.not_exist?(nofile) end def test_readable_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM return if Process.euid == 0 - File.chmod(0200, @file) - assert_file.not_readable?(@file) - File.chmod(0600, @file) - assert_file.readable?(@file) - assert_file.not_readable?(@nofile) + File.chmod(0200, regular_file) + assert_file.not_readable?(regular_file) + File.chmod(0600, regular_file) + assert_file.readable?(regular_file) + assert_file.not_readable?(nofile) end def test_readable_real_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM return if Process.euid == 0 - File.chmod(0200, @file) - assert_file.not_readable_real?(@file) - File.chmod(0600, @file) - assert_file.readable_real?(@file) - assert_file.not_readable_real?(@nofile) + File.chmod(0200, regular_file) + assert_file.not_readable_real?(regular_file) + File.chmod(0600, regular_file) + assert_file.readable_real?(regular_file) + assert_file.not_readable_real?(nofile) end def test_world_readable_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - File.chmod(0006, @file) - assert_file.world_readable?(@file) - File.chmod(0060, @file) - assert_file.not_world_readable?(@file) - File.chmod(0600, @file) - assert_file.not_world_readable?(@file) - assert_file.not_world_readable?(@nofile) + File.chmod(0006, regular_file) + assert_file.world_readable?(regular_file) + File.chmod(0060, regular_file) + assert_file.not_world_readable?(regular_file) + File.chmod(0600, regular_file) + assert_file.not_world_readable?(regular_file) + assert_file.not_world_readable?(nofile) end def test_writable_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM return if Process.euid == 0 - File.chmod(0400, @file) - assert_file.not_writable?(@file) - File.chmod(0600, @file) - assert_file.writable?(@file) - assert_file.not_writable?(@nofile) + File.chmod(0400, regular_file) + assert_file.not_writable?(regular_file) + File.chmod(0600, regular_file) + assert_file.writable?(regular_file) + assert_file.not_writable?(nofile) end def test_writable_real_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM return if Process.euid == 0 - File.chmod(0400, @file) - assert_file.not_writable_real?(@file) - File.chmod(0600, @file) - assert_file.writable_real?(@file) - assert_file.not_writable_real?(@nofile) + File.chmod(0400, regular_file) + assert_file.not_writable_real?(regular_file) + File.chmod(0600, regular_file) + assert_file.writable_real?(regular_file) + assert_file.not_writable_real?(nofile) end def test_world_writable_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - File.chmod(0006, @file) - assert_file.world_writable?(@file) - File.chmod(0060, @file) - assert_file.not_world_writable?(@file) - File.chmod(0600, @file) - assert_file.not_world_writable?(@file) - assert_file.not_world_writable?(@nofile) + File.chmod(0006, regular_file) + assert_file.world_writable?(regular_file) + File.chmod(0060, regular_file) + assert_file.not_world_writable?(regular_file) + File.chmod(0600, regular_file) + assert_file.not_world_writable?(regular_file) + assert_file.not_world_writable?(nofile) end def test_executable_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - File.chmod(0100, @file) - assert_file.executable?(@file) - File.chmod(0600, @file) - assert_file.not_executable?(@file) - assert_file.not_executable?(@nofile) + File.chmod(0100, regular_file) + assert_file.executable?(regular_file) + File.chmod(0600, regular_file) + assert_file.not_executable?(regular_file) + assert_file.not_executable?(nofile) end def test_executable_real_p return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - File.chmod(0100, @file) - assert_file.executable_real?(@file) - File.chmod(0600, @file) - assert_file.not_executable_real?(@file) - assert_file.not_executable_real?(@nofile) + File.chmod(0100, regular_file) + assert_file.executable_real?(regular_file) + File.chmod(0600, regular_file) + assert_file.not_executable_real?(regular_file) + assert_file.not_executable_real?(nofile) end def test_file_p assert_file.not_file?(@dir) - assert_file.file?(@file) - assert_file.not_file?(@nofile) + assert_file.file?(regular_file) + assert_file.not_file?(nofile) end def test_zero_p assert_nothing_raised { File.zero?(@dir) } - assert_file.not_zero?(@file) - assert_file.zero?(@zerofile) - assert_file.not_zero?(@nofile) + assert_file.not_zero?(regular_file) + assert_file.zero?(zerofile) + assert_file.not_zero?(nofile) end def test_size_p assert_nothing_raised { File.size?(@dir) } - assert_equal(3, File.size?(@file)) - assert_file.not_size?(@zerofile) - assert_file.not_size?(@nofile) + assert_equal(3, File.size?(regular_file)) + assert_file.not_size?(zerofile) + assert_file.not_size?(nofile) end def test_owned_p ## xxx return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - assert_file.owned?(@file) - assert_file.grpowned?(@file) + assert_file.owned?(regular_file) + assert_file.grpowned?(regular_file) end def test_suid_sgid_sticky ## xxx - assert_file.not_setuid?(@file) - assert_file.not_setgid?(@file) - assert_file.not_sticky?(@file) + assert_file.not_setuid?(regular_file) + assert_file.not_setgid?(regular_file) + assert_file.not_sticky?(regular_file) end def test_path_identical_p - assert_file.identical?(@file, @file) - assert_file.not_identical?(@file, @zerofile) - assert_file.not_identical?(@file, @nofile) - assert_file.not_identical?(@nofile, @file) + assert_file.identical?(regular_file, regular_file) + assert_file.not_identical?(regular_file, zerofile) + assert_file.not_identical?(regular_file, nofile) + assert_file.not_identical?(nofile, regular_file) end def test_io_identical_p - open(@file) {|f| + open(regular_file) {|f| assert_file.identical?(f, f) - assert_file.identical?(@file, f) - assert_file.identical?(f, @file) + assert_file.identical?(regular_file, f) + assert_file.identical?(f, regular_file) } end def test_closed_io_identical_p - io = open(@file) {|f| f} + io = open(regular_file) {|f| f} assert_raise(IOError) { - File.identical?(@file, io) + File.identical?(regular_file, io) } - File.unlink(@file) - assert_file.not_exist?(@file) + File.unlink(regular_file) + assert_file.not_exist?(regular_file) end def test_s_size assert_integer(File.size(@dir)) - assert_equal(3, File.size(@file)) - assert_equal(0, File.size(@zerofile)) - assert_raise(Errno::ENOENT) { File.size(@nofile) } + assert_equal(3, File.size(regular_file)) + assert_equal(0, File.size(zerofile)) + assert_raise(Errno::ENOENT) { File.size(nofile) } end def test_ftype assert_equal("directory", File.ftype(@dir)) - assert_equal("file", File.ftype(@file)) - assert_equal("link", File.ftype(@symlinkfile)) if @symlinkfile - assert_equal("file", File.ftype(@hardlinkfile)) if @hardlinkfile - assert_raise(Errno::ENOENT) { File.ftype(@nofile) } + assert_equal("file", File.ftype(regular_file)) + assert_equal("link", File.ftype(symlinkfile)) if symlinkfile + assert_equal("file", File.ftype(hardlinkfile)) if hardlinkfile + assert_raise(Errno::ENOENT) { File.ftype(nofile) } end def test_atime - t1 = File.atime(@file) - t2 = File.open(@file) {|f| f.atime} + t1 = File.atime(regular_file) + t2 = File.open(regular_file) {|f| f.atime} assert_kind_of(Time, t1) assert_kind_of(Time, t2) assert_equal(t1, t2) - assert_raise(Errno::ENOENT) { File.atime(@nofile) } + assert_raise(Errno::ENOENT) { File.atime(nofile) } end def test_mtime - t1 = File.mtime(@file) - t2 = File.open(@file) {|f| f.mtime} + t1 = File.mtime(regular_file) + t2 = File.open(regular_file) {|f| f.mtime} assert_kind_of(Time, t1) assert_kind_of(Time, t2) assert_equal(t1, t2) - assert_raise(Errno::ENOENT) { File.mtime(@nofile) } + assert_raise(Errno::ENOENT) { File.mtime(nofile) } end def test_ctime - t1 = File.ctime(@file) - t2 = File.open(@file) {|f| f.ctime} + t1 = File.ctime(regular_file) + t2 = File.open(regular_file) {|f| f.ctime} assert_kind_of(Time, t1) assert_kind_of(Time, t2) assert_equal(t1, t2) - assert_raise(Errno::ENOENT) { File.ctime(@nofile) } + assert_raise(Errno::ENOENT) { File.ctime(nofile) } end def test_chmod return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - assert_equal(1, File.chmod(0444, @file)) - assert_equal(0444, File.stat(@file).mode % 01000) - assert_equal(0, File.open(@file) {|f| f.chmod(0222)}) - assert_equal(0222, File.stat(@file).mode % 01000) - File.chmod(0600, @file) - assert_raise(Errno::ENOENT) { File.chmod(0600, @nofile) } + assert_equal(1, File.chmod(0444, regular_file)) + assert_equal(0444, File.stat(regular_file).mode % 01000) + assert_equal(0, File.open(regular_file) {|f| f.chmod(0222)}) + assert_equal(0222, File.stat(regular_file).mode % 01000) + File.chmod(0600, regular_file) + assert_raise(Errno::ENOENT) { File.chmod(0600, nofile) } end def test_lchmod return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM - assert_equal(1, File.lchmod(0444, @file)) - assert_equal(0444, File.stat(@file).mode % 01000) - File.lchmod(0600, @file) - assert_raise(Errno::ENOENT) { File.lchmod(0600, @nofile) } + assert_equal(1, File.lchmod(0444, regular_file)) + assert_equal(0444, File.stat(regular_file).mode % 01000) + File.lchmod(0600, regular_file) + assert_raise(Errno::ENOENT) { File.lchmod(0600, nofile) } rescue NotImplementedError end @@ -404,39 +449,39 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L449 end def test_symlink - return unless @symlinkfile - assert_equal("link", File.ftype(@symlinkfile)) - assert_raise(Errno::EEXIST) { File.symlink(@file, @file) } + return unless symlinkfile + assert_equal("link", File.ftype(symlinkfile)) + assert_raise(Errno::EEXIST) { File.symlink(regular_file, regular_file) } end def test_utime t = Time.local(2000) - File.utime(t + 1, t + 2, @zerofile) - assert_equal(t + 1, File.atime(@zerofile)) - assert_equal(t + 2, File.mtime(@zerofile)) + File.utime(t + 1, t + 2, zerofile) + assert_equal(t + 1, File.atime(zerofile)) + assert_equal(t + 2, File.mtime(zerofile)) end def test_hardlink - return unless @hardlinkfile - assert_equal("file", File.ftype(@hardlinkfile)) - assert_raise(Errno::EEXIST) { File.link(@file, @file) } + return unless hardlinkfile + assert_equal("file", File.ftype(hardlinkfile)) + assert_raise(Errno::EEXIST) { File.link(regular_file, regular_file) } end def test_readlink - return unless @symlinkfile - assert_equal(@file, File.readlink(@symlinkfile)) - assert_raise(Errno::EINVAL) { File.readlink(@file) } - assert_raise(Errno::ENOENT) { File.readlink(@nofile) } + return unless symlinkfile + assert_equal(regular_file, File.readlink(symlinkfile)) + assert_raise(Errno::EINVAL) { File.readlink(regular_file) } + assert_raise(Errno::ENOENT) { File.readlink(nofile) } if fs = Encoding.find("filesystem") - assert_equal(fs, File.readlink(@symlinkfile).encoding) + assert_equal(fs, File.readlink(symlinkfile).encoding) end rescue NotImplementedError end d (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/