Adding hex format for native command exit codes (#21067)

This commit is contained in:
Stéphane BARIZIEN
2025-07-31 08:23:20 +05:00
committed by GitHub
parent 11eeae97f0
commit a993992298
3 changed files with 12 additions and 6 deletions
@@ -133,7 +133,7 @@ namespace System.Management.Automation
}
}
#nullable enable
#nullable enable
/// <summary>
/// This exception is used by the NativeCommandProcessor to indicate an error
/// when a native command returns a non-zero exit code.
@@ -187,7 +187,7 @@ namespace System.Management.Automation
public int ProcessId { get; }
}
#nullable restore
#nullable restore
/// <summary>
/// Provides way to create and execute native commands.
@@ -972,11 +972,17 @@ namespace System.Management.Automation
}
const string errorId = nameof(CommandBaseStrings.ProgramExitedWithNonZeroCode);
#if UNIX
string hexFormatStr = "0x{0:X2}";
#else
string hexFormatStr = "0x{0:X8}";
#endif
string errorMsg = StringUtil.Format(
CommandBaseStrings.ProgramExitedWithNonZeroCode,
NativeCommandName,
_nativeProcess.ExitCode);
_nativeProcess.ExitCode,
string.Format(CultureInfo.InvariantCulture, hexFormatStr, _nativeProcess.ExitCode));
var exception = new NativeCommandExitException(
Path,
@@ -163,7 +163,7 @@
however that is used for ApplicationFailedExceptions thrown when the NativeCommandProcessor fails in an unexpected way.
In this case, we have a more specific error for the native command scenario, so the two are not conflated.
-->
<value>Program "{0}" ended with non-zero exit code: {1}.</value>
<value>Program "{0}" ended with non-zero exit code: {1} ({2}).</value>
</data>
<data name="ShouldProcessMessage" xml:space="preserve">
<value>Performing the operation "{0}" on target "{1}".</value>
@@ -48,7 +48,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
}
It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" {
@@ -61,7 +61,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
}
It 'Non-zero exit code generates a non-teminating error for $ErrorActionPreference = ''SilentlyContinue''' {