-
Notifications
You must be signed in to change notification settings - Fork 31.3k
Expand file tree
/
Copy pathfind-root.test.ts
More file actions
98 lines (78 loc) · 3.31 KB
/
Copy pathfind-root.test.ts
File metadata and controls
98 lines (78 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { findRootDirAndLockFiles } from './find-root'
describe('findRootDirAndLockFiles()', () => {
it('ignores stray parent lockfiles without a package manifest', async () => {
const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-'))
try {
const appDir = join(rootDir, 'app')
const parentLockfile = join(rootDir, 'package-lock.json')
const appLockfile = join(appDir, 'package-lock.json')
await mkdir(appDir)
await writeFile(parentLockfile, '{}')
await writeFile(join(appDir, 'package.json'), '{}')
await writeFile(appLockfile, '{}')
const result = findRootDirAndLockFiles(appDir)
expect(result.rootDir).toBe(appDir)
expect(result.lockFiles).toEqual([appLockfile])
} finally {
await rm(rootDir, { recursive: true, force: true })
}
})
it('ignores parent package roots that do not declare workspaces', async () => {
const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-'))
try {
const appDir = join(rootDir, 'app')
const parentLockfile = join(rootDir, 'bun.lock')
const appLockfile = join(appDir, 'bun.lock')
await mkdir(appDir)
await writeFile(join(rootDir, 'package.json'), '{}')
await writeFile(parentLockfile, '')
await writeFile(join(appDir, 'package.json'), '{}')
await writeFile(appLockfile, '')
const result = findRootDirAndLockFiles(appDir)
expect(result.rootDir).toBe(appDir)
expect(result.lockFiles).toEqual([appLockfile])
} finally {
await rm(rootDir, { recursive: true, force: true })
}
})
it('keeps higher package roots when they declare workspaces', async () => {
const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-'))
try {
const appDir = join(rootDir, 'apps', 'docs')
const parentLockfile = join(rootDir, 'package-lock.json')
const appLockfile = join(appDir, 'package-lock.json')
await mkdir(appDir, { recursive: true })
await writeFile(
join(rootDir, 'package.json'),
JSON.stringify({ workspaces: ['apps/*'] })
)
await writeFile(parentLockfile, '{}')
await writeFile(join(appDir, 'package.json'), '{}')
await writeFile(appLockfile, '{}')
const result = findRootDirAndLockFiles(appDir)
expect(result.rootDir).toBe(rootDir)
expect(result.lockFiles).toEqual([appLockfile, parentLockfile])
} finally {
await rm(rootDir, { recursive: true, force: true })
}
})
it('keeps higher pnpm workspace roots ahead of nested lockfiles', async () => {
const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-'))
try {
const appDir = join(rootDir, 'apps', 'docs')
const workspaceFile = join(rootDir, 'pnpm-workspace.yaml')
await mkdir(appDir, { recursive: true })
await writeFile(workspaceFile, 'packages:\n - apps/*\n')
await writeFile(join(appDir, 'package.json'), '{}')
await writeFile(join(appDir, 'pnpm-lock.yaml'), '')
const result = findRootDirAndLockFiles(appDir)
expect(result.rootDir).toBe(rootDir)
expect(result.lockFiles).toEqual([workspaceFile])
} finally {
await rm(rootDir, { recursive: true, force: true })
}
})
})