Skip to content

Instantly share code, notes, and snippets.

@jmhodges
Created September 21, 2009 08:56
Show Gist options
  • Save jmhodges/190144 to your computer and use it in GitHub Desktop.
Save jmhodges/190144 to your computer and use it in GitHub Desktop.
def test_open_with_write_clears_existing_file
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 'Yada Yada', File.read(path)
end
def test_File_new_in_write_mode_writes_empty_file_immediately
path = '/path/to/file.txt'
assert !File.exists?(path), "test file should not exist yet"
f = File.new(other, 'w')
assert File.exists?(path), "test file should now exist"
end
def test_File_new_errors_if_directory_above_it_does_not_exist
assert_raises(Errno::ENOENT) {
File.new('/path/to/file.txt', 'w')
}
assert_raises(Errno::ENOENT) {
File.new('/path/to/file.txt', 'r')
}
assert !File.exists?('/path/to/file.txt'), "file is not written to"
FileUtils.mkdir_p('/path/to')
assert_nothing_raised {
File.new('/path/to/file.txt', 'w')
}
assert_nothing_raised {
File.new('/path/to/file.txt', 'r')
}
assert File.exists?('/path/to/file.txt')
assert_equal "", File.read('/path/to/file.txt')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment