summaryrefslogtreecommitdiff
path: root/test/ruby/test_file.rb
blob: e326b97b07f3a7bb48841d184ab8ad971a0f8808 (plain)
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
require 'test/unit'
require 'tempfile'
require 'ut_eof'

class TestFile < Test::Unit::TestCase

  # I don't know Ruby's spec about "unlink-before-close" exactly.
  # This test asserts current behaviour.
  def test_unlink_before_close
    filename = File.basename(__FILE__) + ".#{$$}"
    w = File.open(filename, "w")
    w << "foo"
    w.close
    r = File.open(filename, "r")
    begin
      if /(mswin|bccwin|mingw|emx)/ =~ RUBY_PLATFORM
        assert_raise(Errno::EACCES) {File.unlink(filename)}
      else
	assert_nothing_raised {File.unlink(filename)}
      end
    ensure
      r.close
      File.unlink(filename) if File.exist?(filename)
    end
  end

  include TestEOF
  def open_file(content)
    f = Tempfile.new("test-eof")
    f << content
    f.rewind
    yield f
  end
  alias open_file_rw open_file

  include TestEOF::Seek

  def test_truncate_wbuf # [ruby-dev:24191]
    f = Tempfile.new("test-truncate")
    f.print "abc"
    f.truncate(0)
    f.print "def"
    f.close
    assert_equal("\0\0\0def", File.read(f.path))
  end

  def test_truncate_rbuf # [ruby-dev:24197]
    f = Tempfile.new("test-truncate")
    f.puts "abc"
    f.puts "def"
    f.close
    f.open
    assert_equal("abc\n", f.gets)
    f.truncate(3)
    assert_equal(nil, f.gets)
  end

  def test_truncate_beyond_eof
    f = Tempfile.new("test-truncate")
    f.print "abc"
    f.truncate 10
    assert_equal("\0" * 7, f.read(100), "[ruby-dev:24532]")
  end

  def test_read_all_extended_file
    f = Tempfile.new("test-extended-file")
    assert_nil(f.getc)
    open(f.path, "w") {|g| g.print "a" }
    assert_equal("a", f.read)
  end

  def test_gets_extended_file
    f = Tempfile.new("test-extended-file")
    assert_nil(f.getc)
    open(f.path, "w") {|g| g.print "a" }
    assert_equal("a", f.gets("a"))
  end

  def test_gets_para_extended_file
    f = Tempfile.new("test-extended-file")
    assert_nil(f.getc)
    open(f.path, "w") {|g| g.print "\na" }
    assert_equal("a", f.gets(""))
  end

  def test_each_byte_extended_file
    f = Tempfile.new("test-extended-file")
    assert_nil(f.getc)
    open(f.path, "w") {|g| g.print "a" }
    result = []
    f.each_byte {|b| result << b.chr }
    assert_equal([?a], result)
  end

  def test_getc_extended_file
    f = Tempfile.new("test-extended-file")
    assert_nil(f.getc)
    open(f.path, "w") {|g| g.print "a" }
    assert_equal(?a, f.getc)
  end

  def test_s_chown
    assert_nothing_raised { File.chown -1, -1 }
    assert_nothing_raised { File.chown nil, nil }
  end

  def test_chown
    assert_nothing_raised {
      File.open(__FILE__) {|f| f.chown -1, -1 }
    }
    # [ruby-dev:27140]
    assert_nothing_raised {
      File.open(__FILE__) {|f| f.chown nil, nil }
    }
  end

end