Skip to content

Commit d8966d0

Browse files
(FACT-3133) check for command alias in powershell
Powershell aliases certain commands that were not detected by the `which` facter function; for example, `echo` is aliased to `Write-Output`. Previously, facter's custom fact resolver would erroneously report that `echo` did not exist in the subshell for a Windows custom fact. With this patch, custom facts for Windows will check if the command is aliased before trying to resolve it via the path.
1 parent 13ccc8b commit d8966d0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/facter/custom_facts/core/execution/windows.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ def search_paths
88

99
DEFAULT_COMMAND_EXTENSIONS = %w[.COM .EXE .BAT .CMD].freeze
1010

11+
def aliased_command?(command)
12+
_output, status = Open3.capture2("powershell.exe Get-Alias #{command}")
13+
status.success?
14+
end
15+
1116
def which(bin)
17+
return bin if aliased_command?(bin)
18+
1219
if absolute_path?(bin)
1320
return bin if File.executable?(bin)
1421
else

spec/custom_facts/core/execution/windows_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
allow(ENV).to receive(:[]).with('PATHEXT').and_return nil
3737
end
3838

39+
context 'when using a command aliased to a Powershell command' do
40+
it 'checks for the alias and returns the original queried command' do
41+
allow(executor).to receive(:aliased_command?).with('foo.exe').and_return true
42+
43+
expect(executor.which('foo.exe')).to eq 'foo.exe'
44+
end
45+
end
46+
3947
context 'when it is provided with an absolute path' do
4048
it 'returns the path to binary if executable' do
4149
allow(File).to receive(:executable?).with('C:\Tools\foo.exe').and_return true
@@ -66,13 +74,15 @@
6674
it 'returns the absolute path if found' do
6775
allow(File).to receive(:executable?).with('C:\Windows\system32\foo.exe').and_return false
6876
allow(File).to receive(:executable?).with('C:\Windows\foo.exe').and_return true
77+
allow(executor).to receive(:aliased_command?).with('foo.exe').and_return false
6978

7079
expect(executor.which('foo.exe')).to eq 'C:\Windows\foo.exe'
7180
end
7281

7382
it "won't check all paths if one is executable" do
7483
allow(File).to receive(:executable?).with('C:\Windows\system32\foo.exe').and_return false
7584
allow(File).to receive(:executable?).with('C:\Windows\foo.exe').and_return true
85+
allow(executor).to receive(:aliased_command?).with('foo.exe').and_return false
7686

7787
expect(File).not_to receive(:executable?).with('C:\Windows\System32\Wbem\foo.exe')
7888
executor.which('foo.exe')
@@ -87,6 +97,7 @@
8797
allow(File).to receive(:executable?).with('C:\Windows\foo' + ext).and_return false
8898
end
8999
allow(File).to receive(:executable?).with('C:\Windows\foo.EXE').and_return true
100+
allow(executor).to receive(:aliased_command?).with('foo').and_return false
90101

91102
expect(executor.which('foo')).to eq 'C:\Windows\foo.EXE'
92103
end
@@ -95,6 +106,7 @@
95106
allow(File).to receive(:executable?).with('C:\Windows\system32\foo.exe').and_return false
96107
allow(File).to receive(:executable?).with('C:\Windows\foo.exe').and_return false
97108
allow(File).to receive(:executable?).with('C:\Windows\System32\Wbem\foo.exe').and_return false
109+
allow(executor).to receive(:aliased_command?).with('foo.exe').and_return false
98110

99111
expect(executor.which('foo.exe')).to be nil
100112
end

0 commit comments

Comments
 (0)