1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/classes'
4 describe "IO#sysseek on a file" do
5 # TODO: This should be made more generic with seek spec
7 @file = File.open(File.dirname(__FILE__) + '/fixtures/readlines.txt', 'r+')
8 @io = IO.open @file.fileno, 'r'
12 # we *must* close both in order to not leak descriptors
13 @io.close unless @io.closed?
14 @file.close unless @file.closed? rescue Errno::EBADF
17 it "moves the read position relative to the current position with SEEK_CUR" do
18 @io.sysseek(10, IO::SEEK_CUR)
19 @io.readline.should == "igne une.\n"
22 it "raises an error when called after buffered reads" do
24 lambda { @io.sysseek(-5, IO::SEEK_CUR) }.should raise_error(IOError)
27 it "warns if called immediately after a buffered IO#write" do
29 # copy contents to a separate file
30 tmpfile = File.open("/tmp/tmp_IO_sysseek", "w")
31 tmpfile.write(@file.read)
32 tmpfile.seek(0, File::SEEK_SET)
34 tmpfile.write("abcde")
35 lambda { tmpfile.sysseek(10) }.should complain(/sysseek/)
38 File.unlink(tmpfile.path)
42 it "moves the read position relative to the start with SEEK_SET" do
43 @io.sysseek(42, IO::SEEK_SET)
44 @io.readline.should == "quí está la línea tres.\n"
47 it "moves the read position relative to the end with SEEK_END" do
48 @io.sysseek(1, IO::SEEK_END)
50 # this is the safest way of checking the EOF when
51 # sys-* methods are invoked
54 }.should raise_error(EOFError)
56 @io.sysseek(-25, IO::SEEK_END)
57 @io.sysread(7).should == "cinco.\n"
60 it "can handle any numerical argument without breaking and can seek past EOF" do
61 @io.sysseek(1.2).should == 1
62 @io.sysseek(2**10).should == 1024
63 @io.sysseek(2**32).should == 4294967296
64 lambda { @io.sysseek(2**128) }.should raise_error(RangeError)
67 it "raises IOError on closed stream" do
68 lambda { IOSpecs.closed_file.sysseek(0) }.should raise_error(IOError)