6
6
import assert from 'assert'
7
7
import * as vscode from 'vscode'
8
8
import * as path from 'path'
9
- import { existsSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'fs'
9
+ import { existsSync , mkdirSync , promises as fsPromises , readFileSync , rmSync , writeFileSync } from 'fs'
10
10
import { FakeExtensionContext } from '../fakeExtensionContext'
11
11
import { fsCommon } from '../../srcShared/fs'
12
12
import * as os from 'os'
13
13
import { isMinimumVersion } from '../../shared/vscode/env'
14
+ import Sinon from 'sinon'
15
+ import * as extensionUtilities from '../../shared/extensionUtilities'
14
16
15
17
function isWin ( ) {
16
18
return os . platform ( ) === 'win32'
17
19
}
18
20
19
21
describe ( 'FileSystem' , function ( ) {
20
22
let fakeContext : vscode . ExtensionContext
23
+ let sandbox : Sinon . SinonSandbox
21
24
22
25
before ( async function ( ) {
23
26
fakeContext = await FakeExtensionContext . create ( )
24
27
} )
25
28
26
29
beforeEach ( async function ( ) {
27
30
await makeTestRoot ( )
31
+ sandbox = Sinon . createSandbox ( )
28
32
} )
29
33
30
34
afterEach ( async function ( ) {
31
35
await deleteTestRoot ( )
36
+ sandbox . restore ( )
32
37
} )
33
38
34
39
describe ( 'readFileAsString()' , function ( ) {
@@ -153,12 +158,25 @@ describe('FileSystem', function () {
153
158
const paths = [ 'a' , 'a/b' , 'a/b/c' , 'a/b/c/d/' ]
154
159
155
160
paths . forEach ( async function ( p ) {
156
- it ( `creates path : '${ p } '` , async function ( ) {
161
+ it ( `creates folder : '${ p } '` , async function ( ) {
157
162
const dirPath = createTestPath ( p )
158
163
await fsCommon . mkdir ( dirPath )
159
164
assert . ok ( existsSync ( dirPath ) )
160
165
} )
161
166
} )
167
+
168
+ paths . forEach ( async function ( p ) {
169
+ it ( `creates folder but uses the "fs" module if in C9: '${ p } '` , async function ( ) {
170
+ sandbox . stub ( extensionUtilities , 'isCloud9' ) . returns ( true )
171
+ const mkdirSpy = sandbox . spy ( fsPromises , 'mkdir' )
172
+ const dirPath = createTestPath ( p )
173
+
174
+ await fsCommon . mkdir ( dirPath )
175
+
176
+ assert . ok ( existsSync ( dirPath ) )
177
+ assert . ok ( mkdirSpy . calledOnce )
178
+ } )
179
+ } )
162
180
} )
163
181
164
182
describe ( 'readdir()' , function ( ) {
@@ -192,6 +210,32 @@ describe('FileSystem', function () {
192
210
function sorted ( i : [ string , vscode . FileType ] [ ] ) {
193
211
return i . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) )
194
212
}
213
+
214
+ it ( 'uses the "fs" readdir implementation if in C9' , async function ( ) {
215
+ sandbox . stub ( extensionUtilities , 'isCloud9' ) . returns ( true )
216
+ const readdirSpy = sandbox . spy ( fsPromises , 'readdir' )
217
+
218
+ await makeFile ( 'a.txt' )
219
+ await makeFile ( 'b.txt' )
220
+ await makeFile ( 'c.txt' )
221
+ mkdirSync ( createTestPath ( 'dirA' ) )
222
+ mkdirSync ( createTestPath ( 'dirB' ) )
223
+ mkdirSync ( createTestPath ( 'dirC' ) )
224
+
225
+ const files = await fsCommon . readdir ( testRootPath ( ) )
226
+ assert . deepStrictEqual (
227
+ sorted ( files ) ,
228
+ sorted ( [
229
+ [ 'a.txt' , vscode . FileType . File ] ,
230
+ [ 'b.txt' , vscode . FileType . File ] ,
231
+ [ 'c.txt' , vscode . FileType . File ] ,
232
+ [ 'dirA' , vscode . FileType . Directory ] ,
233
+ [ 'dirB' , vscode . FileType . Directory ] ,
234
+ [ 'dirC' , vscode . FileType . Directory ] ,
235
+ ] )
236
+ )
237
+ assert . ok ( readdirSpy . calledOnce )
238
+ } )
195
239
} )
196
240
197
241
describe ( 'delete()' , function ( ) {
@@ -209,6 +253,20 @@ describe('FileSystem', function () {
209
253
210
254
assert . ok ( ! existsSync ( dirPath ) )
211
255
} )
256
+
257
+ it ( 'uses the "fs" rm method if in C9' , async function ( ) {
258
+ sandbox . stub ( extensionUtilities , 'isCloud9' ) . returns ( true )
259
+ const rmdirSpy = sandbox . spy ( fsPromises , 'rm' )
260
+ // Folder with subfolders
261
+ const dirPath = await makeFolder ( 'a/b/deleteMe' )
262
+
263
+ mkdirSync ( dirPath , { recursive : true } )
264
+
265
+ await fsCommon . delete ( dirPath )
266
+
267
+ assert . ok ( ! existsSync ( dirPath ) )
268
+ assert . ok ( rmdirSpy . calledOnce )
269
+ } )
212
270
} )
213
271
214
272
describe ( 'stat()' , function ( ) {
0 commit comments