Files
effect/packages/platform/node-shared/test/NodeChildProcessSpawner.test.ts
T

93 lines
3.3 KiB
TypeScript

import { buildSpawnOptions } from "@effect/platform-node-shared/internal/nodeChildProcessSpawner"
import * as NodeChildProcessSpawner from "@effect/platform-node-shared/NodeChildProcessSpawner"
import * as NodeFileSystem from "@effect/platform-node-shared/NodeFileSystem"
import * as NodePath from "@effect/platform-node-shared/NodePath"
import { assert, describe, it } from "@effect/vitest"
import * as ChildProcessSpawnerTest from "effect-test/unstable/process/ChildProcessSpawnerTest"
import * as Effect from "effect/Effect"
import * as FileSystem from "effect/FileSystem"
import * as Layer from "effect/Layer"
import * as ChildProcess from "effect/unstable/process/ChildProcess"
const NodeServices = NodeChildProcessSpawner.layer.pipe(
Layer.provideMerge(Layer.mergeAll(
NodeFileSystem.layer,
NodePath.layer
))
)
ChildProcessSpawnerTest.suite("NodeChildProcessSpawner", NodeServices, {
processGroups: true
})
describe("buildSpawnOptions", () => {
const base = { stdio: "pipe" } as const
it("defaults to hiding non-detached Windows children", () => {
assert.deepStrictEqual(buildSpawnOptions({}, base, "win32"), {
stdio: "pipe",
detached: false,
shell: undefined,
windowsHide: true
})
assert.deepStrictEqual(buildSpawnOptions({ detached: true }, base, "win32"), {
stdio: "pipe",
detached: true,
shell: undefined,
windowsHide: false
})
assert.deepStrictEqual(buildSpawnOptions({ detached: false }, base, "win32"), {
stdio: "pipe",
detached: false,
shell: undefined,
windowsHide: true
})
})
it("allows windowsHide to be configured independently of detached", () => {
assert.deepStrictEqual(
buildSpawnOptions({ detached: false, windowsHide: false }, base, "win32"),
{
stdio: "pipe",
detached: false,
shell: undefined,
windowsHide: false
}
)
assert.deepStrictEqual(
buildSpawnOptions({ detached: true, windowsHide: true }, base, "win32"),
{
stdio: "pipe",
detached: true,
shell: undefined,
windowsHide: true
}
)
})
})
it.live("kills every process in a pipeline", () =>
Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const directory = yield* fs.makeTempDirectoryScoped()
const rootHeartbeat = `${directory}/root-heartbeat`
const childHeartbeat = `${directory}/child-heartbeat`
const handle = yield* ChildProcess.make(
"sh",
["-c", "while :; do printf x >> \"$1\"; sleep 0.01; done", "pipeline-root", rootHeartbeat]
).pipe(ChildProcess.pipeTo(ChildProcess.make(
"sh",
["-c", "while :; do printf x >> \"$1\"; sleep 0.01; done", "pipeline-child", childHeartbeat]
)))
yield* Effect.sleep("100 millis")
yield* handle.kill({ killSignal: "SIGKILL" })
const rootSizeAfterKill = (yield* fs.stat(rootHeartbeat)).size
const childSizeAfterKill = (yield* fs.stat(childHeartbeat)).size
yield* Effect.sleep("100 millis")
const rootFinalSize = (yield* fs.stat(rootHeartbeat)).size
const childFinalSize = (yield* fs.stat(childHeartbeat)).size
assert.strictEqual(rootFinalSize, rootSizeAfterKill)
assert.strictEqual(childFinalSize, childSizeAfterKill)
}).pipe(Effect.scoped, Effect.provide(NodeServices)))