diff options
| author | Luke Kanies <luke@madstop.com> | 2009-02-13 18:24:34 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2009-02-13 18:24:34 -0600 |
| commit | 3fbec120768d84d208b14f574dfe916e25cfdbef (patch) | |
| tree | 865d59f4ea9cf3782db46ce1ae7fd54b95945035 /spec/unit/util | |
| parent | a2270b4a4f093c6c4f171dcf0c0e05fe101dd979 (diff) | |
| parent | 2561c8e252dcf66890513458750bb1329a03beec (diff) | |
| download | puppet-3fbec120768d84d208b14f574dfe916e25cfdbef.tar.gz puppet-3fbec120768d84d208b14f574dfe916e25cfdbef.tar.xz puppet-3fbec120768d84d208b14f574dfe916e25cfdbef.zip | |
Merge branch '0.24.x'
Conflicts:
lib/puppet/indirector/facts/facter.rb
lib/puppet/provider/augeas/augeas.rb
lib/puppet/util/filetype.rb
spec/unit/indirector/facts/facter.rb
spec/unit/provider/augeas/augeas.rb
test/util/filetype.rb
Diffstat (limited to 'spec/unit/util')
| -rwxr-xr-x | spec/unit/util/autoload.rb | 39 | ||||
| -rw-r--r-- | spec/unit/util/filetype.rb | 110 |
2 files changed, 149 insertions, 0 deletions
diff --git a/spec/unit/util/autoload.rb b/spec/unit/util/autoload.rb new file mode 100755 index 000000000..ff717d6c5 --- /dev/null +++ b/spec/unit/util/autoload.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +require 'puppet/util/autoload' + +describe Puppet::Util::Autoload do + before do + @autoload = Puppet::Util::Autoload.new("foo", "tmp") + + @autoload.stubs(:eachdir).yields "/my/dir" + end + + describe "when loading a file" do + [RuntimeError, LoadError, SyntaxError].each do |error| + it "should not die an if a #{error.to_s} exception is thrown" do + FileTest.stubs(:exists?).returns true + + Kernel.expects(:load).raises error + + lambda { @autoload.load("foo") }.should_not raise_error + end + end + end + + describe "when loading all files" do + before do + Dir.stubs(:glob).returns "file.rb" + end + + [RuntimeError, LoadError, SyntaxError].each do |error| + it "should not die an if a #{error.to_s} exception is thrown" do + Kernel.expects(:require).raises error + + lambda { @autoload.loadall }.should_not raise_error + end + end + end +end diff --git a/spec/unit/util/filetype.rb b/spec/unit/util/filetype.rb new file mode 100644 index 000000000..0506b6b47 --- /dev/null +++ b/spec/unit/util/filetype.rb @@ -0,0 +1,110 @@ +#!/usr/bin/env ruby + +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +require 'puppet/util/filetype' + +# XXX Import all of the tests into this file. +describe Puppet::Util::FileType do + describe "when backing up a file" do + before do + @file = Puppet::Util::FileType.filetype(:flat).new("/my/file") + end + + it "should do nothing if the file does not exist" do + File.expects(:exists?).with("/my/file").returns false + @file.expects(:bucket).never + @file.backup + end + + it "should use its filebucket to backup the file if it exists" do + File.expects(:exists?).with("/my/file").returns true + + bucket = mock 'bucket' + bucket.expects(:backup).with("/my/file") + + @file.expects(:bucket).returns bucket + @file.backup + end + + it "should use the filebucket named 'puppet' if it finds one" do + bucket = mock 'bucket' + bucket.expects(:bucket).returns "mybucket" + + Puppet::Type.type(:filebucket).expects(:[]).with("puppet").returns bucket + + @file.bucket.should == "mybucket" + end + + it "should use the default filebucket if none named 'puppet' is found" do + bucket = mock 'bucket' + bucket.expects(:bucket).returns "mybucket" + + Puppet::Type.type(:filebucket).expects(:[]).with("puppet").returns nil + Puppet::Type.type(:filebucket).expects(:mkdefaultbucket).returns bucket + + @file.bucket.should == "mybucket" + end + end + + describe "the flat filetype" do + before do + @type = Puppet::Util::FileType.filetype(:flat) + end + it "should exist" do + @type.should_not be_nil + end + + describe "when the file already exists" do + it "should return the file's contents when asked to read it" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns true + File.expects(:read).with("/my/file").returns "my text" + + file.read.should == "my text" + end + + it "should unlink the file when asked to remove it" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns true + File.expects(:unlink).with("/my/file") + + file.remove + end + end + + describe "when the file does not exist" do + it "should return an empty string when asked to read the file" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns false + + file.read.should == "" + end + end + + describe "when writing the file" do + before do + @file = @type.new("/my/file") + FileUtils.stubs(:cp) + + @tempfile = stub 'tempfile', :print => nil, :close => nil, :flush => nil, :path => "/other/file" + Tempfile.stubs(:new).returns @tempfile + end + + it "should first create a temp file and copy its contents over to the file location" do + Tempfile.expects(:new).with("puppet").returns @tempfile + @tempfile.expects(:print).with("my text") + @tempfile.expects(:flush) + @tempfile.expects(:close) + FileUtils.expects(:cp).with(@tempfile.path, "/my/file") + + @file.write "my text" + end + + it "should set the selinux default context on the file" do + @file.expects(:set_selinux_default_context).with("/my/file") + @file.write "eh" + end + end + end +end |
