1
1
import fs from 'node:fs/promises' ;
2
2
import path from 'node:path' ;
3
- import { afterEach , beforeEach , describe , expect , it , test , vi } from 'vitest' ;
4
- import { FilesMap } from './filesmap.js' ;
3
+ import { afterEach , beforeEach , describe , expect , assert , it , test , vi } from 'vitest' ;
4
+ import { FilesMap , FilesMapGraph } from './filesmap.js' ;
5
5
6
6
const tmpDir = path . join ( __dirname , `.tmp-${ path . basename ( __filename ) } ` ) ;
7
7
@@ -27,19 +27,17 @@ describe('FilesMap', () => {
27
27
} ;
28
28
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
29
29
30
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
31
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
30
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
31
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
32
32
33
- expect ( node1 ) . toBeDefined ( ) ;
34
- expect ( node2 ) . toBeDefined ( ) ;
35
33
expect ( logger . warn ) . not . toHaveBeenCalled ( ) ;
36
34
37
- expect ( await node1 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
35
+ expect ( await node1 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
38
36
{
39
37
"/file1": "",
40
38
}
41
39
` ) ;
42
- expect ( await node2 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
40
+ expect ( await node2 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
43
41
{
44
42
"/file2": "",
45
43
}
@@ -58,16 +56,16 @@ describe('FilesMap', () => {
58
56
} ;
59
57
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
60
58
61
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
62
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
59
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
60
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
63
61
64
- expect ( await node1 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
62
+ expect ( await node1 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
65
63
{
66
64
"/file1": "",
67
65
"/file2": "",
68
66
}
69
67
` ) ;
70
- expect ( await node2 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
68
+ expect ( await node2 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
71
69
{
72
70
"/file1": "",
73
71
"/file2": "",
@@ -91,24 +89,24 @@ describe('FilesMap', () => {
91
89
} ;
92
90
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
93
91
94
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
95
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
96
- const node3 = graph . getFilesMapByFolder ( folders [ 2 ] ) ;
92
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
93
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
94
+ const node3 = getFilesMapByFolder ( graph , folders [ 2 ] ) ;
97
95
98
- expect ( await node1 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
96
+ expect ( await node1 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
99
97
{
100
98
"/file1": "",
101
99
"/file2": "",
102
100
"/file3": "",
103
101
}
104
102
` ) ;
105
- expect ( await node2 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
103
+ expect ( await node2 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
106
104
{
107
105
"/file2": "",
108
106
"/file3": "",
109
107
}
110
108
` ) ;
111
- expect ( await node3 ? .toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
109
+ expect ( await node3 . toFiles ( logger as any ) ) . toMatchInlineSnapshot ( `
112
110
{
113
111
"/file3": "",
114
112
}
@@ -128,11 +126,11 @@ describe('FilesMap', () => {
128
126
} ;
129
127
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
130
128
131
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
132
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
133
- const node3 = graph . getFilesMapByFolder ( folders [ 2 ] ) ;
129
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
130
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
131
+ const node3 = getFilesMapByFolder ( graph , folders [ 2 ] ) ;
134
132
135
- const dependents = [ ...( node3 ? .allDependents ( ) ?? [ ] ) ] ;
133
+ const dependents = [ ...node3 . allDependents ( ) ] ;
136
134
137
135
expect ( dependents ) . toHaveLength ( 2 ) ;
138
136
expect ( dependents ) . toContain ( node1 ) ;
@@ -152,14 +150,14 @@ describe('FilesMap', () => {
152
150
} ;
153
151
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
154
152
155
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
156
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
157
- const node3 = graph . getFilesMapByFolder ( folders [ 2 ] ) ;
153
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
154
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
155
+ const node3 = getFilesMapByFolder ( graph , folders [ 2 ] ) ;
158
156
159
- node1 ? .unlink ( ) ;
157
+ node1 . unlink ( ) ;
160
158
161
- const dependents3 = [ ...( node3 ? .allDependents ( ) ?? [ ] ) ] ;
162
- const dependents2 = [ ...( node2 ? .allDependents ( ) ?? [ ] ) ] ;
159
+ const dependents3 = [ ...node3 . allDependents ( ) ] ;
160
+ const dependents2 = [ ...node2 . allDependents ( ) ] ;
163
161
164
162
expect ( dependents2 ) . toHaveLength ( 0 ) ;
165
163
@@ -180,19 +178,19 @@ describe('FilesMap', () => {
180
178
} ;
181
179
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
182
180
183
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
184
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
181
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
182
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
185
183
186
- const dependents = [ ...( node2 ? .allDependents ( ) ?? [ ] ) ] ;
184
+ const dependents = [ ...node2 . allDependents ( ) ] ;
187
185
188
186
expect ( dependents ) . toHaveLength ( 1 ) ;
189
187
190
188
// now we remove the config
191
- await fs . unlink ( path . join ( node1 ! . path , '.tk-config.json' ) ) ;
189
+ await fs . unlink ( path . join ( node1 . path , '.tk-config.json' ) ) ;
192
190
193
- graph . updateFilesMapByFolder ( node1 ! . path , logger as any ) ;
191
+ graph . updateFilesMapByFolder ( node1 . path , logger as any ) ;
194
192
195
- const newDependents = [ ...( node2 ? .allDependents ( ) ?? [ ] ) ] ;
193
+ const newDependents = [ ...node2 . allDependents ( ) ] ;
196
194
197
195
expect ( newDependents ) . toHaveLength ( 0 ) ;
198
196
} ) ;
@@ -209,19 +207,19 @@ describe('FilesMap', () => {
209
207
} ;
210
208
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
211
209
212
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
213
- const node2 = graph . getFilesMapByFolder ( folders [ 1 ] ) ;
210
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
211
+ const node2 = getFilesMapByFolder ( graph , folders [ 1 ] ) ;
214
212
215
- const dependents = [ ...( node2 ? .allDependents ( ) ?? [ ] ) ] ;
213
+ const dependents = [ ...node2 . allDependents ( ) ] ;
216
214
217
215
expect ( dependents ) . toHaveLength ( 0 ) ;
218
216
219
217
// now we add the config
220
- await fs . writeFile ( path . join ( node1 ! . path , '.tk-config.json' ) , JSON . stringify ( { extends : '../folder2' } ) ) ;
218
+ await fs . writeFile ( path . join ( node1 . path , '.tk-config.json' ) , JSON . stringify ( { extends : '../folder2' } ) ) ;
221
219
222
- graph . updateFilesMapByFolder ( node1 ! . path , logger as any ) ;
220
+ graph . updateFilesMapByFolder ( node1 . path , logger as any ) ;
223
221
224
- const newDependents = [ ...( node2 ? .allDependents ( ) ?? [ ] ) ] ;
222
+ const newDependents = [ ...node2 . allDependents ( ) ] ;
225
223
226
224
expect ( newDependents ) . toHaveLength ( 1 ) ;
227
225
expect ( newDependents ) . toContain ( node1 ) ;
@@ -240,18 +238,19 @@ describe('FilesMap', () => {
240
238
} ;
241
239
const graph = await FilesMap . initGraph ( folders , logger as any ) ;
242
240
243
- const node1 = graph . getFilesMapByFolder ( folders [ 0 ] ) ;
241
+ const node1 = getFilesMapByFolder ( graph , folders [ 0 ] ) ;
244
242
245
243
expect ( graph . getFilesMapByFolder ( folder3 ) ) . toBeUndefined ( ) ;
246
244
247
245
// now we add the config and the new folder
248
- await fs . writeFile ( path . join ( node1 ! . path , '.tk-config.json' ) , JSON . stringify ( { extends : '../folder3' } ) ) ;
246
+ await fs . writeFile ( path . join ( node1 . path , '.tk-config.json' ) , JSON . stringify ( { extends : '../folder3' } ) ) ;
249
247
await fs . mkdir ( folder3 ) ;
250
248
251
- graph . updateFilesMapByFolder ( node1 ! . path , logger as any ) ;
249
+ graph . updateFilesMapByFolder ( node1 . path , logger as any ) ;
252
250
253
- const node3 = graph . getFilesMapByFolder ( folder3 ) ;
254
- const newDependents = [ ...( node3 ?. allDependents ( ) ?? [ ] ) ] ;
251
+ const node3 = getFilesMapByFolder ( graph , folder3 ) ;
252
+
253
+ const newDependents = [ ...node3 . allDependents ( ) ] ;
255
254
256
255
expect ( newDependents ) . toHaveLength ( 1 ) ;
257
256
expect ( newDependents ) . toContain ( node1 ) ;
@@ -269,3 +268,11 @@ async function scaffoldTestFolders(folders: [name: string, files: Record<string,
269
268
}
270
269
}
271
270
}
271
+
272
+ function getFilesMapByFolder ( graph : FilesMapGraph , folder : string ) {
273
+ const node = graph . getFilesMapByFolder ( folder ) ;
274
+
275
+ assert ( node !== undefined ) ;
276
+
277
+ return node ;
278
+ }
0 commit comments