Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

test(macos): cover the empty-file cleanup after a failed start

EtienneLescoteddab6e1ec9b
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index ae00fd98b..843d76301 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -108,6 +108,7 @@ import { scoreDeviceNameMatch } from "../recording/deviceNameMatching";
import {
describeSalvagedTake,
nativeMacSalvageTarget,
+ removeEmptyNativeMacCapture,
salvageNativeMacCapture,
} from "../recording/nativeMacCaptureSalvage";
import {
@@ -3228,14 +3229,7 @@ export function registerIpcHandlers(
nativeMacPauseRanges = [];
nativeMacIsPaused = false;
if (failedOutputPath) {
- await fs
- .stat(failedOutputPath)
- .then((statInfo) =>
- statInfo.isFile() && statInfo.size === 0
- ? fs.rm(failedOutputPath, { force: true })
- : undefined,
- )
- .catch(() => undefined);
+ await removeEmptyNativeMacCapture(failedOutputPath);
}
await stopCursorRecording();
return { success: false, error: error instanceof Error ? error.message : String(error) };
diff --git a/electron/recording/nativeMacCaptureSalvage.test.ts b/electron/recording/nativeMacCaptureSalvage.test.ts
index 1cf39180c..18102e482 100644
--- a/electron/recording/nativeMacCaptureSalvage.test.ts
+++ b/electron/recording/nativeMacCaptureSalvage.test.ts
@@ -7,6 +7,7 @@ import {
describeSalvagedTake,
inspectNativeMacCapture,
nativeMacSalvageTarget,
+ removeEmptyNativeMacCapture,
salvageNativeMacCapture,
} from "./nativeMacCaptureSalvage";
@@ -233,6 +234,30 @@ describe("nativeMacSalvageTarget", () => {
});
});
+describe("removeEmptyNativeMacCapture", () => {
+ it("deletes the empty file a start that never began leaves", async () => {
+ const file = path.join(dir, "empty.mp4");
+ await fs.writeFile(file, "");
+ await removeEmptyNativeMacCapture(file);
+ await expect(fs.stat(file)).rejects.toThrow();
+ });
+
+ it("never deletes a file with a single byte in it", async () => {
+ const file = await take("writer-died-1s-no-moof", 1);
+ await removeEmptyNativeMacCapture(file);
+ expect(await sizeOf(file)).toBe(1);
+ });
+
+ it("leaves a directory alone and says nothing when the file is missing", async () => {
+ const sub = await fs.mkdtemp(path.join(dir, "sub-"));
+ await removeEmptyNativeMacCapture(sub);
+ expect((await fs.stat(sub)).isDirectory()).toBe(true);
+ await expect(removeEmptyNativeMacCapture(path.join(dir, "missing.mp4"))).resolves.toBe(
+ undefined,
+ );
+ });
+});
+
describe("describeSalvagedTake", () => {
/** Verbatim from the helper, in the disk-full end-to-end run. */
it("keeps the helper's sentence and adds a description that says something", () => {
diff --git a/electron/recording/nativeMacCaptureSalvage.ts b/electron/recording/nativeMacCaptureSalvage.ts
index 90f3d6dc6..6071e1d09 100644
--- a/electron/recording/nativeMacCaptureSalvage.ts
+++ b/electron/recording/nativeMacCaptureSalvage.ts
@@ -509,6 +509,24 @@ export function nativeMacSalvageTarget(
return !result.ok && result.exited && targetPath ? targetPath : null;
}
+/**
+ * Deletes the file a failed start left, only while it is still empty.
+ *
+ * The helper creates its output before the first frame, so a start that never
+ * began leaves a 0-byte mp4. Any byte on disk keeps the file: an empty one holds
+ * nothing to lose. Never throws.
+ */
+export async function removeEmptyNativeMacCapture(filePath: string): Promise<void> {
+ try {
+ const stats = await fs.stat(filePath);
+ if (stats.isFile() && stats.size === 0) {
+ await fs.rm(filePath, { force: true });
+ }
+ } catch {
+ // Nothing there, or nothing we may remove: either way nothing is lost.
+ }
+}
+
/** NSError descriptions that say nothing a person can act on. */
const GENERIC_ERROR_DESCRIPTIONS = new Set(["The operation could not be completed"]);