1
1
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
2
// See LICENSE in the project root for license information.
3
3
4
- /// <reference types='mocha' />
5
-
6
- import { assert } from 'chai' ;
7
4
import * as path from 'path' ;
8
5
import { LockFile , getProcessStartTime , getProcessStartTimeFromProcStat } from '../LockFile' ;
9
6
import { FileSystem } from '../FileSystem' ;
@@ -21,33 +18,34 @@ describe('LockFile', () => {
21
18
22
19
describe ( 'getLockFilePath' , ( ) => {
23
20
it ( 'only acceps alphabetical characters for resource name' , ( ) => {
24
- assert . doesNotThrow ( ( ) => {
21
+ expect ( ( ) => {
25
22
LockFile . getLockFilePath ( process . cwd ( ) , 'foo123' ) ;
26
- } ) ;
27
- assert . doesNotThrow ( ( ) => {
23
+ } ) . not . toThrow ( ) ;
24
+ expect ( ( ) => {
28
25
LockFile . getLockFilePath ( process . cwd ( ) , 'bar.123' ) ;
29
- } ) ;
30
- assert . doesNotThrow ( ( ) => {
26
+ } ) . not . toThrow ( ) ;
27
+ expect ( ( ) => {
31
28
LockFile . getLockFilePath ( process . cwd ( ) , 'foo.bar' ) ;
32
- } ) ;
33
- assert . doesNotThrow ( ( ) => {
29
+ } ) . not . toThrow ( ) ;
30
+ expect ( ( ) => {
34
31
LockFile . getLockFilePath ( process . cwd ( ) , 'lock-file.123' ) ;
35
- } ) ;
36
- assert . throws ( ( ) => {
32
+ } ) . not . toThrow ( ) ;
33
+
34
+ expect ( ( ) => {
37
35
LockFile . getLockFilePath ( process . cwd ( ) , '.foo123' ) ;
38
- } ) ;
39
- assert . throws ( ( ) => {
36
+ } ) . toThrow ( ) ;
37
+ expect ( ( ) => {
40
38
LockFile . getLockFilePath ( process . cwd ( ) , 'foo123.' ) ;
41
- } ) ;
42
- assert . throws ( ( ) => {
39
+ } ) . toThrow ( ) ;
40
+ expect ( ( ) => {
43
41
LockFile . getLockFilePath ( process . cwd ( ) , '-foo123' ) ;
44
- } ) ;
45
- assert . throws ( ( ) => {
42
+ } ) . toThrow ( ) ;
43
+ expect ( ( ) => {
46
44
LockFile . getLockFilePath ( process . cwd ( ) , 'foo123-' ) ;
47
- } ) ;
48
- assert . throws ( ( ) => {
45
+ } ) . toThrow ( ) ;
46
+ expect ( ( ) => {
49
47
LockFile . getLockFilePath ( process . cwd ( ) , '' ) ;
50
- } ) ;
48
+ } ) . toThrow ( ) ;
51
49
} ) ;
52
50
} ) ;
53
51
@@ -63,50 +61,52 @@ describe('LockFile', () => {
63
61
it ( 'returns undefined if too few values are contained in /proc/[pid]/stat (1)' , ( ) => {
64
62
const stat : string = createStatOutput ( '(bash)' , 1 ) ;
65
63
const ret : string | undefined = getProcessStartTimeFromProcStat ( stat ) ;
66
- assert . strictEqual ( ret , undefined ) ;
64
+ expect ( ret ) . toBeUndefined ( ) ;
67
65
} ) ;
68
66
it ( 'returns undefined if too few values are contained in /proc/[pid]/stat (2)' , ( ) => {
69
67
const stat : string = createStatOutput ( '(bash)' , 0 ) ;
70
68
const ret : string | undefined = getProcessStartTimeFromProcStat ( stat ) ;
71
- assert . strictEqual ( ret , undefined ) ;
69
+ expect ( ret ) . toBeUndefined ( ) ;
72
70
} ) ;
73
71
it ( 'returns the correct start time if the second value in /proc/[pid]/stat contains spaces' , ( ) => {
74
72
let stat : string = createStatOutput ( '(bash 2)' , 18 ) ;
75
73
const value22 : string = '12345' ;
76
74
stat += ` ${ value22 } ` ;
77
75
const ret : string | undefined = getProcessStartTimeFromProcStat ( stat ) ;
78
- assert . strictEqual ( ret , value22 ) ;
76
+ expect ( ret ) . toEqual ( value22 ) ;
79
77
} ) ;
80
78
it ( 'returns the correct start time if there are 22 values in /proc/[pid]/stat, including a trailing line '
81
79
+ 'terminator' , ( ) => {
82
80
let stat : string = createStatOutput ( '(bash)' , 18 ) ;
83
81
const value22 : string = '12345' ;
84
82
stat += ` ${ value22 } \n` ;
85
83
const ret : string | undefined = getProcessStartTimeFromProcStat ( stat ) ;
86
- assert . strictEqual ( ret , value22 ) ;
84
+ expect ( ret ) . toEqual ( value22 ) ;
87
85
} ) ;
88
86
it ( 'returns the correct start time if the second value in /proc/[pid]/stat does not contain spaces' , ( ) => {
89
87
let stat : string = createStatOutput ( '(bash)' , 18 ) ;
90
88
const value22 : string = '12345' ;
91
89
stat += ` ${ value22 } ` ;
92
90
const ret : string | undefined = getProcessStartTimeFromProcStat ( stat ) ;
93
- assert . strictEqual ( ret , value22 ) ;
91
+ expect ( ret ) . toEqual ( value22 ) ;
94
92
} ) ;
95
93
} ) ;
96
94
97
95
if ( process . platform === 'darwin' || process . platform === 'linux' ) {
98
96
describe ( 'Linux and Mac' , ( ) => {
99
97
describe ( 'getLockFilePath()' , ( ) => {
100
98
it ( 'returns a resolved path containing the pid' , ( ) => {
101
- assert . equal (
102
- path . join ( process . cwd ( ) , `test#${ process . pid } .lock` ) ,
99
+ expect (
100
+ path . join ( process . cwd ( ) , `test#${ process . pid } .lock` )
101
+ ) . toEqual (
103
102
LockFile . getLockFilePath ( './' , 'test' )
104
103
) ;
105
104
} ) ;
106
105
107
106
it ( 'allows for overridden pid' , ( ) => {
108
- assert . equal (
109
- path . join ( process . cwd ( ) , `test#99.lock` ) ,
107
+ expect (
108
+ path . join ( process . cwd ( ) , `test#99.lock` )
109
+ ) . toEqual (
110
110
LockFile . getLockFilePath ( './' , 'test' , 99 )
111
111
) ;
112
112
} ) ;
@@ -122,20 +122,20 @@ describe('LockFile', () => {
122
122
const lock : LockFile | undefined = LockFile . tryAcquire ( testFolder , resourceName ) ;
123
123
124
124
// The lockfile should exist and be in a clean state
125
- assert . isDefined ( lock ) ;
126
- assert . isFalse ( lock ! . dirtyWhenAcquired ) ;
127
- assert . isFalse ( lock ! . isReleased ) ;
128
- assert . isTrue ( FileSystem . exists ( pidLockFileName ) ) ;
125
+ expect ( lock ) . toBeDefined ( ) ;
126
+ expect ( lock ! . dirtyWhenAcquired ) . toEqual ( false ) ;
127
+ expect ( lock ! . isReleased ) . toEqual ( false ) ;
128
+ expect ( FileSystem . exists ( pidLockFileName ) ) . toEqual ( true ) ;
129
129
130
130
// Ensure that we can release the "clean" lockfile
131
131
lock ! . release ( ) ;
132
- assert . isFalse ( FileSystem . exists ( pidLockFileName ) ) ;
133
- assert . isTrue ( lock ! . isReleased ) ;
132
+ expect ( FileSystem . exists ( pidLockFileName ) ) . toEqual ( false ) ;
133
+ expect ( lock ! . isReleased ) . toEqual ( true ) ;
134
134
135
135
// Ensure we cannot release the lockfile twice
136
- assert . throws ( ( ) => {
136
+ expect ( ( ) => {
137
137
lock ! . release ( ) ;
138
- } ) ;
138
+ } ) . toThrow ( ) ;
139
139
} ) ;
140
140
141
141
it ( 'cannot acquire a lock if another valid lock exists' , ( ) => {
@@ -166,23 +166,25 @@ describe('LockFile', () => {
166
166
const lock : LockFile | undefined = LockFile . tryAcquire ( testFolder , resourceName ) ;
167
167
168
168
// this lock should be undefined since there is an existing lock
169
- assert . isUndefined ( lock ) ;
169
+ expect ( lock ) . toBeUndefined ( ) ;
170
170
} ) ;
171
171
} ) ;
172
172
}
173
173
174
174
if ( process . platform === 'win32' ) {
175
175
describe ( 'getLockFilePath()' , ( ) => {
176
176
it ( 'returns a resolved path that doesn\'t contain' , ( ) => {
177
- assert . equal (
178
- path . join ( process . cwd ( ) , `test.lock` ) ,
177
+ expect (
178
+ path . join ( process . cwd ( ) , `test.lock` )
179
+ ) . toEqual (
179
180
LockFile . getLockFilePath ( './' , 'test' )
180
181
) ;
181
182
} ) ;
182
183
183
184
it ( 'ignores pid that is passed in' , ( ) => {
184
- assert . equal (
185
- path . join ( process . cwd ( ) , `test.lock` ) ,
185
+ expect (
186
+ path . join ( process . cwd ( ) , `test.lock` )
187
+ ) . toEqual (
186
188
LockFile . getLockFilePath ( './' , 'test' , 99 )
187
189
) ;
188
190
} ) ;
@@ -202,7 +204,7 @@ describe('LockFile', () => {
202
204
const lock : LockFile | undefined = LockFile . tryAcquire ( testFolder , resourceName ) ;
203
205
204
206
// this lock should be undefined since there is an existing lock
205
- assert . isUndefined ( lock ) ;
207
+ expect ( lock ) . toBeUndefined ( ) ;
206
208
lockFileHandle . close ( ) ;
207
209
} ) ;
208
210
@@ -219,15 +221,15 @@ describe('LockFile', () => {
219
221
220
222
const lock : LockFile | undefined = LockFile . tryAcquire ( testFolder , resourceName ) ;
221
223
222
- assert . isDefined ( lock ) ;
223
- assert . isTrue ( lock ! . dirtyWhenAcquired ) ;
224
- assert . isFalse ( lock ! . isReleased ) ;
225
- assert . isTrue ( FileSystem . exists ( lockFileName ) ) ;
224
+ expect ( lock ) . toBeDefined ( ) ;
225
+ expect ( lock ! . dirtyWhenAcquired ) . toEqual ( true ) ;
226
+ expect ( lock ! . isReleased ) . toEqual ( false ) ;
227
+ expect ( FileSystem . exists ( lockFileName ) ) . toEqual ( true ) ;
226
228
227
229
// Ensure that we can release the "dirty" lockfile
228
230
lock ! . release ( ) ;
229
- assert . isFalse ( FileSystem . exists ( lockFileName ) ) ;
230
- assert . isTrue ( lock ! . isReleased ) ;
231
+ expect ( FileSystem . exists ( lockFileName ) ) . toEqual ( false ) ;
232
+ expect ( lock ! . isReleased ) . toEqual ( true ) ;
231
233
} ) ;
232
234
233
235
it ( 'can acquire and close a clean lockfile' , ( ) => {
@@ -241,20 +243,20 @@ describe('LockFile', () => {
241
243
const lock : LockFile | undefined = LockFile . tryAcquire ( testFolder , resourceName ) ;
242
244
243
245
// The lockfile should exist and be in a clean state
244
- assert . isDefined ( lock ) ;
245
- assert . isFalse ( lock ! . dirtyWhenAcquired ) ;
246
- assert . isFalse ( lock ! . isReleased ) ;
247
- assert . isTrue ( FileSystem . exists ( lockFileName ) ) ;
246
+ expect ( lock ) . toBeDefined ( ) ;
247
+ expect ( lock ! . dirtyWhenAcquired ) . toEqual ( false ) ;
248
+ expect ( lock ! . isReleased ) . toEqual ( false ) ;
249
+ expect ( FileSystem . exists ( lockFileName ) ) . toEqual ( true ) ;
248
250
249
251
// Ensure that we can release the "clean" lockfile
250
252
lock ! . release ( ) ;
251
- assert . isFalse ( FileSystem . exists ( lockFileName ) ) ;
252
- assert . isTrue ( lock ! . isReleased ) ;
253
+ expect ( FileSystem . exists ( lockFileName ) ) . toEqual ( false ) ;
254
+ expect ( lock ! . isReleased ) . toEqual ( true ) ;
253
255
254
256
// Ensure we cannot release the lockfile twice
255
- assert . throws ( ( ) => {
257
+ expect ( ( ) => {
256
258
lock ! . release ( ) ;
257
- } ) ;
259
+ } ) . toThrow ( ) ;
258
260
} ) ;
259
261
}
260
262
} ) ;
0 commit comments