Please review pull request #400: (#12101) Make Puppet::Util::absolute_path? usable in autoloader opened by (pcarlisle)

Description:

This makes Puppet::Util::absolute_path? check File::ALT_SEPARATOR to determine
path behavior of the local system instead of Puppet.features, which required
having an Autoload instance already.

Add shared context for specs to imitate windows or posix as prerequisite for this change.

  • Opened: Tue Jan 24 19:15:02 UTC 2012
  • Based on: puppetlabs:2.7.x (ce443216b90f9488ab16449682a8b843fd94cbe7)
  • Requested merge: pcarlisle:ticket/2.7.x/12101-autoload-path (791f94fa4bc4a26dbc7764cd7f85643274a3c480)

Diff follows:

diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb
index 5203fb0..d870465 100644
--- a/lib/puppet/util.rb
+++ b/lib/puppet/util.rb
@@ -196,7 +196,7 @@ def absolute_path?(path, platform=nil)
       :posix   => %r!^/!,
     }
     require 'puppet'
-    platform ||= Puppet.features.microsoft_windows? ? :windows : :posix
+    platform ||= File::ALT_SEPARATOR ? :windows : :posix
 
     !! (path =~ regexes[platform])
   end
diff --git a/lib/puppet/util/autoload.rb b/lib/puppet/util/autoload.rb
index 2e8710a..8051d01 100644
--- a/lib/puppet/util/autoload.rb
+++ b/lib/puppet/util/autoload.rb
@@ -48,7 +48,7 @@ def self.loaded(file)
 
   def initialize(obj, path, options = {})
     @path = path.to_s
-    raise ArgumentError, "Autoload paths cannot be fully qualified" if @path !~ /^\w/
+    raise ArgumentError, "Autoload paths cannot be fully qualified" if absolute_path?(@path)
     @object = obj
 
     self.class[obj] = self
diff --git a/spec/shared_behaviours/path_parameters.rb b/spec/shared_behaviours/path_parameters.rb
index bdcd4cf..2d195a1 100755
--- a/spec/shared_behaviours/path_parameters.rb
+++ b/spec/shared_behaviours/path_parameters.rb
@@ -87,26 +87,7 @@ def instance(path)
     @param = param
   end
 
-  before :each do
-    @file_separator = File::SEPARATOR
-  end
-  after :each do
-    with_verbose_disabled do
-      verbose, $VERBOSE = $VERBOSE, nil
-      File::SEPARATOR = @file_separator
-      $VERBOSE = verbose
-    end
-  end
-
-  describe "on a Unix-like platform it" do
-    before :each do
-      with_verbose_disabled do
-        File::SEPARATOR = '/'
-      end
-      Puppet.features.stubs(:microsoft_windows?).returns(false)
-      Puppet.features.stubs(:posix?).returns(true)
-    end
-
+  describe "on a Unix-like platform it", :as_platform => :posix do
     if array then
       it_should_behave_like "all pathname parameters with arrays", false
     end
@@ -134,15 +115,7 @@ def instance(path)
     end
   end
 
-  describe "on a Windows-like platform it" do
-    before :each do
-      with_verbose_disabled do
-        File::SEPARATOR = '\\'
-      end
-      Puppet.features.stubs(:microsoft_windows?).returns(true)
-      Puppet.features.stubs(:posix?).returns(false)
-    end
-
+  describe "on a Windows-like platform it", :as_platform => :windows do
     if array then
       it_should_behave_like "all pathname parameters with arrays", true
     end
