spec/unit/util/loader_spec.rb in facter-1.6.18 vs spec/unit/util/loader_spec.rb in facter-1.7.0.rc1

- old
+ new

@@ -19,28 +19,10 @@ describe Facter::Util::Loader do before :each do Facter::Util::Loader.any_instance.unstub(:load_all) end - def with_env(values) - old = {} - values.each do |var, value| - if old_val = ENV[var] - old[var] = old_val - end - ENV[var] = value - end - yield - values.each do |var, value| - if old.include?(var) - ENV[var] = old[var] - else - ENV.delete(var) - end - end - end - it "should have a method for loading individual facts by name" do Facter::Util::Loader.new.should respond_to(:load) end it "should have a method for loading all facts" do @@ -49,36 +31,111 @@ it "should have a method for returning directories containing facts" do Facter::Util::Loader.new.should respond_to(:search_path) end + describe "#valid_seach_path?" do + before :each do + @loader = Facter::Util::Loader.new + @settings = mock 'settings' + @settings.stubs(:value).returns "/eh" + end + + it "should cache the result of a previous check" do + Pathname.any_instance.expects(:absolute?).returns(true).once + + # we explicitly want two calls here to check that we get + # the second from the cache + @loader.should be_valid_search_path "/foo" + @loader.should be_valid_search_path "/foo" + end + + # Used to have test for " " as a directory since that should + # be a relative directory, but on Windows in both 1.8.7 and + # 1.9.3 it is an absolute directory (WTF Windows). Considering + # we didn't have a valid use case for a " " directory, the + # test was removed. + + [ + '.', + '..', + '...', + '.foo', + '../foo', + 'foo', + 'foo/bar', + 'foo/../bar', + ' /', + ' \/', + ].each do |dir| + it "should be false for relative path #{dir}" do + @loader.should_not be_valid_search_path dir + end + end + [ + '/.', + '/..', + '/...', + '/.foo', + '/../foo', + '/foo', + '/foo/bar', + '/foo/../bar', + '/ ', + '/ /..', + ].each do |dir| + it "should be true for absolute path #{dir}" do + @loader.should be_valid_search_path dir + end + end + end + describe "when determining the search path" do before do @loader = Facter::Util::Loader.new @settings = mock 'settings' @settings.stubs(:value).returns "/eh" end it "should include the facter subdirectory of all paths in ruby LOAD_PATH" do dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") } + @loader.stubs(:valid_search_path?).returns(true) paths = @loader.search_path dirs.each do |dir| paths.should be_include(dir) end end + it "should warn the user when an invalid search path has been excluded" do + dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") } + @loader.stubs(:valid_search_path?).returns(false) + dirs.each do |dir| + Facter.expects(:debugonce).with("Relative directory #{dir} removed from search path.").once + end + paths = @loader.search_path + end + + it "should exclude invalid search paths" do + dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") } + @loader.stubs(:valid_search_path?).returns(false) + paths = @loader.search_path + dirs.each do |dir| + paths.should_not be_include(dir) + end + end + it "should include all search paths registered with Facter" do Facter.expects(:search_path).returns %w{/one /two} paths = @loader.search_path paths.should be_include("/one") paths.should be_include("/two") end describe "and the FACTERLIB environment variable is set" do it "should include all paths in FACTERLIB" do - with_env "FACTERLIB" => "/one/path:/two/path" do + Facter::Util::Resolution.with_env "FACTERLIB" => "/one/path#{File::PATH_SEPARATOR}/two/path" do paths = @loader.search_path %w{/one/path /two/path}.each do |dir| paths.should be_include(dir) end end @@ -93,11 +150,11 @@ end it "should load values from the matching environment variable if one is present" do Facter.expects(:add).with("testing") - with_env "facter_testing" => "yayness" do + Facter::Util::Resolution.with_env "facter_testing" => "yayness" do @loader.load(:testing) end end it "should load any files in the search path with names matching the fact name" do @@ -111,25 +168,25 @@ @loader.load(:testing) end it 'should load any ruby files in directories matching the fact name in the search path in sorted order regardless of the order returned by Dir.entries' do - @loader = TestLoader.new + @loader = TestLoader.new - @loader.stubs(:search_path).returns %w{/one/dir} - FileTest.stubs(:exist?).returns false - FileTest.stubs(:directory?).with("/one/dir/testing").returns true - @loader.stubs(:search_path).returns %w{/one/dir} + @loader.stubs(:search_path).returns %w{/one/dir} + FileTest.stubs(:exist?).returns false + FileTest.stubs(:directory?).with("/one/dir/testing").returns true + @loader.stubs(:search_path).returns %w{/one/dir} - Dir.stubs(:entries).with("/one/dir/testing").returns %w{foo.rb bar.rb} - %w{/one/dir/testing/foo.rb /one/dir/testing/bar.rb}.each do |f| - File.stubs(:directory?).with(f).returns false - Kernel.stubs(:load).with(f) - end + Dir.stubs(:entries).with("/one/dir/testing").returns %w{foo.rb bar.rb} + %w{/one/dir/testing/foo.rb /one/dir/testing/bar.rb}.each do |f| + File.stubs(:directory?).with(f).returns false + Kernel.stubs(:load).with(f) + end - @loader.load(:testing) - @loader.loaded_files.should == %w{/one/dir/testing/bar.rb /one/dir/testing/foo.rb} + @loader.load(:testing) + @loader.loaded_files.should == %w{/one/dir/testing/bar.rb /one/dir/testing/foo.rb} end it "should load any ruby files in directories matching the fact name in the search path" do @loader.expects(:search_path).returns %w{/one/dir} FileTest.stubs(:exist?).returns false @@ -154,11 +211,11 @@ @loader.load(:testing) end end describe "when loading all facts" do - before do + before :each do @loader = Facter::Util::Loader.new @loader.stubs(:search_path).returns [] FileTest.stubs(:directory?).returns true end @@ -199,25 +256,25 @@ @loader.load_all end it 'should load all files in sorted order for any given directory regardless of the order returned by Dir.entries' do - @loader = TestLoader.new + @loader = TestLoader.new - @loader.stubs(:search_path).returns %w{/one/dir} - Dir.stubs(:entries).with("/one/dir").returns %w{foo.rb bar.rb} + @loader.stubs(:search_path).returns %w{/one/dir} + Dir.stubs(:entries).with("/one/dir").returns %w{foo.rb bar.rb} - %w{/one/dir}.each { |f| File.stubs(:directory?).with(f).returns true } + %w{/one/dir}.each { |f| File.stubs(:directory?).with(f).returns true } - %w{/one/dir/foo.rb /one/dir/bar.rb}.each do |f| - File.stubs(:directory?).with(f).returns false - Kernel.expects(:load).with(f) - end + %w{/one/dir/foo.rb /one/dir/bar.rb}.each do |f| + File.stubs(:directory?).with(f).returns false + Kernel.expects(:load).with(f) + end - @loader.load_all + @loader.load_all - @loader.loaded_files.should == %w{/one/dir/bar.rb /one/dir/foo.rb} + @loader.loaded_files.should == %w{/one/dir/bar.rb /one/dir/foo.rb} end it "should not load files in the util subdirectory" do @loader.expects(:search_path).returns %w{/one/dir} @@ -265,11 +322,11 @@ it "should load all facts from the environment" do Facter.expects(:add).with('one') Facter.expects(:add).with('two') - with_env "facter_one" => "yayness", "facter_two" => "boo" do + Facter::Util::Resolution.with_env "facter_one" => "yayness", "facter_two" => "boo" do @loader.load_all end end it "should only load all facts one time" do @@ -279,10 +336,10 @@ end end it "should load facts on the facter search path only once" do facterlibdir = File.expand_path(File.dirname(__FILE__) + '../../../fixtures/unit/util/loader') - with_env 'FACTERLIB' => facterlibdir do + Facter::Util::Resolution.with_env 'FACTERLIB' => facterlibdir do Facter::Util::Loader.new.load_all Facter.value(:nosuchfact).should be_nil end end end