Add File.link as a copy operation

This commit is contained in:
Scott Taylor 2009-09-29 22:48:50 -04:00
parent 731122a1ac
commit 6f08568d8f
2 changed files with 43 additions and 0 deletions

View File

@ -114,6 +114,14 @@ module FakeFS
def self.readlines(path)
read(path).split("\n")
end
def self.link(source, dest)
raise(Errno::ENOENT, "No such file or directory - #{source} or #{dest}") if !File.exists?(source)
raise(Errno::EEXIST, "File exists - #{source} or #{dest}") if File.exists?(dest)
FileUtils.cp(source, dest)
0
end
attr_reader :path

View File

@ -913,6 +913,41 @@ class FakeFSTest < Test::Unit::TestCase
def test_tmpdir
assert Dir.tmpdir == "/tmp"
end
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
def here(fname)
RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname)