[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Pieter van de Bruggen pieter at puppetlabs.com
Tue May 10 08:08:27 UTC 2011


The following commit has been merged in the experimental branch:
commit 6aea116701b8e03558ef7a5a15766b267af14281
Author: Pieter van de Bruggen <pieter at puppetlabs.com>
Date:   Thu Mar 24 18:55:32 2011 -0700

    (#6770) Add support for version :latest.
    
    Specifying a version of `:latest` will find the most recent version of the
    named interface installed in your RUBYLIB, and attempt to load that.  This is
    unlikely to provide a stable dependency in the future, so should be used
    sparingly, acknowledging the dangers.
    
    Reviewed-By: Daniel Pittman

diff --git a/lib/puppet/application/interface_base.rb b/lib/puppet/application/interface_base.rb
index c1c0204..841f3ca 100644
--- a/lib/puppet/application/interface_base.rb
+++ b/lib/puppet/application/interface_base.rb
@@ -72,10 +72,10 @@ class Puppet::Application::InterfaceBase < Puppet::Application
     @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
 
     # TODO: These should be configurable versions.
-    unless Puppet::Interface.interface?(@type, '0.0.1')
-      raise "Could not find version #{1} of interface '#{@type}'"
+    unless Puppet::Interface.interface?(@type, :latest)
+      raise "Could not find any version of interface '#{@type}'"
     end
-    @interface = Puppet::Interface[@type, '0.0.1']
+    @interface = Puppet::Interface[@type, :latest]
     @format ||= @interface.default_format
 
     # We copy all of the app options to the interface.
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
index 27cbb75..a667c6b 100644
--- a/lib/puppet/interface.rb
+++ b/lib/puppet/interface.rb
@@ -57,6 +57,10 @@ class Puppet::Interface
   attr_reader :name
 
   def initialize(name, version, &block)
+    unless Puppet::Interface::InterfaceCollection.validate_version(version)
+      raise ArgumentError, "Cannot create interface with invalid version number '#{version}'!"
+    end
+
     @name = Puppet::Interface::InterfaceCollection.underscorize(name)
     @version = version
     @default_format = :pson
diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb
index 1158923..92e2933 100644
--- a/lib/puppet/interface/interface_collection.rb
+++ b/lib/puppet/interface/interface_collection.rb
@@ -33,7 +33,7 @@ module Puppet::Interface::InterfaceCollection
       v_dir = File.join dir, %w[puppet interface v*]
       Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f|
         v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1')
-        if v =~ SEMVER_VERSION
+        if validate_version(v)
           versions << v
         else
           warn "'#{v}' (#{f}) is not a valid version string; skipping"
@@ -43,6 +43,10 @@ module Puppet::Interface::InterfaceCollection
     return versions.uniq.sort { |a, b| compare_versions(a, b)  }
   end
 
+  def self.validate_version(version)
+    !!(SEMVER_VERSION =~ version.to_s)
+  end
+
   def self.compare_versions(a, b)
     a, b = [a, b].map do |x|
       parts = SEMVER_VERSION.match(x).to_a[1..4]
@@ -60,11 +64,18 @@ module Puppet::Interface::InterfaceCollection
   end
 
   def self.[](name, version)
-    @interfaces[underscorize(name)][version] if interface?(name, version)
+    version = versions(name).last if version == :latest
+    unless version.nil?
+      @interfaces[underscorize(name)][version] if interface?(name, version)
+    end
   end
 
   def self.interface?(name, version)
+    version = versions(name).last if version == :latest
+    return false if version.nil?
+
     name = underscorize(name)
+
     unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version)
       require "puppet/interface/v#{version}/#{name}"
     end
diff --git a/spec/unit/application/interface_base_spec.rb b/spec/unit/application/interface_base_spec.rb
index 15d4655..d82325b 100644
--- a/spec/unit/application/interface_base_spec.rb
+++ b/spec/unit/application/interface_base_spec.rb
@@ -4,7 +4,19 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
 require 'puppet/application/interface_base'
 
 describe Puppet::Application::InterfaceBase do
-  base_interface = Puppet::Interface[:basetest, '0.0.1']
+  before :all do
+    @dir = Dir.mktmpdir
+    $LOAD_PATH.push(@dir)
+    FileUtils.mkdir_p(File.join @dir, 'puppet', 'interface', 'v0.0.1')
+    FileUtils.touch(File.join @dir, 'puppet', 'interface', 'v0.0.1', 'basetest.rb')
+  end
+
+  after :all do
+    FileUtils.remove_entry_secure @dir
+    $LOAD_PATH.pop
+  end
+
+  base_interface = Puppet::Interface.define(:basetest, '0.0.1')
   class Puppet::Application::InterfaceBase::Basetest < Puppet::Application::InterfaceBase
   end
 
diff --git a/spec/unit/interface/interface_collection_spec.rb b/spec/unit/interface/interface_collection_spec.rb
index a943c2e..3e4b9d6 100644
--- a/spec/unit/interface/interface_collection_spec.rb
+++ b/spec/unit/interface/interface_collection_spec.rb
@@ -63,6 +63,32 @@ describe Puppet::Interface::InterfaceCollection do
     end
   end
 
+  describe "::validate_version" do
+    it 'should permit three number versions' do
+      subject.validate_version('10.10.10').should == true
+    end
+
+    it 'should permit versions with appended descriptions' do
+      subject.validate_version('10.10.10beta').should == true
+    end
+
+    it 'should not permit versions with more than three numbers' do
+      subject.validate_version('1.2.3.4').should == false
+    end
+
+    it 'should not permit versions with only two numbers' do
+      subject.validate_version('10.10').should == false
+    end
+
+    it 'should not permit versions with only one number' do
+      subject.validate_version('123').should == false
+    end
+
+    it 'should not permit versions with text in any position but at the end' do
+      subject.validate_version('v1.1.1').should == false
+    end
+  end
+
   describe "::compare_versions" do
     # (a <=> b) should be:
     #   -1 if a < b
@@ -125,6 +151,17 @@ describe Puppet::Interface::InterfaceCollection do
       subject.instance_variable_get("@interfaces")[:foo]['0.0.1'] = 10
     end
 
+    before :each do
+      @dir = Dir.mktmpdir
+      @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'interface')
+      $LOAD_PATH.push(@dir)
+    end
+
+    after :each do
+      FileUtils.remove_entry_secure @dir
+      $LOAD_PATH.pop
+    end
+
     it "should return the interface with the given name" do
       subject["foo", '0.0.1'].should == 10
     end
@@ -133,6 +170,15 @@ describe Puppet::Interface::InterfaceCollection do
       subject.expects(:require).with('puppet/interface/v0.0.1/bar')
       subject["bar", '0.0.1']
     end
+
+    it "should attempt to load the interface with the greatest version for specified version :latest" do
+      %w[ 1.2.1 1.2.2 ].each do |version|
+        FileUtils.mkdir_p(File.join @lib, "v#{version}")
+        FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb')
+      end
+      subject.expects(:require).with('puppet/interface/v1.2.2/fozzie')
+      subject['fozzie', :latest]
+    end
   end
 
   describe "::interface?" do
diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb
index 060a71f..cf7d209 100755
--- a/spec/unit/interface_spec.rb
+++ b/spec/unit/interface_spec.rb
@@ -36,6 +36,10 @@ describe Puppet::Interface do
       proc { Puppet::Interface.new(:no_version) }.should raise_error(ArgumentError)
     end
 
+    it "should require a valid version number" do
+      proc { Puppet::Interface.new(:bad_version, 'Rasins') }.should raise_error(ArgumentError)
+    end
+
     it "should instance-eval any provided block" do
       face = Puppet::Interface.new(:interface_test_block,'0.0.1') do
         action(:something) do

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list