test: add parameter options to usage of type ExecFileFn

This commit is contained in:
Jörg Krzeslak 2025-07-24 18:38:21 +02:00
parent c0f0dc5192
commit 08a833f1cf
22 changed files with 71 additions and 41 deletions

View file

@ -121,7 +121,7 @@ export async function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => { execFile("assimp", ["export", filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -67,7 +67,7 @@ export async function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => { execFile("ebook-convert", [filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -30,20 +30,25 @@ export function convert(
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("dvisvgm", [...inputArgs, filePath, "-o", targetPath], (error, stdout, stderr) => { execFile(
if (error) { "dvisvgm",
reject(`error: ${error}`); [...inputArgs, filePath, "-o", targetPath],
} options,
(error, stdout, stderr) => {
if (error) {
reject(`error: ${error}`);
}
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}); },
);
}); });
} }

View file

@ -735,6 +735,7 @@ export async function convert(
execFile( execFile(
"ffmpeg", "ffmpeg",
[...ffmpegArgs, "-i", filePath, ...extraArgs, targetPath], [...ffmpegArgs, "-i", filePath, ...extraArgs, targetPath],
options,
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);

View file

@ -318,7 +318,7 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("gm", ["convert", filePath, targetPath], (error, stdout, stderr) => { execFile("gm", ["convert", filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -472,6 +472,7 @@ export function convert(
execFile( execFile(
"magick", "magick",
[...inputArgs, filePath, ...outputArgs, targetPath], [...inputArgs, filePath, ...outputArgs, targetPath],
options,
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);

View file

@ -37,7 +37,7 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("inkscape", [filePath, "-o", targetPath], (error, stdout, stderr) => { execFile("inkscape", [filePath, "-o", targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -19,7 +19,7 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("heif-convert", [filePath, targetPath], (error, stdout, stderr) => { execFile("heif-convert", [filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -31,7 +31,7 @@ export function convert(
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile(tool, [filePath, targetPath], (error, stdout, stderr) => { execFile(tool, [filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,4 +1,5 @@
import { execFile } from "node:child_process"; import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types.ts";
export const properties = { export const properties = {
from: { from: {
@ -136,8 +137,8 @@ export function convert(
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> { ): Promise<string> {
const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath; const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath;
@ -157,7 +158,7 @@ export function convert(
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("soffice", args, (error, stdout, stderr) => { execFile("soffice", args, options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,4 +1,5 @@
import { execFile } from "node:child_process"; import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types.ts";
export const properties = { export const properties = {
from: { from: {
@ -124,8 +125,8 @@ export function convert(
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> { ): Promise<string> {
// set xelatex here // set xelatex here
const xelatex = ["pdf", "latex"]; const xelatex = ["pdf", "latex"];
@ -143,7 +144,7 @@ export function convert(
args.push("-o", targetPath); args.push("-o", targetPath);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("pandoc", args, (error, stdout, stderr) => { execFile("pandoc", args, options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -31,20 +31,25 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("potrace", [filePath, "-o", targetPath, "-b", convertTo], (error, stdout, stderr) => { execFile(
if (error) { "potrace",
reject(`error: ${error}`); [filePath, "-o", targetPath, "-b", convertTo],
} options,
(error, stdout, stderr) => {
if (error) {
reject(`error: ${error}`);
}
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}); },
);
}); });
} }

View file

@ -19,7 +19,7 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("resvg", [filePath, targetPath], (error, stdout, stderr) => { execFile("resvg", [filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,7 +1,7 @@
export type ExecFileFn = ( export type ExecFileFn = (
cmd: string, cmd: string,
args: string[], args: string[],
options: import("child_process").ExecFileOptions | undefined, options: import("child_process").ExecFileOptions | unknown | undefined | null,
callback: (err: Error | null, stdout: string, stderr: string) => void, callback: (err: Error | null, stdout: string, stderr: string) => void,
) => void; ) => void;

View file

@ -120,7 +120,7 @@ export function convert(
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("vips", [action, filePath, targetPath], (error, stdout, stderr) => { execFile("vips", [action, filePath, targetPath], options, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -25,6 +25,7 @@ export function convert(
execFile( execFile(
"latexmk", "latexmk",
["-xelatex", "-interaction=nonstopmode", `-output-directory=${outputPath}`, filePath], ["-xelatex", "-interaction=nonstopmode", `-output-directory=${outputPath}`, filePath],
options,
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);

View file

@ -37,6 +37,7 @@ test("convert respects eps filetype", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -63,6 +64,7 @@ test("convert respects pdf filetype", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -89,6 +91,7 @@ test("convert respects svgz conversion target type", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);

View file

@ -6,6 +6,7 @@ let calls: string[][] = [];
function mockExecFile( function mockExecFile(
_cmd: string, _cmd: string,
args: string[], args: string[],
options: unknown,
callback: (err: Error | null, stdout: string, stderr: string) => void, callback: (err: Error | null, stdout: string, stderr: string) => void,
) { ) {
calls.push(args); calls.push(args);

View file

@ -13,6 +13,7 @@ export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
callback(null, "Fake stdout", ""); callback(null, "Fake stdout", "");
@ -30,6 +31,7 @@ export async function runConvertFailTest(convertFn: ConvertFnWithExecFile) {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
callback(new Error("Test error"), "", ""); callback(new Error("Test error"), "", "");
@ -51,9 +53,10 @@ export async function runConvertLogsStderror(convertFn: ConvertFnWithExecFile) {
const mockExecFile = ( const mockExecFile = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
cb: (err: Error | null, stdout: string, stderr: string) => void, options: unknown,
callback: (err: Error | null, stdout: string, stderr: string) => void,
) => { ) => {
cb(null, "", "Fake stderr"); callback(null, "", "Fake stderr");
}; };
await convertFn("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile); await convertFn("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);

View file

@ -37,6 +37,7 @@ test("convert respects ico conversion target type", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -72,6 +73,7 @@ test("convert respects ico conversion target type with svg as input filetype", a
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -109,6 +111,7 @@ test("convert respects ico conversion target type with emf as input filetype", a
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -150,6 +153,7 @@ test("convert respects emf as input filetype", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);

View file

@ -37,6 +37,7 @@ test("convert uses djxl with input filetype being jxl", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
command = _cmd; command = _cmd;
@ -63,6 +64,7 @@ test("convert uses cjxl with output filetype being jxl", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
command = _cmd; command = _cmd;

View file

@ -37,6 +37,7 @@ test("convert uses action pdfload with filetype being pdf", async () => {
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);
@ -63,6 +64,7 @@ test("convert uses action copy with filetype being anything but pdf", async () =
const mockExecFile: ExecFileFn = ( const mockExecFile: ExecFileFn = (
_cmd: string, _cmd: string,
_args: string[], _args: string[],
options: unknown,
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => { ) => {
calls.push(_args); calls.push(_args);