diff --git a/lib/puppet/string/string_collection.rb b/lib/puppet/string/string_collection.rb index f8fa38b9c..ecd99359d 100644 --- a/lib/puppet/string/string_collection.rb +++ b/lib/puppet/string/string_collection.rb @@ -1,77 +1,123 @@ +# -*- coding: utf-8 -*- require 'puppet/string' module Puppet::String::StringCollection SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ @strings = Hash.new { |hash, key| hash[key] = {} } def self.strings unless @loaded @loaded = true $LOAD_PATH.each do |dir| next unless FileTest.directory?(dir) Dir.chdir(dir) do Dir.glob("puppet/string/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| iname = file.sub(/\.rb/, '') begin require iname rescue Exception => detail puts detail.backtrace if Puppet[:trace] raise "Could not load #{iname} from #{dir}/#{file}: #{detail}" end end end end end return @strings.keys end def self.validate_version(version) !!(SEMVER_VERSION =~ version.to_s) end + def self.cmp_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) @strings[underscorize(name)][version] if string?(name, version) end def self.string?(name, version) name = underscorize(name) - cache = @strings[name] - return true if cache.has_key?(version) - - loaded = cache.keys + return true if @strings[name].has_key?(version) - module_names = ["puppet/string/#{name}"] - unless version == :current - module_names << "#{name}@#{version}/puppet/string/#{name}" - end + # We always load the current version file; the common case is that we have + # the expected version and any compatibility versions in the same file, + # the default. Which means that this is almost always the case. + # + # We use require to avoid executing the code multiple times, like any + # other Ruby library that we might want to use. --daniel 2011-04-06 + begin + require "puppet/string/#{name}" - module_names.each do |module_name| - begin - require module_name - if version == :current || !module_name.include?('@') - loaded = (cache.keys - loaded).first - cache[:current] = cache[loaded] unless loaded.nil? - end - return true if cache.has_key?(version) - rescue LoadError => e - raise unless e.message =~ /-- #{module_name}$/ - # pass + # If we wanted :current, we need to index to find that; direct version + # requests just work™ as they go. --daniel 2011-04-06 + if version == :current then + # We need to find current out of this. This is the largest version + # number that doesn't have a dedicated on-disk file present; those + # represent "experimental" versions of strings, which we don't fully + # support yet. + # + # We walk the versions from highest to lowest and take the first version + # that is not defined in an explicitly versioned file on disk as the + # current version. + # + # This constrains us to only ship experimental versions with *one* + # version in the file, not multiple, but given you can't reliably load + # them except by side-effect when you ignore that rule this seems safe + # enough... + # + # Given those constraints, and that we are not going to ship a versioned + # interface that is not :current in this release, we are going to leave + # these thoughts in place, and just punt on the actual versioning. + # + # When we upgrade the core to support multiple versions we can solve the + # problems then; as lazy as possible. + # + # We do support multiple versions in the same file, though, so we sort + # versions here and return the last item in that set. + # + # --daniel 2011-04-06 + latest_ver = @strings[name].keys.sort {|a, b| cmp_versions(a, b) }.last + @strings[name][:current] = @strings[name][latest_ver] end + rescue LoadError => e + raise unless e.message =~ %r{-- puppet/string/#{name}$} + # ...guess we didn't find the file; return a much better problem. end - return false + # Now, either we have the version in our set of strings, or we didn't find + # the version they were looking for. In the future we will support + # loading versioned stuff from some look-aside part of the Ruby load path, + # but we don't need that right now. + # + # So, this comment is a place-holder for that. --daniel 2011-04-06 + return !! @strings[name].has_key?(version) end def self.register(string) @strings[underscorize(string.name)][string.version] = string end def self.underscorize(name) unless name.to_s =~ /^[-_a-z]+$/i then raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid string name" end name.to_s.downcase.split(/[-_]/).join('_').to_sym end end diff --git a/spec/unit/string/string_collection_spec.rb b/spec/unit/string/string_collection_spec.rb index fa63f18e3..fab647da0 100755 --- a/spec/unit/string/string_collection_spec.rb +++ b/spec/unit/string/string_collection_spec.rb @@ -1,192 +1,184 @@ #!/usr/bin/env ruby require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') require 'tmpdir' describe Puppet::String::StringCollection do # To avoid cross-pollution we have to save and restore both the hash # containing all the string data, and the array used by require. Restoring # both means that we don't leak side-effects across the code. --daniel 2011-04-06 before :each do @original_strings = subject.instance_variable_get("@strings").dup @original_required = $".dup subject.instance_variable_get("@strings").clear end after :each do subject.instance_variable_set("@strings", @original_strings) $".clear ; @original_required.each do |item| $" << item end end describe "::strings" do it "REVISIT: should have some tests here, if we describe it" 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 "::[]" do before :each do subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10 end before :each do @dir = Dir.mktmpdir @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string') $LOAD_PATH.push(@dir) end after :each do FileUtils.remove_entry_secure @dir $LOAD_PATH.pop end it "should return the string with the given name" do subject["foo", '0.0.1'].should == 10 end it "should attempt to load the string if it isn't found" do subject.expects(:require).with('puppet/string/bar') - subject.expects(:require).with('bar@0.0.1/puppet/string/bar') subject["bar", '0.0.1'] end it "should attempt to load the default string for the specified version :current" do subject.expects(:require).never # except... subject.expects(:require).with('puppet/string/fozzie') subject['fozzie', :current] end end describe "::string?" do before :each do subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10 end it "should return true if the string specified is registered" do subject.string?("foo", '0.0.1').should == true end it "should attempt to require the string if it is not registered" do subject.expects(:require).with do |file| subject.instance_variable_get("@strings")[:bar]['0.0.1'] = true file == 'puppet/string/bar' end subject.string?("bar", '0.0.1').should == true end it "should return true if requiring the string registered it" do subject.stubs(:require).with do subject.instance_variable_get("@strings")[:bar]['0.0.1'] = 20 end end - it "should require the string by version if the 'current' version isn't it" do - subject.expects(:require).with('puppet/string/bar'). - raises(LoadError, 'no such file to load -- puppet/string/bar') - subject.expects(:require).with do |file| - subject.instance_variable_get("@strings")[:bar]['0.0.1'] = true - file == 'bar@0.0.1/puppet/string/bar' - end - subject.string?("bar", '0.0.1').should == true - end - it "should return false if the string is not registered" do subject.stubs(:require).returns(true) - subject.string?("bar", '0.0.1').should == false + subject.string?("bar", '0.0.1').should be_false end - it "should return false if there is a LoadError requiring the string" do + it "should return false if the string file itself is missing" do subject.stubs(:require). - raises(LoadError, 'no such file to load -- puppet/string/bar').then. - raises(LoadError, 'no such file to load -- bar@0.0.1/puppet/string/bar') - subject.string?("bar", '0.0.1').should == false + raises(LoadError, 'no such file to load -- puppet/string/bar') + subject.string?("bar", '0.0.1').should be_false end it "should register the version loaded by `:current` as `:current`" do subject.expects(:require).with do |file| subject.instance_variable_get("@strings")[:huzzah]['2.0.1'] = :huzzah_string file == 'puppet/string/huzzah' end subject.string?("huzzah", :current) subject.instance_variable_get("@strings")[:huzzah][:current].should == :huzzah_string end - it "should register the version loaded from `puppet/string/{name}` as `:current`" do - subject.expects(:require).with do |file| - subject.instance_variable_get("@strings")[:huzzah]['2.0.1'] = :huzzah_string - file == 'puppet/string/huzzah' + context "with something on disk" do + before :each do + write_scratch_string :huzzah do |fh| + fh.puts < {'0.0.1' => string}} end end describe "::underscorize" do faulty = [1, "#foo", "$bar", "sturm und drang", :"sturm und drang"] valid = { "Foo" => :foo, :Foo => :foo, "foo_bar" => :foo_bar, :foo_bar => :foo_bar, "foo-bar" => :foo_bar, :"foo-bar" => :foo_bar, } valid.each do |input, expect| it "should map #{input.inspect} to #{expect.inspect}" do result = subject.underscorize(input) result.should == expect end end faulty.each do |input| it "should fail when presented with #{input.inspect} (#{input.class})" do expect { subject.underscorize(input) }. should raise_error ArgumentError, /not a valid string name/ end end end end diff --git a/spec/unit/string_spec.rb b/spec/unit/string_spec.rb index 358668f9b..9b7cd887c 100755 --- a/spec/unit/string_spec.rb +++ b/spec/unit/string_spec.rb @@ -1,145 +1,144 @@ #!/usr/bin/env ruby require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') describe Puppet::String do before :all do @strings = Puppet::String::StringCollection.instance_variable_get("@strings").dup end before :each do Puppet::String::StringCollection.instance_variable_get("@strings").clear end after :all do Puppet::String::StringCollection.instance_variable_set("@strings", @strings) end describe "#define" do it "should register the string" do string = Puppet::String.define(:string_test_register, '0.0.1') string.should == Puppet::String[:string_test_register, '0.0.1'] end it "should load actions" do Puppet::String.any_instance.expects(:load_actions) Puppet::String.define(:string_test_load_actions, '0.0.1') end it "should require a version number" do proc { Puppet::String.define(:no_version) }.should raise_error(ArgumentError) end end describe "#initialize" do it "should require a version number" do proc { Puppet::String.new(:no_version) }.should raise_error(ArgumentError) end it "should require a valid version number" do proc { Puppet::String.new(:bad_version, 'Rasins') }.should raise_error(ArgumentError) end it "should instance-eval any provided block" do face = Puppet::String.new(:string_test_block, '0.0.1') do action(:something) do when_invoked { "foo" } end end face.something.should == "foo" end end it "should have a name" do Puppet::String.new(:me, '0.0.1').name.should == :me end it "should stringify with its own name" do Puppet::String.new(:me, '0.0.1').to_s.should =~ /\bme\b/ end it "should allow overriding of the default format" do face = Puppet::String.new(:me, '0.0.1') face.set_default_format :foo face.default_format.should == :foo end it "should default to :pson for its format" do Puppet::String.new(:me, '0.0.1').default_format.should == :pson end # Why? it "should create a class-level autoloader" do Puppet::String.autoloader.should be_instance_of(Puppet::Util::Autoload) end it "should try to require strings that are not known" do Puppet::String::StringCollection.expects(:require).with "puppet/string/foo" - Puppet::String::StringCollection.expects(:require).with "foo@0.0.1/puppet/string/foo" Puppet::String[:foo, '0.0.1'] end it "should be able to load all actions in all search paths" it_should_behave_like "things that declare options" do def add_options_to(&block) Puppet::String.new(:with_options, '0.0.1', &block) end end describe "with string-level options" do it "should not return any action-level options" do string = Puppet::String.new(:with_options, '0.0.1') do option "--foo" option "--bar" action :baz do option "--quux" end end string.options.should =~ [:foo, :bar] end it "should fail when a string option duplicates an action option" do expect { Puppet::String.new(:action_level_options, '0.0.1') do action :bar do option "--foo" end option "--foo" end }.should raise_error ArgumentError, /Option foo conflicts with existing option foo on/i end it "should work when two actions have the same option" do string = Puppet::String.new(:with_options, '0.0.1') do action :foo do option "--quux" end action :bar do option "--quux" end end string.get_action(:foo).options.should =~ [:quux] string.get_action(:bar).options.should =~ [:quux] end end describe "with inherited options" do let :string do parent = Class.new(Puppet::String) parent.option("--inherited") string = parent.new(:example, '0.2.1') string.option("--local") string end describe "#options" do it "should list inherited options" do string.options.should =~ [:inherited, :local] end end describe "#get_option" do it "should return an inherited option object" do string.get_option(:inherited).should be_an_instance_of Puppet::String::Option end end end end