Skip to content

Commit

Permalink
Improve warning when additional PowerShell isn't found
Browse files Browse the repository at this point in the history
Since we added logic which searches for possible intended permutations
of the given additional PowerShell path, we needed to make the warning
show only if none of the permutations were found. This was accomplished
by suppressing it in the first iterator and then yielding it again after
the permutations were exhausted with the warning unsuppressed.
  • Loading branch information
andyleejordan committed Nov 21, 2024
1 parent 0b61253 commit a7251c8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,17 @@ export class PowerShellExeFinder {
}

exePath = untildify(exePath);

// Always search for what the user gave us first
yield new PossiblePowerShellExe(exePath, versionName);

// Also search for `pwsh[.exe]` and `powershell[.exe]` if missing
const args: [string, undefined, boolean, boolean]
// Must be a tuple type and is suppressing the warning
= [versionName, undefined, true, true];

// Handle Windows where '.exe' and 'powershell' are things
// Always search for what the user gave us first, but with the warning
// suppressed so we can display it after all possibilities are exhausted
yield new PossiblePowerShellExe(exePath, ...args);

// Also search for `pwsh[.exe]` and `powershell[.exe]` if missing
if (this.platformDetails.operatingSystem === OperatingSystem.Windows) {
// Handle Windows where '.exe' and 'powershell' are things
if (!exePath.endsWith("pwsh.exe") && !exePath.endsWith("powershell.exe")) {
if (exePath.endsWith("pwsh") || exePath.endsWith("powershell")) {
// Add extension if that was missing
Expand All @@ -260,9 +260,13 @@ export class PowerShellExeFinder {
yield new PossiblePowerShellExe(path.join(exePath, "pwsh.exe"), ...args);
yield new PossiblePowerShellExe(path.join(exePath, "powershell.exe"), ...args);
}
} else if (!exePath.endsWith("pwsh")) { // Always just 'pwsh' on non-Windows
} else if (!exePath.endsWith("pwsh")) {
// Always just 'pwsh' on non-Windows
yield new PossiblePowerShellExe(path.join(exePath, "pwsh"), ...args);
}

// If we're still being iterated over, no permutation of the given path existed so yield an object with the warning unsuppressed
yield new PossiblePowerShellExe(exePath, versionName, false, undefined, false);
}
}
}
Expand Down
67 changes: 66 additions & 1 deletion test/core/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ if (process.platform === "win32") {
},
environmentVars: {},
expectedPowerShellSequence: [
{
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
displayName: "pwsh",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
displayName: "pwsh",
Expand All @@ -479,6 +484,11 @@ if (process.platform === "win32") {
displayName: "pwsh-tilde",
supportsProperArguments: true
},
{
exePath: path.join(os.homedir(), "pwsh", "pwsh.exe"),
displayName: "pwsh-tilde",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh",
displayName: "pwsh-no-exe",
Expand All @@ -499,6 +509,11 @@ if (process.platform === "win32") {
displayName: "pwsh-no-exe",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh",
displayName: "pwsh-no-exe",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\",
displayName: "pwsh-folder",
Expand All @@ -514,6 +529,11 @@ if (process.platform === "win32") {
displayName: "pwsh-folder",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\",
displayName: "pwsh-folder",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh",
displayName: "pwsh-folder-no-slash",
Expand All @@ -534,6 +554,16 @@ if (process.platform === "win32") {
displayName: "pwsh-folder-no-slash",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh",
displayName: "pwsh-folder-no-slash",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
displayName: "pwsh-single-quotes",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
displayName: "pwsh-single-quotes",
Expand All @@ -544,6 +574,11 @@ if (process.platform === "win32") {
displayName: "pwsh-double-quotes",
supportsProperArguments: true
},
{
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
displayName: "pwsh-double-quotes",
supportsProperArguments: true
},
],
filesystem: {},
}
Expand Down Expand Up @@ -760,7 +795,7 @@ if (process.platform === "win32") {

successAdditionalTestCases = [
{ // Also sufficient for macOS as the behavior is the same
name: "Linux (Additional PowerShell Executables)",
name: "Linux/macOS (Additional PowerShell Executables)",
platformDetails: {
operatingSystem: platform.OperatingSystem.Linux,
isOS64Bit: true,
Expand All @@ -773,6 +808,16 @@ if (process.platform === "win32") {
displayName: "pwsh",
supportsProperArguments: true
},
{
exePath: "/home/bin/pwsh",
displayName: "pwsh",
supportsProperArguments: true
},
{
exePath: path.join(os.homedir(), "bin", "pwsh"),
displayName: "pwsh-tilde",
supportsProperArguments: true
},
{
exePath: path.join(os.homedir(), "bin", "pwsh"),
displayName: "pwsh-tilde",
Expand All @@ -788,6 +833,11 @@ if (process.platform === "win32") {
displayName: "pwsh-folder",
supportsProperArguments: true
},
{
exePath: "/home/bin/",
displayName: "pwsh-folder",
supportsProperArguments: true
},
{
exePath: "/home/bin",
displayName: "pwsh-folder-no-slash",
Expand All @@ -798,6 +848,16 @@ if (process.platform === "win32") {
displayName: "pwsh-folder-no-slash",
supportsProperArguments: true
},
{
exePath: "/home/bin",
displayName: "pwsh-folder-no-slash",
supportsProperArguments: true
},
{
exePath: "/home/bin/pwsh",
displayName: "pwsh-single-quotes",
supportsProperArguments: true
},
{
exePath: "/home/bin/pwsh",
displayName: "pwsh-single-quotes",
Expand All @@ -808,6 +868,11 @@ if (process.platform === "win32") {
displayName: "pwsh-double-quotes",
supportsProperArguments: true
},
{
exePath: "/home/bin/pwsh",
displayName: "pwsh-double-quotes",
supportsProperArguments: true
},
],
filesystem: {},
}
Expand Down

0 comments on commit a7251c8

Please sign in to comment.