2009-05-29 17:23:39 +00:00
|
|
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'fakefs'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class FakeFSTest < Test::Unit::TestCase
|
|
|
|
include FakeFS
|
|
|
|
|
|
|
|
def setup
|
|
|
|
FileSystem.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_be_initialized_empty
|
|
|
|
fs = FileSystem
|
|
|
|
assert_equal 0, fs.files.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def xtest_can_be_initialized_with_an_existing_directory
|
|
|
|
fs = FileSystem
|
|
|
|
fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
|
|
|
|
assert_equal 1, fs.files.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_create_directories
|
|
|
|
FileUtils.mkdir_p("/path/to/dir")
|
2009-07-27 07:09:06 +00:00
|
|
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
2009-09-13 02:14:07 +00:00
|
|
|
|
|
|
|
def test_can_create_directories_with_mkpath
|
|
|
|
FileUtils.mkpath("/path/to/dir")
|
|
|
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
|
|
end
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 17:41:00 +00:00
|
|
|
def test_can_delete_directories
|
|
|
|
FileUtils.mkdir_p("/path/to/dir")
|
|
|
|
FileUtils.rmdir("/path/to/dir")
|
|
|
|
assert File.exists?("/path/to/")
|
|
|
|
assert File.exists?("/path/to/dir") == false
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
|
|
|
|
def test_knows_directories_exist
|
|
|
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
|
|
|
assert File.exists?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_knows_directories_are_directories
|
|
|
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
|
|
|
assert File.directory?(path)
|
|
|
|
end
|
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
def test_knows_symlink_directories_are_directories
|
|
|
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
|
|
|
FileUtils.ln_s path, sympath = '/sympath'
|
|
|
|
assert File.directory?(sympath)
|
|
|
|
end
|
2009-06-04 00:47:10 +00:00
|
|
|
|
2009-06-12 22:22:48 +00:00
|
|
|
def test_knows_non_existent_directories_arent_directories
|
|
|
|
path = 'does/not/exist/'
|
|
|
|
assert_equal RealFile.directory?(path), File.directory?(path)
|
|
|
|
end
|
|
|
|
|
2009-05-29 18:00:44 +00:00
|
|
|
def test_doesnt_overwrite_existing_directories
|
|
|
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
|
|
|
assert File.exists?(path)
|
|
|
|
FileUtils.mkdir_p("/path/to")
|
|
|
|
assert File.exists?(path)
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def test_can_create_symlinks
|
|
|
|
FileUtils.mkdir_p(target = "/path/to/target")
|
|
|
|
FileUtils.ln_s(target, "/path/to/link")
|
2009-07-27 07:09:06 +00:00
|
|
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
2009-06-01 10:52:08 +00:00
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(Errno::EEXIST) do
|
2009-06-01 10:52:08 +00:00
|
|
|
FileUtils.ln_s(target, '/path/to/link')
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
2009-10-08 21:34:22 +00:00
|
|
|
|
|
|
|
def test_can_force_creation_of_symlinks
|
|
|
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
|
|
|
FileUtils.ln_s(target, "/path/to/link")
|
|
|
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
|
|
|
FileUtils.ln_s(target, '/path/to/link', :force => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_symlink_using_ln_sf
|
|
|
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
|
|
|
FileUtils.ln_s(target, "/path/to/link")
|
|
|
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
|
|
|
FileUtils.ln_sf(target, '/path/to/link')
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
|
|
|
|
def test_can_follow_symlinks
|
|
|
|
FileUtils.mkdir_p(target = "/path/to/target")
|
|
|
|
FileUtils.ln_s(target, link = "/path/to/symlink")
|
|
|
|
assert_equal target, File.readlink(link)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_knows_symlinks_are_symlinks
|
|
|
|
FileUtils.mkdir_p(target = "/path/to/target")
|
|
|
|
FileUtils.ln_s(target, link = "/path/to/symlink")
|
|
|
|
assert File.symlink?(link)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_create_files
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f.write "Yatta!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert File.exists?(path)
|
2009-09-29 15:00:36 +00:00
|
|
|
assert File.readable?(path)
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
2009-09-30 06:52:25 +00:00
|
|
|
def test_file_opens_in_read_only_mode
|
|
|
|
File.open("foo", "w") { |f| f << "foo" }
|
|
|
|
|
|
|
|
f = File.open("foo")
|
|
|
|
|
|
|
|
assert_raises(IOError) do
|
|
|
|
f << "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 06:57:10 +00:00
|
|
|
def test_file_opens_in_invalid_mode
|
|
|
|
FileUtils.touch("foo")
|
|
|
|
|
|
|
|
assert_raises(ArgumentError) do
|
|
|
|
File.open("foo", "an_illegal_mode")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:03:21 +00:00
|
|
|
def test_raises_error_when_cannot_find_file_in_read_mode
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
File.open("does_not_exist", "r")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:13:58 +00:00
|
|
|
def test_raises_error_when_cannot_find_file_in_read_write_mode
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
File.open("does_not_exist", "r+")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:20:59 +00:00
|
|
|
def test_creates_files_in_write_only_mode
|
|
|
|
File.open("foo", "w")
|
|
|
|
assert File.exists?("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_creates_files_in_read_write_truncate_mode
|
|
|
|
File.open("foo", "w+")
|
|
|
|
assert File.exists?("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_creates_files_in_append_write_only
|
|
|
|
File.open("foo", "a")
|
|
|
|
assert File.exists?("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_creates_files_in_append_read_write
|
|
|
|
File.open("foo", "a+")
|
|
|
|
assert File.exists?("foo")
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:28:33 +00:00
|
|
|
def test_file_in_write_only_raises_error_when_reading
|
|
|
|
FileUtils.touch("foo")
|
|
|
|
|
|
|
|
f = File.open("foo", "w")
|
|
|
|
|
|
|
|
assert_raises(IOError) do
|
|
|
|
f.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:36:56 +00:00
|
|
|
def test_file_in_write_mode_truncates_existing_file
|
|
|
|
File.open("foo", "w") { |f| f << "contents" }
|
|
|
|
|
|
|
|
f = File.open("foo", "w")
|
|
|
|
|
|
|
|
assert_equal "", File.read("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_in_read_write_truncation_mode_truncates_file
|
|
|
|
File.open("foo", "w") { |f| f << "foo" }
|
|
|
|
|
|
|
|
f = File.open("foo", "w+")
|
|
|
|
|
|
|
|
assert_equal "", File.read("foo")
|
|
|
|
end
|
|
|
|
|
2009-09-30 07:28:33 +00:00
|
|
|
def test_file_in_append_write_only_raises_error_when_reading
|
|
|
|
FileUtils.touch("foo")
|
|
|
|
|
|
|
|
f = File.open("foo", "a")
|
|
|
|
|
|
|
|
assert_raises(IOError) do
|
|
|
|
f.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def test_can_read_files_once_written
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f.write "Yatta!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Yatta!", File.read(path)
|
|
|
|
end
|
|
|
|
|
2009-07-16 20:19:03 +00:00
|
|
|
def test_can_write_to_files
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << 'Yada Yada'
|
|
|
|
end
|
|
|
|
assert_equal 'Yada Yada', File.read(path)
|
|
|
|
end
|
|
|
|
|
2009-10-29 09:18:12 +00:00
|
|
|
def test_raises_error_when_opening_with_binary_mode_only
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
File.open("/foo", "b")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_open_file_in_binary_mode
|
|
|
|
File.open("/foo", "wb") { |x| x << "a" }
|
|
|
|
assert_equal "a", File.read("/foo")
|
|
|
|
end
|
|
|
|
|
2009-09-29 15:46:30 +00:00
|
|
|
def test_can_chunk_io_when_reading
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << 'Yada Yada'
|
|
|
|
end
|
|
|
|
file = File.new(path, 'r')
|
|
|
|
assert_equal 'Yada', file.read(4)
|
|
|
|
assert_equal ' Yada', file.read(5)
|
|
|
|
assert_equal '', file.read
|
|
|
|
file.rewind
|
|
|
|
assert_equal 'Yada Yada', file.read
|
|
|
|
end
|
|
|
|
|
2009-09-29 14:03:21 +00:00
|
|
|
def test_can_get_size_of_files
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << 'Yada Yada'
|
|
|
|
end
|
|
|
|
assert_equal 9, File.size(path)
|
|
|
|
end
|
|
|
|
|
2009-09-29 15:08:22 +00:00
|
|
|
def test_can_check_if_file_has_size?
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << 'Yada Yada'
|
|
|
|
end
|
|
|
|
assert File.size?(path)
|
2009-09-29 15:21:12 +00:00
|
|
|
assert_nil File.size?("/path/to/other.txt")
|
2009-09-29 15:08:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_check_size?_of_empty_file
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << ''
|
|
|
|
end
|
2009-09-29 15:21:12 +00:00
|
|
|
assert_nil File.size?("/path/to/file.txt")
|
2009-09-29 15:08:22 +00:00
|
|
|
end
|
|
|
|
|
2009-09-29 15:28:53 +00:00
|
|
|
def test_raises_error_on_mtime_if_file_does_not_exist
|
|
|
|
assert_raise Errno::ENOENT do
|
|
|
|
File.mtime('/path/to/file.txt')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_return_mtime_on_existing_file
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f << ''
|
|
|
|
end
|
|
|
|
assert File.mtime('/path/to/file.txt').is_a?(Time)
|
|
|
|
end
|
|
|
|
|
2009-05-29 18:46:49 +00:00
|
|
|
def test_can_read_with_File_readlines
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
2009-09-09 23:43:28 +00:00
|
|
|
f.puts "Yatta!", "Gatta!"
|
|
|
|
f.puts ["woot","toot"]
|
2009-05-29 18:46:49 +00:00
|
|
|
end
|
|
|
|
|
2009-09-09 23:43:28 +00:00
|
|
|
assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
|
2009-05-29 18:46:49 +00:00
|
|
|
end
|
|
|
|
|
2009-07-16 20:19:03 +00:00
|
|
|
def test_File_close_disallows_further_access
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
file = File.open(path, 'w')
|
|
|
|
file.write 'Yada'
|
|
|
|
file.close
|
|
|
|
assert_raise IOError do
|
|
|
|
file.read
|
|
|
|
end
|
|
|
|
end
|
2009-07-19 21:23:02 +00:00
|
|
|
|
2009-05-29 18:46:49 +00:00
|
|
|
def test_can_read_from_file_objects
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f.write "Yatta!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Yatta!", File.new(path).read
|
|
|
|
end
|
|
|
|
|
2009-06-04 00:47:10 +00:00
|
|
|
def test_file_read_errors_appropriately
|
|
|
|
assert_raise Errno::ENOENT do
|
|
|
|
File.read('anything')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def test_knows_files_are_files
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f.write "Yatta!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert File.file?(path)
|
|
|
|
end
|
2009-05-30 17:56:06 +00:00
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
def test_knows_symlink_files_are_files
|
|
|
|
path = '/path/to/file.txt'
|
|
|
|
File.open(path, 'w') do |f|
|
|
|
|
f.write "Yatta!"
|
|
|
|
end
|
|
|
|
FileUtils.ln_s path, sympath='/sympath'
|
|
|
|
|
|
|
|
assert File.file?(sympath)
|
|
|
|
end
|
2009-06-04 00:47:10 +00:00
|
|
|
|
2009-06-12 21:54:32 +00:00
|
|
|
def test_knows_non_existent_files_arent_files
|
2009-06-12 22:16:16 +00:00
|
|
|
assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
|
2009-06-12 21:54:32 +00:00
|
|
|
end
|
|
|
|
|
2009-05-30 17:56:06 +00:00
|
|
|
def test_can_chown_files
|
|
|
|
good = 'file.txt'
|
|
|
|
bad = 'nofile.txt'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open(good,'w') { |f| f.write "foo" }
|
2009-05-30 17:56:06 +00:00
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
|
|
|
|
assert_equal [good], out
|
2009-05-30 17:56:06 +00:00
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [good], FileUtils.chown('noone', 'nogroup', good)
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
FileUtils.chown('noone', 'nogroup', bad)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [good], FileUtils.chown('noone', 'nogroup', [good])
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
FileUtils.chown('noone', 'nogroup', [good, bad])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_chown_R_files
|
|
|
|
FileUtils.mkdir_p '/path/'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
|
|
|
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
2009-05-30 17:56:06 +00:00
|
|
|
resp = FileUtils.chown_R('no', 'no', '/path')
|
|
|
|
assert_equal ['/path'], resp
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dir_globs_paths
|
|
|
|
FileUtils.mkdir_p '/path'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
|
|
|
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
2009-07-19 21:16:21 +00:00
|
|
|
|
|
|
|
FileUtils.mkdir_p '/path/bar'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
|
2009-07-19 21:16:21 +00:00
|
|
|
|
2009-07-19 21:23:02 +00:00
|
|
|
FileUtils.cp_r '/path/bar', '/path/bar2'
|
|
|
|
|
2009-05-30 17:56:06 +00:00
|
|
|
assert_equal ['/path'], Dir['/path']
|
2009-07-19 21:23:02 +00:00
|
|
|
assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
|
2009-07-19 21:16:21 +00:00
|
|
|
|
|
|
|
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
|
2009-07-28 07:25:31 +00:00
|
|
|
assert_equal ['/path/foo'], Dir['/path/foo']
|
2009-07-19 21:16:21 +00:00
|
|
|
|
2009-07-28 07:25:31 +00:00
|
|
|
assert_equal ['/path'], Dir['/path*']
|
2009-07-28 08:12:52 +00:00
|
|
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
|
|
|
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
|
|
|
|
|
|
|
|
FileUtils.cp_r '/path', '/otherpath'
|
|
|
|
|
|
|
|
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dir_glob_handles_root
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
|
|
|
|
# this fails. the root dir should be named '/' but it is '.'
|
|
|
|
#assert_equal ['/'], Dir['/']
|
2009-05-30 17:56:06 +00:00
|
|
|
end
|
2009-05-30 18:30:46 +00:00
|
|
|
|
2009-10-29 07:10:30 +00:00
|
|
|
def test_dir_glob_handles_recursive_globs
|
|
|
|
File.open('/one/two/three/four.rb', 'w')
|
|
|
|
File.open('/one/five.rb', 'w')
|
|
|
|
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
|
|
|
|
assert_equal ['/one/two'], Dir['/one/**/two']
|
|
|
|
assert_equal ['/one/two/three'], Dir['/one/**/three']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dir_recursive_glob_ending_in_wildcards_only_returns_files
|
|
|
|
File.open('/one/two/three/four.rb', 'w')
|
|
|
|
File.open('/one/five.rb', 'w')
|
|
|
|
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
|
|
|
|
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**']
|
|
|
|
end
|
|
|
|
|
2009-05-30 18:30:46 +00:00
|
|
|
def test_chdir_changes_directories_like_a_boss
|
|
|
|
# I know memes!
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
assert_equal '.', FileSystem.fs.name
|
|
|
|
assert_equal({}, FileSystem.fs['path'])
|
|
|
|
Dir.chdir '/path' do
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
|
|
|
File.open('foobar', 'w') { |f| f.write 'foo'}
|
2009-05-30 18:30:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal '.', FileSystem.fs.name
|
|
|
|
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
|
|
|
|
|
|
|
c = nil
|
|
|
|
Dir.chdir '/path' do
|
2009-09-22 02:42:23 +00:00
|
|
|
c = File.open('foo', 'r') { |f| f.read }
|
2009-05-30 18:30:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'foo', c
|
|
|
|
end
|
|
|
|
|
2009-05-31 03:21:44 +00:00
|
|
|
def test_chdir_shouldnt_keep_us_from_absolute_paths
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
|
|
|
|
Dir.chdir '/path' do
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
|
|
|
File.open('/foobar', 'w') { |f| f.write 'foo'}
|
2009-05-31 03:21:44 +00:00
|
|
|
end
|
|
|
|
assert_equal ['foo'], FileSystem.fs['path'].keys.sort
|
|
|
|
assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
|
|
|
|
|
|
|
|
Dir.chdir '/path' do
|
|
|
|
FileUtils.rm('foo')
|
|
|
|
FileUtils.rm('/foobar')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [], FileSystem.fs['path'].keys.sort
|
|
|
|
assert_equal ['path'], FileSystem.fs.keys.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chdir_should_be_nestable
|
|
|
|
FileUtils.mkdir_p '/path/me'
|
|
|
|
Dir.chdir '/path' do
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
2009-05-31 03:21:44 +00:00
|
|
|
Dir.chdir 'me' do
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foobar', 'w') { |f| f.write 'foo'}
|
2009-05-31 03:21:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['foo','me'], FileSystem.fs['path'].keys.sort
|
|
|
|
assert_equal ['foobar'], FileSystem.fs['path']['me'].keys.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
|
|
|
|
assert_raise(Errno::ENOENT) do
|
|
|
|
Dir.chdir('/nope') do
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-30 18:30:46 +00:00
|
|
|
def test_chdir_shouldnt_lose_state_because_of_errors
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
|
|
|
|
Dir.chdir '/path' do
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
|
|
|
File.open('foobar', 'w') { |f| f.write 'foo'}
|
2009-05-30 18:30:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Dir.chdir('/path') do
|
|
|
|
raise Exception
|
|
|
|
end
|
|
|
|
rescue Exception # hardcore
|
|
|
|
end
|
|
|
|
|
2009-05-31 03:40:21 +00:00
|
|
|
Dir.chdir('/path') do
|
|
|
|
begin
|
|
|
|
Dir.chdir('nope'){ }
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['/path'], FileSystem.dir_levels
|
|
|
|
end
|
|
|
|
|
2009-05-30 18:30:46 +00:00
|
|
|
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
|
|
|
end
|
2009-05-30 18:31:19 +00:00
|
|
|
|
2009-05-31 03:40:21 +00:00
|
|
|
def test_chdir_with_no_block_is_awesome
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
Dir.chdir('/path')
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
|
|
|
assert_equal ['subdir'], FileSystem.current_dir.keys
|
|
|
|
Dir.chdir('subdir')
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
2009-05-31 03:40:21 +00:00
|
|
|
assert_equal ['foo'], FileSystem.current_dir.keys
|
|
|
|
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
Dir.chdir('subsubdir')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ['foo'], FileSystem.current_dir.keys
|
|
|
|
end
|
|
|
|
|
2009-07-27 17:56:56 +00:00
|
|
|
def test_current_dir_reflected_by_pwd
|
|
|
|
FileUtils.mkdir_p '/path'
|
|
|
|
Dir.chdir('/path')
|
|
|
|
|
|
|
|
assert_equal '/path', Dir.pwd
|
|
|
|
assert_equal '/path', Dir.getwd
|
|
|
|
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
|
|
|
Dir.chdir('subdir')
|
|
|
|
|
|
|
|
assert_equal '/path/subdir', Dir.pwd
|
|
|
|
assert_equal '/path/subdir', Dir.getwd
|
|
|
|
end
|
|
|
|
|
2009-05-30 18:31:19 +00:00
|
|
|
def test_file_open_defaults_to_read
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo','w') { |f| f.write 'bar' }
|
|
|
|
assert_equal 'bar', File.open('foo') { |f| f.read }
|
2009-05-30 18:31:19 +00:00
|
|
|
end
|
2009-05-31 04:22:57 +00:00
|
|
|
|
|
|
|
def test_flush_exists_on_file
|
2009-09-22 02:42:23 +00:00
|
|
|
r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
|
2009-05-31 04:22:57 +00:00
|
|
|
assert_equal 'foo', r.path
|
|
|
|
end
|
2009-05-31 05:04:17 +00:00
|
|
|
|
|
|
|
def test_mv_should_raise_error_on_missing_file
|
|
|
|
assert_raise(Errno::ENOENT) do
|
|
|
|
FileUtils.mv 'blafgag', 'foo'
|
|
|
|
end
|
|
|
|
end
|
2009-05-31 06:24:29 +00:00
|
|
|
|
|
|
|
def test_mv_actually_works
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'bar' }
|
2009-05-31 06:24:29 +00:00
|
|
|
FileUtils.mv 'foo', 'baz'
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'bar', File.open('baz') { |f| f.read }
|
2009-05-31 06:24:29 +00:00
|
|
|
end
|
2009-05-31 06:30:50 +00:00
|
|
|
|
2009-06-04 00:53:07 +00:00
|
|
|
def test_cp_actually_works
|
|
|
|
File.open('foo', 'w') {|f| f.write 'bar' }
|
|
|
|
FileUtils.cp('foo', 'baz')
|
|
|
|
assert_equal 'bar', File.read('baz')
|
|
|
|
end
|
|
|
|
|
2009-06-11 22:11:44 +00:00
|
|
|
def test_cp_file_into_dir
|
2009-06-04 00:53:07 +00:00
|
|
|
File.open('foo', 'w') {|f| f.write 'bar' }
|
|
|
|
FileUtils.mkdir_p 'baz'
|
|
|
|
|
2009-06-11 22:11:44 +00:00
|
|
|
FileUtils.cp('foo', 'baz')
|
|
|
|
assert_equal 'bar', File.read('baz/foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cp_overwrites_dest_file
|
|
|
|
File.open('foo', 'w') {|f| f.write 'FOO' }
|
|
|
|
File.open('bar', 'w') {|f| f.write 'BAR' }
|
|
|
|
|
|
|
|
FileUtils.cp('foo', 'bar')
|
|
|
|
assert_equal 'FOO', File.read('bar')
|
2009-06-04 00:53:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cp_fails_on_no_source
|
|
|
|
assert_raise Errno::ENOENT do
|
|
|
|
FileUtils.cp('foo', 'baz')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cp_fails_on_directory_copy
|
|
|
|
FileUtils.mkdir_p 'baz'
|
|
|
|
|
|
|
|
assert_raise Errno::EISDIR do
|
|
|
|
FileUtils.cp('baz', 'bar')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-31 06:30:50 +00:00
|
|
|
def test_cp_r_doesnt_tangle_files_together
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('foo', 'w') { |f| f.write 'bar' }
|
2009-05-31 06:30:50 +00:00
|
|
|
FileUtils.cp_r('foo', 'baz')
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('baz', 'w') { |f| f.write 'quux' }
|
|
|
|
assert_equal 'bar', File.open('foo') { |f| f.read }
|
2009-05-31 06:30:50 +00:00
|
|
|
end
|
2009-05-31 06:40:45 +00:00
|
|
|
|
|
|
|
def test_cp_r_should_raise_error_on_missing_file
|
|
|
|
# Yes, this error sucks, but it conforms to the original Ruby
|
|
|
|
# method.
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
FileUtils.cp_r 'blafgag', 'foo'
|
|
|
|
end
|
|
|
|
end
|
2009-05-31 09:24:33 +00:00
|
|
|
|
2009-06-01 10:25:12 +00:00
|
|
|
def test_cp_r_handles_copying_directories
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
|
2009-06-01 10:25:12 +00:00
|
|
|
|
|
|
|
FileUtils.mkdir_p 'baz'
|
|
|
|
|
|
|
|
# To a previously uncreated directory
|
|
|
|
FileUtils.cp_r('subdir', 'quux')
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'footext', File.open('quux/foo') { |f| f.read }
|
2009-06-01 10:25:12 +00:00
|
|
|
|
|
|
|
# To a directory that already exists
|
|
|
|
FileUtils.cp_r('subdir', 'baz')
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
|
2009-06-01 10:25:12 +00:00
|
|
|
|
|
|
|
# To a subdirectory of a directory that does not exist
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(Errno::ENOENT) do
|
2009-06-01 10:25:12 +00:00
|
|
|
FileUtils.cp_r('subdir', 'nope/something')
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-06-01 10:25:12 +00:00
|
|
|
end
|
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
def test_cp_r_only_copies_into_directories
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
2009-06-01 10:52:08 +00:00
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('bar', 'w') { |f| f.write 'bartext' }
|
2009-06-01 10:52:08 +00:00
|
|
|
|
|
|
|
assert_raises(Errno::EEXIST) do
|
|
|
|
FileUtils.cp_r 'subdir', 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.mkdir_p 'otherdir'
|
|
|
|
FileUtils.ln_s 'otherdir', 'symdir'
|
|
|
|
|
|
|
|
FileUtils.cp_r 'subdir', 'symdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
|
2009-06-01 10:52:08 +00:00
|
|
|
end
|
|
|
|
|
2009-07-19 22:23:42 +00:00
|
|
|
def test_cp_r_sets_parent_correctly
|
|
|
|
FileUtils.mkdir_p '/path/foo'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
|
|
|
|
File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
|
2009-07-19 22:23:42 +00:00
|
|
|
|
|
|
|
FileUtils.cp_r '/path/foo', '/path/bar'
|
|
|
|
|
|
|
|
assert File.exists?('/path/bar/baz')
|
|
|
|
FileUtils.rm_rf '/path/bar/baz'
|
|
|
|
assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
|
|
|
|
end
|
|
|
|
|
2009-05-31 09:24:33 +00:00
|
|
|
def test_clone_clones_normal_files
|
2009-09-22 02:42:23 +00:00
|
|
|
RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
|
2009-06-01 05:41:11 +00:00
|
|
|
assert !File.exists?(here('foo'))
|
2009-05-31 09:24:33 +00:00
|
|
|
FileSystem.clone(here('foo'))
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'bar', File.open(here('foo')) { |f| f.read }
|
2009-05-31 09:24:33 +00:00
|
|
|
ensure
|
|
|
|
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
|
|
|
end
|
2009-06-01 10:52:08 +00:00
|
|
|
|
2009-06-02 06:54:04 +00:00
|
|
|
def test_clone_clones_directories
|
|
|
|
RealFileUtils.mkdir_p(here('subdir'))
|
|
|
|
|
|
|
|
FileSystem.clone(here('subdir'))
|
|
|
|
|
|
|
|
assert File.exists?(here('subdir')), 'subdir was cloned'
|
|
|
|
assert File.directory?(here('subdir')), 'subdir is a directory'
|
|
|
|
ensure
|
|
|
|
RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir'))
|
|
|
|
end
|
|
|
|
|
2009-06-02 07:16:19 +00:00
|
|
|
def test_clone_clones_dot_files_even_hard_to_find_ones
|
|
|
|
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
2009-06-02 06:54:04 +00:00
|
|
|
assert !File.exists?(here('subdir'))
|
2009-06-02 07:16:19 +00:00
|
|
|
|
2009-06-02 06:54:04 +00:00
|
|
|
FileSystem.clone(here('subdir'))
|
|
|
|
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
|
2009-06-02 07:16:19 +00:00
|
|
|
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
|
2009-06-02 06:54:04 +00:00
|
|
|
ensure
|
|
|
|
RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir'))
|
|
|
|
end
|
|
|
|
|
2009-06-01 11:13:53 +00:00
|
|
|
def test_putting_a_dot_at_end_copies_the_contents
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
2009-06-01 11:13:53 +00:00
|
|
|
|
|
|
|
FileUtils.mkdir_p 'newdir'
|
|
|
|
FileUtils.cp_r 'subdir/.', 'newdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
|
2009-06-01 11:13:53 +00:00
|
|
|
end
|
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
def test_file_can_read_from_symlinks
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('first', 'w') { |f| f.write '1'}
|
2009-06-01 10:52:08 +00:00
|
|
|
FileUtils.ln_s 'first', 'one'
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal '1', File.open('one') { |f| f.read }
|
2009-06-01 10:52:08 +00:00
|
|
|
|
|
|
|
FileUtils.mkdir_p 'subdir'
|
2009-09-22 02:42:23 +00:00
|
|
|
File.open('subdir/nother','w') { |f| f.write 'works' }
|
2009-06-01 10:52:08 +00:00
|
|
|
FileUtils.ln_s 'subdir', 'new'
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_equal 'works', File.open('new/nother') { |f| f.read }
|
2009-06-01 10:52:08 +00:00
|
|
|
end
|
2009-07-19 21:16:21 +00:00
|
|
|
|
2009-09-30 05:26:52 +00:00
|
|
|
def test_can_symlink_through_file
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
File.symlink("/foo", "/bar")
|
|
|
|
|
|
|
|
assert File.symlink?("/bar")
|
|
|
|
end
|
|
|
|
|
2009-06-13 07:22:37 +00:00
|
|
|
def test_files_can_be_touched
|
2009-06-14 05:29:00 +00:00
|
|
|
FileUtils.touch('touched_file')
|
|
|
|
assert File.exists?('touched_file')
|
2009-06-14 06:56:14 +00:00
|
|
|
list = ['newfile', 'another']
|
|
|
|
FileUtils.touch(list)
|
|
|
|
list.each { |fp| assert(File.exists?(fp)) }
|
2009-06-14 05:29:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_does_not_work_if_the_dir_path_cannot_be_found
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(Errno::ENOENT) do
|
2009-06-14 05:29:00 +00:00
|
|
|
FileUtils.touch('this/path/should/not/be/here')
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-06-14 06:56:14 +00:00
|
|
|
FileUtils.mkdir_p('subdir')
|
|
|
|
list = ['subdir/foo', 'nosubdir/bar']
|
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(Errno::ENOENT) do
|
2009-06-14 06:56:14 +00:00
|
|
|
FileUtils.touch(list)
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-06-13 07:22:37 +00:00
|
|
|
end
|
2009-06-02 06:54:04 +00:00
|
|
|
|
2009-09-13 17:41:00 +00:00
|
|
|
def test_extname
|
|
|
|
assert File.extname("test.doc") == ".doc"
|
|
|
|
end
|
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
# Directory tests
|
|
|
|
def test_new_directory
|
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_nothing_raised do
|
2009-09-13 13:50:08 +00:00
|
|
|
Dir.new('/this/path/should/be/here')
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_directory_does_not_work_if_dir_path_cannot_be_found
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(Errno::ENOENT) do
|
2009-09-13 13:50:08 +00:00
|
|
|
Dir.new('/this/path/should/not/be/here')
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_close
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
assert dir.close.nil?
|
|
|
|
|
2009-09-22 02:42:23 +00:00
|
|
|
assert_raises(IOError) do
|
2009-09-13 13:50:08 +00:00
|
|
|
dir.each { |dir| dir }
|
2009-09-22 02:42:23 +00:00
|
|
|
end
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_each
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
yielded = []
|
|
|
|
dir.each do |dir|
|
|
|
|
yielded << dir
|
|
|
|
end
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
assert yielded.size == test.size
|
|
|
|
test.each { |t| assert yielded.include?(t) }
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_path
|
2009-09-22 02:42:23 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
good_path = '/this/path/should/be/here'
|
|
|
|
assert_equal good_path, Dir.new('/this/path/should/be/here').path
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_pos
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
assert dir.pos == 0
|
|
|
|
dir.read
|
|
|
|
assert dir.pos == 1
|
|
|
|
dir.read
|
|
|
|
assert dir.pos == 2
|
|
|
|
dir.read
|
|
|
|
assert dir.pos == 3
|
|
|
|
dir.read
|
|
|
|
assert dir.pos == 4
|
|
|
|
dir.read
|
|
|
|
assert dir.pos == 5
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_pos_assign
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
assert dir.pos == 0
|
|
|
|
dir.pos = 2
|
|
|
|
assert dir.pos == 2
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_read
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
assert dir.pos == 0
|
|
|
|
d = dir.read
|
|
|
|
assert dir.pos == 1
|
|
|
|
assert d == '.'
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
d = dir.read
|
|
|
|
assert dir.pos == 2
|
|
|
|
assert d == '..'
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_read_past_length
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_not_nil d
|
|
|
|
d = dir.read
|
|
|
|
assert_nil d
|
|
|
|
end
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_rewind
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
d = dir.read
|
|
|
|
d = dir.read
|
|
|
|
assert dir.pos == 2
|
|
|
|
dir.rewind
|
|
|
|
assert dir.pos == 0
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_seek
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 13:50:08 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 13:50:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.new('/this/path/should/be/here')
|
|
|
|
|
|
|
|
d = dir.seek 1
|
|
|
|
assert d == '..'
|
|
|
|
assert dir.pos == 1
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_class_delete
|
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
Dir.delete('/this/path/should/be/here')
|
|
|
|
assert File.exists?('/this/path/should/be/here') == false
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_class_delete_does_not_act_on_non_empty_directory
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
2009-09-22 02:37:55 +00:00
|
|
|
assert_raises(SystemCallError) do
|
2009-09-13 14:10:42 +00:00
|
|
|
Dir.delete('/this/path/should/be/here')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_entries
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
yielded = Dir.entries('/this/path/should/be/here')
|
|
|
|
assert yielded.size == test.size
|
|
|
|
test.each { |t| assert yielded.include?(t) }
|
|
|
|
end
|
|
|
|
|
2009-10-26 14:51:26 +00:00
|
|
|
def test_directory_entries_works_with_trailing_slash
|
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
|
|
|
|
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
|
|
|
end
|
|
|
|
|
|
|
|
yielded = Dir.entries('/this/path/should/be/here/')
|
|
|
|
assert yielded.size == test.size
|
|
|
|
test.each { |t| assert yielded.include?(t) }
|
|
|
|
end
|
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
def test_directory_foreach
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
yielded = []
|
|
|
|
Dir.foreach('/this/path/should/be/here') do |dir|
|
|
|
|
yielded << dir
|
|
|
|
end
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
assert yielded.size == test.size
|
|
|
|
test.each { |t| assert yielded.include?(t) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_mkdir
|
|
|
|
Dir.mkdir('/path')
|
|
|
|
assert File.exists?('/path')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_mkdir_relative
|
|
|
|
FileUtils.mkdir_p('/new/root')
|
|
|
|
FileSystem.chdir('/new/root')
|
|
|
|
Dir.mkdir('path')
|
|
|
|
assert File.exists?('/new/root/path')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_mkdir_not_recursive
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
Dir.mkdir('/path/does/not/exist')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_open
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
dir = Dir.open('/this/path/should/be/here')
|
2009-09-22 02:37:55 +00:00
|
|
|
assert dir.path == '/this/path/should/be/here'
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory_open_block
|
2009-09-13 14:50:34 +00:00
|
|
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
FileUtils.mkdir_p('/this/path/should/be/here')
|
|
|
|
|
|
|
|
test.each do |f|
|
2009-09-13 14:50:34 +00:00
|
|
|
FileUtils.touch("/this/path/should/be/here/#{f}")
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
yielded = []
|
|
|
|
Dir.open('/this/path/should/be/here') do |dir|
|
|
|
|
yielded << dir
|
|
|
|
end
|
2009-09-22 02:37:55 +00:00
|
|
|
|
2009-09-13 14:10:42 +00:00
|
|
|
assert yielded.size == test.size
|
|
|
|
test.each { |t| assert yielded.include?(t) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tmpdir
|
2009-09-13 14:50:34 +00:00
|
|
|
assert Dir.tmpdir == "/tmp"
|
2009-09-13 14:10:42 +00:00
|
|
|
end
|
2009-09-30 02:48:50 +00:00
|
|
|
|
|
|
|
def test_hard_link_creates_file
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
assert File.exists?("/bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hard_link_with_missing_file_raises_error
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hard_link_with_existing_destination_file
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
FileUtils.touch("/bar")
|
|
|
|
|
|
|
|
assert_raises(Errno::EEXIST) do
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hard_link_returns_0_when_successful
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
assert_equal 0, File.link("/foo", "/bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hard_link_returns_duplicate_file
|
|
|
|
File.open("/foo", "w") { |x| x << "some content" }
|
|
|
|
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
assert_equal "some content", File.read("/bar")
|
|
|
|
end
|
2009-09-13 14:10:42 +00:00
|
|
|
|
2009-10-01 05:39:07 +00:00
|
|
|
def test_hard_link_with_directory_raises_error
|
|
|
|
Dir.mkdir "/foo"
|
|
|
|
|
|
|
|
assert_raises(Errno::EPERM) do
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-30 05:23:26 +00:00
|
|
|
def test_file_stat_returns_file_stat_object
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
assert_equal File::Stat, File.stat("/foo").class
|
|
|
|
end
|
|
|
|
|
2009-10-01 06:42:29 +00:00
|
|
|
def test_can_delete_file_with_delete
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
File.delete("/foo")
|
|
|
|
|
|
|
|
assert !File.exists?("/foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_delete_multiple_files_with_delete
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
FileUtils.touch("/bar")
|
|
|
|
|
|
|
|
File.delete("/foo", "/bar")
|
|
|
|
|
|
|
|
assert !File.exists?("/foo")
|
|
|
|
assert !File.exists?("/bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_raises_argument_error_with_no_filename_given
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
File.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_returns_number_one_when_given_one_arg
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
assert_equal 1, File.delete("/foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_returns_number_two_when_given_two_args
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
FileUtils.touch("/bar")
|
|
|
|
|
|
|
|
assert_equal 2, File.delete("/foo", "/bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_raises_error_when_first_file_does_not_exist
|
|
|
|
assert_raises Errno::ENOENT do
|
|
|
|
File.delete("/foo")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_does_not_raise_error_when_second_file_does_not_exist
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
File.delete("/foo", "/bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink_is_alias_for_delete
|
|
|
|
assert_equal File.method(:unlink), File.method(:delete)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink_removes_only_one_file_content
|
|
|
|
File.open("/foo", "w") { |f| f << "some_content" }
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
|
|
|
|
File.unlink("/bar")
|
|
|
|
File.read("/foo") == "some_content"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_link_reports_correct_stat_info_after_unlinking
|
|
|
|
File.open("/foo", "w") { |f| f << "some_content" }
|
|
|
|
File.link("/foo", "/bar")
|
|
|
|
|
|
|
|
File.unlink("/bar")
|
|
|
|
assert_equal 1, File.stat("/foo").nlink
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_works_with_symlink
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
File.symlink("/foo", "/bar")
|
|
|
|
|
|
|
|
File.unlink("/bar")
|
|
|
|
|
|
|
|
assert File.exists?("/foo")
|
|
|
|
assert !File.exists?("/bar")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_works_with_symlink_source
|
|
|
|
FileUtils.touch("/foo")
|
|
|
|
File.symlink("/foo", "/bar")
|
|
|
|
|
|
|
|
File.unlink("/foo")
|
|
|
|
|
|
|
|
assert !File.exists?("/foo")
|
|
|
|
end
|
|
|
|
|
2009-10-29 09:37:41 +00:00
|
|
|
def test_file_seek_returns_0
|
|
|
|
File.open("/foo", "w") do |f|
|
|
|
|
f << "one\ntwo\nthree"
|
|
|
|
end
|
|
|
|
|
|
|
|
file = File.open("/foo", "r")
|
|
|
|
|
|
|
|
assert_equal 0, file.seek(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_seek_seeks_to_location
|
|
|
|
File.open("/foo", "w") do |f|
|
|
|
|
f << "123"
|
|
|
|
end
|
|
|
|
|
|
|
|
file = File.open("/foo", "r")
|
|
|
|
file.seek(1)
|
|
|
|
assert_equal "23", file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_seek_seeks_to_correct_location
|
|
|
|
File.open("/foo", "w") do |f|
|
|
|
|
f << "123"
|
|
|
|
end
|
|
|
|
|
|
|
|
file = File.open("/foo", "r")
|
|
|
|
file.seek(2)
|
|
|
|
assert_equal "3", file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_seek_can_take_negative_offset
|
|
|
|
File.open("/foo", "w") do |f|
|
|
|
|
f << "123456789"
|
|
|
|
end
|
|
|
|
|
|
|
|
file = File.open("/foo", "r")
|
|
|
|
|
|
|
|
file.seek(-1, IO::SEEK_END)
|
|
|
|
assert_equal "9", file.read
|
|
|
|
|
|
|
|
file.seek(-2, IO::SEEK_END)
|
|
|
|
assert_equal "89", file.read
|
|
|
|
|
|
|
|
file.seek(-3, IO::SEEK_END)
|
|
|
|
assert_equal "789", file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_have_constants_inherited_from_descending_from_io
|
|
|
|
assert_equal IO::SEEK_CUR, File::SEEK_CUR
|
|
|
|
assert_equal IO::SEEK_END, File::SEEK_END
|
|
|
|
assert_equal IO::SEEK_SET, File::SEEK_SET
|
|
|
|
end
|
|
|
|
|
2009-06-02 06:54:04 +00:00
|
|
|
def here(fname)
|
|
|
|
RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname)
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|