4
4
import { expect , use } from 'chai' ;
5
5
import * as fs from 'fs-extra' ;
6
6
import * as path from 'path' ;
7
- import * as TypeMoq from 'typemoq' ;
8
7
import { FileSystem } from '../../../client/common/platform/fileSystem' ;
9
- import { IFileSystem , IPlatformService , TemporaryFile } from '../../../client/common/platform/types' ;
8
+ import { TemporaryFile } from '../../../client/common/platform/types' ;
10
9
// tslint:disable:no-require-imports no-var-requires
11
10
const assertArrays = require ( 'chai-arrays' ) ;
12
11
use ( require ( 'chai-as-promised' ) ) ;
13
12
use ( assertArrays ) ;
14
13
15
14
// tslint:disable-next-line:max-func-body-length
16
15
suite ( 'FileSystem' , ( ) => {
17
- let platformService : TypeMoq . IMock < IPlatformService > ;
18
- let fileSystem : IFileSystem ;
19
16
const fileToAppendTo = path . join ( __dirname , 'created_for_testing_dummy.txt' ) ;
20
17
setup ( ( ) => {
21
- platformService = TypeMoq . Mock . ofType < IPlatformService > ( ) ;
22
- fileSystem = new FileSystem ( platformService . object ) ;
23
- cleanTestFiles ( ) ;
18
+ cleanTestFiles ( ) ; // This smells like functional testing...
24
19
} ) ;
25
20
teardown ( cleanTestFiles ) ;
26
21
function cleanTestFiles ( ) {
27
22
if ( fs . existsSync ( fileToAppendTo ) ) {
28
23
fs . unlinkSync ( fileToAppendTo ) ;
29
24
}
30
25
}
26
+
31
27
test ( 'ReadFile returns contents of a file' , async ( ) => {
32
28
const file = __filename ;
29
+ const filesystem = new FileSystem ( ) ;
33
30
const expectedContents = await fs . readFile ( file ) . then ( buffer => buffer . toString ( ) ) ;
34
- const content = await fileSystem . readFile ( file ) ;
31
+
32
+ const content = await filesystem . readFile ( file ) ;
35
33
36
34
expect ( content ) . to . be . equal ( expectedContents ) ;
37
35
} ) ;
38
36
39
37
test ( 'ReadFile throws an exception if file does not exist' , async ( ) => {
40
- const readPromise = fs . readFile ( 'xyz' , { encoding : 'utf8' } ) ;
38
+ const filesystem = new FileSystem ( ) ;
39
+
40
+ const readPromise = filesystem . readFile ( 'xyz' ) ;
41
+
41
42
await expect ( readPromise ) . to . be . rejectedWith ( ) ;
42
43
} ) ;
43
44
44
- function caseSensitivityFileCheck ( isWindows : boolean , isOsx : boolean , isLinux : boolean ) {
45
- platformService . setup ( p => p . isWindows ) . returns ( ( ) => isWindows ) ;
46
- platformService . setup ( p => p . isMac ) . returns ( ( ) => isOsx ) ;
47
- platformService . setup ( p => p . isLinux ) . returns ( ( ) => isLinux ) ;
45
+ suite ( 'Case sensitivity' , ( ) => {
48
46
const path1 = 'c:\\users\\Peter Smith\\my documents\\test.txt' ;
49
47
const path2 = 'c:\\USERS\\Peter Smith\\my documents\\test.TXT' ;
50
48
const path3 = 'c:\\USERS\\Peter Smith\\my documents\\test.exe' ;
51
49
52
- if ( isWindows ) {
53
- expect ( fileSystem . arePathsSame ( path1 , path2 ) ) . to . be . equal ( true , 'file paths do not match (windows)' ) ;
54
- } else {
55
- expect ( fileSystem . arePathsSame ( path1 , path2 ) ) . to . be . equal ( false , 'file match (non windows)' ) ;
56
- }
50
+ test ( 'Case sensitivity is ignored when comparing file names on windows' , ( ) => {
51
+ const isWindows = true ;
52
+ const filesystem = new FileSystem ( isWindows ) ;
57
53
58
- expect ( fileSystem . arePathsSame ( path1 , path1 ) ) . to . be . equal ( true , '1. file paths do not match' ) ;
59
- expect ( fileSystem . arePathsSame ( path2 , path2 ) ) . to . be . equal ( true , '2. file paths do not match' ) ;
60
- expect ( fileSystem . arePathsSame ( path1 , path3 ) ) . to . be . equal ( false , '2. file paths do not match' ) ;
61
- }
54
+ const same12 = filesystem . arePathsSame ( path1 , path2 ) ;
55
+ const same11 = filesystem . arePathsSame ( path1 , path1 ) ;
56
+ const same22 = filesystem . arePathsSame ( path2 , path2 ) ;
57
+ const same13 = filesystem . arePathsSame ( path1 , path3 ) ;
62
58
63
- test ( 'Case sensitivity is ignored when comparing file names on windows' , async ( ) => {
64
- caseSensitivityFileCheck ( true , false , false ) ;
65
- } ) ;
59
+ expect ( same12 ) . to . be . equal ( true , 'file paths do not match (windows)' ) ;
60
+ expect ( same11 ) . to . be . equal ( true , '1. file paths do not match' ) ;
61
+ expect ( same22 ) . to . be . equal ( true , '2. file paths do not match' ) ;
62
+ expect ( same13 ) . to . be . equal ( false , '2. file paths do not match' ) ;
63
+ } ) ;
66
64
67
- test ( 'Case sensitivity is not ignored when comparing file names on osx' , async ( ) => {
68
- caseSensitivityFileCheck ( false , true , false ) ;
69
- } ) ;
65
+ test ( 'Case sensitivity is not ignored when comparing file names on non-Windows' , ( ) => {
66
+ const isWindows = false ;
67
+ const filesystem = new FileSystem ( isWindows ) ;
68
+
69
+ const same12 = filesystem . arePathsSame ( path1 , path2 ) ;
70
+ const same11 = filesystem . arePathsSame ( path1 , path1 ) ;
71
+ const same22 = filesystem . arePathsSame ( path2 , path2 ) ;
72
+ const same13 = filesystem . arePathsSame ( path1 , path3 ) ;
70
73
71
- test ( 'Case sensitivity is not ignored when comparing file names on linux' , async ( ) => {
72
- caseSensitivityFileCheck ( false , false , true ) ;
74
+ expect ( same12 ) . to . be . equal ( false , 'file match (non windows)' ) ;
75
+ expect ( same11 ) . to . be . equal ( true , '1. file paths do not match' ) ;
76
+ expect ( same22 ) . to . be . equal ( true , '2. file paths do not match' ) ;
77
+ expect ( same13 ) . to . be . equal ( false , '2. file paths do not match' ) ;
78
+ } ) ;
73
79
} ) ;
80
+
74
81
test ( 'Check existence of files synchronously' , async ( ) => {
75
- expect ( fileSystem . fileExistsSync ( __filename ) ) . to . be . equal ( true , 'file not found' ) ;
82
+ const filesystem = new FileSystem ( ) ;
83
+
84
+ expect ( filesystem . fileExistsSync ( __filename ) ) . to . be . equal ( true , 'file not found' ) ;
76
85
} ) ;
77
86
78
87
test ( 'Test searching for files' , async ( ) => {
79
88
const searchPattern = `${ path . basename ( __filename , __filename . substring ( __filename . length - 3 ) ) } .*` ;
80
- const files = await fileSystem . search ( path . join ( __dirname , searchPattern ) ) ;
89
+ const filesystem = new FileSystem ( ) ;
90
+
91
+ const files = await filesystem . search ( path . join ( __dirname , searchPattern ) ) ;
92
+
81
93
expect ( files ) . to . be . array ( ) ;
82
94
expect ( files . length ) . to . be . at . least ( 1 ) ;
83
95
const expectedFileName = __filename . replace ( / \\ / g, '/' ) ;
84
96
const fileName = files [ 0 ] . replace ( / \\ / g, '/' ) ;
85
97
expect ( fileName ) . to . equal ( expectedFileName ) ;
86
98
} ) ;
99
+
87
100
test ( 'Ensure creating a temporary file results in a unique temp file path' , async ( ) => {
88
- const tempFile = await fileSystem . createTemporaryFile ( '.tmp' ) ;
89
- const tempFile2 = await fileSystem . createTemporaryFile ( '.tmp' ) ;
101
+ const filesystem = new FileSystem ( ) ;
102
+
103
+ const tempFile = await filesystem . createTemporaryFile ( '.tmp' ) ;
104
+ const tempFile2 = await filesystem . createTemporaryFile ( '.tmp' ) ;
105
+
90
106
expect ( tempFile . filePath ) . to . not . equal ( tempFile2 . filePath , 'Temp files must be unique, implementation of createTemporaryFile is off.' ) ;
91
107
} ) ;
108
+
92
109
test ( 'Ensure writing to a temp file is supported via file stream' , async ( ) => {
93
- await fileSystem . createTemporaryFile ( '.tmp' ) . then ( ( tf : TemporaryFile ) => {
110
+ const filesystem = new FileSystem ( ) ;
111
+
112
+ await filesystem . createTemporaryFile ( '.tmp' ) . then ( ( tf : TemporaryFile ) => {
94
113
expect ( tf ) . to . not . equal ( undefined , 'Error trying to create a temporary file' ) ;
95
- const writeStream = fileSystem . createWriteStream ( tf . filePath ) ;
114
+ const writeStream = filesystem . createWriteStream ( tf . filePath ) ;
96
115
writeStream . write ( 'hello' , 'utf8' , ( err : Error ) => {
97
116
expect ( err ) . to . equal ( undefined , `Failed to write to a temp file, error is ${ err } ` ) ;
98
117
} ) ;
99
118
} , ( failReason ) => {
100
119
expect ( failReason ) . to . equal ( 'No errors occurred' , `Failed to create a temporary file with error ${ failReason } ` ) ;
101
120
} ) ;
102
121
} ) ;
122
+
103
123
test ( 'Ensure chmod works against a temporary file' , async ( ) => {
104
- await fileSystem . createTemporaryFile ( '.tmp' ) . then ( async ( fl : TemporaryFile ) => {
105
- await fileSystem . chmod ( fl . filePath , '7777' ) . then (
124
+ const filesystem = new FileSystem ( ) ;
125
+
126
+ await filesystem . createTemporaryFile ( '.tmp' ) . then ( async ( fl : TemporaryFile ) => {
127
+ await filesystem . chmod ( fl . filePath , '7777' ) . then (
106
128
( _success : void ) => {
107
129
// cannot check for success other than we got here, chmod in Windows won't have any effect on the file itself.
108
130
} ,
@@ -111,13 +133,19 @@ suite('FileSystem', () => {
111
133
} ) ;
112
134
} ) ;
113
135
} ) ;
136
+
114
137
test ( 'Getting hash for non existent file should throw error' , async ( ) => {
115
- const promise = fileSystem . getFileHash ( 'some unknown file' ) ;
138
+ const filesystem = new FileSystem ( ) ;
139
+
140
+ const promise = filesystem . getFileHash ( 'some unknown file' ) ;
116
141
117
142
await expect ( promise ) . to . eventually . be . rejected ;
118
143
} ) ;
144
+
119
145
test ( 'Getting hash for a file should return non-empty string' , async ( ) => {
120
- const hash = await fileSystem . getFileHash ( __filename ) ;
146
+ const filesystem = new FileSystem ( ) ;
147
+
148
+ const hash = await filesystem . getFileHash ( __filename ) ;
121
149
122
150
expect ( hash ) . to . be . length . greaterThan ( 0 ) ;
123
151
} ) ;
0 commit comments