diff --git a/lib/facter.rb b/lib/facter.rb index 12a9fdeb8f..4bc1695bb7 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -102,6 +102,19 @@ def debugonce(message) nil 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 + # + # @api public + def define_fact(name, options = {}, &block) + options[:fact_type] = :custom + LegacyFacter.define_fact(name, options, &block) + end + def on_message(&block) Facter::Log.on_message(&block) end diff --git a/spec/facter/facter_spec.rb b/spec/facter/facter_spec.rb index bbb526b607..8cc9cf3ae1 100644 --- a/spec/facter/facter_spec.rb +++ b/spec/facter/facter_spec.rb @@ -296,6 +296,16 @@ def mock_collection(method, os_name = nil, error = nil) end end + describe '#define_fact' do + it 'sends call to LegacyFacter' do + allow(LegacyFacter).to receive(:define_fact) + + Facter.define_fact('fact_name') {} + + expect(LegacyFacter).to have_received(:define_fact).once.with('fact_name', {:fact_type=>:custom}) + end + end + describe '#trace?' do it 'returns trace variable' do expect(Facter).not_to be_trace