diff options
| author | Josh Cooper <josh@puppetlabs.com> | 2011-07-18 23:05:35 -0700 |
|---|---|---|
| committer | Jacob Helwig <jacob@puppetlabs.com> | 2011-08-19 13:48:29 -0700 |
| commit | c26f3e5f79fa2caa17d18be41e32871ac09b1dc4 (patch) | |
| tree | a27fb91f82ba4e98bb4ed4281151a150f0b51fac /spec/unit/util | |
| parent | bfeb33734358528209221be09b97ffc8513d3f87 (diff) | |
| download | puppet-c26f3e5f79fa2caa17d18be41e32871ac09b1dc4.tar.gz puppet-c26f3e5f79fa2caa17d18be41e32871ac09b1dc4.tar.xz puppet-c26f3e5f79fa2caa17d18be41e32871ac09b1dc4.zip | |
Fix tests with "relative" paths on Windows
Absolute paths on Unix, e.g. /foo/bar, are not absolute on Windows,
which breaks many test cases. This commit adds a method to
PuppetSpec::Files.make_absolute that makes the path absolute in
test cases.
On Unix (Puppet.features.posix?) it is a no-op. On Windows,
(Puppet.features.microsoft_windows?) the drive from the current
working directory is prepended.
Reviewed-by: Jacob Helwig <jacob@puppetlabs.com>
(cherry picked from commit 462a95e3d077b1915a919399b846068816c84583)
Conflicts:
spec/unit/parser/functions/extlookup_spec.rb
Diffstat (limited to 'spec/unit/util')
| -rwxr-xr-x | spec/unit/util/autoload_spec.rb | 38 | ||||
| -rwxr-xr-x | spec/unit/util/backups_spec.rb | 29 | ||||
| -rwxr-xr-x | spec/unit/util/log_spec.rb | 6 | ||||
| -rwxr-xr-x | spec/unit/util/network_device/config_spec.rb | 8 | ||||
| -rwxr-xr-x | spec/unit/util/rdoc/parser_spec.rb | 4 | ||||
| -rwxr-xr-x | spec/unit/util/settings/file_setting_spec.rb | 9 | ||||
| -rwxr-xr-x | spec/unit/util/settings_spec.rb | 14 | ||||
| -rwxr-xr-x | spec/unit/util/storage_spec.rb | 2 |
8 files changed, 68 insertions, 42 deletions
diff --git a/spec/unit/util/autoload_spec.rb b/spec/unit/util/autoload_spec.rb index d61b7689e..100975f27 100755 --- a/spec/unit/util/autoload_spec.rb +++ b/spec/unit/util/autoload_spec.rb @@ -4,6 +4,8 @@ require 'spec_helper' require 'puppet/util/autoload' describe Puppet::Util::Autoload do + include PuppetSpec::Files + before do @autoload = Puppet::Util::Autoload.new("foo", "tmp") @@ -15,32 +17,38 @@ describe Puppet::Util::Autoload do end describe "when building the search path" do + before :each do + @dira = make_absolute('/a') + @dirb = make_absolute('/b') + @dirc = make_absolute('/c') + end + it "should collect all of the plugins and lib directories that exist in the current environment's module path" do Puppet.settings.expects(:value).with(:environment).returns "foo" - Puppet.settings.expects(:value).with(:modulepath, :foo).returns "/a:/b:/c" - Dir.expects(:entries).with("/a").returns %w{one two} - Dir.expects(:entries).with("/b").returns %w{one two} + Puppet.settings.expects(:value).with(:modulepath, :foo).returns "#{@dira}#{File::PATH_SEPARATOR}#{@dirb}#{File::PATH_SEPARATOR}#{@dirc}" + Dir.expects(:entries).with(@dira).returns %w{one two} + Dir.expects(:entries).with(@dirb).returns %w{one two} FileTest.stubs(:directory?).returns false - FileTest.expects(:directory?).with("/a").returns true - FileTest.expects(:directory?).with("/b").returns true - %w{/a/one/plugins /a/two/lib /b/one/plugins /b/two/lib}.each do |d| + FileTest.expects(:directory?).with(@dira).returns true + FileTest.expects(:directory?).with(@dirb).returns true + ["#{@dira}/one/plugins", "#{@dira}/two/lib", "#{@dirb}/one/plugins", "#{@dirb}/two/lib"].each do |d| FileTest.expects(:directory?).with(d).returns true end - @autoload.module_directories.should == %w{/a/one/plugins /a/two/lib /b/one/plugins /b/two/lib} + @autoload.module_directories.should == ["#{@dira}/one/plugins", "#{@dira}/two/lib", "#{@dirb}/one/plugins", "#{@dirb}/two/lib"] end it "should not look for lib directories in directories starting with '.'" do Puppet.settings.expects(:value).with(:environment).returns "foo" - Puppet.settings.expects(:value).with(:modulepath, :foo).returns "/a" - Dir.expects(:entries).with("/a").returns %w{. ..} - - FileTest.expects(:directory?).with("/a").returns true - FileTest.expects(:directory?).with("/a/./lib").never - FileTest.expects(:directory?).with("/a/./plugins").never - FileTest.expects(:directory?).with("/a/../lib").never - FileTest.expects(:directory?).with("/a/../plugins").never + Puppet.settings.expects(:value).with(:modulepath, :foo).returns @dira + Dir.expects(:entries).with(@dira).returns %w{. ..} + + FileTest.expects(:directory?).with(@dira).returns true + FileTest.expects(:directory?).with("#{@dira}/./lib").never + FileTest.expects(:directory?).with("#{@dira}/./plugins").never + FileTest.expects(:directory?).with("#{@dira}/../lib").never + FileTest.expects(:directory?).with("#{@dira}/../plugins").never @autoload.module_directories end diff --git a/spec/unit/util/backups_spec.rb b/spec/unit/util/backups_spec.rb index 611c19304..d2f36a6e6 100755 --- a/spec/unit/util/backups_spec.rb +++ b/spec/unit/util/backups_spec.rb @@ -4,28 +4,31 @@ require 'spec_helper' require 'puppet/util/backups' describe Puppet::Util::Backups do + include PuppetSpec::Files + before do FileTest.stubs(:exists?).returns true + @nosuchfile = make_absolute('/no/such/file') end describe "when backing up a file" do it "should noop if the file does not exist" do FileTest.expects(:exists?).returns false - file = Puppet::Type.type(:file).new(:name => '/no/such/file') + file = Puppet::Type.type(:file).new(:name => @nosuchfile) file.expects(:bucket).never file.perform_backup end it "should succeed silently if self[:backup] is false" do - file = Puppet::Type.type(:file).new(:name => '/no/such/file', :backup => false) + file = Puppet::Type.type(:file).new(:name => @nosuchfile, :backup => false) file.expects(:bucket).never FileTest.expects(:exists?).never file.perform_backup end it "a bucket should be used when provided" do - path = '/my/file' + path = make_absolute('/my/file') File.stubs(:stat).with(path).returns(mock('stat', :ftype => 'file')) @@ -39,7 +42,7 @@ describe Puppet::Util::Backups do end it "should propagate any exceptions encountered when backing up to a filebucket" do - path = '/my/file' + path = make_absolute('/my/file') File.stubs(:stat).with(path).returns(mock('stat', :ftype => 'file')) @@ -54,7 +57,7 @@ describe Puppet::Util::Backups do describe "and no filebucket is configured" do it "should remove any local backup if one exists" do - path = '/my/file' + path = make_absolute('/my/file') FileTest.stubs(:exists?).returns true backup = path + ".foo" @@ -69,7 +72,7 @@ describe Puppet::Util::Backups do end it "should fail when the old backup can't be removed" do - path = '/my/file' + path = make_absolute('/my/file') FileTest.stubs(:exists?).returns true backup = path + ".foo" @@ -84,7 +87,7 @@ describe Puppet::Util::Backups do end it "should not try to remove backups that don't exist" do - path = '/my/file' + path = make_absolute('/my/file') FileTest.stubs(:exists?).returns true backup = path + ".foo" @@ -99,7 +102,7 @@ describe Puppet::Util::Backups do end it "a copy should be created in the local directory" do - path = '/my/file' + path = make_absolute('/my/file') FileTest.stubs(:exists?).with(path).returns true FileUtils.expects(:cp_r).with(path, path + ".foo", :preserve => true) @@ -109,7 +112,7 @@ describe Puppet::Util::Backups do end it "should propagate exceptions if no backup can be created" do - path = '/my/file' + path = make_absolute('/my/file') FileTest.stubs(:exists?).with(path).returns true FileUtils.expects(:cp_r).raises ArgumentError @@ -122,13 +125,13 @@ describe Puppet::Util::Backups do describe "when backing up a directory" do it "a bucket should work when provided" do - path = '/my/dir' + path = make_absolute('/my/dir') File.stubs(:file?).returns true - Find.expects(:find).with(path).yields("/my/dir/file") + Find.expects(:find).with(path).yields(make_absolute("/my/dir/file")) bucket = stub('bucket', :name => "eh") - bucket.expects(:backup).with("/my/dir/file").returns true + bucket.expects(:backup).with(make_absolute("/my/dir/file")).returns true file = Puppet::Type.type(:file).new(:name => path, :backup => 'foo') file.stubs(:bucket).returns bucket @@ -139,7 +142,7 @@ describe Puppet::Util::Backups do end it "should do nothing when recursing" do - path = '/my/dir' + path = make_absolute('/my/dir') bucket = stub('bucket', :name => "eh") bucket.expects(:backup).never diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb index 1baa0d5af..39da4b010 100755 --- a/spec/unit/util/log_spec.rb +++ b/spec/unit/util/log_spec.rb @@ -4,6 +4,8 @@ require 'spec_helper' require 'puppet/util/log' describe Puppet::Util::Log do + include PuppetSpec::Files + it "should write a given message to the specified destination" do arraydest = [] Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(arraydest)) @@ -167,7 +169,7 @@ describe Puppet::Util::Log do describe "when setting the source as a RAL object" do it "should tag itself with any tags the source has" do - source = Puppet::Type.type(:file).new :path => "/foo/bar" + source = Puppet::Type.type(:file).new :path => make_absolute("/foo/bar") log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source) source.tags.each do |tag| log.tags.should be_include(tag) @@ -188,7 +190,7 @@ describe Puppet::Util::Log do end it "should copy over any file and line information" do - source = Puppet::Type.type(:file).new :path => "/foo/bar" + source = Puppet::Type.type(:file).new :path => make_absolute("/foo/bar") source.file = "/my/file" source.line = 50 log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source) diff --git a/spec/unit/util/network_device/config_spec.rb b/spec/unit/util/network_device/config_spec.rb index d69358a92..d9bd3d979 100755 --- a/spec/unit/util/network_device/config_spec.rb +++ b/spec/unit/util/network_device/config_spec.rb @@ -4,9 +4,11 @@ require 'spec_helper' require 'puppet/util/network_device/config' describe Puppet::Util::NetworkDevice::Config do + include PuppetSpec::Files + before(:each) do - Puppet[:deviceconfig] = "/dummy" - FileTest.stubs(:exists?).with("/dummy").returns(true) + Puppet[:deviceconfig] = make_absolute("/dummy") + FileTest.stubs(:exists?).with(make_absolute("/dummy")).returns(true) end describe "when initializing" do @@ -15,7 +17,7 @@ describe Puppet::Util::NetworkDevice::Config do end it "should use the deviceconfig setting as pathname" do - Puppet.expects(:[]).with(:deviceconfig).returns("/dummy") + Puppet.expects(:[]).with(:deviceconfig).returns(make_absolute("/dummy")) Puppet::Util::NetworkDevice::Config.new end diff --git a/spec/unit/util/rdoc/parser_spec.rb b/spec/unit/util/rdoc/parser_spec.rb index 92b50e09b..4c2c79e88 100755 --- a/spec/unit/util/rdoc/parser_spec.rb +++ b/spec/unit/util/rdoc/parser_spec.rb @@ -8,6 +8,8 @@ require 'rdoc/options' require 'rdoc/rdoc' describe RDoc::Parser, :'fails_on_ruby_1.9.2' => true do + include PuppetSpec::Files + before :each do File.stubs(:stat).with("init.pp") @top_level = stub_everything 'toplevel', :file_relative_name => "init.pp" @@ -21,7 +23,7 @@ describe RDoc::Parser, :'fails_on_ruby_1.9.2' => true do Puppet::Parser::Parser.stubs(:new).returns(parser) parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new('')).at_least_once parser.expects(:file=).with("module/manifests/init.pp") - parser.expects(:file=).with("/dev/null/manifests/site.pp") + parser.expects(:file=).with(make_absolute("/dev/null/manifests/site.pp")) @parser.scan end diff --git a/spec/unit/util/settings/file_setting_spec.rb b/spec/unit/util/settings/file_setting_spec.rb index 489628a78..01d891f08 100755 --- a/spec/unit/util/settings/file_setting_spec.rb +++ b/spec/unit/util/settings/file_setting_spec.rb @@ -7,8 +7,10 @@ require 'puppet/util/settings/file_setting' describe Puppet::Util::Settings::FileSetting do FileSetting = Puppet::Util::Settings::FileSetting + include PuppetSpec::Files + before do - @basepath = Puppet.features.posix? ? "/somepath" : "C:/somepath" + @basepath = make_absolute("/somepath") end describe "when determining whether the service user should be used" do @@ -165,7 +167,10 @@ describe Puppet::Util::Settings::FileSetting do it "should fully qualified returned files if necessary (#795)" do @settings.stubs(:value).with(:mydir).returns "myfile" - @file.to_resource.title.should == File.join(Dir.getwd, "myfile") + path = File.join(Dir.getwd, "myfile") + # Dir.getwd can return windows paths with backslashes, so we normalize them using expand_path + path = File.expand_path(path) if Puppet.features.microsoft_windows? + @file.to_resource.title.should == path end it "should set the mode on the file if a mode is provided" do diff --git a/spec/unit/util/settings_spec.rb b/spec/unit/util/settings_spec.rb index a2cd57a0c..f7cb19936 100755 --- a/spec/unit/util/settings_spec.rb +++ b/spec/unit/util/settings_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' require 'ostruct' describe Puppet::Util::Settings do + include PuppetSpec::Files + describe "when specifying defaults" do before do @settings = Puppet::Util::Settings.new @@ -378,7 +380,7 @@ describe Puppet::Util::Settings do end it "should use its current ':config' value for the file to parse" do - myfile = Puppet.features.posix? ? "/my/file" : "C:/myfile" # do not stub expand_path here, as this leads to a stack overflow, when mocha tries to use it + myfile = make_absolute("/my/file") # do not stub expand_path here, as this leads to a stack overflow, when mocha tries to use it @settings[:config] = myfile File.expects(:read).with(myfile).returns "[main]" @@ -445,25 +447,27 @@ describe Puppet::Util::Settings do it "should support specifying all metadata (owner, group, mode) in the configuration file" do @settings.setdefaults :section, :myfile => ["/myfile", "a"] + otherfile = make_absolute("/other/file") text = "[main] - myfile = /other/file {owner = service, group = service, mode = 644} + myfile = #{otherfile} {owner = service, group = service, mode = 644} " @settings.expects(:read_file).returns(text) @settings.parse - @settings[:myfile].should == "/other/file" + @settings[:myfile].should == otherfile @settings.metadata(:myfile).should == {:owner => "suser", :group => "sgroup", :mode => "644"} end it "should support specifying a single piece of metadata (owner, group, or mode) in the configuration file" do @settings.setdefaults :section, :myfile => ["/myfile", "a"] + otherfile = make_absolute("/other/file") text = "[main] - myfile = /other/file {owner = service} + myfile = #{otherfile} {owner = service} " file = "/some/file" @settings.expects(:read_file).returns(text) @settings.parse - @settings[:myfile].should == "/other/file" + @settings[:myfile].should == otherfile @settings.metadata(:myfile).should == {:owner => "suser"} end diff --git a/spec/unit/util/storage_spec.rb b/spec/unit/util/storage_spec.rb index 90c11aa69..575ad1ef3 100755 --- a/spec/unit/util/storage_spec.rb +++ b/spec/unit/util/storage_spec.rb @@ -8,7 +8,7 @@ describe Puppet::Util::Storage do include PuppetSpec::Files before(:all) do - @basepath = Puppet.features.posix? ? "/somepath" : "C:/somepath" + @basepath = make_absolute("/somepath") Puppet[:statedir] = tmpdir("statedir") end |
