Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge 8a44a4f into 2bca424
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie committed Apr 23, 2020
2 parents 2bca424 + 8a44a4f commit 06ebd53
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 107 deletions.
52 changes: 0 additions & 52 deletions lib/custom_facts/core/legacy_facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def self.clear
# @api public
def self.reset
@collection = nil
reset_search_path!
end

# Loads all facts.
Expand All @@ -206,55 +205,4 @@ def self.reset
def self.loadfacts
collection.load_all
end

# Register directories to be searched for facts. The registered directories
# must be absolute paths or they will be ignored.
#
# @param dirs [String] directories to search
#
# @return [void]
#
# @api public
def self.search(*dirs)
@search_path += dirs
end

# Returns the registered search directories.
#
# @return [Array<String>] An array of the directories searched
#
# @api public
def self.search_path
@search_path.dup
end

# Reset the Facter search directories.
#
# @api private
# @return [void]
def self.reset_search_path!
@search_path = []
end

reset_search_path!

# Registers directories to be searched for external facts.
#
# @param dirs [Array<String>] directories to search
#
# @return [void]
#
# @api public
def self.search_external(dirs)
LegacyFacter::Util::Config.external_facts_dirs += dirs
end

# Returns the registered search directories.
#
# @return [Array<String>] An array of the directories searched
#
# @api public
def self.search_external_path
LegacyFacter::Util::Config.external_facts_dirs.dup
end
end
20 changes: 8 additions & 12 deletions lib/custom_facts/util/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@ def self.windows_data_dir
ENV['ProgramData'] || ENV['APPDATA'] if LegacyFacter::Util::Config.windows?
end

def self.external_facts_dirs=(dir)
@external_facts_dirs = dir
end

def self.external_facts_dirs
@external_facts_dirs
Facter::Options.external_dir
end

def self.setup_default_ext_facts_dirs
if LegacyFacter::Util::Root.root?
windows_dir = windows_data_dir
@external_facts_dirs = if windows_dir
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
else
['/opt/puppetlabs/facter/facts.d']
end
Facter::Options[:default_external_dir] = if windows_dir
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
else
['/opt/puppetlabs/facter/facts.d']
end
elsif ENV['HOME']
@external_facts_dirs =
Facter::Options[:default_external_dir] =
[File.expand_path(File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'))]
else
@external_facts_dirs = []
Facter::Options[:default_external_dir] = []
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/custom_facts/util/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def search_path

search_paths.delete_if { |path| !valid_search_path?(path) }

LegacyFacter.search_path.each do |path|
Facter::Options.custom_dir.each do |path|
if valid_search_path?(path)
search_paths << path
else
Expand Down
7 changes: 2 additions & 5 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def add(name, options = {}, &block)
def clear
@already_searched = {}
LegacyFacter.clear
Options[:custom_dir] = []
LegacyFacter.collection.invalidate_custom_facts
LegacyFacter.collection.reload_custom_facts
end
Expand Down Expand Up @@ -133,8 +134,6 @@ def reset
LegacyFacter.reset
Options[:custom_dir] = []
Options[:external_dir] = []
LegacyFacter.search(*Options.custom_dir)
LegacyFacter.search_external(Options.external_dir)
nil
end

Expand All @@ -148,7 +147,6 @@ def reset
# @api public
def search(*dirs)
Options[:custom_dir] += dirs
LegacyFacter.search(*dirs)
end

# Registers directories to be searched for external facts.
Expand All @@ -159,8 +157,7 @@ def search(*dirs)
#
# @api public
def search_external(dirs)
Options[:external_dir] += dirs
LegacyFacter.search_external(dirs)
Options[:external_dir] = dirs
end

# Returns the registered search directories.for external facts.
Expand Down
10 changes: 0 additions & 10 deletions lib/framework/core/fact_loaders/external_fact_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ def all_facts

private

# The search paths must be set before creating the fact collection.
# If we set them after, they will not be visible.
def load_search_paths
LegacyFacter.reset_search_path!
LegacyFacter.search(*Options.custom_dir) if Options.custom_dir?
LegacyFacter.search_external(Options.external_dir) if Options.external_dir?
end

def load_custom_facts
custom_facts = []

load_search_paths
custom_facts_to_load = LegacyFacter.collection.custom_facts

custom_facts_to_load&.each do |custom_fact_name|
Expand All @@ -41,7 +32,6 @@ def load_custom_facts
def load_external_facts
external_facts = []

load_search_paths
external_facts_to_load = LegacyFacter.collection.external_facts

external_facts_to_load&.each do |external_fact_name|
Expand Down
2 changes: 2 additions & 0 deletions lib/framework/core/options/config_file_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def augment_custom(file_global_conf)
end

@options[:custom_dir] = file_global_conf['custom-dir'] unless file_global_conf['custom-dir'].nil?
@options[:config_file_custom_dir] = @options[:custom_dir]
end

def augment_external(global_conf)
Expand All @@ -56,6 +57,7 @@ def augment_external(global_conf)
end

@options[:external_dir] = [global_conf['external-dir']].flatten unless global_conf['external-dir'].nil?
@options[:config_file_external_dir] = @options[:external_dir]
end

def augment_ruby(global_conf)
Expand Down
25 changes: 21 additions & 4 deletions lib/framework/core/options/option_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ class OptionStore
@show_legacy = true
@block = true
@custom_dir = []
@config_file_custom_dir = []
@custom_facts = true
@external_dir = []
@config_file_external_dir = []
@default_external_dir = []
@external_facts = true
@ruby = true
@blocked_facts = []
@user_query = []

class << self
attr_reader :debug, :verbose, :log_level, :show_legacy, :trace,
:custom_dir, :external_dir, :ruby,
:custom_dir, :ruby,
:custom_facts, :blocked_facts

attr_accessor :config, :user_query, :strict, :json, :haml, :external_facts,
:cache, :yaml, :puppet, :ttls, :block, :cli
:cache, :yaml, :puppet, :ttls, :block, :cli, :config_file_custom_dir,
:config_file_external_dir, :default_external_dir

def all
options = {}
Expand All @@ -45,8 +49,14 @@ def ruby=(bool)
end
end

def external_dir
return @default_external_dir if @external_dir.empty? && @external_facts

@external_dir
end

def external_dir=(dirs)
return unless dirs.any?
dirs = @config_file_external_dir unless dirs.any?

@external_dir = dirs
end
Expand All @@ -57,8 +67,14 @@ def blocked_facts=(*facts)
@blocked_facts.flatten!
end

def custom_dir
return @config_file_custom_dir unless @custom_dir.any?

@custom_dir
end

def custom_dir=(*dirs)
return unless dirs.any?
dirs = @config_file_custom_dir unless dirs.any?

@ruby = true

Expand Down Expand Up @@ -145,6 +161,7 @@ def reset
@custom_dir = []
@custom_facts = true
@external_dir = []
@default_external_dir = []
@external_facts = true
@ruby = true
@blocked_facts = []
Expand Down
2 changes: 1 addition & 1 deletion spec/custom_facts/util/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
it 'onlies output new values when explicitly set' do
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
new_value = ['/usr/share/newdir']
LegacyFacter::Util::Config.external_facts_dirs = new_value
Facter::Options[:external_dir] = new_value
expect(LegacyFacter::Util::Config.external_facts_dirs).to eq new_value
end
end
Expand Down
11 changes: 7 additions & 4 deletions spec/custom_facts/util/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def loader_from(places)
end

it 'includes all search paths registered with Facter' do
allow(LegacyFacter).to receive(:search_path).and_return %w[/one /two]
allow(Facter::Options).to receive(:custom_dir).and_return %w[/one /two]

allow(loader).to receive(:valid_search_path?).and_return true

allow(File).to receive(:directory?).and_return false
Expand All @@ -104,7 +105,8 @@ def loader_from(places)
end

it 'warns on invalid search paths registered with Facter' do
allow(LegacyFacter).to receive(:search_path).and_return %w[/one two/three]
allow(Facter::Options).to receive(:custom_dir).and_return %w[/one two/three]

allow(loader).to receive(:valid_search_path?).and_return false
allow(loader).to receive(:valid_search_path?).with('/one').and_return true
allow(loader).to receive(:valid_search_path?).with('two/three').and_return false
Expand All @@ -119,8 +121,9 @@ def loader_from(places)
expect(paths).to match_array(['/one'])
end

it 'strips paths that are valid paths but not are not present' do
allow(LegacyFacter).to receive(:search_path).and_return %w[/one /two]
it 'strips paths that are valid paths but are not present' do
allow(Facter::Options).to receive(:custom_dir).and_return %w[/one /two]

allow(loader).to receive(:valid_search_path?).and_return false
allow(loader).to receive(:valid_search_path?).with('/one').and_return true
allow(loader).to receive(:valid_search_path?).with('/two').and_return true
Expand Down
18 changes: 9 additions & 9 deletions spec/facter/facter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@
end

describe '#search' do
it 'sends call to LegacyFacter' do
it 'sends call to Facter::Options' do
dirs = ['/dir1', '/dir2']

expect(LegacyFacter).to receive(:search).with(dirs)
Facter.search(dirs)
expect(Facter::Options).to receive(:[]=).with(:custom_dir, dirs)
Facter.search(*dirs)
end
end

Expand All @@ -292,10 +292,10 @@
end

describe '#search_external' do
it 'sends call to LegacyFacter' do
it 'sends call to Facter::Options' do
dirs = ['/dir1', '/dir2']
expect(Facter::Options).to receive(:[]=).with(:external_dir, dirs)

expect(LegacyFacter).to receive(:search_external).with(dirs)
Facter.search_external(dirs)
end
end
Expand All @@ -315,15 +315,15 @@
end

it 'adds custom facts dirs' do
allow(LegacyFacter).to receive(:search)
allow(Facter::Options).to receive(:[]=)
Facter.reset
expect(LegacyFacter).to have_received(:search).once
expect(Facter::Options).to have_received(:[]=).with(:custom_dir, [])
end

it 'add external facts dirs' do
allow(LegacyFacter).to receive(:search_external)
allow(Facter::Options).to receive(:[]=)
Facter.reset
expect(LegacyFacter).to have_received(:search_external).once
expect(Facter::Options).to have_received(:[]=).with(:external_dir, [])
end
end

Expand Down
25 changes: 16 additions & 9 deletions spec/framework/core/options/option_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe Facter::OptionStore do
subject(:option_store) { Facter::OptionStore }

before do
option_store.reset
end

after do
option_store.reset
end
Expand All @@ -14,9 +18,12 @@
blocked_facts: [],
cli: nil,
custom_dir: [],
config_file_custom_dir: [],
custom_facts: true,
debug: false,
external_dir: [],
config_file_external_dir: [],
default_external_dir: [],
external_facts: true,
log_level: :warn,
ruby: true,
Expand Down Expand Up @@ -57,15 +64,15 @@
end

describe '#external_dir=' do
context 'with empty array' do
it 'does not override existing array' do
option_store.external_dir = ['/path1']

option_store.external_dir = []

expect(option_store.external_dir).to eq(['/path1'])
end
end
# context 'with empty array' do
# it 'does not override existing array' do
# option_store.external_dir = ['/path1']
#
# option_store.external_dir = []
#
# expect(option_store.external_dir).to eq(['/path1'])
# end
# end

context 'with array' do
it 'sets dirs' do
Expand Down

0 comments on commit 06ebd53

Please sign in to comment.