diff --git a/spec/shared_contexts/platform.rb b/spec/shared_contexts/platform.rb
new file mode 100644
index 0000000..bb5a36e
--- /dev/null
+++ b/spec/shared_contexts/platform.rb
@@ -0,0 +1,38 @@
+# Contexts for stubbing platforms
+
+shared_context "windows", :as_platform => :windows do
+  before :each do
+    Facter.stubs(:value).with(:operatingsystem).returns 'Windows'
+    Puppet.features.stubs(:microsoft_windows?).returns(true)
+    Puppet.features.stubs(:posix?).returns(false)
+  end
+
+  around do |example|
+    file_alt_separator = File::ALT_SEPARATOR
+    with_verbose_disabled do
+      File::ALT_SEPARATOR = '\\'
+    end
+    example.run
+    with_verbose_disabled do
+      File::ALT_SEPARATOR = file_alt_separator
+    end
+  end
+end
+
+shared_context "posix", :as_platform => :posix do
+  before :each do
+    Puppet.features.stubs(:microsoft_windows?).returns(false)
+    Puppet.features.stubs(:posix?).returns(true)
+  end
+
+  around do |example|
+    file_alt_separator = File::ALT_SEPARATOR
+    with_verbose_disabled do
+      File::ALT_SEPARATOR = nil
+    end
+    example.run
+    with_verbose_disabled do
+      File::ALT_SEPARATOR = file_alt_separator
+    end
+  end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index e0227be..fd208e9 100755
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -24,6 +24,10 @@ module PuppetSpec
 require 'monkey_patches/alias_should_to_must'
 require 'monkey_patches/publicize_methods'
 
+Pathname.glob("#{dir}/shared_contexts/*.rb") do |file|
+  require file.relative_path_from(Pathname.new(dir))
+end
+
 Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour|
   require behaviour.relative_path_from(Pathname.new(dir))
 end
diff --git a/spec/unit/parser/functions/generate_spec.rb b/spec/unit/parser/functions/generate_spec.rb
index e508053..8b9d851 100755
--- a/spec/unit/parser/functions/generate_spec.rb
+++ b/spec/unit/parser/functions/generate_spec.rb
@@ -41,11 +41,7 @@
     scope.function_generate([command]).should == 'yay'
   end
 
-  describe "on Windows" do
-    before :each do
-      Puppet.features.stubs(:microsoft_windows?).returns(true)
-    end
-
+  describe "on Windows", :as_platform => :windows do
     it "should accept lower-case drive letters" do
       command = 'd:/command/foo'
       Dir.expects(:chdir).with(File.dirname(command)).returns("yay")
@@ -73,11 +69,7 @@
     end
   end
 
-  describe "on non-Windows" do
-    before :each do
-      Puppet.features.stubs(:microsoft_windows?).returns(false)
-    end
-
+  describe "on non-Windows", :as_platform => :posix do
     it "should reject backslashes" do
       lambda { scope.function_generate(['/com\\mand']) }.should raise_error(Puppet::ParseError)
     end
diff --git a/spec/unit/provider/exec/windows_spec.rb b/spec/unit/provider/exec/windows_spec.rb
index 748646a..5e1b5c9 100755
--- a/spec/unit/provider/exec/windows_spec.rb
+++ b/spec/unit/provider/exec/windows_spec.rb
@@ -2,18 +2,12 @@
 
 require 'spec_helper'
 
-describe Puppet::Type.type(:exec).provider(:windows) do
+describe Puppet::Type.type(:exec).provider(:windows), :as_platform => :windows do
   include PuppetSpec::Files
 
   let(:resource) { Puppet::Type.type(:exec).new(:title => 'C:\foo', :provider => :windows) }
   let(:provider) { described_class.new(resource) }
 
-  before :each do
-    Facter.stubs(:value).with(:operatingsystem).returns 'Windows'
-    Puppet.features.stubs(:microsoft_windows?).returns(true)
-    Puppet.features.stubs(:posix?).returns(false)
-  end
-
   after :all do
     # This provider may not be suitable on some machines, so we want to reset
     # the default so it isn't used by mistake in future specs.
diff --git a/spec/unit/type/exec_spec.rb b/spec/unit/type/exec_spec.rb
index 5a4c392..3bb607b 100755
--- a/spec/unit/type/exec_spec.rb
+++ b/spec/unit/type/exec_spec.rb
@@ -187,12 +187,7 @@ def exec_tester(command, exitstatus = 0, rest = {})
   end
 
   describe "when setting user" do
