File: syswrite_test.rb

package info (click to toggle)
ruby-fakefs 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 544 kB
  • sloc: ruby: 7,606; makefile: 5
file content (66 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require_relative '../../test_helper'

# File SysWrite test class
class FileSysWriteTest < Minitest::Test
  def setup
    FakeFS.activate!
    FakeFS::FileSystem.clear
  end

  def teardown
    FakeFS.deactivate!
    FakeFS::FileSystem.clear
  end

  def test_returns_one_byte_when_written
    f = File.open 'foo', 'w'
    result = f.syswrite 'a'
    assert_equal 1, result
  end

  def test_returns_two_bytes_when_two_written
    f = File.open 'foo', 'w'
    result = f.syswrite 'ab'
    assert_equal 2, result
  end

  def test_syswrite_writes_file
    f = File.open 'foo', 'w'
    f.syswrite 'abcdef'
    f.close

    assert_equal 'abcdef', File.read('foo')
  end

  def test_writes_to_the_actual_position_when_called_after_buffered_io_read
    File.open('foo', 'w') do |file|
      file.syswrite('012345678901234567890123456789')
    end

    file = File.open('foo', 'r+')
    file.read(5)
    file.syswrite('abcde')

    File.open('foo') do |f|
      assert_equal '01234abcde', f.sysread(10)
    end
  end

  def test_writes_all_of_the_strings_bytes_but_does_not_buffer_them
    File.open('foo', 'w') do |file|
      file.syswrite('012345678901234567890123456789')
    end

    file = File.open('foo', 'r+')
    file.syswrite('abcde')

    File.open('foo') do |f|
      assert_equal 'abcde56789', f.sysread(10)
      f.seek(0)
      f.fsync
      assert_equal 'abcde56789', f.sysread(10)
    end
  end
end