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