Files
pnpm/exec/lifecycle/test/index.ts
Brandon Cheng a2f5f5c990 test: fix file system race conditions in tests by switching to an IPC server (#7472)
* test: create new @pnpm/test-ipc-server private util package

* test: use @pnpm/test-ipc-server for previously refactored tests

* test: use @pnpm/test-ipc-server for tests using json-append

* test: change how --no-bail is passed to avoid passing it to scripts

This test began failing after the conversion to use
`@pnpm/test-echo-server` since the `--no-bail` script was being passed
to scripts.

Changing how --no-bail is configured to fix this test.

* test: use @pnpm/test-ipc-server in exec/lifecycle fixture tests

* test: use @pnpm/test-ipc-server in pkg-manager/headless fixture tests

* test: use @pnpm/test-ipc-server in exec/prepare-package fixture tests

* test: switch pnpm test from json-append to @pnpm.e2e/hello-world-js-bin

* test: fix and re-enable 'rebuild multiple packages in correct order'

The pnpm-workspace.yaml file didn't contain all packages, causing:

```
2023-12-22T02:24:46.2277155Z FAIL test/recursive.ts
2023-12-22T02:24:46.2277881Z   ● rebuild multiple packages in correct order
2023-12-22T02:24:46.2278348Z
2023-12-22T02:24:46.2278734Z     expect(received).toStrictEqual(expected) // deep equality
2023-12-22T02:24:46.2279302Z
2023-12-22T02:24:46.2279517Z     - Expected  - 1
2023-12-22T02:24:46.2279932Z     + Received  + 0
2023-12-22T02:24:46.2280186Z
2023-12-22T02:24:46.2280791Z       Array [
2023-12-22T02:24:46.2281256Z         "project-1",
2023-12-22T02:24:46.2281733Z     -   "project-2",
2023-12-22T02:24:46.2282135Z       ]
2023-12-22T02:24:46.2282334Z
2023-12-22T02:24:46.2282475Z       216 |   }, [])
2023-12-22T02:24:46.2282870Z       217 |
2023-12-22T02:24:46.2283788Z     > 218 |   expect(server1.getMessages()).toStrictEqual(['project-1', 'project-2'])
2023-12-22T02:24:46.2284725Z           |                                 ^
2023-12-22T02:24:46.2285802Z       219 |   expect(server2.getMessages()).toStrictEqual(['project-1', 'project-3'])
2023-12-22T02:24:46.2286683Z       220 | })
2023-12-22T02:24:46.2287049Z       221 |
2023-12-22T02:24:46.2287269Z
2023-12-22T02:24:46.2287588Z       at Object.<anonymous> (test/recursive.ts:218:33)
```
2024-01-01 16:40:03 +01:00

103 lines
3.3 KiB
TypeScript

/// <reference path="../../../__typings__/index.d.ts"/>
import path from 'path'
import { runLifecycleHook, runPostinstallHooks } from '@pnpm/lifecycle'
import { PnpmError } from '@pnpm/error'
import { createTestIpcServer } from '@pnpm/test-ipc-server'
import { fixtures } from '@pnpm/test-fixtures'
const f = fixtures(path.join(__dirname, 'fixtures'))
const rootModulesDir = path.join(__dirname, '..', 'node_modules')
test('runLifecycleHook()', async () => {
const pkgRoot = f.find('simple')
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('postinstall', pkg, {
depPath: '/simple/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect(server.getLines()).toStrictEqual(['install'])
})
test('runLifecycleHook() escapes the args passed to the script', async () => {
const pkgRoot = f.find('escape-args')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('echo', pkg, {
depPath: '/escape-args/1.0.0',
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
args: ['Revert "feature (#1)"'],
})
expect((await import(path.join(pkgRoot, 'output.json'))).default).toStrictEqual(['Revert "feature (#1)"'])
})
test('runLifecycleHook() sets frozen-lockfile to false', async () => {
const pkgRoot = f.find('inspect-frozen-lockfile')
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('postinstall', pkg, {
depPath: '/inspect-frozen-lockfile/1.0.0',
pkgRoot,
rawConfig: {
'frozen-lockfile': true,
},
rootModulesDir,
unsafePerm: true,
})
expect(server.getLines()).toStrictEqual(['empty string'])
})
test('runPostinstallHooks()', async () => {
const pkgRoot = f.find('with-many-scripts')
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
await runPostinstallHooks({
depPath: '/with-many-scripts/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect(server.getLines()).toStrictEqual(['preinstall', 'install', 'postinstall'])
})
test('runLifecycleHook() should throw an error while missing script start or file server.js', async () => {
const pkgRoot = f.find('without-script-start-serverjs')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await expect(
runLifecycleHook('start', pkg, {
depPath: '/without-script-start-serverjs/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
).rejects.toThrow(new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js'))
})
test('preinstall script does not trigger node-gyp rebuild', async () => {
const pkgRoot = f.find('gyp-with-preinstall')
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
await runPostinstallHooks({
depPath: '/gyp-with-preinstall/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect(server.getLines()).toStrictEqual(['preinstall'])
})