|
1 | | -import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 1 | +import { |
| 2 | + type PathLike, |
| 3 | + chmodSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + realpathSync, |
| 7 | + rmSync, |
| 8 | + symlinkSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
2 | 11 | import { tmpdir } from "node:os"; |
3 | 12 | import { join, resolve } from "node:path"; |
4 | 13 | import { afterEach, describe, expect, it, vi } from "vitest"; |
@@ -173,6 +182,103 @@ describe("findFfBinary", () => { |
173 | 182 | expect(findFfBinary("ffmpeg")).toBe(projectBinary); |
174 | 183 | }); |
175 | 184 |
|
| 185 | + function installerProject( |
| 186 | + name: "ffmpeg" | "ffprobe", |
| 187 | + { viaInstaller = false, mode = 0o755 } = {}, |
| 188 | + ) { |
| 189 | + const project = realpathSync(mkdtempSync(join(tmpdir(), "hf-ff-installer-"))); |
| 190 | + const platformPackage = `@${name}-installer/${process.platform}-${process.arch}`; |
| 191 | + const packageDir = viaInstaller |
| 192 | + ? join(project, "store", "node_modules", platformPackage) |
| 193 | + : join(project, "node_modules", platformPackage); |
| 194 | + mkdirSync(packageDir, { recursive: true }); |
| 195 | + writeFileSync(join(packageDir, "package.json"), "{}"); |
| 196 | + if (viaInstaller) { |
| 197 | + const installerDir = join(project, "store", "node_modules", `@${name}-installer`, name); |
| 198 | + mkdirSync(installerDir, { recursive: true }); |
| 199 | + writeFileSync(join(installerDir, "package.json"), "{}"); |
| 200 | + mkdirSync(join(project, "node_modules", `@${name}-installer`), { recursive: true }); |
| 201 | + symlinkSync( |
| 202 | + installerDir, |
| 203 | + join(project, "node_modules", `@${name}-installer`, name), |
| 204 | + "junction", |
| 205 | + ); |
| 206 | + } |
| 207 | + const binary = join(packageDir, process.platform === "win32" ? `${name}.exe` : name); |
| 208 | + writeFileSync(binary, ""); |
| 209 | + chmodSync(binary, mode); |
| 210 | + return { project, binary }; |
| 211 | + } |
| 212 | + |
| 213 | + async function findInProjectDir( |
| 214 | + project: string, |
| 215 | + name: "ffmpeg" | "ffprobe", |
| 216 | + systemBinaries: string[] = [], |
| 217 | + ) { |
| 218 | + vi.stubEnv("HYPERFRAMES_FFMPEG_PATH", ""); |
| 219 | + vi.stubEnv("HYPERFRAMES_FFPROBE_PATH", ""); |
| 220 | + process.env.PATH = ""; |
| 221 | + vi.spyOn(process, "cwd").mockReturnValue(project); |
| 222 | + vi.resetModules(); |
| 223 | + vi.doMock("node:fs", async (importOriginal) => { |
| 224 | + const actual = await importOriginal<typeof import("node:fs")>(); |
| 225 | + const existsSync = (candidate: PathLike) => |
| 226 | + systemBinaries.includes(String(candidate)) || |
| 227 | + (String(candidate).startsWith(project) && actual.existsSync(candidate)); |
| 228 | + return { ...actual, existsSync, default: { ...actual, existsSync } }; |
| 229 | + }); |
| 230 | + vi.doMock("node:child_process", () => { |
| 231 | + const mocked = { |
| 232 | + execFileSync: () => { |
| 233 | + throw new Error("not found"); |
| 234 | + }, |
| 235 | + }; |
| 236 | + return { ...mocked, default: mocked }; |
| 237 | + }); |
| 238 | + try { |
| 239 | + const { findFfBinary } = await importFresh(); |
| 240 | + return findFfBinary(name); |
| 241 | + } finally { |
| 242 | + vi.unstubAllEnvs(); |
| 243 | + rmSync(project, { recursive: true, force: true }); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + it.each(["ffmpeg", "ffprobe"] as const)( |
| 248 | + "falls back to the project's @%s-installer package", |
| 249 | + async (name) => { |
| 250 | + const { project, binary } = installerProject(name); |
| 251 | + |
| 252 | + expect(await findInProjectDir(project, name)).toBe(binary); |
| 253 | + }, |
| 254 | + ); |
| 255 | + |
| 256 | + it("finds the platform package next to the installer package (pnpm layout)", async () => { |
| 257 | + const { project, binary } = installerProject("ffprobe", { viaInstaller: true }); |
| 258 | + |
| 259 | + expect(await findInProjectDir(project, "ffprobe")).toBe(binary); |
| 260 | + }); |
| 261 | + |
| 262 | + it.skipIf(process.platform === "win32")( |
| 263 | + "prefers a system install in a common dir over the installer package", |
| 264 | + async () => { |
| 265 | + const { project } = installerProject("ffprobe"); |
| 266 | + |
| 267 | + expect(await findInProjectDir(project, "ffprobe", ["/usr/bin/ffprobe"])).toBe( |
| 268 | + "/usr/bin/ffprobe", |
| 269 | + ); |
| 270 | + }, |
| 271 | + ); |
| 272 | + |
| 273 | + it.skipIf(process.platform === "win32")( |
| 274 | + "skips an installer binary that is not executable", |
| 275 | + async () => { |
| 276 | + const { project } = installerProject("ffprobe", { mode: 0o644 }); |
| 277 | + |
| 278 | + expect(await findInProjectDir(project, "ffprobe")).toBeUndefined(); |
| 279 | + }, |
| 280 | + ); |
| 281 | + |
176 | 282 | it("returns undefined when the binary is nowhere, and caches the miss until cleared", async () => { |
177 | 283 | delete process.env.HYPERFRAMES_FFMPEG_PATH; |
178 | 284 | Object.defineProperty(process, "platform", { value: "linux", configurable: true }); |
|
0 commit comments