summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorPaul Lathrop <paul@tertiusfamily.net>2008-04-07 12:44:31 -0700
committerPaul Lathrop <paul@tertiusfamily.net>2008-04-07 12:44:31 -0700
commit7295626c25ab878aaaf6ec577a42f91c0f18fdbc (patch)
treed158f4e624eca1e45aab73ac99aa12902dc669da /spec
parent69cf800462ee9ca1e0a9ba33a83d532699129842 (diff)
downloadpuppet-7295626c25ab878aaaf6ec577a42f91c0f18fdbc.tar.gz
puppet-7295626c25ab878aaaf6ec577a42f91c0f18fdbc.tar.xz
puppet-7295626c25ab878aaaf6ec577a42f91c0f18fdbc.zip
Used stubs to decouple our code behavior from the behavior of the underlying filesystem, as well as removing the need to sleep (which caused the tests to take a long time).
Diffstat (limited to 'spec')
-rw-r--r--spec/unit/util/loadedfile.rb42
1 files changed, 23 insertions, 19 deletions
diff --git a/spec/unit/util/loadedfile.rb b/spec/unit/util/loadedfile.rb
index b57475f96..083120e20 100644
--- a/spec/unit/util/loadedfile.rb
+++ b/spec/unit/util/loadedfile.rb
@@ -6,42 +6,51 @@ require 'tempfile'
require 'puppet/util/loadedfile'
describe Puppet::Util::LoadedFile do
- before(:all) do
- # First, save and adjust the timeout so tests don't take forever.
- @saved_filetimeout = Puppet[:filetimeout]
- Puppet[:filetimeout] = 5
- end
-
before(:each) do
@f = Tempfile.new('loadedfile_test')
@f.puts "yayness"
@f.flush
+
@loaded = Puppet::Util::LoadedFile.new(@f.path)
+
+ fake_ctime = Time.now - (2 * Puppet[:filetimeout])
+ @stat = stub('stat', :ctime => fake_ctime)
+ @fake_now = Time.now + (2 * Puppet[:filetimeout])
end
it "should recognize when the file has not changed" do
- sleep(Puppet[:filetimeout])
+ # Use fake "now" so that we can be sure changed? actually checks, without sleeping
+ # for Puppet[:filetimeout] seconds.
+ Time.stubs(:now).returns(@fake_now)
@loaded.changed?.should == false
end
it "should recognize when the file has changed" do
- @f.puts "booness"
- @f.flush
- sleep(Puppet[:filetimeout])
+ # Fake File.stat so we don't have to depend on the filesystem granularity. Doing a flush()
+ # just didn't do the job.
+ File.stubs(:stat).returns(@stat)
+ # Use fake "now" so that we can be sure changed? actually checks, without sleeping
+ # for Puppet[:filetimeout] seconds.
+ Time.stubs(:now).returns(@fake_now)
@loaded.changed?.should be_an_instance_of(Time)
end
it "should not catch a change until the timeout has elapsed" do
- @f.puts "yay"
- @f.flush
+ # Fake File.stat so we don't have to depend on the filesystem granularity. Doing a flush()
+ # just didn't do the job.
+ File.stubs(:stat).returns(@stat)
@loaded.changed?.should be(false)
- sleep(Puppet[:filetimeout])
+ # Use fake "now" so that we can be sure changed? actually checks, without sleeping
+ # for Puppet[:filetimeout] seconds.
+ Time.stubs(:now).returns(@fake_now)
@loaded.changed?.should_not be(false)
end
it "should consider a file changed when that file is missing" do
@f.close!
- sleep(Puppet[:filetimeout])
+ # Use fake "now" so that we can be sure changed? actually checks, without sleeping
+ # for Puppet[:filetimeout] seconds.
+ Time.stubs(:now).returns(@fake_now)
@loaded.changed?.should_not be(false)
end
@@ -53,9 +62,4 @@ describe Puppet::Util::LoadedFile do
after(:each) do
@f.close
end
-
- after(:all) do
- # Restore the saved timeout.
- Puppet[:filetimeout] = @saved_filetimeout
- end
end