Skip to content

Commit

Permalink
Add microseconds specs for File.mtime, .atime and .ctime
Browse files Browse the repository at this point in the history
Reference: jruby/jruby#4520 (comment)

Three attemps to avoid zero in these values with small sleep (if failed).

And some code refactoring.
  • Loading branch information
AlexWayfer committed Mar 6, 2017
1 parent b0de2aa commit 1321c46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
12 changes: 10 additions & 2 deletions core/file/atime_spec.rb
Expand Up @@ -4,15 +4,23 @@
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
rm_r @file
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
Expand Down
12 changes: 10 additions & 2 deletions core/file/ctime_spec.rb
Expand Up @@ -3,15 +3,23 @@
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
@file = nil
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
Expand Down
10 changes: 9 additions & 1 deletion core/file/mtime_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1321c46

Please sign in to comment.