jmhodges (owner)

Revisions

gist: 190144 Download_button fork
public
Public Clone URL: git://gist.github.com/190144.git
Embed All Files: show embed
fakefs_File_new_tests.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  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