From cfe62e4f91db459b97cc84ea4815369ba9c2b874 Mon Sep 17 00:00:00 2001 From: Oana Tanasoiu Date: Tue, 22 Sep 2020 09:15:40 +0300 Subject: [PATCH] (FACT-2317) Add Facter.define_fact method --- lib/facter.rb | 13 +++++++++++++ spec/facter/facter_spec.rb | 10 ++++++++++ 2 files changed, 23 insertions(+) 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..a2b7ce2386 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