Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

1.6 KB
Raw Blame
import { join } from "node:path";
import { mkdir } from "node:fs/promises";
import { afterEach, expect, test } from "bun:test";
import { acquireScanExecutionLock } from "../src/runtime.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";

const { temporaryDirectory, cleanup } = createApiTestFixtures();
afterEach(cleanup);

test("scan execution excludes a second process and recovers after the owner exits", async () => {
  const root = await temporaryDirectory();
  const state = join(root, "state");
  const scan = join(root, "scan");
  await mkdir(scan);
  const module = new URL("../src/runtime.ts", import.meta.url).href;
  const child = Bun.spawn(
    [
      process.execPath,
      "--eval",
      `
    const { acquireScanExecutionLock } = await import(${JSON.stringify(module)});
    const release = await acquireScanExecutionLock(${JSON.stringify(state)}, ${JSON.stringify(scan)});
    console.log("locked");
    process.on("exit", release);
    setInterval(() => {}, 60_000);
  `,
    ],
    { stdout: "pipe", stderr: "pipe" },
  );
  try {
    const reader = child.stdout.getReader();
    expect(new TextDecoder().decode((await reader.read()).value)).toBe(
      "locked\n",
    );
    reader.releaseLock();
    await expect(acquireScanExecutionLock(state, scan)).rejects.toThrow(
      "already running",
    );
  } finally {
    child.kill("SIGKILL");
    await child.exited;
  }
  const release = await acquireScanExecutionLock(state, scan);
  try {
    await expect(acquireScanExecutionLock(state, scan)).rejects.toThrow(
      "already running",
    );
  } finally {
    release();
  }
  (await acquireScanExecutionLock(state, scan))();
});