From ddadca3a3a836170081ed9361758e4e610056826 Mon Sep 17 00:00:00 2001 From: Julien Goux Date: Fri, 10 Apr 2026 08:33:39 +0200 Subject: [PATCH] fix: await main function --- src/main.test.ts | 57 +++++++++++++++++++++++++++++------------------- src/main.ts | 2 +- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/main.test.ts b/src/main.test.ts index d9995c4..022dd18 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -167,39 +167,52 @@ async function getMainModule(): Promise { return mainModule; } -async function waitForCalls(assertion: () => void): Promise { - let failure: Error | null = null; - - for (let attempt = 0; attempt < 50; attempt += 1) { - try { - assertion(); - return; - } catch (error) { - failure = error instanceof Error ? error : new Error(String(error)); - await Bun.sleep(10); - } - } - - throw failure ?? new Error("Timed out waiting for action side effects"); -} - -test("runs the action entrypoint with omitted version and latest fallback", async () => { +test("awaits the action entrypoint with omitted version and latest fallback", async () => { process.env.GITHUB_WORKSPACE = repo; const cliDir = createFakeCli("supabase 2.84.2"); - const spies = createActionSpies("", cliDir, "/latest/download/"); + let startDownload!: () => void; + let finishDownload!: () => void; + const downloadStarted = new Promise((resolve) => { + startDownload = resolve; + }); + const downloadFinished = new Promise((resolve) => { + finishDownload = () => resolve(path.join(os.tmpdir(), "supabase-cli.tar.gz")); + }); + const spies = { + getInput: spyOn(core, "getInput").mockReturnValue(""), + setOutput: spyOn(core, "setOutput").mockImplementation(() => {}), + addPath: spyOn(core, "addPath").mockImplementation(() => {}), + exportVariable: spyOn(core, "exportVariable").mockImplementation(() => {}), + setFailed: spyOn(core, "setFailed").mockImplementation(() => {}), + downloadTool: spyOn(tc, "downloadTool").mockImplementation(async (url: string) => { + expect(url).toContain("/latest/download/"); + startDownload(); + return downloadFinished; + }), + extractTar: spyOn(tc, "extractTar").mockImplementation(async () => cliDir), + }; const originalArgv1 = process.argv[1]; process.argv[1] = defaultEntrypoint; try { - await import(`./main.ts?entrypoint=${Date.now()}`); - await waitForCalls(() => { - expect(spies.setOutput).toHaveBeenCalledWith("version", "supabase 2.84.2"); - expect(spies.addPath).toHaveBeenCalledWith(cliDir); + let importSettled = false; + const entrypoint = import(`./main.ts?entrypoint=${Date.now()}`).finally(() => { + importSettled = true; }); + + await downloadStarted; + await Bun.sleep(0); + + expect(importSettled).toBe(false); + + finishDownload(); + await entrypoint; } finally { process.argv[1] = originalArgv1 ?? ""; } + expect(spies.setOutput).toHaveBeenCalledWith("version", "supabase 2.84.2"); + expect(spies.addPath).toHaveBeenCalledWith(cliDir); expect(spies.exportVariable).toHaveBeenCalledWith(CLI_CONFIG_REGISTRY, "ghcr.io"); expect(spies.setFailed).not.toHaveBeenCalled(); }); diff --git a/src/main.ts b/src/main.ts index 535b224..77655af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -204,5 +204,5 @@ export async function run(): Promise { } if (process.argv[1] === fileURLToPath(import.meta.url)) { - void run(); + await run(); }