File: pathname_test.rb

package info (click to toggle)
ruby-fakefs 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 496 kB
  • sloc: ruby: 6,810; makefile: 5
file content (153 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require_relative 'test_helper'

# Fake Pathname test class
class PathnameTest < Minitest::Test
  include FakeFS

  def setup
    FakeFS.activate!
    FileSystem.clear

    @path = '/foo'
    @pathname = Pathname.new(@path)
  end

  def teardown
    FakeFS.deactivate!
  end

  def test_filetest_exists_returns_correct_value
    refute @pathname.exist?

    File.write(@path, '')

    assert @pathname.exist?
  end

  def test_root_check_returns_correct_value
    refute @pathname.root?
    root_path = Pathname.new('/')
    assert root_path.root?
  end

  def test_io_each_line_with_block_yields_lines
    File.write(@path, "one\ntwo\nthree\n")

    yielded = []
    @pathname.each_line { |line| yielded << line }

    assert_equal yielded, ["one\n", "two\n", "three\n"]
  end

  def test_io_each_line_without_block_returns_enumerator
    File.write(@path, "one\ntwo\nthree\n")

    assert @pathname.each_line.is_a?(Enumerator)
    assert_equal(['o', 't', 't'], @pathname.each_line.map { |l| l[0] })
    assert_equal ["one\ntwo\nth", "ree\n"], @pathname.each_line('th').to_a
  end

  def test_io_read_returns_file_contents
    File.write(@path, "some\ncontent")

    assert_equal "some\ncontent", @pathname.read
    assert_equal "some\nc", @pathname.read(6)
    assert_equal "e\nco", @pathname.read(4, 3)
  end

  def test_io_binread_returns_file_contents
    File.write(@path, "some\ncontent")

    assert_equal "some\ncontent", @pathname.binread
    assert_equal "some\nc", @pathname.binread(6)
    assert_equal "e\nco", @pathname.binread(4, 3)
  end

  def test_io_binread_reads_contents_as_binary
    File.write(@path, "some\ncontent")

    assert_equal 'ASCII-8BIT', @pathname.binread.encoding.name
  end

  def test_io_readlines_returns_array_of_lines
    File.write(@path, "one\ntwo\nthree\n")

    assert_equal ["one\n", "two\n", "three\n"], @pathname.readlines
  end

  def test_io_sysopen_is_unsupported
    assert_raises(NotImplementedError) { @pathname.sysopen }
  end

  def test_files_are_unlinked
    File.write(@path, '')

    @pathname.unlink
    refute @pathname.exist?
  end

  def test_directories_are_unlinked
    Dir.mkdir(@path)

    @pathname.unlink
    refute @pathname.exist?
  end

  def test_file_is_written
    @pathname.write("some\ncontent")

    assert_equal "some\ncontent", @pathname.read
  end

  def test_pathname_slash
    assert_equal Pathname.new('foo') / 'bar', Pathname.new('foo/bar')
  end

  def test_pathname_size?
    @pathname.write("some\ncontent")
    assert_equal 12, @pathname.size?
  end

  def test_pathname_size
    @pathname.write("some\ncontent")
    assert_equal 12, @pathname.size
  end

  if RUBY_VERSION > '2.4'
    def test_pathname_empty_on_empty_directory
      Dir.mkdir(@path)

      assert_equal true, @pathname.empty?
    end

    def test_pathname_empty_on_non_empty_directory
      Dir.mkdir(@path)
      file_path = File.join(@path, 'a_file.txt')
      FileUtils.touch(file_path)

      assert_equal false, @pathname.empty?
    end

    def test_pathname_empty_on_empty_file
      File.write(@path, '')

      assert_equal true, @pathname.empty?
    end

    def test_pathname_empty_on_non_empty_file
      File.write(@path, "some\ncontent")

      assert_equal false, @pathname.empty?
    end

    def test_pathname_empty_on_nonexistent_path
      refute @pathname.exist?

      assert_equal false, @pathname.empty?
    end
  else
    def test_pathname_empty_not_implemented
      assert_equal false, Pathname.instance_methods.include?(:empty?)
    end
  end
end