-    describe "on POSIX systems" do
-      before :each do
-        Puppet.features.stubs(:posix?).returns(true)
-        Puppet.features.stubs(:microsoft_windows?).returns(false)
-      end
-
+    describe "on POSIX systems", :as_platform => :posix do
       it "should fail if we are not root" do
         Puppet.features.stubs(:root?).returns(false)
         expect { Puppet::Type.type(:exec).new(:name => '/bin/true whatever', :user => 'input') }.
@@ -208,10 +203,8 @@ def exec_tester(command, exitstatus = 0, rest = {})
       end
     end
 
-    describe "on Windows systems" do
+    describe "on Windows systems", :as_platform => :windows do
       before :each do
-        Puppet.features.stubs(:posix?).returns(false)
-        Puppet.features.stubs(:microsoft_windows?).returns(true)
         Puppet.features.stubs(:root?).returns(true)
       end
 
@@ -489,13 +482,13 @@ def test(command, valid)
         end
       end
     end
+  end
 
-    describe "when setting creates" do
-      it_should_behave_like "all path parameters", :creates, :array => true do
-        def instance(path)
-          # Specify shell provider so we don't have to care about command validation
-          Puppet::Type.type(:exec).new(:name => @executable, :creates => path, :provider => :shell)
-        end
+  describe "when setting creates" do
+    it_should_behave_like "all path parameters", :creates, :array => true do
+      def instance(path)
+        # Specify shell provider so we don't have to care about command validation
+        Puppet::Type.type(:exec).new(:name => @executable, :creates => path, :provider => :shell)
       end
     end
   end
diff --git a/spec/unit/util/log/destinations_spec.rb b/spec/unit/util/log/destinations_spec.rb
index 3cb8e24..da6494e 100755
--- a/spec/unit/util/log/destinations_spec.rb
+++ b/spec/unit/util/log/destinations_spec.rb
@@ -44,18 +44,14 @@
       end
     end
 
-    describe "on POSIX systems" do
-      before :each do Puppet.features.stubs(:microsoft_windows?).returns false end
-
+    describe "on POSIX systems", :as_platform => :posix do
       let (:abspath) { '/tmp/log' }
       let (:relpath) { 'log' }
 
       it_behaves_like "file destination"
     end
 
-    describe "on Windows systems" do
-      before :each do Puppet.features.stubs(:microsoft_windows?).returns true end
-
+    describe "on Windows systems", :as_platform => :windows do
       let (:abspath) { 'C:\\temp\\log.txt' }
       let (:relpath) { 'log.txt' }
 
diff --git a/spec/unit/util_spec.rb b/spec/unit/util_spec.rb
index 35358d6..77dc313 100755
--- a/spec/unit/util_spec.rb
+++ b/spec/unit/util_spec.rb
@@ -12,18 +12,18 @@ def process_status(exitstatus)
   end
 
   describe "#absolute_path?" do
-    it "should default to the platform of the local system" do
-      Puppet.features.stubs(:posix?).returns(true)
-      Puppet.features.stubs(:microsoft_windows?).returns(false)
-
-      Puppet::Util.should be_absolute_path('/foo')
-      Puppet::Util.should_not be_absolute_path('C:/foo')
-
-      Puppet.features.stubs(:posix?).returns(false)
-      Puppet.features.stubs(:microsoft_windows?).returns(true)
+    describe "on posix systems", :as_platform => :posix do
+      it "should default to the platform of the local system" do
+        Puppet::Util.should be_absolute_path('/foo')
+        Puppet::Util.should_not be_absolute_path('C:/foo')
+      end
+    end
 
-      Puppet::Util.should be_absolute_path('C:/foo')
-      Puppet::Util.should_not be_absolute_path('/foo')
+    describe "on windows", :as_platform => :windows do
+      it "should default to the platform of the local system" do
+        Puppet::Util.should be_absolute_path('C:/foo')
+        Puppet::Util.should_not be_absolute_path('/foo')
+      end
     end
 
     describe "when using platform :posix" do

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to