Skip to content

Instantly share code, notes, and snippets.

@kronos
Created April 7, 2011 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kronos/908805 to your computer and use it in GitHub Desktop.
Save kronos/908805 to your computer and use it in GitHub Desktop.
diff --git a/kernel/common/file.rb b/kernel/common/file.rb
index 5e56cfb..e67341b 100644
--- a/kernel/common/file.rb
+++ b/kernel/common/file.rb
@@ -1010,6 +1010,8 @@ class File < IO
ensure_open_and_writable
raise Errno::EINVAL, "Can't truncate a file to a negative length" if length < 0
+ flush
+ reset_buffering
n = POSIX.ftruncate(@descriptor, length)
Errno.handle if n == -1
n
diff --git a/spec/ruby/core/file/truncate_spec.rb b/spec/ruby/core/file/truncate_spec.rb
index 053c218..c554a5f 100644
--- a/spec/ruby/core/file/truncate_spec.rb
+++ b/spec/ruby/core/file/truncate_spec.rb
@@ -100,6 +100,24 @@ describe "File#truncate" do
rm_r @name
end
+ it "spec a" do
+ @file.seek(0)
+ @file.print "a"
+ @file.truncate(0)
+ @file.print "b"
+ @file.close
+ IO.read(@name).should == "\0b"
+ end
+
+ it "spec b" do
+ File.open(@name, "r+") do |f|
+ f.read(1).should == "1"
+ f.truncate(0)
+ f.read(1).should == nil
+ f.eof?.should == true
+ end
+ end
+
it "truncates a file" do
File.size(@name).should == 10
Before:
rubinius 1.2.4dev (1.8.7 4f52d849 yyyy-mm-dd JI) [x86_64-apple-darwin10.7.3]
..........FF..........
1)
File#truncate replaced unflushed data by FAILED
Expected "ab"
to equal "\000b"
/expectations.rb:15
{ } in Object#__script__ at spec/ruby/core/file/truncate_spec.rb:109
Kernel(Object)#instance_eval at kernel/common/eval.rb:164
{ } in Enumerable(Array)#all? at kernel/common/enumerable.rb:205
Array#each at kernel/bootstrap/array.rb:76
Enumerable(Array)#all? at kernel/common/enumerable.rb:205
Array#each at kernel/bootstrap/array.rb:76
Object#__script__ at spec/ruby/core/file
/truncate_spec.rb:90
Kernel.load at kernel/common/kernel.rb:732
Kernel(Object)#instance_eval at kernel/common/eval.rb:164
Array#each at kernel/bootstrap/array.rb:76
Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:65
Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:90
Rubinius::Loader#script at kernel/loader.rb:591
Rubinius::Loader#main at kernel/loader.rb:721
Rubinius::Loader.main at kernel/loader.rb:760
Object#__script__ at kernel/loader.rb:771
2)
File#truncate spec b FAILED
Expected "2"
to equal nil
/expectations.rb:15
{ } in Object#__script__ at spec/ruby/core/file/truncate_spec.rb:116
IO.open at kernel/common/io.rb:262
{ } in Object#__script__ at spec/ruby/core/file/truncate_spec.rb:113
Kernel(Object)#instance_eval at kernel/common/eval.rb:164
{ } in Enumerable(Array)#all? at kernel/common/enumerable.rb:205
Array#each at kernel/bootstrap/array.rb:76
Enumerable(Array)#all? at kernel/common/enumerable.rb:205
Array#each at kernel/bootstrap/array.rb:76
Object#__script__ at spec/ruby/core/file
/truncate_spec.rb:90
Kernel.load at kernel/common/kernel.rb:732
Kernel(Object)#instance_eval at kernel/common/eval.rb:164
Array#each at kernel/bootstrap/array.rb:76
Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:65
Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:90
Rubinius::Loader#script at kernel/loader.rb:591
Rubinius::Loader#main at kernel/loader.rb:721
Rubinius::Loader.main at kernel/loader.rb:760
Object#__script__ at kernel/loader.rb:771
Finished in 0.049746 seconds
After:
rubinius 1.2.4dev (1.8.7 7b94a33f yyyy-mm-dd JI) [x86_64-apple-darwin10.7.3]
......................
Finished in 0.026328 seconds
1 file, 22 examples, 43 expectations, 0 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment