Skip to content

Commit

Permalink
Test that getPathSegment handles slashes
Browse files Browse the repository at this point in the history
This has been fixed already in a previous comment,
but these tests will prove it.

Closes kohheepeace#11.
  • Loading branch information
aloisklink committed Jun 26, 2020
1 parent 838773d commit 894ebe7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const isAddressInfo = (arg: any): arg is AddressInfo => {
&& arg.port && typeof (arg.port) == 'number';
}

const getPathSegment = (path: string, endSlash: boolean = true) => {
export function getPathSegment(path: string, endSlash: boolean = true) {
path = path?.trim() ?? "";
if (!path.startsWith('/')) {
path = "/" + path;
Expand Down
121 changes: 121 additions & 0 deletions tests/cli-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {promisify} from "util";
import {promises as fsPromises} from "fs";
import {execFile} from "child_process";
import {join as pathJoin, resolve as pathResolve} from "path";

const promiseExecFile = promisify(execFile);

jest.setTimeout(60_000);

const TEST_OUTPUT = "./tests/output";
const TEST_SITE_DIR = "./tests/test-website";
// third-to-last docs path so should be faster
const DOCUSAURUS_TEST_LINK = "https://v2.docusaurus.io/docs/2.0.0-alpha.56/docusaurus.config.js";

async function runDocusaurusPdf(
args: Array<string>,
{
cmd = require.resolve("../bin/index.js"),
cwd = process.cwd(),
} = {},
) {
return await promiseExecFile("node", [cmd, ...args], {cwd});
}

async function isFile(path: string): Promise<boolean> {
try {
const stat = await fsPromises.stat(path);
return stat.isFile();
} catch (error) {
return false;
// throw error;
}
}

async function rmdirRecursive(path: string): Promise<void> {
let files;
try {
files = await fsPromises.readdir(path);
} catch (error) {
if (error.code === "ENOENT") {
return; // dir already doesn't exist
}
throw error;
}

await Promise.all(files.map((file) => {
return fsPromises.unlink(pathJoin(path, file));
})
);
await fsPromises.rmdir(path);
}

describe("testing cli", () => {
beforeAll(async () => {
await rmdirRecursive(TEST_OUTPUT);
await fsPromises.mkdir(TEST_OUTPUT);
});

describe("default", () => {
test("should download docusaurus-website to default docusaurus.pdf", async () => {
await runDocusaurusPdf([DOCUSAURUS_TEST_LINK], {cwd: TEST_OUTPUT});
expect(await isFile(pathJoin(TEST_OUTPUT, "docusaurus.pdf"))).toBe(true);
});
test("should download docusaurus-website to specific location", async () => {
const outputPath = pathJoin(TEST_OUTPUT, "specifc-loc-test.pdf");
await runDocusaurusPdf([DOCUSAURUS_TEST_LINK, outputPath]);
expect(await isFile(outputPath)).toBe(true);
});
test("should fail when using non-docusuarus url", async () => {
const outputPath = pathJoin(TEST_OUTPUT, "should-fail.pdf");
await expect(runDocusaurusPdf(["https://example.invalid", outputPath])).rejects.toThrow();
expect(await isFile(outputPath)).toBe(false);
});
});

describe("from-build", () => {
beforeAll(async () => {
await promiseExecFile(
require.resolve("@docusaurus/core/bin/docusaurus.js"),
["build"], {cwd: TEST_SITE_DIR},
)
});
test("should create pdf from build dir", async () => {
const outputPath = pathJoin(TEST_OUTPUT, "from-build.pdf");
const buildDir = pathJoin(TEST_SITE_DIR, "build");
const docsPath = "/docs-path";
const baseUrl = "/base-url/";
await runDocusaurusPdf(
["from-build", buildDir, docsPath, baseUrl, "--output-file", outputPath],
);
expect(await isFile(outputPath)).toBe(true);
});
test("should still run even if incorrect docsPath/baseUrl slashes", async () => {
const outputPath = pathJoin(TEST_OUTPUT, "from-build-slashes.pdf");
const buildDir = pathJoin(TEST_SITE_DIR, "build");
const docsPath = "docs-path/";
const baseUrl = "base-url";
await runDocusaurusPdf(
["from-build", buildDir, docsPath, baseUrl, "--output-file", outputPath],
);
expect(await isFile(outputPath)).toBe(true);
});
});

describe("from-build-config", () => {
beforeAll(async () => {
await promiseExecFile(
require.resolve("@docusaurus/core/bin/docusaurus.js"),
["build"], {cwd: TEST_SITE_DIR},
)
});
test("should create pdf from build dir", async () => {
const outputPath = pathJoin(TEST_OUTPUT, "from-build-config.pdf");
await runDocusaurusPdf(
["from-build-config", "--output-file", pathResolve(outputPath)],
{cwd: TEST_SITE_DIR},
);
expect(await isFile(outputPath)).toBe(true);
});
});
});

0 comments on commit 894ebe7

Please sign in to comment.