|
1 | 1 | import { join, relative, resolve } from 'path' |
2 | 2 | import { |
3 | 3 | appendFileSync, |
4 | | - ensureDirSync, |
5 | 4 | ensureFileSync, |
6 | | - remove, |
7 | 5 | readFileSync, |
8 | 6 | writeFileSync |
9 | 7 | } from 'fs-extra' |
@@ -55,8 +53,10 @@ const mockedWorkspace = jest.mocked(workspace) |
55 | 53 | const mockedWindow = jest.mocked(window) |
56 | 54 | const mockedOpenTextDocument = jest.fn() |
57 | 55 | const mockedShowTextDocument = jest.fn() |
| 56 | +const mockedFindFiles = jest.fn() |
58 | 57 |
|
59 | 58 | mockedWorkspace.openTextDocument = mockedOpenTextDocument |
| 59 | +mockedWorkspace.findFiles = mockedFindFiles |
60 | 60 | mockedWindow.showTextDocument = mockedShowTextDocument |
61 | 61 |
|
62 | 62 | beforeEach(() => { |
@@ -275,41 +275,67 @@ describe('writeTsv', () => { |
275 | 275 | }) |
276 | 276 |
|
277 | 277 | describe('findDvcRootPaths', () => { |
| 278 | + const convertAsFindDvcConfigFile = (rootPath: string) => ({ |
| 279 | + fsPath: join(rootPath, DOT_DVC, 'config') |
| 280 | + }) |
| 281 | + |
278 | 282 | it('should find the dvc root if it exists in the given folder', async () => { |
279 | | - const dvcRoots = await findDvcRootPaths(dvcDemoPath) |
| 283 | + mockedFindFiles.mockResolvedValue([convertAsFindDvcConfigFile(dvcDemoPath)]) |
| 284 | + const dvcRoots = await findDvcRootPaths() |
280 | 285 |
|
281 | | - expect(dvcRoots).toStrictEqual([dvcDemoPath]) |
| 286 | + expect(dvcRoots).toStrictEqual(new Set([dvcDemoPath])) |
282 | 287 | }) |
283 | 288 |
|
284 | 289 | it('should find multiple roots if available one directory below the given folder', async () => { |
285 | 290 | const parentDir = resolve(dvcDemoPath, '..') |
286 | 291 | const mockDvcRoot = join(parentDir, 'mockDvc') |
287 | | - ensureDirSync(join(mockDvcRoot, DOT_DVC)) |
288 | | - |
289 | | - const dvcRoots = await findDvcRootPaths(parentDir) |
| 292 | + mockedFindFiles.mockResolvedValue([ |
| 293 | + convertAsFindDvcConfigFile(dvcDemoPath), |
| 294 | + convertAsFindDvcConfigFile(mockDvcRoot) |
| 295 | + ]) |
290 | 296 |
|
291 | | - void remove(mockDvcRoot) |
| 297 | + const dvcRoots = await findDvcRootPaths() |
292 | 298 |
|
293 | | - expect([...dvcRoots]).toStrictEqual([dvcDemoPath, mockDvcRoot]) |
| 299 | + expect(dvcRoots).toStrictEqual(new Set([dvcDemoPath, mockDvcRoot])) |
294 | 300 | }) |
295 | 301 |
|
296 | 302 | it('should find a mono-repo root as well as sub-roots if available one directory below the given folder', async () => { |
297 | | - const parentDir = dvcDemoPath |
298 | | - const mockFirstDvcRoot = join(parentDir, 'mockFirstDvc') |
299 | | - const mockSecondDvcRoot = join(parentDir, 'mockSecondDvc') |
300 | | - ensureDirSync(join(mockFirstDvcRoot, DOT_DVC)) |
301 | | - ensureDirSync(join(mockSecondDvcRoot, DOT_DVC)) |
| 303 | + const mockFirstDvcRoot = join(dvcDemoPath, 'mockFirstDvc') |
| 304 | + const mockSecondDvcRoot = join(dvcDemoPath, 'mockSecondDvc') |
| 305 | + mockedFindFiles.mockResolvedValue([ |
| 306 | + convertAsFindDvcConfigFile(dvcDemoPath), |
| 307 | + convertAsFindDvcConfigFile(mockFirstDvcRoot), |
| 308 | + convertAsFindDvcConfigFile(mockSecondDvcRoot) |
| 309 | + ]) |
302 | 310 |
|
303 | | - const dvcRoots = await findDvcRootPaths(parentDir) |
| 311 | + const dvcRoots = await findDvcRootPaths() |
304 | 312 |
|
305 | | - void remove(mockFirstDvcRoot) |
306 | | - void remove(mockSecondDvcRoot) |
| 313 | + expect(dvcRoots).toStrictEqual( |
| 314 | + new Set([dvcDemoPath, mockFirstDvcRoot, mockSecondDvcRoot]) |
| 315 | + ) |
| 316 | + }) |
307 | 317 |
|
308 | | - expect([...dvcRoots]).toStrictEqual([ |
| 318 | + it('should find deeply nested roots if available', async () => { |
| 319 | + const mockFirstDvcRoot = join( |
309 | 320 | dvcDemoPath, |
310 | | - mockFirstDvcRoot, |
311 | | - mockSecondDvcRoot |
| 321 | + 'deep', |
| 322 | + 'deeper', |
| 323 | + 'really_deep', |
| 324 | + 'one' |
| 325 | + ) |
| 326 | + const mockSecondDvcRoot = join(dvcDemoPath, 'one_deep', 'two') |
| 327 | + |
| 328 | + mockedFindFiles.mockResolvedValue([ |
| 329 | + convertAsFindDvcConfigFile(dvcDemoPath), |
| 330 | + convertAsFindDvcConfigFile(mockFirstDvcRoot), |
| 331 | + convertAsFindDvcConfigFile(mockSecondDvcRoot) |
312 | 332 | ]) |
| 333 | + |
| 334 | + const dvcRoots = await findDvcRootPaths() |
| 335 | + |
| 336 | + expect(dvcRoots).toStrictEqual( |
| 337 | + new Set([dvcDemoPath, mockFirstDvcRoot, mockSecondDvcRoot]) |
| 338 | + ) |
313 | 339 | }) |
314 | 340 | }) |
315 | 341 |
|
|
0 commit comments