@@ -5,8 +5,29 @@ import * as assert from "assert";
55import mockFS = require( "mock-fs" ) ;
66import FileSystem = require( "mock-fs/lib/filesystem" ) ;
77import * as path from "path" ;
8+ import rewire = require( "rewire" ) ;
89import * as sinon from "sinon" ;
910import * as platform from "../../src/platform" ;
11+ import * as fs from "fs" ;
12+ import * as vscode from "vscode" ;
13+
14+ // We have to rewire the platform module so that mock-fs can be used, as it
15+ // overrides the fs module but not the vscode.workspace.fs module.
16+ const platformMock = rewire ( "../../src/platform" ) ;
17+
18+ async function fakeCheckIfFileOrDirectoryExists ( targetPath : string | vscode . Uri ) : Promise < boolean > {
19+ try {
20+ fs . lstatSync ( targetPath instanceof vscode . Uri ? targetPath . fsPath : targetPath ) ;
21+ return true ;
22+ } catch {
23+ return false ;
24+ }
25+ }
26+ const utilsMock = {
27+ checkIfFileExists : fakeCheckIfFileOrDirectoryExists ,
28+ checkIfDirectoryExists : fakeCheckIfFileOrDirectoryExists
29+ }
30+ platformMock . __set__ ( "utils" , utilsMock ) ;
1031
1132/**
1233 * Describes a platform on which the PowerShell extension should work,
@@ -469,7 +490,7 @@ function setupTestEnvironment(testPlatform: ITestPlatform) {
469490
470491describe ( "Platform module" , function ( ) {
471492 it ( "Gets the correct platform details" , function ( ) {
472- const platformDetails : platform . IPlatformDetails = platform . getPlatformDetails ( ) ;
493+ const platformDetails : platform . IPlatformDetails = platformMock . getPlatformDetails ( ) ;
473494 switch ( process . platform ) {
474495 case "darwin" :
475496 assert . strictEqual (
@@ -521,7 +542,7 @@ describe("Platform module", function () {
521542 }
522543 } ) ;
523544
524- describe ( "Default PowerShell installation" , async function ( ) {
545+ describe ( "Default PowerShell installation" , function ( ) {
525546 afterEach ( function ( ) {
526547 sinon . restore ( ) ;
527548 mockFS . restore ( ) ;
@@ -531,7 +552,7 @@ describe("Platform module", function () {
531552 it ( `Finds it on ${ testPlatform . name } ` , async function ( ) {
532553 setupTestEnvironment ( testPlatform ) ;
533554
534- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
555+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
535556
536557 const defaultPowerShell = await powerShellExeFinder . getFirstAvailablePowerShellInstallation ( ) ;
537558 const expectedPowerShell = testPlatform . expectedPowerShellSequence [ 0 ] ;
@@ -545,7 +566,7 @@ describe("Platform module", function () {
545566 it ( `Fails gracefully on ${ testPlatform . name } ` , async function ( ) {
546567 setupTestEnvironment ( testPlatform ) ;
547568
548- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
569+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
549570
550571 const defaultPowerShell = await powerShellExeFinder . getFirstAvailablePowerShellInstallation ( ) ;
551572 assert . strictEqual ( defaultPowerShell , undefined ) ;
@@ -563,7 +584,7 @@ describe("Platform module", function () {
563584 it ( `Finds them on ${ testPlatform . name } ` , async function ( ) {
564585 setupTestEnvironment ( testPlatform ) ;
565586
566- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
587+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
567588
568589 const foundPowerShells = await powerShellExeFinder . getAllAvailablePowerShellInstallations ( ) ;
569590
@@ -586,7 +607,7 @@ describe("Platform module", function () {
586607 it ( `Fails gracefully on ${ testPlatform . name } ` , async function ( ) {
587608 setupTestEnvironment ( testPlatform ) ;
588609
589- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
610+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
590611
591612 const foundPowerShells = await powerShellExeFinder . getAllAvailablePowerShellInstallations ( ) ;
592613 assert . strictEqual ( foundPowerShells . length , 0 ) ;
@@ -626,7 +647,7 @@ describe("Platform module", function () {
626647 altWinPSPath = null ;
627648 }
628649
629- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
650+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
630651
631652 assert . strictEqual ( powerShellExeFinder . fixWindowsPowerShellPath ( winPSPath ) , winPSPath ) ;
632653
0 commit comments