@@ -5,8 +5,29 @@ import * as assert from "assert";
5
5
import mockFS = require( "mock-fs" ) ;
6
6
import FileSystem = require( "mock-fs/lib/filesystem" ) ;
7
7
import * as path from "path" ;
8
+ import rewire = require( "rewire" ) ;
8
9
import * as sinon from "sinon" ;
9
10
import * 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 ) ;
10
31
11
32
/**
12
33
* Describes a platform on which the PowerShell extension should work,
@@ -469,7 +490,7 @@ function setupTestEnvironment(testPlatform: ITestPlatform) {
469
490
470
491
describe ( "Platform module" , function ( ) {
471
492
it ( "Gets the correct platform details" , function ( ) {
472
- const platformDetails : platform . IPlatformDetails = platform . getPlatformDetails ( ) ;
493
+ const platformDetails : platform . IPlatformDetails = platformMock . getPlatformDetails ( ) ;
473
494
switch ( process . platform ) {
474
495
case "darwin" :
475
496
assert . strictEqual (
@@ -521,7 +542,7 @@ describe("Platform module", function () {
521
542
}
522
543
} ) ;
523
544
524
- describe ( "Default PowerShell installation" , async function ( ) {
545
+ describe ( "Default PowerShell installation" , function ( ) {
525
546
afterEach ( function ( ) {
526
547
sinon . restore ( ) ;
527
548
mockFS . restore ( ) ;
@@ -531,7 +552,7 @@ describe("Platform module", function () {
531
552
it ( `Finds it on ${ testPlatform . name } ` , async function ( ) {
532
553
setupTestEnvironment ( testPlatform ) ;
533
554
534
- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
555
+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
535
556
536
557
const defaultPowerShell = await powerShellExeFinder . getFirstAvailablePowerShellInstallation ( ) ;
537
558
const expectedPowerShell = testPlatform . expectedPowerShellSequence [ 0 ] ;
@@ -545,7 +566,7 @@ describe("Platform module", function () {
545
566
it ( `Fails gracefully on ${ testPlatform . name } ` , async function ( ) {
546
567
setupTestEnvironment ( testPlatform ) ;
547
568
548
- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
569
+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
549
570
550
571
const defaultPowerShell = await powerShellExeFinder . getFirstAvailablePowerShellInstallation ( ) ;
551
572
assert . strictEqual ( defaultPowerShell , undefined ) ;
@@ -563,7 +584,7 @@ describe("Platform module", function () {
563
584
it ( `Finds them on ${ testPlatform . name } ` , async function ( ) {
564
585
setupTestEnvironment ( testPlatform ) ;
565
586
566
- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
587
+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
567
588
568
589
const foundPowerShells = await powerShellExeFinder . getAllAvailablePowerShellInstallations ( ) ;
569
590
@@ -586,7 +607,7 @@ describe("Platform module", function () {
586
607
it ( `Fails gracefully on ${ testPlatform . name } ` , async function ( ) {
587
608
setupTestEnvironment ( testPlatform ) ;
588
609
589
- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
610
+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
590
611
591
612
const foundPowerShells = await powerShellExeFinder . getAllAvailablePowerShellInstallations ( ) ;
592
613
assert . strictEqual ( foundPowerShells . length , 0 ) ;
@@ -626,7 +647,7 @@ describe("Platform module", function () {
626
647
altWinPSPath = null ;
627
648
}
628
649
629
- const powerShellExeFinder = new platform . PowerShellExeFinder ( testPlatform . platformDetails ) ;
650
+ const powerShellExeFinder = new platformMock . PowerShellExeFinder ( testPlatform . platformDetails ) ;
630
651
631
652
assert . strictEqual ( powerShellExeFinder . fixWindowsPowerShellPath ( winPSPath ) , winPSPath ) ;
632
653
0 commit comments