Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { parse } from 'yaml'
3import {
4 mergePnpmWorkspace,
5 writePnpmWorkspace,
6 standalonePnpmNpmrc,
7} from '../src/pnpm-workspace.js'
8import { createMemoryEnvironment } from '../src/environment.js'
9import { writeFiles } from '../src/add-to-app.js'
10
11describe('pnpm build approvals', () => {
12 it('keeps pnpm 9 dependency additions working in a new standalone app', async () => {
13 const { environment, output } = createMemoryEnvironment('/app')
14 environment.startRun()
15 await writePnpmWorkspace(environment, '/app/pnpm-workspace.yaml', 'pnpm', {
16 pnpm: { onlyBuiltDependencies: ['esbuild'] },
17 })
18 environment.finishRun()
19 expect(output.files['.npmrc']).toBe('ignore-workspace-root-check=true\n')
20 expect(parse(output.files['pnpm-workspace.yaml']).allowBuilds.esbuild).toBe(
21 true,
22 )
23 })
24 it('handles workspace config emitted by framework templates during creation', async () => {
25 const { environment, output } = createMemoryEnvironment('/app')
26 environment.startRun()
27 await environment.writeFile('/app/pnpm-workspace.yaml', 'allowBuilds:\n esbuild: true\n')
28 await writePnpmWorkspace(environment, '/app/pnpm-workspace.yaml', 'pnpm', {
29 pnpm: { onlyBuiltDependencies: ['esbuild'] },
30 }, { workspaceWasPresent: false })
31 environment.finishRun()
32 expect(output.files['.npmrc']).toBe('ignore-workspace-root-check=true\n')
33 })
34 it('preserves project registry settings, comments and explicit root-check choices', async () => {
35 const workspace = await mergePnpmWorkspace(undefined, ['esbuild'])
36 const original =
37 '\uFEFF# project registry\nregistry=https://registry.example.test/\n'
38 expect(await standalonePnpmNpmrc(undefined, workspace, original)).toBe(
39 '\uFEFFignore-workspace-root-check=true\n# project registry\nregistry=https://registry.example.test/\n',
40 )
41 const explicit =
42 'ignore-workspace-root-check=false\nregistry=https://registry.example.test/\n'
43 expect(await standalonePnpmNpmrc(undefined, workspace, explicit)).toBe(
44 explicit,
45 )
46 expect(
47 await standalonePnpmNpmrc(
48 undefined,
49 workspace,
50 '[other]\nignore-workspace-root-check=false\n',
51 ),
52 ).toBe(
53 'ignore-workspace-root-check=true\n[other]\nignore-workspace-root-check=false\n',
54 )
55 })
56 it('does not change existing or custom monorepo root checks', async () => {
57 const standalone = await mergePnpmWorkspace(undefined, ['esbuild'])
58 const monorepo = 'packages:\n - apps/*\nallowBuilds:\n esbuild: true\n'
59 expect(
60 await standalonePnpmNpmrc(monorepo, standalone, undefined),
61 ).toBeUndefined()
62 expect(
63 await standalonePnpmNpmrc(undefined, monorepo, undefined),
64 ).toBeUndefined()
65 })
66 it('defers standalone configuration until an existing app is merged', async () => {
67 const { environment, output } = createMemoryEnvironment('/app')
68 environment.startRun()
69 await writePnpmWorkspace(
70 environment,
71 '/app/pnpm-workspace.yaml',
72 'pnpm',
73 { pnpm: { onlyBuiltDependencies: ['esbuild'] } },
74 { existingProject: true },
75 )
76 environment.finishRun()
77 expect(output.files['.npmrc']).toBeUndefined()
78 expect(output.files['pnpm-workspace.yaml']).toBeDefined()
79 })
80 it('preserves existing npmrc content when adding pnpm config to an older app', async () => {
81 const { environment, output } = createMemoryEnvironment('/app')
82 environment.startRun()
83 await environment.writeFile(
84 '/app/package.json',
85 JSON.stringify({ pnpm: { onlyBuiltDependencies: ['esbuild'] } }),
86 )
87 await environment.writeFile(
88 '/app/.npmrc',
89 '# keep\nregistry=https://registry.example.test/\n',
90 )
91 await writeFiles(
92 environment,
93 '/app',
94 {
95 files: {
96 '/app/package.json': JSON.stringify({
97 pnpm: { onlyBuiltDependencies: ['esbuild'] },
98 }),
99 '/app/pnpm-workspace.yaml': await mergePnpmWorkspace(undefined, [
100 'esbuild',
101 ]),
102 },
103 deletedFiles: [],
104 },
105 true,
106 )
107 environment.finishRun()
108 expect(output.files['.npmrc']).toBe(
109 'ignore-workspace-root-check=true\n# keep\nregistry=https://registry.example.test/\n',
110 )
111 })
112 it('creates a workspace compatible with pnpm 9 and current build approvals', async () => {
113 expect(
114 parse(
115 await mergePnpmWorkspace(undefined, ['esbuild', 'workerd', 'esbuild']),
116 ),
117 ).toEqual({
118 packages: ['.'],
119 allowBuilds: { esbuild: true, workerd: true },
120 })
121 })
122 it('preserves comments, workspace membership, settings and existing approvals', async () => {
123 const input =
124 '# user workspace\npackages:\n - apps/*\nminimumReleaseAge: 1440\nonlyBuiltDependencies:\n - custom-native\nallowBuilds:\n esbuild: true\n'
125 const result = await mergePnpmWorkspace(input, ['esbuild', 'workerd'])
126 expect(result).toContain('# user workspace')
127 expect(parse(result)).toEqual({
128 packages: ['apps/*'],
129 minimumReleaseAge: 1440,
130 onlyBuiltDependencies: ['custom-native'],
131 allowBuilds: { esbuild: true, workerd: true, 'custom-native': true },
132 })
133 })
134 it('retains explicit denials and does not broaden a custom policy', async () => {
135 expect(
136 parse(
137 await mergePnpmWorkspace('allowBuilds:\n optional-native: false\n', [
138 'esbuild',
139 ]),
140 ).allowBuilds,
141 ).toEqual({ 'optional-native': false, esbuild: true })
142 expect(
143 parse(
144 await mergePnpmWorkspace('allowBuilds:\n esbuild: false\n', [
145 'esbuild',
146 ]),
147 ).allowBuilds.esbuild,
148 ).toBe(false)
149 await expect(
150 mergePnpmWorkspace('allowBuilds:\n "*": false\n', ['esbuild']),
151 ).rejects.toThrow('existing pnpm allowBuilds policy')
152 await expect(
153 mergePnpmWorkspace('allowBuilds:\n esbuild@0.25: true\n', ['esbuild']),
154 ).rejects.toThrow('existing pnpm allowBuilds policy')
155 })
156 it('does not block unrelated add-ons or broaden an existing custom policy', async () => {
157 const result = await mergePnpmWorkspace(
158 'allowBuilds:\n "*": false\n',
159 ['esbuild'],
160 ['esbuild'],
161 )
162 expect(parse(result).allowBuilds).toEqual({ '*': false })
163 await expect(
164 mergePnpmWorkspace(result, ['esbuild', 'workerd'], ['esbuild']),
165 ).rejects.toThrow('workerd')
166 })
167 it.each(['allowBuilds: [broken]', 'allowBuilds: {', '- invalid-root'])(
168 'rejects invalid configuration without replacing it: %s',
169 async (input) => {
170 await expect(mergePnpmWorkspace(input, ['esbuild'])).rejects.toThrow()
171 },
172 )
173 it('does not create pnpm settings for other package managers', async () => {
174 const { environment, output } = createMemoryEnvironment()
175 await writePnpmWorkspace(environment, '/app/pnpm-workspace.yaml', 'npm', {
176 pnpm: { onlyBuiltDependencies: ['esbuild'] },
177 })
178 environment.finishRun()
179 expect(output.files).toEqual({})
180 })
181 it('merges existing app settings and manual native approvals before installing an add-on', async () => {
182 const { environment, output } = createMemoryEnvironment('/app')
183 environment.startRun()
184 await environment.writeFile(
185 '/app/package.json',
186 JSON.stringify({ pnpm: { onlyBuiltDependencies: ['manual-native'] } }),
187 )
188 await environment.writeFile(
189 '/app/pnpm-workspace.yaml',
190 '# keep\npackages:\n - apps/*\nallowBuilds:\n esbuild: true\n',
191 )
192 await writeFiles(
193 environment,
194 '/app',
195 {
196 files: {
197 '/app/package.json': JSON.stringify({
198 pnpm: { onlyBuiltDependencies: ['esbuild', 'workerd'] },
199 }),
200 '/app/pnpm-workspace.yaml': await mergePnpmWorkspace(undefined, [
201 'esbuild',
202 'workerd',
203 ]),
204 },
205 deletedFiles: [],
206 },
207 true,
208 )
209 environment.finishRun()
210 const yaml = output.files['pnpm-workspace.yaml']
211 expect(yaml).toContain('# keep')
212 expect(parse(yaml)).toEqual({
213 packages: ['apps/*'],
214 allowBuilds: { esbuild: true, workerd: true, 'manual-native': true },
215 })
216 expect(
217 JSON.parse(output.files['package.json']).pnpm.onlyBuiltDependencies,
218 ).toEqual(['manual-native', 'esbuild', 'workerd'])
219 })
220})