|
| 1 | +import { describe, it, expect, afterEach, vi } from "vite-plus/test"; |
| 2 | +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { delimiter, join } from "node:path"; |
| 5 | +import { runViteInstall } from "./run-install.js"; |
| 6 | +import type { Inputs } from "./types.js"; |
| 7 | + |
| 8 | +const baseInputs: Inputs = { |
| 9 | + version: "latest", |
| 10 | + nodeVersion: undefined, |
| 11 | + nodeVersionFile: undefined, |
| 12 | + workingDirectory: undefined, |
| 13 | + runInstall: [], |
| 14 | + cache: false, |
| 15 | + cacheDependencyPath: undefined, |
| 16 | + registryUrl: undefined, |
| 17 | + scope: undefined, |
| 18 | +}; |
| 19 | + |
| 20 | +describe("runViteInstall", () => { |
| 21 | + let tempDir: string | undefined; |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + vi.unstubAllEnvs(); |
| 25 | + if (tempDir) { |
| 26 | + rmSync(tempDir, { recursive: true, force: true }); |
| 27 | + tempDir = undefined; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + it("stops after the first failed install", async () => { |
| 32 | + tempDir = mkdtempSync(join(tmpdir(), "setup-vp-")); |
| 33 | + const binDir = join(tempDir, "bin"); |
| 34 | + const appDir = join(tempDir, "packages", "app"); |
| 35 | + const libDir = join(tempDir, "packages", "lib"); |
| 36 | + const callsLog = join(tempDir, "calls.log"); |
| 37 | + |
| 38 | + mkdirSync(binDir); |
| 39 | + mkdirSync(appDir, { recursive: true }); |
| 40 | + mkdirSync(libDir, { recursive: true }); |
| 41 | + writeFileSync(join(appDir, ".fail-vp-install"), ""); |
| 42 | + |
| 43 | + writeFileSync( |
| 44 | + join(binDir, "vp"), |
| 45 | + [ |
| 46 | + "#!/bin/sh", |
| 47 | + 'printf "%s %s\\n" "$PWD" "$*" >> "$VP_CALLS_LOG"', |
| 48 | + 'if [ -f ".fail-vp-install" ]; then', |
| 49 | + ' echo "install failed" >&2', |
| 50 | + " exit 1", |
| 51 | + "fi", |
| 52 | + "exit 0", |
| 53 | + "", |
| 54 | + ].join("\n"), |
| 55 | + ); |
| 56 | + chmodSync(join(binDir, "vp"), 0o755); |
| 57 | + |
| 58 | + writeFileSync( |
| 59 | + join(binDir, "vp.cmd"), |
| 60 | + [ |
| 61 | + "@echo off", |
| 62 | + 'echo %CD% %*>>"%VP_CALLS_LOG%"', |
| 63 | + 'if exist ".fail-vp-install" (', |
| 64 | + " echo install failed 1>&2", |
| 65 | + " exit /b 1", |
| 66 | + ")", |
| 67 | + "exit /b 0", |
| 68 | + "", |
| 69 | + ].join("\r\n"), |
| 70 | + ); |
| 71 | + |
| 72 | + vi.stubEnv("GITHUB_WORKSPACE", tempDir); |
| 73 | + vi.stubEnv("PATH", `${binDir}${delimiter}${process.env.PATH ?? ""}`); |
| 74 | + vi.stubEnv("VP_CALLS_LOG", callsLog); |
| 75 | + |
| 76 | + await expect( |
| 77 | + runViteInstall({ |
| 78 | + ...baseInputs, |
| 79 | + runInstall: [{ cwd: "packages/app" }, { cwd: "packages/lib" }], |
| 80 | + }), |
| 81 | + ).rejects.toThrow(`Command "vp install" (cwd: ${appDir}) exited with code 1`); |
| 82 | + |
| 83 | + const calls = readFileSync(callsLog, "utf8").trim().split(/\r?\n/); |
| 84 | + expect(calls).toHaveLength(1); |
| 85 | + expect(calls[0]).toContain(`${join("packages", "app")} install`); |
| 86 | + }); |
| 87 | +}); |
0 commit comments