From 4e4e8034fa2f20fd70292d9bf61d899dc067f7d9 Mon Sep 17 00:00:00 2001 From: Ferenc Erki Date: Sun, 8 Jun 2025 09:00:53 +0200 Subject: [PATCH] Fix command existence checks without which --- ChangeLog | 1 + lib/Rex/Interface/Exec/Base.pm | 2 +- lib/Rex/Interface/Exec/Local.pm | 2 +- t/can_run.t | 6 +++--- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index b6dfec4e4..8feebbaa5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ Revision history for Rex - Prefer GNU tools on Solaris - Fix parsing FreeBSD memory details - Recognize laundry memory on FreeBSD + - Fix command existence checks without which [DOCUMENTATION] diff --git a/lib/Rex/Interface/Exec/Base.pm b/lib/Rex/Interface/Exec/Base.pm index e0990f6f0..21d95c085 100644 --- a/lib/Rex/Interface/Exec/Base.pm +++ b/lib/Rex/Interface/Exec/Base.pm @@ -54,7 +54,7 @@ sub execute_line_based_operation { sub can_run { my ( $self, $commands_to_check, $check_with_command ) = @_; - $check_with_command ||= "which"; + $check_with_command ||= "command -v"; my $exec = Rex::Interface::Exec->create; my $cache = Rex::get_cache(); diff --git a/lib/Rex/Interface/Exec/Local.pm b/lib/Rex/Interface/Exec/Local.pm index 221475cc9..3d6b11e00 100644 --- a/lib/Rex/Interface/Exec/Local.pm +++ b/lib/Rex/Interface/Exec/Local.pm @@ -114,7 +114,7 @@ sub exec { sub can_run { my ( $self, $commands_to_check, $check_with_command ) = @_; - $check_with_command ||= $^O =~ /^MSWin/i ? 'where' : 'which'; + $check_with_command ||= $^O =~ /^MSWin/i ? 'where' : 'command -v'; return $self->SUPER::can_run( $commands_to_check, $check_with_command ); } diff --git a/t/can_run.t b/t/can_run.t index c6fa5752b..4a16b0b5c 100755 --- a/t/can_run.t +++ b/t/can_run.t @@ -11,7 +11,7 @@ use Test::Warnings; use Rex::Commands::Run; { - my $command_to_check = $^O =~ /^MSWin/ ? 'where' : 'which'; + my $command_to_check = $^O =~ /^MSWin/ ? 'where' : 'sh'; my $result = can_run($command_to_check); ok( $result, 'Found checker command' ); } @@ -23,14 +23,14 @@ use Rex::Commands::Run; } { - my @commands_to_check = $^O =~ /^MSWin/ ? 'where' : 'which'; + my @commands_to_check = $^O =~ /^MSWin/ ? 'where' : 'sh'; push @commands_to_check, 'non-existing command'; my $result = can_run(@commands_to_check); ok( $result, 'Multiple commands - existing first' ); } { - my @commands_to_check = $^O =~ /^MSWin/ ? 'where' : 'which'; + my @commands_to_check = $^O =~ /^MSWin/ ? 'where' : 'sh'; unshift @commands_to_check, 'non-existing command'; my $result = can_run(@commands_to_check); ok( $result, 'Multiple commands - non-existing first' );