Truncate an existing file in "w", "w+" modes.
This commit is contained in:
parent
3a5098092c
commit
6d44186cf7
@ -109,6 +109,7 @@ module FakeFS
|
||||
@open = true
|
||||
|
||||
file_creation_mode? ? create_missing_file : check_file_existence!
|
||||
truncate_file if truncation_mode?
|
||||
end
|
||||
|
||||
def close
|
||||
@ -177,5 +178,13 @@ module FakeFS
|
||||
def write_only?
|
||||
@mode == WRITE_ONLY || @mode == APPEND_WRITE_ONLY
|
||||
end
|
||||
|
||||
def truncate_file
|
||||
@file.content = ""
|
||||
end
|
||||
|
||||
def truncation_mode?
|
||||
@mode == READ_WRITE_TRUNCATE || @mode == WRITE_ONLY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -156,6 +156,22 @@ class FakeFSTest < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def test_file_in_append_write_only_raises_error_when_reading
|
||||
FileUtils.touch("foo")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user