diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index e039e332e7f5..4a08b7d10110 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -621,6 +621,13 @@ describe "File" do file.gets(4).should eq("ello") end + it "seeks from the current position" do + file = File.new("#{__DIR__}/data/test_file.txt") + file.gets(5) + file.seek(-4, IO::Seek::Current) + file.tell.should eq(1) + end + it "raises if invoking seek with a closed file" do file = File.new("#{__DIR__}/data/test_file.txt") file.close diff --git a/src/io/file_descriptor.cr b/src/io/file_descriptor.cr index 7122f216a936..12614f226080 100644 --- a/src/io/file_descriptor.cr +++ b/src/io/file_descriptor.cr @@ -131,7 +131,9 @@ class IO::FileDescriptor check_open flush + offset -= @in_buffer_rem.size if whence.current? seek_value = LibC.lseek(@fd, offset, whence) + if seek_value == -1 raise Errno.new "Unable to seek" end