From a45c8a705d2ebbd357778f58fda853d8b44db530 Mon Sep 17 00:00:00 2001 From: Tony Vu Date: Thu, 25 Aug 2022 16:21:16 -0700 Subject: [PATCH] (FACT-3133) allow for `echo` on Windows `echo` was supported for Windows in facter 3, but has been missing from support in facter 4; this change allows for custom facts to use the builtin echo commands in cmd.exe or powershell. --- lib/facter/custom_facts/core/execution/windows.rb | 4 ++++ spec/custom_facts/core/execution/windows_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/facter/custom_facts/core/execution/windows.rb b/lib/facter/custom_facts/core/execution/windows.rb index 983eb61641..970f364627 100644 --- a/lib/facter/custom_facts/core/execution/windows.rb +++ b/lib/facter/custom_facts/core/execution/windows.rb @@ -9,6 +9,10 @@ def search_paths DEFAULT_COMMAND_EXTENSIONS = %w[.COM .EXE .BAT .CMD].freeze def which(bin) + # `echo` is allowed for facter 3.x compatibility, otherwise + # all commands much be found on the PATH or absolute. + return bin if /^echo$/i =~ bin + if absolute_path?(bin) return bin if File.executable?(bin) else diff --git a/spec/custom_facts/core/execution/windows_spec.rb b/spec/custom_facts/core/execution/windows_spec.rb index 90987f53d8..20f0f558b0 100644 --- a/spec/custom_facts/core/execution/windows_spec.rb +++ b/spec/custom_facts/core/execution/windows_spec.rb @@ -36,6 +36,16 @@ allow(ENV).to receive(:[]).with('PATHEXT').and_return nil end + context 'when trying to use builtin windows commands' do + it 'allows echo' do + expect(executor.which('echo')).to eq 'echo' + end + + it 'disallows other builtin windows commands' do + expect(executor.which('dir')).to eq nil + end + end + context 'when it is provided with an absolute path' do it 'returns the path to binary if executable' do allow(File).to receive(:executable?).with('C:\Tools\foo.exe').and_return true