[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:21 UTC 2011


The following commit has been merged in the experimental branch:
commit 1af9bb232ed73f16789f465e89a0d498c39e1b78
Author: Pieter van de Bruggen <pieter at puppetlabs.com>
Date:   Thu Mar 24 15:06:21 2011 -0700

    (#6770) Add version lookup and comparison.
    
    Reviewed-By: Jacob Helwig

diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb
index 51b7534..1158923 100644
--- a/lib/puppet/interface/interface_collection.rb
+++ b/lib/puppet/interface/interface_collection.rb
@@ -1,6 +1,8 @@
 require 'puppet/interface'
 
 module Puppet::Interface::InterfaceCollection
+  SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/
+
   @interfaces = Hash.new { |hash, key| hash[key] = {} }
 
   def self.interfaces
@@ -24,6 +26,39 @@ module Puppet::Interface::InterfaceCollection
     return @interfaces.keys
   end
 
+  def self.versions(name)
+    versions = []
+    $LOAD_PATH.each do |dir|
+      next unless FileTest.directory?(dir)
+      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
+          versions << v
+        else
+          warn "'#{v}' (#{f}) is not a valid version string; skipping"
+        end
+      end
+    end
+    return versions.uniq.sort { |a, b| compare_versions(a, b)  }
+  end
+
+  def self.compare_versions(a, b)
+    a, b = [a, b].map do |x|
+      parts = SEMVER_VERSION.match(x).to_a[1..4]
+      parts[0..2] = parts[0..2].map { |e| e.to_i }
+      parts
+    end
+
+    cmp = a[0..2] <=> b[0..2]
+    if cmp == 0
+      cmp = a[3] <=> b[3]
+      cmp = +1 if a[3].empty? && !b[3].empty?
+      cmp = -1 if b[3].empty? && !a[3].empty?
+    end
+    cmp
+  end
+
   def self.[](name, version)
     @interfaces[underscorize(name)][version] if interface?(name, version)
   end
diff --git a/spec/unit/interface/interface_collection_spec.rb b/spec/unit/interface/interface_collection_spec.rb
index a404d85..a943c2e 100644
--- a/spec/unit/interface/interface_collection_spec.rb
+++ b/spec/unit/interface/interface_collection_spec.rb
@@ -1,6 +1,7 @@
 #!/usr/bin/env ruby
 
 require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'tmpdir'
 
 describe Puppet::Interface::InterfaceCollection do
   before :all do
@@ -18,6 +19,107 @@ describe Puppet::Interface::InterfaceCollection do
   describe "::interfaces" do
   end
 
+  describe "::versions" do
+    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 an empty array when no versions are loadable" do
+      subject.versions(:fozzie).should == []
+    end
+
+    it "should return versions loadable as puppet/interface/v{version}/{name}" do
+      FileUtils.mkdir_p(File.join @lib, 'v1.0.0')
+      FileUtils.touch(File.join @lib, 'v1.0.0', 'fozzie.rb')
+      subject.versions(:fozzie).should == ['1.0.0']
+    end
+
+    it "should an ordered list of all versions loadable as puppet/interface/v{version}/{name}" do
+      %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 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.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ]
+    end
+
+    it "should not return a version for an empty puppet/interface/v{version}/{name}" do
+      FileUtils.mkdir_p(File.join @lib, 'v1.0.0', 'fozzie')
+      subject.versions(:fozzie).should == []
+    end
+
+    it "should an ordered list of all versions loadable as puppet/interface/v{version}/{name}/*.rb" do
+      %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version|
+        FileUtils.mkdir_p(File.join @lib, "v#{version}", "fozzie")
+        FileUtils.touch(File.join @lib, "v#{version}", 'fozzie', 'action.rb')
+      end
+      subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ]
+    end
+  end
+
+  describe "::compare_versions" do
+    # (a <=> b) should be:
+    #   -1 if a < b
+    #   0  if a == b
+    #   1  if a > b
+    it 'should sort major version numbers numerically' do
+      subject.compare_versions('1.0.0', '2.0.0').should == -1
+      subject.compare_versions('2.0.0', '1.1.1').should == 1
+      subject.compare_versions('2.0.0', '10.0.0').should == -1
+    end
+
+    it 'should sort minor version numbers numerically' do
+      subject.compare_versions('0.1.0', '0.2.0').should == -1
+      subject.compare_versions('0.2.0', '0.1.1').should == 1
+      subject.compare_versions('0.2.0', '0.10.0').should == -1
+    end
+
+    it 'should sort tiny version numbers numerically' do
+      subject.compare_versions('0.0.1', '0.0.2').should == -1
+      subject.compare_versions('0.0.2', '0.0.1').should == 1
+      subject.compare_versions('0.0.2', '0.0.10').should == -1
+    end
+
+    it 'should sort major version before minor version' do
+      subject.compare_versions('1.1.0', '1.2.0').should == -1
+      subject.compare_versions('1.2.0', '1.1.1').should == 1
+      subject.compare_versions('1.2.0', '1.10.0').should == -1
+
+      subject.compare_versions('1.1.0', '2.2.0').should == -1
+      subject.compare_versions('2.2.0', '1.1.1').should == 1
+      subject.compare_versions('2.2.0', '1.10.0').should == 1
+    end
+
+    it 'should sort minor version before tiny version' do
+      subject.compare_versions('0.1.1', '0.1.2').should == -1
+      subject.compare_versions('0.1.2', '0.1.1').should == 1
+      subject.compare_versions('0.1.2', '0.1.10').should == -1
+
+      subject.compare_versions('0.1.1', '0.2.2').should == -1
+      subject.compare_versions('0.2.2', '0.1.1').should == 1
+      subject.compare_versions('0.2.2', '0.1.10').should == 1
+    end
+
+    it 'should sort appended strings asciibetically' do
+      subject.compare_versions('0.0.0a', '0.0.0b').should == -1
+      subject.compare_versions('0.0.0beta1', '0.0.0beta2').should == -1
+      subject.compare_versions('0.0.0beta1', '0.0.0rc1').should == -1
+      subject.compare_versions('0.0.0beta1', '0.0.0alpha1').should == 1
+      subject.compare_versions('0.0.0beta1', '0.0.0beta1').should == 0
+    end
+
+    it "should sort appended strings before 'whole' versions" do
+      subject.compare_versions('0.0.1a', '0.0.1').should == -1
+      subject.compare_versions('0.0.1', '0.0.1beta').should == 1
+    end
+  end
+
   describe "::[]" do
     before :each do
       subject.instance_variable_get("@interfaces")[:foo]['0.0.1'] = 10

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list