mirror of
https://github.com/C4illin/ConvertX.git
synced 2025-11-05 06:23:18 +00:00
test: add unit test for dvisvgm.ts
This commit is contained in:
@@ -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 = {
|
||||
from: {
|
||||
@@ -14,8 +15,8 @@ export function convert(
|
||||
fileType: string,
|
||||
convertTo: string,
|
||||
targetPath: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
options?: unknown,
|
||||
execFile: ExecFileFn = execFileOriginal, // to make it mockable
|
||||
): Promise<string> {
|
||||
const inputArgs: string[] = [];
|
||||
if (fileType === "eps") {
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
runConvertFailTest,
|
||||
runConvertLogsStderror,
|
||||
runConvertSuccessTest,
|
||||
} from "./convertersHelper.test.ts";
|
||||
} from "./helpers/converters.ts";
|
||||
|
||||
test("convert resolves when execFile succeeds", async () => {
|
||||
await runConvertSuccessTest(convert);
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
runConvertFailTest,
|
||||
runConvertLogsStderror,
|
||||
runConvertSuccessTest,
|
||||
} from "./convertersHelper.test.ts";
|
||||
} from "./helpers/converters.ts";
|
||||
|
||||
test("convert resolves when execFile succeeds", async () => {
|
||||
await runConvertSuccessTest(convert);
|
||||
|
||||
93
tests/converters/dvisvgm.test.ts
Normal file
93
tests/converters/dvisvgm.test.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import type { ExecFileException } from "node:child_process";
|
||||
import { expect, test } from "bun:test";
|
||||
import { convert } from "../../src/converters/dvisvgm.ts";
|
||||
import { ExecFileFn } from "../../src/converters/types.ts";
|
||||
import {
|
||||
runConvertFailTest,
|
||||
runConvertLogsStderror,
|
||||
runConvertSuccessTest,
|
||||
} from "./helpers/converters.ts";
|
||||
|
||||
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 eps 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,
|
||||
) => {
|
||||
callback(null, "Fake stdout", "");
|
||||
};
|
||||
|
||||
const result = await convert("input.obj", "eps", "stl", "output.stl", undefined, mockExecFile);
|
||||
|
||||
console.log = originalConsoleLog;
|
||||
|
||||
expect(result).toBe("Done");
|
||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||
});
|
||||
|
||||
test("convert respects pdf 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,
|
||||
) => {
|
||||
callback(null, "Fake stdout", "");
|
||||
};
|
||||
|
||||
const result = await convert("input.obj", "pdf", "stl", "output.stl", undefined, mockExecFile);
|
||||
|
||||
console.log = originalConsoleLog;
|
||||
|
||||
expect(result).toBe("Done");
|
||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||
});
|
||||
|
||||
test("convert respects svgz 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,
|
||||
) => {
|
||||
callback(null, "Fake stdout", "");
|
||||
};
|
||||
|
||||
const result = await convert("input.obj", "eps", "svgz", "output.stl", undefined, mockExecFile);
|
||||
|
||||
console.log = originalConsoleLog;
|
||||
|
||||
expect(result).toBe("Done");
|
||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ExecFileException } from "node:child_process";
|
||||
import { expect } from "bun:test";
|
||||
import { ConvertFnWithExecFile, ExecFileFn } from "../../src/converters/types.ts";
|
||||
import { ConvertFnWithExecFile, ExecFileFn } from "../../../src/converters/types.ts";
|
||||
|
||||
export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
|
||||
const originalConsoleLog = console.log;
|
||||
Reference in New Issue
Block a user