File: file_test.rb

package info (click to toggle)
ruby-fakefs 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,622; makefile: 5
file content (114 lines) | stat: -rw-r--r-- 2,442 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
# frozen_string_literal: true

require_relative '../test_helper'

# Fake File test class
class FakeFileTest < Minitest::Test
  include FakeFS

  def setup
    FileSystem.clear

    @file = FakeFile.new
  end

  def test_fake_file_has_empty_content_by_default
    assert_equal '', @file.content
  end

  def test_fake_file_can_read_and_write_to_content
    @file.content = 'foobar'
    assert_equal 'foobar', @file.content
  end

  def test_fake_file_has_1_link_by_default
    assert_equal [@file], @file.links
  end

  def test_fake_file_can_create_link
    other_file = FakeFile.new

    @file.link(other_file)

    assert_equal [@file, other_file], @file.links
  end

  def test_fake_file_wont_add_link_to_same_file_twice
    other_file = FakeFile.new

    @file.link other_file
    @file.link other_file

    assert_equal [@file, other_file], @file.links
  end

  def test_links_are_mutual
    other_file = FakeFile.new

    @file.link(other_file)

    assert_equal [@file, other_file], other_file.links
  end

  def test_can_link_multiple_files
    file_two   = FakeFile.new
    file_three = FakeFile.new

    @file.link file_two
    @file.link file_three

    assert_equal [@file, file_two, file_three], @file.links
    assert_equal [@file, file_two, file_three], file_two.links
    assert_equal [@file, file_two, file_three], file_three.links
  end

  def test_links_share_same_content
    other_file = FakeFile.new

    @file.link other_file

    @file.content = 'foobar'

    assert_equal 'foobar', other_file.content
  end

  def test_clone_creates_new_inode
    clone = @file.clone
    refute clone.inode.equal?(@file.inode)
  end

  def test_cloning_does_not_use_same_content_object
    clone = @file.clone

    clone.content = 'foo'
    @file.content = 'bar'

    assert_equal 'foo', clone.content
    assert_equal 'bar', @file.content
  end

  def test_raises_an_error_with_the_correct_path
    path = '/some/non/existing/file'
    begin
      FakeFS::File.new path
      msg = nil
    rescue Errno::ENOENT => e
      msg = e.message
    end
    assert_equal "No such file or directory - #{path}", msg
  end

  def test_file_size_question_works
    assert_nil FileTest.size?('does-not-exist.txt')

    File.open('empty.txt', 'w') do |f|
      f << ''
    end
    assert_nil FileTest.size?('empty.txt')

    File.open('one-char.txt', 'w') do |f|
      f << 'a'
    end
    assert_equal 1, FileTest.size?('one-char.txt')
  end
end