From 1321c46178fbb41a0e498c6ebf316311631caded Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Tue, 7 Mar 2017 00:35:23 +0300 Subject: [PATCH] Add microseconds specs for File.mtime, .atime and .ctime Reference: https://github.com/jruby/jruby/issues/4520#issuecomment-284501906 Three attemps to avoid zero in these values with small sleep (if failed). And some code refactoring. --- core/file/atime_spec.rb | 12 ++++++++++-- core/file/ctime_spec.rb | 12 ++++++++++-- core/file/mtime_spec.rb | 10 +++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/core/file/atime_spec.rb b/core/file/atime_spec.rb index d53ef3b2c2..845616456d 100644 --- a/core/file/atime_spec.rb +++ b/core/file/atime_spec.rb @@ -4,6 +4,11 @@ before :each do @file = tmp('test.txt') touch @file + 3.times do + @atime = File.atime(@file) + break if @atime.usec > 0 + sleep 0.1 + end end after :each do @@ -11,8 +16,11 @@ end it "returns the last access time for the named file as a Time object" do - File.atime(@file) - File.atime(@file).should be_kind_of(Time) + @atime.should be_kind_of(Time) + end + + it "returns the last access time for the named file with microseconds" do + @atime.usec.should > 0 end it "raises an Errno::ENOENT exception if the file is not found" do diff --git a/core/file/ctime_spec.rb b/core/file/ctime_spec.rb index efcce3af7b..59d21e3643 100644 --- a/core/file/ctime_spec.rb +++ b/core/file/ctime_spec.rb @@ -3,6 +3,11 @@ describe "File.ctime" do before :each do @file = __FILE__ + 3.times do + @ctime = File.ctime(@file) + break if @ctime.usec > 0 + sleep 0.1 + end end after :each do @@ -10,8 +15,11 @@ end it "Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself)." do - File.ctime(@file) - File.ctime(@file).should be_kind_of(Time) + @ctime.should be_kind_of(Time) + end + + it "Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself) with microseconds." do + @ctime.usec.should > 0 end it "accepts an object that has a #to_path method" do diff --git a/core/file/mtime_spec.rb b/core/file/mtime_spec.rb index ed17ab6c82..d36b8a362b 100644 --- a/core/file/mtime_spec.rb +++ b/core/file/mtime_spec.rb @@ -3,7 +3,11 @@ describe "File.mtime" do before :each do @filename = tmp('i_exist') - touch(@filename) { @mtime = Time.now } + 3.times do + touch(@filename) { @mtime = Time.now } + break if @mtime.usec > 0 + sleep 0.1 + end end after :each do @@ -15,6 +19,10 @@ File.mtime(@filename).should be_close(@mtime, 2.0) end + it "returns the modification Time of the file with microseconds" do + File.mtime(@filename).usec.should > 0 + end + it "raises an Errno::ENOENT exception if the file is not found" do lambda { File.mtime('bogus') }.should raise_error(Errno::ENOENT) end