Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions packages/cli/src/whisper/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, afterEach } from "vitest";
import { writeFileSync, readFileSync, mkdirSync, rmSync } from "node:fs";
import { writeFileSync, readFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import {
Expand All @@ -14,8 +14,7 @@ import {
import { detectSpeechOnset } from "./transcribe.js";

function tmpFile(name: string, content: string): string {
const dir = join(tmpdir(), `hf-normalize-test-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const dir = mkdtempSync(join(tmpdir(), "hf-normalize-test-"));
dirs.push(dir);
const path = join(dir, name);
writeFileSync(path, content);
Expand Down Expand Up @@ -478,8 +477,7 @@ describe("whisper-cpp zero-duration interpolation", () => {

describe("patchCaptionHtml", () => {
it("replaces const script = [] in HTML files", () => {
const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);

const html = `<html><body><script>
Expand All @@ -501,8 +499,7 @@ describe("patchCaptionHtml", () => {
});

it("replaces const TRANSCRIPT = [] variant", () => {
const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);

const html = `<script>const TRANSCRIPT = [];</script>`;
Expand All @@ -516,8 +513,7 @@ describe("patchCaptionHtml", () => {
});

it("does not modify HTML files without matching script patterns", () => {
const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);

const html = `<html><body><script>console.log("hello");</script></body></html>`;
Expand All @@ -530,8 +526,7 @@ describe("patchCaptionHtml", () => {
});

it("skips empty word arrays", () => {
const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);

const html = `<script>const script = [];</script>`;
Expand Down Expand Up @@ -572,7 +567,7 @@ describe("detectSpeechOnset", () => {
const amplitude = energyFn(t);
buf.writeInt16LE(Math.round(amplitude * 32767), 44 + i * 2);
}
const path = join(tmpdir(), `hf-wav-test-${Date.now()}-${Math.floor(Math.random() * 1e6)}.wav`);
const path = join(mkdtempSync(join(tmpdir(), "hf-wav-test-")), "tone.wav");
writeFileSync(path, buf);
dirs.push(path);
return path;
Expand Down
7 changes: 5 additions & 2 deletions scripts/generate-catalog-previews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
readFileSync,
existsSync,
mkdirSync,
mkdtempSync,
cpSync,
rmSync,
writeFileSync,
Expand Down Expand Up @@ -170,8 +171,10 @@ export async function prepareProjectDir(
item: CatalogItem,
options: PrepareOptions = {},
): Promise<string> {
const tmpDir = join(tmpdir(), `hf-catalog-${item.name}-${Date.now()}`);
mkdirSync(tmpDir, { recursive: true });
// mkdtemp rather than a name built from `Date.now()`: it picks the random
// suffix and creates the directory 0700 in one syscall, so nothing can
// pre-create or symlink the path between choosing it and making it.
const tmpDir = mkdtempSync(join(tmpdir(), `hf-catalog-${item.name}-`));
cpSync(item.sourceDir, tmpDir, { recursive: true });
mirrorRegistryTargets(tmpDir);

Expand Down
Loading