mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-10 19:28:05 -04:00
* 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) ```
146 lines
4.4 KiB
TypeScript
146 lines
4.4 KiB
TypeScript
/// <reference lib="esnext.disposable" />
|
|
import execa from 'execa'
|
|
import fs from 'fs'
|
|
import net from 'net'
|
|
import path from 'path'
|
|
import { setTimeout } from 'timers/promises'
|
|
import { promisify } from 'util'
|
|
import { prepare } from '@pnpm/prepare'
|
|
import { createTestIpcServer } from '@pnpm/test-ipc-server'
|
|
|
|
const pnpmBin = path.join(__dirname, '../../../pnpm/bin/pnpm.cjs')
|
|
|
|
describe('TestEchoServer', () => {
|
|
describe('lifecycle', () => {
|
|
it('cleans up through Symbol.asyncDispose', async () => {
|
|
let listenPath: string
|
|
|
|
// eslint-disable-next-line no-lone-blocks
|
|
{
|
|
await using server = await createTestIpcServer()
|
|
listenPath = server.listenPath
|
|
await expect(fs.promises.access(server.listenPath)).resolves.not.toThrow()
|
|
}
|
|
|
|
// The Symbol.asyncDispose method should have been called by this point and
|
|
// removed the listening file.
|
|
await expect(fs.promises.access(listenPath)).rejects.toThrow('ENOENT')
|
|
})
|
|
|
|
it('throws if another server is listening on same socket', async () => {
|
|
await using server = await createTestIpcServer()
|
|
await expect(createTestIpcServer(server.listenPath)).rejects.toThrow('EADDRINUSE')
|
|
})
|
|
})
|
|
|
|
describe('message handling', () => {
|
|
it('receives messages', async () => {
|
|
await using server = await createTestIpcServer()
|
|
await using client = await createClient(server.listenPath)
|
|
await client.sendLine('hello')
|
|
await client.sendLine('world')
|
|
|
|
// Wait a short amount of time for the server to handle incoming messages.
|
|
await setTimeout(50)
|
|
expect(server.getBuffer()).toStrictEqual('hello\nworld\n')
|
|
expect(server.getLines()).toStrictEqual(['hello', 'world'])
|
|
})
|
|
|
|
it('clears messages', async () => {
|
|
await using server = await createTestIpcServer()
|
|
await using client = await createClient(server.listenPath)
|
|
await client.sendLine('hello')
|
|
await client.sendLine('world')
|
|
|
|
// Wait a short amount of time for the server to handle incoming messages.
|
|
await setTimeout(50)
|
|
expect(server.getLines()).toStrictEqual(['hello', 'world'])
|
|
|
|
server.clear()
|
|
|
|
expect(server.getLines()).toStrictEqual([])
|
|
})
|
|
})
|
|
|
|
describe('generated scripts', () => {
|
|
it('generates working send message script', async () => {
|
|
await using server = await createTestIpcServer()
|
|
|
|
prepare({
|
|
scripts: {
|
|
build: server.sendLineScript('build script'),
|
|
},
|
|
})
|
|
|
|
await execa('node', [pnpmBin, 'run', 'build'])
|
|
|
|
expect(server.getLines()).toStrictEqual(['build script'])
|
|
})
|
|
|
|
it('send message script works with &&', async () => {
|
|
await using server = await createTestIpcServer()
|
|
|
|
prepare({
|
|
scripts: {
|
|
build: `${server.sendLineScript('message1')} && ${server.sendLineScript('message2')}`,
|
|
},
|
|
})
|
|
|
|
await execa('node', [pnpmBin, 'run', 'build'])
|
|
|
|
expect(server.getLines()).toStrictEqual(['message1', 'message2'])
|
|
})
|
|
|
|
it('generates working stdin script', async () => {
|
|
await using server = await createTestIpcServer()
|
|
|
|
prepare({
|
|
scripts: {
|
|
build: `node -e "process.stdout.write('build script')" | ${server.generateSendStdinScript()}`,
|
|
},
|
|
})
|
|
|
|
await execa('node', [pnpmBin, 'run', 'build'])
|
|
|
|
expect(server.getLines()).toStrictEqual(['build script'])
|
|
})
|
|
})
|
|
|
|
it('has working client binary', async () => {
|
|
const project = prepare({
|
|
scripts: {
|
|
build: "node -e \"process.stdout.write('build script')\" | test-ipc-server-client ./test.sock",
|
|
},
|
|
})
|
|
|
|
await using server = await createTestIpcServer(path.join(project.dir(), './test.sock'))
|
|
|
|
await execa('node', [pnpmBin, 'run', 'build'])
|
|
|
|
expect(server.getLines()).toStrictEqual(['build script'])
|
|
})
|
|
})
|
|
|
|
interface TestClient extends AsyncDisposable {
|
|
sendLine: (message: string) => Promise<void>
|
|
}
|
|
|
|
function createClient (handle: string): Promise<TestClient> {
|
|
const client = net.connect(handle)
|
|
|
|
const write = promisify(client.write).bind(client)
|
|
const destroy = promisify(client.destroy).bind(client)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
client.once('error', reject)
|
|
client.once('ready', () => {
|
|
resolve({
|
|
sendLine: (message: string) => write(message + '\n'),
|
|
[Symbol.asyncDispose]: async () => {
|
|
await destroy(undefined)
|
|
},
|
|
})
|
|
})
|
|
})
|
|
}
|