Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket/2.7.x/12101 reapply autoload #611

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/puppet/util.rb
Expand Up @@ -196,8 +196,13 @@ def absolute_path?(path, platform=nil)
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
:posix => %r!^/!,
}
require 'puppet'
platform ||= Puppet.features.microsoft_windows? ? :windows : :posix

# Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard
# library uses that to test what platform it's on. Normally in Puppet we
# would use Puppet.features.microsoft_windows?, but this method needs to
# be called during the initialization of features so it can't depend on
# that.
platform ||= File::ALT_SEPARATOR ? :windows : :posix

!! (path =~ regexes[platform])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/util/autoload.rb
Expand Up @@ -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
Expand Down
31 changes: 2 additions & 29 deletions spec/shared_behaviours/path_parameters.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/shared_contexts/platform.rb
@@ -0,0 +1,43 @@
# Contexts for stubbing platforms
# In a describe or context block, adding :as_platform => :windows or
# :as_platform => :posix will stub the relevant Puppet features, as well as
# the behavior of Ruby's filesystem methods by changing File::ALT_SEPARATOR.

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
# prevent Ruby from warning about changing a constant
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
# prevent Ruby from warning about changing a constant
with_verbose_disabled do
File::ALT_SEPARATOR = nil
end
example.run
with_verbose_disabled do
File::ALT_SEPARATOR = file_alt_separator
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -25,6 +25,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
Expand Down
12 changes: 2 additions & 10 deletions spec/unit/parser/functions/generate_spec.rb
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions spec/unit/provider/exec/windows_spec.rb
Expand Up @@ -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.
Expand Down
23 changes: 8 additions & 15 deletions spec/unit/type/exec_spec.rb
Expand Up @@ -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') }.
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions spec/unit/util/log/destinations_spec.rb
Expand Up @@ -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' }

Expand Down
22 changes: 11 additions & 11 deletions spec/unit/util_spec.rb
Expand Up @@ -30,18 +30,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
Expand Down