test: add unit test for imagemagick.ts
This commit is contained in:
parent
7f9c8868fd
commit
f1730ede97
3 changed files with 198 additions and 6 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import { execFile } from "node:child_process";
|
import { execFile as execFileOriginal } from "node:child_process";
|
||||||
|
import { ExecFileFn } from "./types.ts";
|
||||||
|
|
||||||
// declare possible conversions
|
// declare possible conversions
|
||||||
export const properties = {
|
export const properties = {
|
||||||
|
|
@ -445,8 +446,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> {
|
||||||
let outputArgs: string[] = [];
|
let outputArgs: string[] = [];
|
||||||
let inputArgs: string[] = [];
|
let inputArgs: string[] = [];
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import type { ExecFileException } from "node:child_process";
|
import type { ExecFileException } from "node:child_process";
|
||||||
import { expect, test } from "bun:test";
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
import { convert } from "../../src/converters/dvisvgm.ts";
|
import { convert } from "../../src/converters/dvisvgm.ts";
|
||||||
import { ExecFileFn } from "../../src/converters/types.ts";
|
import { ExecFileFn } from "../../src/converters/types.ts";
|
||||||
import {
|
import {
|
||||||
|
|
@ -8,6 +8,12 @@ import {
|
||||||
runConvertSuccessTest,
|
runConvertSuccessTest,
|
||||||
} from "./helpers/converters.ts";
|
} from "./helpers/converters.ts";
|
||||||
|
|
||||||
|
let calls: string[][] = [];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
calls = [];
|
||||||
|
});
|
||||||
|
|
||||||
test("convert resolves when execFile succeeds", async () => {
|
test("convert resolves when execFile succeeds", async () => {
|
||||||
await runConvertSuccessTest(convert);
|
await runConvertSuccessTest(convert);
|
||||||
});
|
});
|
||||||
|
|
@ -33,14 +39,16 @@ test("convert respects eps filetype", async () => {
|
||||||
_args: string[],
|
_args: string[],
|
||||||
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
) => {
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
callback(null, "Fake stdout", "");
|
callback(null, "Fake stdout", "");
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await convert("input.obj", "eps", "stl", "output.stl", undefined, mockExecFile);
|
const result = await convert("input.eps", "eps", "stl", "output.stl", undefined, mockExecFile);
|
||||||
|
|
||||||
console.log = originalConsoleLog;
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
expect(result).toBe("Done");
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["--eps", "input.eps", "output.stl"]));
|
||||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -57,14 +65,16 @@ test("convert respects pdf filetype", async () => {
|
||||||
_args: string[],
|
_args: string[],
|
||||||
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
) => {
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
callback(null, "Fake stdout", "");
|
callback(null, "Fake stdout", "");
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await convert("input.obj", "pdf", "stl", "output.stl", undefined, mockExecFile);
|
const result = await convert("input.pdf", "pdf", "stl", "output.stl", undefined, mockExecFile);
|
||||||
|
|
||||||
console.log = originalConsoleLog;
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
expect(result).toBe("Done");
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["--pdf", "input.pdf", "output.stl"]));
|
||||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -81,13 +91,15 @@ test("convert respects svgz conversion target type", async () => {
|
||||||
_args: string[],
|
_args: string[],
|
||||||
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
) => {
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
callback(null, "Fake stdout", "");
|
callback(null, "Fake stdout", "");
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await convert("input.obj", "eps", "svgz", "output.stl", undefined, mockExecFile);
|
const result = await convert("input.obj", "eps", "svgz", "output.svgz", undefined, mockExecFile);
|
||||||
|
|
||||||
console.log = originalConsoleLog;
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
expect(result).toBe("Done");
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-z", "input.obj", "output.svgz"]));
|
||||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
});
|
});
|
||||||
|
|
|
||||||
179
tests/converters/imagemagick.test.ts
Normal file
179
tests/converters/imagemagick.test.ts
Normal file
|
|
@ -0,0 +1,179 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/imagemagick.ts";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types.ts";
|
||||||
|
import {
|
||||||
|
runConvertFailTest,
|
||||||
|
runConvertLogsStderror,
|
||||||
|
runConvertSuccessTest,
|
||||||
|
} from "./helpers/converters.ts";
|
||||||
|
|
||||||
|
let calls: string[][] = [];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
calls = [];
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert resolves when execFile succeeds", async () => {
|
||||||
|
await runConvertSuccessTest(convert);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert rejects when execFile fails", async () => {
|
||||||
|
await runConvertFailTest(convert);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert logs stderr when present", async () => {
|
||||||
|
await runConvertLogsStderror(convert);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects ico conversion target type", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.obj", "eps", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"input.obj",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects ico conversion target type with svg as input filetype", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.svg", "svg", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"-density",
|
||||||
|
"512",
|
||||||
|
"input.svg",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects ico conversion target type with emf as input filetype", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.emf", "emf", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"emf:delegate=false",
|
||||||
|
"-density",
|
||||||
|
"300",
|
||||||
|
"white",
|
||||||
|
"-alpha",
|
||||||
|
"remove",
|
||||||
|
"input.emf",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects emf as input filetype", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
calls.push(_args);
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.emf", "emf", "obj", "output.obj", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"emf:delegate=false",
|
||||||
|
"-density",
|
||||||
|
"300",
|
||||||
|
"-background",
|
||||||
|
"white",
|
||||||
|
"-alpha",
|
||||||
|
"remove",
|
||||||
|
"input.emf",
|
||||||
|
"output.obj",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue