From 68685ddc23be4a9cf4f9838efb64b1d8a6a47109 Mon Sep 17 00:00:00 2001 From: Filipovici-Andrei Date: Tue, 9 Jun 2020 15:31:46 +0300 Subject: [PATCH] FACT-2650 Fix bug when loading external facts (#543) * (maint) changelog for 4.0.24 release * (FACT-2650) Fix bug with wrong slashes in external fact file paths --- lib/custom_facts/util/loader.rb | 2 ++ spec/custom_facts/util/loader_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/custom_facts/util/loader.rb b/lib/custom_facts/util/loader.rb index d8a909e91..f37bae1e5 100644 --- a/lib/custom_facts/util/loader.rb +++ b/lib/custom_facts/util/loader.rb @@ -39,6 +39,8 @@ def load_all paths = search_path paths&.each do |dir| + # clean the search path of wrong slashes and backslashes + dir = dir.gsub(%r{[\/\\]+}, File::SEPARATOR) # dir is already an absolute path Dir.glob(File.join(dir, '*.rb')).each do |path| # exclude dirs that end with .rb diff --git a/spec/custom_facts/util/loader_spec.rb b/spec/custom_facts/util/loader_spec.rb index 6e803b8f9..decb269ec 100755 --- a/spec/custom_facts/util/loader_spec.rb +++ b/spec/custom_facts/util/loader_spec.rb @@ -280,6 +280,24 @@ def loader_from(places) loader.load_all loader.load_all end + + context 'when directory path has wrong slashes' do + before do + allow(Dir).to receive(:glob).with('/one/dir/*.rb').and_return %w[/one/dir/a.rb] + end + + dir_paths = ['//one///dir', '//one///\\dir', '/one///\/\dir', '\one///\\dir'] + + dir_paths.each do |dir_path| + it 'corrects the directory path' do + allow(loader).to receive(:search_path).and_return [dir_path] + + loader.load_all + + expect(Dir).to have_received(:glob).with('/one/dir/*.rb') + end + end + end end it 'loads facts on the facter search path only once' do