From 4dd9de349ef05a5127d044e2176130dd531ae2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 9 Jun 2019 17:12:37 +0200 Subject: [PATCH] [FIX] tests: check links for main README.md as well --- README.md | 2 +- tests/doc_link_checker.test.ts | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 49592ee8..f1ac73d1 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Some npm scripts are available: | `npm run minify` | minify the prebuilt owl.js file | | `npm run test` | run all tests | | `npm run test:watch` | run all tests, and keep a watcher | -| `npm run tools` | build tools applications, start a static server (see [here](tools/readme.md)) | +| `npm run tools` | build tools applications, start a static server (see [here](doc/tooling.md)) | | `npm run tools:watch` | same as `tools`, but with a watcher to rebuild owl | ## Documentation diff --git a/tests/doc_link_checker.test.ts b/tests/doc_link_checker.test.ts index c7213bf5..30c9c123 100644 --- a/tests/doc_link_checker.test.ts +++ b/tests/doc_link_checker.test.ts @@ -9,10 +9,18 @@ import * as fs from "fs"; const LINK_REGEXP = /\[([^\[]+)\]\(([^\)]+)\)/g; const HEADING_REGEXP = /\n(#+\s*)(.*)/g; +// files to be checked +function getFiles(): string[] { + const DOCFILES = fs.readdirSync("doc").map(f => `doc/${f}`); + const MAINREADME = "README.md"; + return DOCFILES.concat(MAINREADME); +} + test("All markdown links work", () => { let linkNumber = 0; let invalidLinkNumber = 0; - const data = readDocData(); + const files = getFiles(); + const data = readDocData(files); for (let file of data) { for (let link of file.links) { // DEBUG: uncomment next line @@ -50,17 +58,20 @@ function isLinkValid( files: FileData[] ): boolean { const parts = link.link.split("#"); + const currentParts = current.name.split("/"); + const path = currentParts.length > 1 ? currentParts[0] + '/' : ""; + const fullName = path + parts[0]; if (parts.length === 1) { // no # in url if (parts[0].endsWith(".md")) { // it is a local md file - if (!files.find(f => f.name === parts[0])) { + if (!files.find(f => f.name === fullName)) { return false; } } } else { const file = - parts[0] === "" ? current : files.find(f => f.name === parts[0]); + parts[0] === "" ? current : files.find(f => f.name === fullName); if (!file) { return false; } @@ -88,17 +99,16 @@ function slugify(str) { .replace(/-+$/, ""); // Trim - from end of text } -function readDocData(): FileData[] { +function readDocData(files: string[]): FileData[] { const result: FileData[] = []; - const FILES = fs.readdirSync("doc"); - for (let file of FILES) { + for (let file of files) { const fileData: FileData = { name: file, links: [], sections: [] }; - const content = fs.readFileSync(`doc/${file}`, { encoding: "utf8" }); + const content = fs.readFileSync(file, { encoding: "utf8" }); let m; // get links info do {