From cf19bd8dea141a59cdff5a7f1edc56d3620ab0e2 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 15 Dec 2008 00:22:09 -0600 Subject: Not using a temporary file when locking files for writing. The temporary file was not actually useful, because we could never really get atomic renames, for annoying, complicated reasons. This hopefully finally fixes #1812. Signed-off-by: Luke Kanies --- spec/unit/util/file_locking.rb | 43 ++++++------------------------------------ 1 file changed, 6 insertions(+), 37 deletions(-) (limited to 'spec/unit') diff --git a/spec/unit/util/file_locking.rb b/spec/unit/util/file_locking.rb index a8b0c1840..70003faa1 100755 --- a/spec/unit/util/file_locking.rb +++ b/spec/unit/util/file_locking.rb @@ -98,49 +98,18 @@ describe Puppet::Util::FileLocking do Puppet::Util::FileLocking.writelock('/file') end - it "should write to a temporary file" do + it "should allow the caller to write to the locked file" do fh = mock 'fh' File.expects(:open).yields fh lfh = mock 'locked_filehandle' - fh.expects(:lock_exclusive).yields lfh + fh.expects(:lock_exclusive).yields(lfh) - tf = mock 'tmp_filehandle' - File.expects(:open).with { |path, *args| path == "/file.tmp" }.yields tf + lfh.expects(:print).with "foo" - result = nil - File.stubs(:rename) - Puppet::Util::FileLocking.writelock('/file') { |f| result = f } - result.should equal(tf) - end - - it "should rename the temporary file to the normal file" do - fh = stub 'fh' - fh.stubs(:lock_exclusive).yields fh - File.stubs(:open).yields fh - - File.expects(:rename).with("/file.tmp", "/file") - Puppet::Util::FileLocking.writelock('/file') { |f| } - end - - it "should fail if it cannot rename the file" do - fh = stub 'fh' - fh.stubs(:lock_exclusive).yields fh - File.stubs(:open).yields fh - - File.expects(:rename).with("/file.tmp", "/file").raises(RuntimeError) - lambda { Puppet::Util::FileLocking.writelock('/file') { |f| } }.should raise_error(Puppet::Error) - end - - it "should remove the temporary file if the rename fails" do - fh = stub 'fh' - fh.stubs(:lock_exclusive).yields fh - File.stubs(:open).yields fh - - File.expects(:rename).with("/file.tmp", "/file").raises(RuntimeError) - File.expects(:exist?).with("/file.tmp").returns true - File.expects(:unlink).with("/file.tmp") - lambda { Puppet::Util::FileLocking.writelock('/file') { |f| } }.should raise_error(Puppet::Error) + Puppet::Util::FileLocking.writelock('/file') do |f| + f.print "foo" + end end end end -- cgit