-
-
Notifications
You must be signed in to change notification settings - Fork 940
/
catalog.ts
324 lines (289 loc) · 8.12 KB
/
catalog.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { type Lockfile } from '@pnpm/lockfile-types'
import { type ProjectManifest } from '@pnpm/types'
import { readProjects } from '@pnpm/filter-workspace-packages'
import { preparePackages } from '@pnpm/prepare'
import { install } from '@pnpm/plugin-commands-installation'
import { DEFAULT_OPTS } from './utils'
import path from 'path'
import readYamlFile from 'read-yaml-file'
import writeYamlFile from 'write-yaml-file'
/**
* A utility to make writing catalog tests easier by reducing boilerplate.
*/
class CatalogTestsController {
private readonly workspaceDir: string
constructor (readonly pkgs: Array<{ location: string, package: ProjectManifest }>) {
preparePackages(pkgs)
this.workspaceDir = process.cwd()
}
async writeWorkspaceYml (data: unknown) {
await writeYamlFile(path.join(this.workspaceDir, 'pnpm-workspace.yaml'), data)
}
async install (opts?: { frozenLockfile?: boolean, filter?: readonly string[] }) {
const { allProjects, selectedProjectsGraph } = await readProjects(this.workspaceDir, opts?.filter?.map(parentDir => ({ parentDir })) ?? [])
await install.handler({
...DEFAULT_OPTS,
allProjects,
dir: this.workspaceDir,
lockfileDir: this.workspaceDir,
recursive: true,
selectedProjectsGraph,
workspaceDir: this.workspaceDir,
useBetaCatalogsFeat: true,
rawLocalConfig: {
...DEFAULT_OPTS.rawLocalConfig,
// Many tests check if the pnpm-lock.yaml is updated after a
// package.json or pnpm-workspace.yaml change. Setting frozen-lockfile
// to false is necessary for these tests to pass on CI since this
// defaults to true on CI.
'frozen-lockfile': opts?.frozenLockfile ?? true,
},
})
}
async lockfile (): Promise<Lockfile> {
return readYamlFile(path.join(this.workspaceDir, WANTED_LOCKFILE))
}
async updateProjectManifest (location: string, manifest: ProjectManifest): Promise<void> {
const { selectedProjectsGraph } = await readProjects(this.workspaceDir, [{ parentDir: location }])
await selectedProjectsGraph[path.join(this.workspaceDir, location)].package.writeProjectManifest(manifest)
}
}
test('installing with "catalog:" should work', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
dependencies: {
'is-positive': 'catalog:',
},
},
},
// Empty second project to create a multi-package workspace.
{
location: 'packages/project2',
package: {},
},
])
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: { 'is-positive': '1.0.0' },
})
await ctrl.install()
const lockfile = await ctrl.lockfile()
expect(lockfile.importers['packages/project1']).toEqual({
dependencies: {
'is-positive': {
specifier: 'catalog:',
version: '1.0.0',
},
},
})
})
test('importer to importer dependency with "catalog:" should work', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
name: 'project1',
dependencies: {
project2: 'workspace:*',
},
},
},
{
location: 'packages/project2',
package: {
name: 'project2',
dependencies: {
'is-positive': 'catalog:',
},
},
},
])
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: { 'is-positive': '1.0.0' },
})
await ctrl.install()
const lockfile = await ctrl.lockfile()
expect(lockfile.importers['packages/project2']).toEqual({
dependencies: {
'is-positive': {
specifier: 'catalog:',
version: '1.0.0',
},
},
})
})
test('lockfile contains catalog snapshots', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
dependencies: {
'is-positive': 'catalog:',
},
},
},
{
location: 'packages/project2',
package: {
dependencies: {
'is-negative': 'catalog:',
},
},
},
])
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: {
'is-positive': '^1.0.0',
'is-negative': '^1.0.0',
},
})
await ctrl.install()
const lockfile = await ctrl.lockfile()
expect(lockfile.catalogs).toStrictEqual({
default: {
'is-positive': { specifier: '^1.0.0', version: '1.0.0' },
'is-negative': { specifier: '^1.0.0', version: '1.0.0' },
},
})
})
test('lockfile is updated if catalog config changes', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
dependencies: {
'is-positive': 'catalog:',
},
},
},
])
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: { 'is-positive': '=1.0.0' },
})
await ctrl.install()
expect((await ctrl.lockfile()).importers['packages/project1']).toEqual({
dependencies: {
'is-positive': {
specifier: 'catalog:',
version: '1.0.0',
},
},
})
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: {
'is-positive': '=3.1.0',
},
})
await ctrl.install()
expect((await ctrl.lockfile()).importers['packages/project1']).toEqual({
dependencies: {
'is-positive': {
specifier: 'catalog:',
version: '3.1.0',
},
},
})
})
test('lockfile catalog snapshots retain existing entries on --filter', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
dependencies: {
'is-negative': 'catalog:',
},
},
},
{
location: 'packages/project2',
package: {
dependencies: {
'is-positive': 'catalog:',
},
},
},
])
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: {
'is-positive': '^1.0.0',
'is-negative': '^1.0.0',
},
})
await ctrl.install()
expect((await ctrl.lockfile()).catalogs).toStrictEqual({
default: {
'is-negative': { specifier: '^1.0.0', version: '1.0.0' },
'is-positive': { specifier: '^1.0.0', version: '1.0.0' },
},
})
// Update catalog definitions so pnpm triggers a rerun.
await ctrl.writeWorkspaceYml({
packages: ['packages/*'],
catalog: {
'is-positive': '=3.1.0',
'is-negative': '^1.0.0',
},
})
await ctrl.install({ filter: ['packages/project2'] })
expect((await ctrl.lockfile()).catalogs).toStrictEqual({
default: {
// The is-negative snapshot should be carried from the previous install,
// despite the current filtered install not using it.
'is-negative': { specifier: '^1.0.0', version: '1.0.0' },
'is-positive': { specifier: '=3.1.0', version: '3.1.0' },
},
})
})
test('lockfile catalog snapshots should keep unused entries', async () => {
const ctrl = new CatalogTestsController([
{
location: 'packages/project1',
package: {
dependencies: {
'is-positive': 'catalog:',
},
},
},
])
await writeYamlFile('pnpm-workspace.yaml', {
packages: ['packages/*'],
catalog: {
'is-positive': '=1.0.0',
},
})
{
await ctrl.install()
const lockfile = await ctrl.lockfile()
expect(lockfile.importers['packages/project1']?.dependencies?.['is-positive']).toEqual({
specifier: 'catalog:',
version: '1.0.0',
})
expect(lockfile.catalogs?.default).toStrictEqual({
'is-positive': { specifier: '=1.0.0', version: '1.0.0' },
})
}
await ctrl.updateProjectManifest('packages/project1', { dependencies: { 'is-positive': '=1.0.0' } })
await ctrl.install()
{
const lockfile = await ctrl.lockfile()
expect(lockfile.importers['packages/project1']).toEqual({
dependencies: {
'is-positive': {
specifier: '=1.0.0',
version: '1.0.0',
},
},
})
expect(lockfile.catalogs?.default).toStrictEqual({
'is-positive': { specifier: '=1.0.0', version: '1.0.0' },
})
}
})