Skip to content

Commit

Permalink
Add unit tests to verify platform module behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
daviwil committed Sep 6, 2017
1 parent f2aa2a2 commit d4b8217
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 16 deletions.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
"Invoke-Build Build"
],
"problemMatcher": []
},
{
"taskName": "Test",
"suppressTaskName": true,
"args": [
"Invoke-Build Test"
],
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": []
}
]
}
34 changes: 21 additions & 13 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface PlatformDetails {
isProcess64Bit: boolean
}

interface PowerShellExeDetails {
export interface PowerShellExeDetails {
versionName: string;
exePath: string;
}
Expand Down Expand Up @@ -58,10 +58,18 @@ export function getDefaultPowerShellPath(
// Find the path to powershell.exe based on the current platform
// and the user's desire to run the x86 version of PowerShell
if (platformDetails.operatingSystem == OperatingSystem.Windows) {
powerShellExePath =
use32Bit || !process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')
? System32PowerShellPath
: SysnativePowerShellPath
if (use32Bit) {
powerShellExePath =
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
? SysWow64PowerShellPath
: System32PowerShellPath
}
else {
powerShellExePath =
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
? System32PowerShellPath
: SysnativePowerShellPath
}
}
else if (platformDetails.operatingSystem == OperatingSystem.MacOS) {
powerShellExePath = "/usr/local/bin/powershell";
Expand All @@ -81,6 +89,9 @@ export const System32PowerShellPath = getWindowsSystemPowerShellPath('System32')
export const SysnativePowerShellPath = getWindowsSystemPowerShellPath('Sysnative');
export const SysWow64PowerShellPath = getWindowsSystemPowerShellPath('SysWow64');

export const WindowsPowerShell64BitLabel = "Windows PowerShell (x64)";
export const WindowsPowerShell32BitLabel = "Windows PowerShell (x86)";

const powerShell64BitPathOn32Bit = SysnativePowerShellPath.toLocaleLowerCase();
const powerShell32BitPathOn64Bit = SysWow64PowerShellPath.toLocaleLowerCase();

Expand All @@ -96,38 +107,35 @@ export function fixWindowsPowerShellPath(powerShellExePath: string, platformDeta
return powerShellExePath;
}

export function getPowerShellExeItems(platformDetails: PlatformDetails): PowerShellExeDetails[] {
export function getAvailablePowerShellExes(platformDetails: PlatformDetails): PowerShellExeDetails[] {

var paths: PowerShellExeDetails[] = [];

const windowsPowerShell64BitLabel = "Windows PowerShell (x64)";
const windowsPowerShell32BitLabel = "Windows PowerShell (x86)";

if (platformDetails.operatingSystem === OperatingSystem.Windows) {
const psCoreInstallPath =
(!platformDetails.isProcess64Bit ? process.env.ProgramW6432 : process.env.ProgramFiles) + '\\PowerShell';

if (platformDetails.isProcess64Bit) {
paths.push({
versionName: windowsPowerShell64BitLabel,
versionName: WindowsPowerShell64BitLabel,
exePath: System32PowerShellPath
})

paths.push({
versionName: windowsPowerShell32BitLabel,
versionName: WindowsPowerShell32BitLabel,
exePath: SysWow64PowerShellPath
})
}
else {
if (platformDetails.isOS64Bit) {
paths.push({
versionName: windowsPowerShell64BitLabel,
versionName: WindowsPowerShell64BitLabel,
exePath: SysnativePowerShellPath
})
}

paths.push({
versionName: windowsPowerShell32BitLabel,
versionName: WindowsPowerShell32BitLabel,
exePath: System32PowerShellPath
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import {
OperatingSystem, PlatformDetails, getDefaultPowerShellPath,
getPlatformDetails, fixWindowsPowerShellPath,
getPowerShellExeItems } from './platform';
getAvailablePowerShellExes } from './platform';

export enum SessionStatus {
NotStarted,
Expand Down Expand Up @@ -657,7 +657,7 @@ export class SessionManager implements Middleware {

var currentExePath = this.powerShellExePath.toLowerCase();
var powerShellItems =
getPowerShellExeItems(this.platformDetails)
getAvailablePowerShellExes(this.platformDetails)
.filter(item => item.exePath.toLowerCase() !== currentExePath)
.map(item => {
return new SessionMenuItem(
Expand Down
121 changes: 121 additions & 0 deletions test/platform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import * as assert from 'assert';
import * as vscode from 'vscode';
import * as platform from '../src/platform';

function checkDefaultPowerShellPath(platformDetails, expectedPath) {
test("returns correct default path", () => {
assert.equal(
platform.getDefaultPowerShellPath(platformDetails),
expectedPath);
});
}

function checkAvailableWindowsPowerShellPaths(
platformDetails: platform.PlatformDetails,
expectedPaths: platform.PowerShellExeDetails[]) {
test("correctly enumerates available Windows PowerShell paths", () => {

// The system may return PowerShell Core paths so only
// enumerate the first list items.
let enumeratedPaths = platform.getAvailablePowerShellExes(platformDetails);
for (var i; i < expectedPaths.length; i++) {
assert.equal(enumeratedPaths[i], expectedPaths[i]);
}
});
}

function checkFixedWindowsPowerShellpath(platformDetails, inputPath, expectedPath) {
test("fixes incorrect Windows PowerShell Sys* path", () => {
assert.equal(
platform.fixWindowsPowerShellPath(inputPath, platformDetails),
expectedPath);
});
}

suite("Platform module", () => {

suite("64-bit Windows, 64-bit VS Code", () => {
let platformDetails: platform.PlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: true,
isProcess64Bit: true
};

checkDefaultPowerShellPath(
platformDetails,
platform.System32PowerShellPath);

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell64BitLabel,
exePath: platform.System32PowerShellPath
},
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.SysWow64PowerShellPath
}
]);

checkFixedWindowsPowerShellpath(
platformDetails,
platform.SysnativePowerShellPath,
platform.System32PowerShellPath);
});

suite("64-bit Windows, 32-bit VS Code", () => {
let platformDetails: platform.PlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: true,
isProcess64Bit: false
};

checkDefaultPowerShellPath(
platformDetails,
platform.SysnativePowerShellPath);

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell64BitLabel,
exePath: platform.SysnativePowerShellPath
},
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.System32PowerShellPath
}
]);

checkFixedWindowsPowerShellpath(
platformDetails,
platform.SysWow64PowerShellPath,
platform.System32PowerShellPath);
});

suite("32-bit Windows, 32-bit VS Code", () => {
let platformDetails: platform.PlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: false,
isProcess64Bit: false
};

checkDefaultPowerShellPath(
platformDetails,
platform.System32PowerShellPath);

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.System32PowerShellPath
}
]);
});
});
7 changes: 6 additions & 1 deletion vscode-powershell.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ task BuildEditorServices {

task BuildAll BuildEditorServices, Build -Before Package

task Test Build, {
Write-Host "`n### Running extension tests" -ForegroundColor Green
exec { & npm run test }
}

task Package {

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

# The default task is to run the entire CI build
task . GetExtensionVersion, CleanAll, BuildAll, Package, UploadArtifacts
task . GetExtensionVersion, CleanAll, BuildAll, Test, Package, UploadArtifacts

0 comments on commit d4b8217

Please sign in to comment.