-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathinstall.e2e.ts
More file actions
363 lines (326 loc) · 11.5 KB
/
install.e2e.ts
File metadata and controls
363 lines (326 loc) · 11.5 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import http from 'node:http'
import os from 'node:os'
import events from 'node:events'
import { existsSync } from 'node:fs'
import fs from 'node:fs/promises'
import { platform } from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import execa from 'execa'
import { runServer } from 'verdaccio'
import { describe, expect, it } from 'vitest'
import createDebug from 'debug'
import picomatch from 'picomatch'
import pkg from '../package.json'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(__dirname, '..')
const distDir = path.join(projectRoot, 'dist')
const tempdirPrefix = 'netlify-cli-e2e-test--'
const debug = createDebug('netlify-cli:e2e')
const isNodeModules = picomatch('**/node_modules/**', { dot: true })
const shouldCopyCLIFile = async (src: string) => {
if (isNodeModules(src)) return false
try {
const st = await fs.lstat(src) // DO NOT follow symlinks
if (st.isSocket() || st.isFIFO() || st.isCharacterDevice() || st.isBlockDevice()) {
return false
}
} catch {
// If we can't lstat it, skip it
return false
}
return true
}
const itWithMockNpmRegistry = it.extend<{ registry: { address: string; cwd: string } }>({
registry: async (
// Vitest requires this argument is destructured even if no properties are used
// eslint-disable-next-line no-empty-pattern
{},
use,
) => {
try {
if (!(await fs.stat(distDir)).isDirectory()) {
throw new Error(`found unexpected non-directory at "${distDir}"`)
}
} catch (err) {
throw new Error(
'"dist" directory does not exist or is not a directory. The project must be built before running E2E tests.',
{ cause: err },
)
}
const verdaccioStorageDir = await fs.mkdtemp(path.join(os.tmpdir(), `${tempdirPrefix}verdaccio-storage`))
const server: http.Server = (await runServer({
self_path: __dirname,
storage: verdaccioStorageDir,
web: { title: 'Test Registry' },
max_body_size: '128mb',
// Disable user registration
max_users: -1,
log: { level: 'fatal' },
uplinks: {
npmjs: {
url: 'https://registry.npmjs.org/',
maxage: '1d',
cache: true,
},
},
packages: {
'@*/*': {
access: '$all',
publish: 'noone',
proxy: 'npmjs',
},
'netlify-cli': {
access: '$all',
publish: '$all',
},
netlify: {
access: '$all',
publish: '$all',
},
'**': {
access: '$all',
publish: 'noone',
proxy: 'npmjs',
},
},
})) as http.Server
await Promise.all([
Promise.race([
events.once(server, 'listening'),
events.once(server, 'error').then(() => {
throw new Error('Verdaccio server failed to start')
}),
]),
server.listen(),
])
const address = server.address()
if (address === null || typeof address === 'string') {
throw new Error('Failed to open Verdaccio server')
}
const registryURL = new URL(
`http://${
address.family === 'IPv6' && address.address === '::' ? 'localhost' : address.address
}:${address.port.toString()}`,
)
// The CLI publishing process modifies the workspace, so copy it to a temporary directory. This
// lets us avoid contaminating the user's workspace when running these tests locally.
const publishWorkspace = await fs.mkdtemp(path.join(os.tmpdir(), `${tempdirPrefix}publish-workspace`))
await fs.cp(projectRoot, publishWorkspace, {
recursive: true,
verbatimSymlinks: true,
// At this point, the project is built. As long as we limit the prepublish script to built-
// ins, node_modules are not be necessary to publish the package.
filter: shouldCopyCLIFile,
})
await fs.writeFile(
path.join(publishWorkspace, '.npmrc'),
`//${registryURL.hostname}:${registryURL.port}/:_authToken=dummy`,
)
await execa('npm', ['publish', `--registry=${registryURL.toString()}`, '--tag=testing'], {
cwd: publishWorkspace,
stdio: debug.enabled ? 'inherit' : 'ignore',
})
// TODO: Figure out why calling this script is failing on Windows.
if (platform() !== 'win32') {
// Publishing `netlify` package
await execa.node(path.resolve(projectRoot, 'scripts/netlifyPackage.js'), {
cwd: publishWorkspace,
stdio: debug.enabled ? 'inherit' : 'ignore',
})
await execa('npm', ['publish', `--registry=${registryURL.toString()}`, '--tag=testing'], {
cwd: publishWorkspace,
stdio: debug.enabled ? 'inherit' : 'ignore',
})
}
await fs.rm(publishWorkspace, { force: true, recursive: true })
const testWorkspace = await fs.mkdtemp(path.join(os.tmpdir(), tempdirPrefix))
await use({
address: registryURL.toString(),
cwd: testWorkspace,
})
await Promise.all([
events.once(server, 'close'),
server.close(),
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
server.closeAllConnections(),
])
await fs.rm(testWorkspace, { force: true, recursive: true })
await fs.rm(verdaccioStorageDir, { force: true, recursive: true })
},
})
type Test = { packageName: string }
type InstallTest = Test & {
install: [cmd: string, args: string[]]
lockfile: string
cleanInstall: [cmd: string, args: string[]]
}
type RunTest = Test & { run: [cmd: string, args: string[]] }
const installTests: [packageManager: string, config: InstallTest][] = [
[
'npm',
{
packageName: 'netlify-cli',
install: ['npm', ['install', 'netlify-cli@testing']],
cleanInstall: ['npm', ['ci']],
lockfile: 'package-lock.json',
},
],
[
'pnpm',
{
packageName: 'netlify-cli',
install: ['pnpm', ['add', 'netlify-cli@testing']],
cleanInstall: ['pnpm', ['install', '--frozen-lockfile']],
lockfile: 'pnpm-lock.yaml',
},
],
[
'yarn',
{
packageName: 'netlify-cli',
install: ['yarn', ['add', 'netlify-cli@testing']],
cleanInstall: ['yarn', ['install', '--frozen-lockfile']],
lockfile: 'yarn.lock',
},
],
]
describe.each(installTests)('%s → installs the cli and runs commands without errors', (packageManager, config) => {
// TODO: Figure out why this flow is failing on Windows.
const npxOnWindows = platform() === 'win32' && 'run' in config
itWithMockNpmRegistry.skipIf(npxOnWindows)('runs the commands without errors', async ({ registry }) => {
// Install
const cwd = registry.cwd
const installResult = await execa(...config.install, {
cwd,
env: { npm_config_registry: registry.address },
all: true,
reject: false,
})
if (installResult.exitCode !== 0) {
throw new Error(
`Install failed for ${packageManager}\nExit code: ${installResult.exitCode.toString()}\n\n${
installResult.all || ''
}`,
)
}
expect(
existsSync(path.join(cwd, config.lockfile)),
`Generated lock file ${config.lockfile} does not exist in ${cwd}`,
).toBe(true)
// Regression test: ensure we don't trigger known `npm ci` bugs: https://github.com/npm/cli/issues/7622.
const cleanInstallResult = await execa(...config.cleanInstall, {
cwd,
env: { npm_config_registry: registry.address },
all: true,
reject: false,
})
if (cleanInstallResult.exitCode !== 0) {
throw new Error(
`Clean install failed for ${packageManager}\nExit code: ${cleanInstallResult.exitCode.toString()}\n\n${
cleanInstallResult.all || ''
}`,
)
}
const binary = path.resolve(path.join(cwd, `./node_modules/.bin/netlify${platform() === 'win32' ? '.cmd' : ''}`))
// Help
const helpResult = await execa(binary, ['help'], { cwd, all: true, reject: false })
if (helpResult.exitCode !== 0) {
throw new Error(
`Help command failed: ${binary} help\nExit code: ${helpResult.exitCode.toString()}\n\n${helpResult.all || ''}`,
)
}
const helpOutput = helpResult.stdout
expect(helpOutput.trim(), `Help command does not start with '⬥ Netlify CLI'\\n\\nVERSION: ${helpOutput}`).toMatch(
/^⬥ Netlify CLI\n\nVERSION/,
)
expect(
helpOutput,
`Help command does not include '${config.packageName}/${pkg.version}':\n\n${helpOutput}`,
).toContain(`${config.packageName}/${pkg.version}`)
expect(helpOutput, `Help command does not include '$ netlify [COMMAND]':\n\n${helpOutput}`).toMatch(
'$ netlify [COMMAND]',
)
// Unlink
const unlinkResult = await execa(binary, ['unlink'], { cwd, all: true, reject: false })
if (unlinkResult.exitCode !== 0) {
throw new Error(
`Unlink command failed: ${binary} unlink\nExit code: ${unlinkResult.exitCode.toString()}\n\n${
unlinkResult.all || ''
}`,
)
}
const unlinkOutput = unlinkResult.stdout
expect(unlinkOutput, `Unlink command includes command context':\n\n${unlinkOutput}`).toContain(
`Run netlify link to link it`,
)
})
})
const runTests: [packageManager: string, config: RunTest][] = [
[
'npx',
{
packageName: 'netlify',
run: ['npx', ['-y', 'netlify@testing']],
},
],
[
'pnpx',
{
packageName: 'netlify',
run: ['pnpx', ['netlify@testing']],
},
],
]
describe.each(runTests)('%s → runs cli commands without errors', (packageManager, config) => {
// TODO: Figure out why this flow is failing on Windows.
const npxOnWindows = platform() === 'win32' && 'run' in config
itWithMockNpmRegistry.skipIf(npxOnWindows)('runs commands without errors', async ({ registry }) => {
const [cmd, args] = config.run
const env = {
npm_config_registry: registry.address,
}
// Install
const installResult = await execa(cmd, [...args], { env, all: true, reject: false })
if (installResult.exitCode !== 0) {
throw new Error(
`Install failed for ${packageManager}\nExit code: ${installResult.exitCode.toString()}\n\n${
installResult.all || ''
}`,
)
}
// Help
const helpResult = await execa(cmd, [...args, 'help'], { env, all: true, reject: false })
if (helpResult.exitCode !== 0) {
throw new Error(
`Help command failed: ${cmd} ${args.join(' ')} help\nExit code: ${helpResult.exitCode.toString()}\n\n${
helpResult.all || ''
}`,
)
}
const helpOutput = helpResult.stdout
expect(helpOutput.trim(), `Help command does not start with '⬥ Netlify CLI'\\n\\nVERSION: ${helpOutput}`).toMatch(
/^⬥ Netlify CLI\n\nVERSION/,
)
expect(
helpOutput,
`Help command does not include '${config.packageName}/${pkg.version}':\n\n${helpOutput}`,
).toContain(`${config.packageName}/${pkg.version}`)
expect(helpOutput, `Help command does not include '$ netlify [COMMAND]':\n\n${helpOutput}`).toMatch(
'$ netlify [COMMAND]',
)
// Unlink
const unlinkResult = await execa(cmd, [...args, 'unlink'], { env, all: true, reject: false })
if (unlinkResult.exitCode !== 0) {
throw new Error(
`Unlink command failed: ${cmd} ${args.join(' ')} unlink\nExit code: ${unlinkResult.exitCode.toString()}\n\n${
unlinkResult.all || ''
}`,
)
}
const unlinkOutput = unlinkResult.stdout
expect(unlinkOutput, `Unlink command includes command context':\n\n${unlinkOutput}`).toContain(
`Run ${cmd} netlify link to link it`,
)
})
})