Skip to content

Commit d4b8217

Browse files
committed
Add unit tests to verify platform module behavior
1 parent f2aa2a2 commit d4b8217

File tree

5 files changed

+162
-16
lines changed

5 files changed

+162
-16
lines changed

.vscode/tasks.json

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@
5252
"Invoke-Build Build"
5353
],
5454
"problemMatcher": []
55+
},
56+
{
57+
"taskName": "Test",
58+
"suppressTaskName": true,
59+
"args": [
60+
"Invoke-Build Test"
61+
],
62+
"group": {
63+
"kind": "test",
64+
"isDefault": true
65+
},
66+
"problemMatcher": []
5567
}
5668
]
5769
}

src/platform.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface PlatformDetails {
2222
isProcess64Bit: boolean
2323
}
2424

25-
interface PowerShellExeDetails {
25+
export interface PowerShellExeDetails {
2626
versionName: string;
2727
exePath: string;
2828
}
@@ -58,10 +58,18 @@ export function getDefaultPowerShellPath(
5858
// Find the path to powershell.exe based on the current platform
5959
// and the user's desire to run the x86 version of PowerShell
6060
if (platformDetails.operatingSystem == OperatingSystem.Windows) {
61-
powerShellExePath =
62-
use32Bit || !process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')
63-
? System32PowerShellPath
64-
: SysnativePowerShellPath
61+
if (use32Bit) {
62+
powerShellExePath =
63+
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
64+
? SysWow64PowerShellPath
65+
: System32PowerShellPath
66+
}
67+
else {
68+
powerShellExePath =
69+
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
70+
? System32PowerShellPath
71+
: SysnativePowerShellPath
72+
}
6573
}
6674
else if (platformDetails.operatingSystem == OperatingSystem.MacOS) {
6775
powerShellExePath = "/usr/local/bin/powershell";
@@ -81,6 +89,9 @@ export const System32PowerShellPath = getWindowsSystemPowerShellPath('System32')
8189
export const SysnativePowerShellPath = getWindowsSystemPowerShellPath('Sysnative');
8290
export const SysWow64PowerShellPath = getWindowsSystemPowerShellPath('SysWow64');
8391

92+
export const WindowsPowerShell64BitLabel = "Windows PowerShell (x64)";
93+
export const WindowsPowerShell32BitLabel = "Windows PowerShell (x86)";
94+
8495
const powerShell64BitPathOn32Bit = SysnativePowerShellPath.toLocaleLowerCase();
8596
const powerShell32BitPathOn64Bit = SysWow64PowerShellPath.toLocaleLowerCase();
8697

@@ -96,38 +107,35 @@ export function fixWindowsPowerShellPath(powerShellExePath: string, platformDeta
96107
return powerShellExePath;
97108
}
98109

99-
export function getPowerShellExeItems(platformDetails: PlatformDetails): PowerShellExeDetails[] {
110+
export function getAvailablePowerShellExes(platformDetails: PlatformDetails): PowerShellExeDetails[] {
100111

101112
var paths: PowerShellExeDetails[] = [];
102113

103-
const windowsPowerShell64BitLabel = "Windows PowerShell (x64)";
104-
const windowsPowerShell32BitLabel = "Windows PowerShell (x86)";
105-
106114
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
107115
const psCoreInstallPath =
108116
(!platformDetails.isProcess64Bit ? process.env.ProgramW6432 : process.env.ProgramFiles) + '\\PowerShell';
109117

110118
if (platformDetails.isProcess64Bit) {
111119
paths.push({
112-
versionName: windowsPowerShell64BitLabel,
120+
versionName: WindowsPowerShell64BitLabel,
113121
exePath: System32PowerShellPath
114122
})
115123

116124
paths.push({
117-
versionName: windowsPowerShell32BitLabel,
125+
versionName: WindowsPowerShell32BitLabel,
118126
exePath: SysWow64PowerShellPath
119127
})
120128
}
121129
else {
122130
if (platformDetails.isOS64Bit) {
123131
paths.push({
124-
versionName: windowsPowerShell64BitLabel,
132+
versionName: WindowsPowerShell64BitLabel,
125133
exePath: SysnativePowerShellPath
126134
})
127135
}
128136

129137
paths.push({
130-
versionName: windowsPowerShell32BitLabel,
138+
versionName: WindowsPowerShell32BitLabel,
131139
exePath: System32PowerShellPath
132140
})
133141
}

src/session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import {
2727
OperatingSystem, PlatformDetails, getDefaultPowerShellPath,
2828
getPlatformDetails, fixWindowsPowerShellPath,
29-
getPowerShellExeItems } from './platform';
29+
getAvailablePowerShellExes } from './platform';
3030

3131
export enum SessionStatus {
3232
NotStarted,
@@ -657,7 +657,7 @@ export class SessionManager implements Middleware {
657657

658658
var currentExePath = this.powerShellExePath.toLowerCase();
659659
var powerShellItems =
660-
getPowerShellExeItems(this.platformDetails)
660+
getAvailablePowerShellExes(this.platformDetails)
661661
.filter(item => item.exePath.toLowerCase() !== currentExePath)
662662
.map(item => {
663663
return new SessionMenuItem(

test/platform.test.ts

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import * as assert from 'assert';
6+
import * as vscode from 'vscode';
7+
import * as platform from '../src/platform';
8+
9+
function checkDefaultPowerShellPath(platformDetails, expectedPath) {
10+
test("returns correct default path", () => {
11+
assert.equal(
12+
platform.getDefaultPowerShellPath(platformDetails),
13+
expectedPath);
14+
});
15+
}
16+
17+
function checkAvailableWindowsPowerShellPaths(
18+
platformDetails: platform.PlatformDetails,
19+
expectedPaths: platform.PowerShellExeDetails[]) {
20+
test("correctly enumerates available Windows PowerShell paths", () => {
21+
22+
// The system may return PowerShell Core paths so only
23+
// enumerate the first list items.
24+
let enumeratedPaths = platform.getAvailablePowerShellExes(platformDetails);
25+
for (var i; i < expectedPaths.length; i++) {
26+
assert.equal(enumeratedPaths[i], expectedPaths[i]);
27+
}
28+
});
29+
}
30+
31+
function checkFixedWindowsPowerShellpath(platformDetails, inputPath, expectedPath) {
32+
test("fixes incorrect Windows PowerShell Sys* path", () => {
33+
assert.equal(
34+
platform.fixWindowsPowerShellPath(inputPath, platformDetails),
35+
expectedPath);
36+
});
37+
}
38+
39+
suite("Platform module", () => {
40+
41+
suite("64-bit Windows, 64-bit VS Code", () => {
42+
let platformDetails: platform.PlatformDetails = {
43+
operatingSystem: platform.OperatingSystem.Windows,
44+
isOS64Bit: true,
45+
isProcess64Bit: true
46+
};
47+
48+
checkDefaultPowerShellPath(
49+
platformDetails,
50+
platform.System32PowerShellPath);
51+
52+
checkAvailableWindowsPowerShellPaths(
53+
platformDetails,
54+
[
55+
{
56+
versionName: platform.WindowsPowerShell64BitLabel,
57+
exePath: platform.System32PowerShellPath
58+
},
59+
{
60+
versionName: platform.WindowsPowerShell32BitLabel,
61+
exePath: platform.SysWow64PowerShellPath
62+
}
63+
]);
64+
65+
checkFixedWindowsPowerShellpath(
66+
platformDetails,
67+
platform.SysnativePowerShellPath,
68+
platform.System32PowerShellPath);
69+
});
70+
71+
suite("64-bit Windows, 32-bit VS Code", () => {
72+
let platformDetails: platform.PlatformDetails = {
73+
operatingSystem: platform.OperatingSystem.Windows,
74+
isOS64Bit: true,
75+
isProcess64Bit: false
76+
};
77+
78+
checkDefaultPowerShellPath(
79+
platformDetails,
80+
platform.SysnativePowerShellPath);
81+
82+
checkAvailableWindowsPowerShellPaths(
83+
platformDetails,
84+
[
85+
{
86+
versionName: platform.WindowsPowerShell64BitLabel,
87+
exePath: platform.SysnativePowerShellPath
88+
},
89+
{
90+
versionName: platform.WindowsPowerShell32BitLabel,
91+
exePath: platform.System32PowerShellPath
92+
}
93+
]);
94+
95+
checkFixedWindowsPowerShellpath(
96+
platformDetails,
97+
platform.SysWow64PowerShellPath,
98+
platform.System32PowerShellPath);
99+
});
100+
101+
suite("32-bit Windows, 32-bit VS Code", () => {
102+
let platformDetails: platform.PlatformDetails = {
103+
operatingSystem: platform.OperatingSystem.Windows,
104+
isOS64Bit: false,
105+
isProcess64Bit: false
106+
};
107+
108+
checkDefaultPowerShellPath(
109+
platformDetails,
110+
platform.System32PowerShellPath);
111+
112+
checkAvailableWindowsPowerShellPaths(
113+
platformDetails,
114+
[
115+
{
116+
versionName: platform.WindowsPowerShell32BitLabel,
117+
exePath: platform.System32PowerShellPath
118+
}
119+
]);
120+
});
121+
});

vscode-powershell.build.ps1

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ task BuildEditorServices {
9292

9393
task BuildAll BuildEditorServices, Build -Before Package
9494

95+
task Test Build, {
96+
Write-Host "`n### Running extension tests" -ForegroundColor Green
97+
exec { & npm run test }
98+
}
99+
95100
task Package {
96101

97102
if ($script:psesBuildScriptPath) {
@@ -112,4 +117,4 @@ task UploadArtifacts -If { $env:AppVeyor } {
112117
}
113118

114119
# The default task is to run the entire CI build
115-
task . GetExtensionVersion, CleanAll, BuildAll, Package, UploadArtifacts
120+
task . GetExtensionVersion, CleanAll, BuildAll, Test, Package, UploadArtifacts

0 commit comments

Comments
 (0)