From bbd50d838b6dbc487233ac5cdaee0db657d64f93 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 30 Nov 2021 10:23:31 +0000 Subject: [PATCH 1/2] fix(index): do not assume win32 usage for binaries --- src/index.js | 7 +++++-- src/index.test.js | 6 ++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index adaf883..5643e1b 100644 --- a/src/index.js +++ b/src/index.js @@ -75,8 +75,7 @@ class UnRTF { constructor(binPath) { if (binPath) { this.unrtfPath = path.normalizeTrim(binPath); - } else { - // If not set, expect user to be using Win32 + } else if (process.platform === "win32") { this.unrtfPath = path.joinSafe( __dirname, "lib", @@ -84,6 +83,10 @@ class UnRTF { "unrtf-0.19.3", "bin" ); + } else { + throw new Error( + `${process.platform} UnRTF binaries are not provided, please pass the installation directory as a parameter to the UnRTF instance.` + ); } } diff --git a/src/index.test.js b/src/index.test.js index ba79b64..23d1294 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -2,7 +2,6 @@ /* eslint-disable security/detect-child-process */ /* eslint-disable security/detect-non-literal-fs-filename */ const isHtml = require("is-html"); -const os = require("os"); const path = require("upath"); const semver = require("semver"); const { execFile } = require("child_process"); @@ -15,8 +14,7 @@ const testDirectory = `${__dirname}/../test_files/`; const file = `${testDirectory}test-rtf-complex.rtf`; let testBinaryPath; -const platform = os.platform(); -switch (platform) { +switch (process.platform) { // macOS case "darwin": testBinaryPath = "/usr/local/bin"; @@ -39,7 +37,7 @@ switch (platform) { break; } -if (platform === "win32") { +if (process.platform === "win32") { describe("Constructor", () => { test("Should convert RTF file to HTML without binary set, and use included Windows binary", async () => { const unRtf = new UnRTF(); From 996d3a4c16a56698335fe3161f977f5294627acc Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 30 Nov 2021 10:57:59 +0000 Subject: [PATCH 2/2] test(index): check thrown error on missing binary path param --- src/index.test.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/index.test.js b/src/index.test.js index 23d1294..12b2845 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -37,9 +37,9 @@ switch (process.platform) { break; } -if (process.platform === "win32") { - describe("Constructor", () => { - test("Should convert RTF file to HTML without binary set, and use included Windows binary", async () => { +describe("Constructor", () => { + if (process.platform === "win32") { + test("Should convert RTF file to HTML without binary set on win32, and use included binary", async () => { const unRtf = new UnRTF(); const options = { noPictures: true, @@ -51,8 +51,22 @@ if (process.platform === "win32") { expect(typeof res).toBe("string"); expect(res.substring(0, 6)).toBe(""); }); - }); -} + } + + 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 unRtf = new UnRTF(); + } catch (err) { + expect(err.message).toBe( + `${process.platform} UnRTF binaries are not provided, please pass the installation directory as a parameter to the UnRTF instance.` + ); + } + }); + } +}); describe("Convert Function", () => { let version;