fix: use dependency injection for image-downloader fs mocks
The _fsForTest mutable object pattern fails on Linux Bun because test and source files get different module instances. Switch to passing fs operations as an optional parameter with defaults. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac8e22e2f8
commit
cff360d00a
2 files changed
+40
-21
No files matched your search
@@ -2,11 +2,12 @@ import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import type { Octokits } from "../api/client";
|
||||
|
||||
// Mutable fs reference for cross-platform test compatibility.
|
||||
// On Linux, Bun gives test and source files different fs/promises objects,
|
||||
// so spyOn(fs, "mkdir") in a test file won't intercept calls here.
|
||||
// Tests override these methods directly instead.
|
||||
export const _fsForTest = {
|
||||
export type FsOps = {
|
||||
mkdir: typeof fs.mkdir;
|
||||
writeFile: typeof fs.writeFile;
|
||||
};
|
||||
|
||||
const defaultFsOps: FsOps = {
|
||||
mkdir: fs.mkdir.bind(fs),
|
||||
writeFile: fs.writeFile.bind(fs),
|
||||
};
|
||||
@@ -72,11 +73,12 @@ export async function downloadCommentImages(
|
||||
owner: string,
|
||||
repo: string,
|
||||
comments: CommentWithImages[],
|
||||
fsOps: FsOps = defaultFsOps,
|
||||
): Promise<Map<string, string>> {
|
||||
const urlToPathMap = new Map<string, string>();
|
||||
const downloadsDir = "/tmp/github-images";
|
||||
|
||||
await _fsForTest.mkdir(downloadsDir, { recursive: true });
|
||||
await fsOps.mkdir(downloadsDir, { recursive: true });
|
||||
|
||||
const { imageRegex, htmlImgRegex } = buildImageRegexes();
|
||||
|
||||
@@ -226,7 +228,7 @@ export async function downloadCommentImages(
|
||||
const arrayBuffer = await imageResponse.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
await _fsForTest.writeFile(localPath, buffer);
|
||||
await fsOps.writeFile(localPath, buffer);
|
||||
console.log(`✓ Saved: ${localPath}`);
|
||||
|
||||
urlToPathMap.set(originalUrl, localPath);
|
||||
|
||||
Reference in new issue
Block a user