Skip to content

Commit 350eaff

Browse files
committed
Add powerShellAdditionalExePaths unit tests
1 parent d32d1bb commit 350eaff

File tree

1 file changed

+187
-1
lines changed

1 file changed

+187
-1
lines changed

test/core/platform.test.ts

+187-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as sinon from "sinon";
1111
import * as platform from "../../src/platform";
1212
import * as fs from "fs"; // NOTE: Necessary for mock-fs.
1313
import * as vscode from "vscode";
14+
import { stripQuotePair } from "../../src/utils";
1415

1516
// We have to rewire the platform module so that mock-fs can be used, as it
1617
// overrides the fs module but not the vscode.workspace.fs module.
@@ -34,7 +35,8 @@ async function fakeReadDirectory(targetPath: string | vscode.Uri): Promise<strin
3435
const utilsMock = {
3536
checkIfFileExists: fakeCheckIfFileOrDirectoryExists,
3637
checkIfDirectoryExists: fakeCheckIfFileOrDirectoryExists,
37-
readDirectory: fakeReadDirectory
38+
readDirectory: fakeReadDirectory,
39+
stripQuotePair: stripQuotePair
3840
};
3941

4042
platformMock.__set__("utils", utilsMock);
@@ -62,6 +64,9 @@ interface ITestPlatformSuccessCase extends ITestPlatform {
6264
// Platform configurations where we expect to find a set of PowerShells
6365
let successTestCases: ITestPlatformSuccessCase[];
6466

67+
// Platform configurations for testing the powerShellAdditionalExePaths setting
68+
let additionalPowerShellExes: Record<string, string>;
69+
let successAdditionalTestCases: ITestPlatformSuccessCase[];
6570

6671
if (process.platform === "win32") {
6772
const msixAppDir = path.join(process.env.LOCALAPPDATA!, "Microsoft", "WindowsApps");
@@ -443,8 +448,105 @@ if (process.platform === "win32") {
443448
},
444449
},
445450
];
451+
452+
additionalPowerShellExes = {
453+
"pwsh": "C:\\Users\\test\\pwsh\\pwsh.exe",
454+
"pwsh-no-exe": "C:\\Users\\test\\pwsh\\pwsh",
455+
"pwsh-folder": "C:\\Users\\test\\pwsh\\",
456+
"pwsh-folder-no-slash": "C:\\Users\\test\\pwsh",
457+
"pwsh-single-quotes": "'C:\\Users\\test\\pwsh\\pwsh.exe'",
458+
"pwsh-double-quotes": "\"C:\\Users\\test\\pwsh\\pwsh.exe\"",
459+
};
460+
461+
successAdditionalTestCases = [
462+
{
463+
name: "Windows (Additional PowerShell Executables)",
464+
platformDetails: {
465+
operatingSystem: platform.OperatingSystem.Windows,
466+
isOS64Bit: true,
467+
isProcess64Bit: true,
468+
},
469+
environmentVars: {
470+
"USERPROFILE": "C:\\Users\\test",
471+
},
472+
expectedPowerShellSequence: [
473+
{
474+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
475+
displayName: "pwsh",
476+
supportsProperArguments: true
477+
},
478+
{
479+
exePath: "C:\\Users\\test\\pwsh\\pwsh",
480+
displayName: "pwsh-no-exe",
481+
supportsProperArguments: true
482+
},
483+
{
484+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
485+
displayName: "pwsh-no-exe",
486+
supportsProperArguments: true
487+
},
488+
{
489+
exePath: "C:\\Users\\test\\pwsh\\pwsh\\pwsh.exe",
490+
displayName: "pwsh-no-exe",
491+
supportsProperArguments: true
492+
},
493+
{
494+
exePath: "C:\\Users\\test\\pwsh\\pwsh\\powershell.exe",
495+
displayName: "pwsh-no-exe",
496+
supportsProperArguments: true
497+
},
498+
{
499+
exePath: "C:\\Users\\test\\pwsh\\",
500+
displayName: "pwsh-folder",
501+
supportsProperArguments: true
502+
},
503+
{
504+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
505+
displayName: "pwsh-folder",
506+
supportsProperArguments: true
507+
},
508+
{
509+
exePath: "C:\\Users\\test\\pwsh\\powershell.exe",
510+
displayName: "pwsh-folder",
511+
supportsProperArguments: true
512+
},
513+
{
514+
exePath: "C:\\Users\\test\\pwsh",
515+
displayName: "pwsh-folder-no-slash",
516+
supportsProperArguments: true
517+
},
518+
{
519+
exePath: "C:\\Users\\test\\pwsh.exe",
520+
displayName: "pwsh-folder-no-slash",
521+
supportsProperArguments: true
522+
},
523+
{
524+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
525+
displayName: "pwsh-folder-no-slash",
526+
supportsProperArguments: true
527+
},
528+
{
529+
exePath: "C:\\Users\\test\\pwsh\\powershell.exe",
530+
displayName: "pwsh-folder-no-slash",
531+
supportsProperArguments: true
532+
},
533+
{
534+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
535+
displayName: "pwsh-single-quotes",
536+
supportsProperArguments: true
537+
},
538+
{
539+
exePath: "C:\\Users\\test\\pwsh\\pwsh.exe",
540+
displayName: "pwsh-double-quotes",
541+
supportsProperArguments: true
542+
},
543+
],
544+
filesystem: {},
545+
}
546+
];
446547
} else {
447548
const pwshDailyDir = path.join(os.homedir(), ".powershell-daily");
549+
448550
successTestCases = [
449551
{
450552
name: "Linux (all installations)",
@@ -642,6 +744,66 @@ if (process.platform === "win32") {
642744
},
643745
},
644746
];
747+
748+
additionalPowerShellExes = {
749+
"pwsh": "/home/bin/pwsh",
750+
"pwsh-folder": "/home/bin/",
751+
"pwsh-folder-no-slash": "/home/bin",
752+
"pwsh-single-quotes": "'/home/bin/pwsh'",
753+
"pwsh-double-quotes": "\"/home/bin/pwsh\"",
754+
};
755+
756+
successAdditionalTestCases = [
757+
{ // Also sufficient for macOS as the behavior is the same
758+
name: "Linux (Additional PowerShell Executables)",
759+
platformDetails: {
760+
operatingSystem: platform.OperatingSystem.Linux,
761+
isOS64Bit: true,
762+
isProcess64Bit: true,
763+
},
764+
environmentVars: {
765+
"HOME": "/home/test",
766+
},
767+
expectedPowerShellSequence: [
768+
{
769+
exePath: "/home/bin/pwsh",
770+
displayName: "pwsh",
771+
supportsProperArguments: true
772+
},
773+
{
774+
exePath: "/home/bin/",
775+
displayName: "pwsh-folder",
776+
supportsProperArguments: true
777+
},
778+
{
779+
exePath: "/home/bin/pwsh",
780+
displayName: "pwsh-folder",
781+
supportsProperArguments: true
782+
},
783+
{
784+
exePath: "/home/bin",
785+
displayName: "pwsh-folder-no-slash",
786+
supportsProperArguments: true
787+
},
788+
{
789+
exePath: "/home/bin/pwsh",
790+
displayName: "pwsh-folder-no-slash",
791+
supportsProperArguments: true
792+
},
793+
{
794+
exePath: "/home/bin/pwsh",
795+
displayName: "pwsh-single-quotes",
796+
supportsProperArguments: true
797+
},
798+
{
799+
exePath: "/home/bin/pwsh",
800+
displayName: "pwsh-double-quotes",
801+
supportsProperArguments: true
802+
},
803+
],
804+
filesystem: {},
805+
}
806+
];
645807
}
646808

647809
const errorTestCases: ITestPlatform[] = [
@@ -846,4 +1008,28 @@ describe("Platform module", function () {
8461008
});
8471009
}
8481010
});
1011+
1012+
describe("PowerShell executables from 'powerShellAdditionalExePaths' are found", function () {
1013+
afterEach(function () {
1014+
sinon.restore();
1015+
mockFS.restore();
1016+
});
1017+
1018+
for (const testPlatform of successAdditionalTestCases) {
1019+
it(`Guesses for ${testPlatform.name}`, function () {
1020+
setupTestEnvironment(testPlatform);
1021+
1022+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails, additionalPowerShellExes);
1023+
1024+
let i = 0;
1025+
for (const additionalPwsh of powerShellExeFinder.enumerateAdditionalPowerShellInstallations()) {
1026+
const expectedPowerShell = testPlatform.expectedPowerShellSequence[i];
1027+
i++;
1028+
1029+
assert.strictEqual(additionalPwsh.exePath, expectedPowerShell.exePath);
1030+
assert.strictEqual(additionalPwsh.displayName, expectedPowerShell.displayName);
1031+
}
1032+
});
1033+
}
1034+
});
8491035
});

0 commit comments

Comments
 (0)