-
-
Notifications
You must be signed in to change notification settings - Fork 874
test: add unit tests for converters #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
435f654
4fa4712
2db99ed
2c90454
301fab5
fd4e73e
3975c70
c47c1dd
72b484a
7f9c886
f1730ed
7524f30
5957873
311d251
b1f70ec
d8cbc0a
9f6b815
6452d0b
c0f0dc5
08a833f
4b42a5f
e5ac60c
eac22d5
af68498
9c24cf4
178f009
c6006b5
c010588
c3d461f
3dccbfc
d994c38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { execFile } from "node:child_process"; | ||
| import { execFile as execFileOriginal } from "node:child_process"; | ||
| import { ExecFileFn } from "./types"; | ||
|
|
||
| // declare possible conversions | ||
| export const properties = { | ||
|
|
@@ -17,8 +18,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> { | ||
| let tool = ""; | ||
| if (fileType === "jxl") { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @C4illin What I noticed here when writing the tests: If both
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of them must always be jxl for the tools to work, and in the ui it should only be possible to select jxl if you upload a non-jxl image and vice versa. But some error would be more clear |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export type ExecFileFn = ( | ||
| cmd: string, | ||
| args: string[], | ||
| callback: (err: Error | null, stdout: string, stderr: string) => void, | ||
| options?: import("child_process").ExecFileOptions, | ||
| ) => void; | ||
|
|
||
| export type ConvertFnWithExecFile = ( | ||
| filePath: string, | ||
| fileType: string, | ||
| convertTo: string, | ||
| targetPath: string, | ||
| options: unknown, | ||
| execFileOverride?: ExecFileFn, | ||
| ) => Promise<string>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { test } from "bun:test"; | ||
| import { convert } from "../../src/converters/assimp"; | ||
| import { runCommonTests } from "./helpers/commonTests"; | ||
|
|
||
| runCommonTests(convert); | ||
|
|
||
| test.skip("dummy - required to trigger test detection", () => {}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { test } from "bun:test"; | ||
| import { convert } from "../../src/converters/calibre"; | ||
| import { runCommonTests } from "./helpers/commonTests"; | ||
|
|
||
| runCommonTests(convert); | ||
|
|
||
| test.skip("dummy - required to trigger test detection", () => {}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import type { ExecFileException } from "node:child_process"; | ||
| import { beforeEach, expect, test } from "bun:test"; | ||
| import { convert } from "../../src/converters/dvisvgm"; | ||
| import { ExecFileFn } from "../../src/converters/types"; | ||
| import { runCommonTests } from "./helpers/commonTests"; | ||
|
|
||
| let calls: string[][] = []; | ||
|
|
||
| beforeEach(() => { | ||
| calls = []; | ||
| }); | ||
|
|
||
| runCommonTests(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, | ||
| ) => { | ||
| calls.push(_args); | ||
| callback(null, "Fake stdout", ""); | ||
| }; | ||
|
|
||
| const result = await convert("input.eps", "eps", "stl", "output.stl", undefined, mockExecFile); | ||
|
|
||
| console.log = originalConsoleLog; | ||
|
|
||
| expect(result).toBe("Done"); | ||
| expect(calls[0]).toEqual(expect.arrayContaining(["--eps", "input.eps", "output.stl"])); | ||
| 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, | ||
| ) => { | ||
| calls.push(_args); | ||
| callback(null, "Fake stdout", ""); | ||
| }; | ||
|
|
||
| const result = await convert("input.pdf", "pdf", "stl", "output.stl", undefined, mockExecFile); | ||
|
|
||
| console.log = originalConsoleLog; | ||
|
|
||
| expect(result).toBe("Done"); | ||
| expect(calls[0]).toEqual(expect.arrayContaining(["--pdf", "input.pdf", "output.stl"])); | ||
| 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, | ||
| ) => { | ||
| calls.push(_args); | ||
| callback(null, "Fake stdout", ""); | ||
| }; | ||
|
|
||
| const result = await convert("input.obj", "eps", "svgz", "output.svgz", undefined, mockExecFile); | ||
|
|
||
| console.log = originalConsoleLog; | ||
|
|
||
| expect(result).toBe("Done"); | ||
| expect(calls[0]).toEqual(expect.arrayContaining(["-z", "input.obj", "output.svgz"])); | ||
| expect(loggedMessage).toBe("stdout: Fake stdout"); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.