Skip to content

Commit

Permalink
fix(index): correct message thrown for missing binaries (#363)
Browse files Browse the repository at this point in the history
* fix(index): correct message thrown for missing binaries

* refactor(index): remove nested `else... if`

* refactor(index): replace `os.platform()` with `process.platform`

* chore(index): shorten error message

* test(index): check thrown error on missing binary path param
  • Loading branch information
Fdawgs committed Nov 30, 2021
1 parent 196b528 commit 5249d61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
34 changes: 12 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* eslint-disable security/detect-child-process */
const camelCase = require("camelcase");
const os = require("os");
const path = require("upath");
const { execFile } = require("child_process");
const util = require("util");

const execFileAsync = util.promisify(execFile);
const platform = os.platform();

const errorMessages = {
0: "No Error",
Expand Down Expand Up @@ -89,27 +87,19 @@ class Poppler {
constructor(binPath) {
if (binPath) {
this.popplerPath = path.normalizeTrim(binPath);
} else if (process.platform === "win32") {
this.popplerPath = path.joinSafe(
__dirname,
"lib",
"win32",
"poppler-21.11.0",
"Library",
"bin"
);
} else {
let popplerPath;

// Build path to Poppler binaries based on OS
switch (platform) {
// Windows OS
case "win32":
popplerPath = path.joinSafe(
__dirname,
"lib",
"win32",
"poppler-21.11.0",
"Library",
"bin"
);
break;
default:
throw new Error(`${platform} is NOT supported.`);
}

this.popplerPath = popplerPath;
throw new Error(
`${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.`
);
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* eslint-disable security/detect-non-literal-fs-filename */
const fs = require("fs");
const glob = require("glob");
const os = require("os");
const path = require("upath");
const { execFile } = require("child_process");
const util = require("util");
Expand All @@ -15,8 +14,7 @@ const testDirectory = `${__dirname}/../test_files/`;
const file = `${testDirectory}pdf_1.3_NHS_Constitution.pdf`;

let testBinaryPath;
const platform = os.platform();
switch (platform) {
switch (process.platform) {
// macOS
case "darwin":
testBinaryPath = "/usr/local/bin";
Expand Down Expand Up @@ -61,9 +59,9 @@ describe("Node-Poppler Module", () => {
await clean();
});

if (platform === "win32") {
describe("Constructor", () => {
test("Should convert PDF file to SVG file without binary set, and use included binaries", async () => {
describe("Constructor", () => {
if (process.platform === "win32") {
test("Should convert PDF file to SVG file without binary path set on win32, and use included binaries", async () => {
const poppler = new Poppler();
const options = {
svgFile: true,
Expand All @@ -79,8 +77,22 @@ describe("Node-Poppler Module", () => {
)
).toBe(true);
});
});
}
}

if (process.platform !== "win32") {
test(`Should return an Error object if binary path unset on ${process.platform}`, async () => {
expect.assertions(1);
try {
// eslint-disable-next-line no-unused-vars
const poppler = new Poppler();
} catch (err) {
expect(err.message).toBe(
`${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.`
);
}
});
}
});

describe("pdfAttach Function", () => {
test("Should attach file to PDF file", async () => {
Expand Down

0 comments on commit 5249d61

Please sign in to comment.