[Pkg-puppet-devel] [facter] 159/352: (FACT-65) Collections should be able to directly create facts
Stig Sandbeck Mathisen
ssm at debian.org
Sun Apr 6 22:21:41 UTC 2014
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository facter.
commit 37ac68ed5141ca4c755178607886e62ac4c1d8cd
Author: Adrien Thebo <git at somethingsinistral.net>
Date: Mon Dec 30 11:12:33 2013 -0800
(FACT-65) Collections should be able to directly create facts
The existing `Collection#add` method would create a new fact and
resolution at the same time. This means that facts themselves could not
be created and manipulated. This commit adds `#define_fact` so that new
facts can be created by themselves.
This commit also removes tests inside of the collection for the
:ldapname fact option since that's going the way of the dinosaurs.
---
lib/facter/util/collection.rb | 30 ++++++++++++++++++++++++++++++
spec/unit/util/collection_spec.rb | 27 +++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/lib/facter/util/collection.rb b/lib/facter/util/collection.rb
index 120dd1c..76df949 100644
--- a/lib/facter/util/collection.rb
+++ b/lib/facter/util/collection.rb
@@ -19,8 +19,38 @@ class Facter::Util::Collection
value(name)
end
+ # Define a new fact or extend an existing fact.
+ #
+ # @param name [Symbol] The name of the fact to define
+ # @param options [Hash] A hash of options to set on the fact
+ #
+ # @return [Facter::Util::Fact] The fact that was defined
+ def define_fact(name, options = {}, &block)
+ name = canonicalize(name)
+
+ fact = @facts[name]
+
+ if fact.nil?
+ fact = Facter::Util::Fact.new(name, options)
+ @facts[name] = fact
+ end
+
+ if block_given?
+ fact.instance_eval(&block)
+ end
+
+ fact
+ rescue => e
+ Facter.warn "Unable to add fact #{name}: #{e}"
+ end
+
# Add a resolution mechanism for a named fact. This does not distinguish
# between adding a new fact and adding a new way to resolve a fact.
+ #
+ # @param name [Symbol] The name of the fact to define
+ # @param options [Hash] A hash of options to set on the fact and resolution
+ #
+ # @return [Facter::Util::Fact] The fact that was defined
def add(name, options = {}, &block)
name = canonicalize(name)
diff --git a/spec/unit/util/collection_spec.rb b/spec/unit/util/collection_spec.rb
index 8e211a8..afca69b 100755
--- a/spec/unit/util/collection_spec.rb
+++ b/spec/unit/util/collection_spec.rb
@@ -66,6 +66,33 @@ describe Facter::Util::Collection do
end
end
+ describe "when only defining facts" do
+ it "creates a new fact if no such fact exists" do
+ fact = Facter::Util::Fact.new(:newfact)
+ Facter::Util::Fact.expects(:new).with(:newfact, {}).returns fact
+ expect(collection.define_fact(:newfact)).to equal fact
+ end
+
+ it "returns an existing fact if the fact has already been defined" do
+ fact = collection.define_fact(:newfact)
+ expect(collection.define_fact(:newfact)).to equal fact
+ end
+
+ it "passes options to newly generated facts" do
+ Facter.stubs(:warnonce)
+ fact = collection.define_fact(:newfact, :ldapname => 'NewFact')
+ expect(fact.ldapname).to eq 'NewFact'
+ end
+
+ it "logs a warning if the fact could not be defined" do
+ Facter.expects(:warn).with("Unable to add fact newfact: kaboom!")
+
+ collection.define_fact(:newfact) do
+ raise "kaboom!"
+ end
+ end
+ end
+
describe "when retrieving facts" do
before do
@fact = collection.add("YayNess")
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git
More information about the Pkg-puppet-devel
mailing list