diff --git a/lib/puppet/file_serving/file_base.rb b/lib/puppet/file_serving/file_base.rb index 06b3ad9ef..e87d683aa 100644 --- a/lib/puppet/file_serving/file_base.rb +++ b/lib/puppet/file_serving/file_base.rb @@ -1,75 +1,76 @@ # # Created by Luke Kanies on 2007-10-22. # Copyright (c) 2007. All rights reserved. require 'puppet/file_serving' # The base class for Content and Metadata; provides common # functionality like the behaviour around links. class Puppet::FileServing::FileBase attr_accessor :key # Does our file exist? def exist? begin stat return true rescue => detail return false end end # Return the full path to our file. Fails if there's no path set. def full_path raise(ArgumentError, "You must set a path to get a file's path") unless self.path if relative_path.nil? or relative_path == "" path else File.join(path, relative_path) end end def initialize(key, options = {}) @key = key @links = :manage options.each do |param, value| begin send param.to_s + "=", value rescue NoMethodError raise ArgumentError, "Invalid option %s for %s" % [param, self.class] end end end # Determine how we deal with links. attr_reader :links def links=(value) + value = :manage if value == :ignore raise(ArgumentError, ":links can only be set to :manage or :follow") unless [:manage, :follow].include?(value) @links = value end # Set our base path. attr_reader :path def path=(path) raise ArgumentError.new("Paths must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/ @path = path end # Set a relative path; this is used for recursion, and sets # the file's path relative to the initial recursion point. attr_reader :relative_path def relative_path=(path) raise ArgumentError.new("Relative paths must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/ @relative_path = path end # Stat our file, using the appropriate link-sensitive method. def stat unless defined?(@stat_method) @stat_method = self.links == :manage ? :lstat : :stat end File.send(@stat_method, full_path()) end end diff --git a/spec/unit/file_serving/file_base.rb b/spec/unit/file_serving/file_base.rb index e1a61cd65..ded6ae4a8 100755 --- a/spec/unit/file_serving/file_base.rb +++ b/spec/unit/file_serving/file_base.rb @@ -1,120 +1,124 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/file_base' describe Puppet::FileServing::FileBase do it "should accept a key in the form of a URI" do Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").key.should == "puppet://host/module/dir/file" end it "should allow specification of whether links should be managed" do Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :manage).links.should == :manage end - it "should fail if :links is set to anything other than :manage or :follow" do + it "should consider :ignore links equivalent to :manage links" do + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :ignore).links.should == :manage + end + + it "should fail if :links is set to anything other than :manage, :follow, or :ignore" do proc { Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :else) }.should raise_error(ArgumentError) end it "should default to :manage for :links" do Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").links.should == :manage end it "should allow specification of a path" do FileTest.stubs(:exists?).returns(true) Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :path => "/my/file").path.should == "/my/file" end it "should allow specification of a relative path" do FileTest.stubs(:exists?).returns(true) Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :relative_path => "my/file").relative_path.should == "my/file" end it "should have a means of determining if the file exists" do Puppet::FileServing::FileBase.new("blah").should respond_to(:exist?) end it "should correctly indicate if the file is present" do File.expects(:lstat).with("/my/file").returns(mock("stat")) Puppet::FileServing::FileBase.new("blah", :path => "/my/file").exist?.should be_true end it "should correctly indicate if the file is asbsent" do File.expects(:lstat).with("/my/file").raises RuntimeError Puppet::FileServing::FileBase.new("blah", :path => "/my/file").exist?.should be_false end describe "when setting the base path" do before do @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") end it "should require that the base path be fully qualified" do FileTest.stubs(:exists?).returns(true) proc { @file.path = "unqualified/file" }.should raise_error(ArgumentError) end end describe "when setting the relative path" do it "should require that the relative path be unqualified" do @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") FileTest.stubs(:exists?).returns(true) proc { @file.relative_path = "/qualified/file" }.should raise_error(ArgumentError) end end describe "when determining the full file path" do before do @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end it "should return the path if there is no relative path" do @file.full_path.should == "/this/file" end it "should return the path if the relative_path is set to ''" do @file.relative_path = "" @file.full_path.should == "/this/file" end it "should return the path joined with the relative path if there is a relative path and it is not set to '/' or ''" do @file.relative_path = "not/qualified" @file.full_path.should == "/this/file/not/qualified" end it "should should fail if there is no path set" do @file = Puppet::FileServing::FileBase.new("not/qualified") proc { @file.full_path }.should raise_error(ArgumentError) end end describe "when stat'ing files" do before do @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end it "should stat the file's full path" do @file.stubs(:full_path).returns("/this/file") File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") @file.stat end it "should fail if the file does not exist" do @file.stubs(:full_path).returns("/this/file") File.expects(:lstat).with("/this/file").raises(Errno::ENOENT) proc { @file.stat }.should raise_error(Errno::ENOENT) end it "should use :lstat if :links is set to :manage" do File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") @file.stat end it "should use :stat if :links is set to :follow" do File.expects(:stat).with("/this/file").returns stub("stat", :ftype => "file") @file.links = :follow @file.stat end end end