2020-05-08 11:53:08 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-05-08 12:27:41 -07:00
|
|
|
const { existsSync } = require("fs");
|
2020-11-24 16:25:08 -08:00
|
|
|
// eslint-disable-next-line unicorn/import-style
|
2020-05-08 11:53:08 -07:00
|
|
|
const { join } = require("path");
|
|
|
|
const { promisify } = require("util");
|
|
|
|
const globby = require("globby");
|
|
|
|
const jsYaml = require("js-yaml");
|
|
|
|
const stripJsonComments = require("strip-json-comments");
|
2021-01-10 20:46:00 -08:00
|
|
|
const test = require("ava").default;
|
2020-05-08 11:53:08 -07:00
|
|
|
const markdownlint = require("../lib/markdownlint");
|
|
|
|
const markdownlintPromise = promisify(markdownlint);
|
|
|
|
const readConfigPromise = promisify(markdownlint.readConfig);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses JSONC text.
|
|
|
|
*
|
|
|
|
* @param {string} json JSON to parse.
|
|
|
|
* @returns {Object} Object representation.
|
|
|
|
*/
|
|
|
|
function jsoncParse(json) {
|
|
|
|
return JSON.parse(stripJsonComments(json));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses YAML text.
|
|
|
|
*
|
|
|
|
* @param {string} yaml YAML to parse.
|
|
|
|
* @returns {Object} Object representation.
|
|
|
|
*/
|
|
|
|
function yamlParse(yaml) {
|
2021-02-06 19:23:55 -08:00
|
|
|
return jsYaml.load(yaml);
|
2020-05-08 11:53:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lints a test repository.
|
|
|
|
*
|
2021-01-10 20:46:00 -08:00
|
|
|
* @param {Object} t Test instance.
|
2020-05-08 11:53:08 -07:00
|
|
|
* @param {string[]} globPatterns Array of files to in/exclude.
|
|
|
|
* @param {string} configPath Path to config file.
|
2021-01-10 20:46:00 -08:00
|
|
|
* @returns {Promise} Test result.
|
2020-05-08 11:53:08 -07:00
|
|
|
*/
|
2021-01-10 20:46:00 -08:00
|
|
|
function lintTestRepo(t, globPatterns, configPath) {
|
|
|
|
t.plan(1);
|
|
|
|
return Promise.all([
|
2020-05-08 11:53:08 -07:00
|
|
|
globby(globPatterns),
|
|
|
|
// @ts-ignore
|
|
|
|
readConfigPromise(configPath, [ jsoncParse, yamlParse ])
|
|
|
|
]).then((globbyAndReadConfigResults) => {
|
|
|
|
const [ files, config ] = globbyAndReadConfigResults;
|
|
|
|
const options = {
|
|
|
|
files,
|
|
|
|
config
|
|
|
|
};
|
2021-06-13 16:37:29 -07:00
|
|
|
console.log(`${t.title}: Linting ${files.length} files...`);
|
2020-05-08 11:53:08 -07:00
|
|
|
return markdownlintPromise(options).then((results) => {
|
|
|
|
const resultsString = results.toString();
|
2020-09-06 20:34:10 -07:00
|
|
|
if (resultsString.length > 0) {
|
2020-05-08 11:53:08 -07:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(resultsString);
|
|
|
|
}
|
2021-02-06 19:55:22 -08:00
|
|
|
t.is(resultsString.length, 0, "Unexpected linting violations");
|
2020-05-08 11:53:08 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run markdownlint the same way the corresponding repositories do
|
|
|
|
|
2021-01-10 20:46:00 -08:00
|
|
|
test("https://github.com/eslint/eslint", (t) => {
|
2020-05-08 11:53:08 -07:00
|
|
|
const rootDir = "./test-repos/eslint-eslint";
|
|
|
|
const globPatterns = [ join(rootDir, "docs/**/*.md") ];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.yml");
|
2021-01-10 20:46:00 -08:00
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
2020-05-08 11:53:08 -07:00
|
|
|
});
|
|
|
|
|
2021-01-10 20:46:00 -08:00
|
|
|
test("https://github.com/mkdocs/mkdocs", (t) => {
|
2020-05-08 11:53:08 -07:00
|
|
|
const rootDir = "./test-repos/mkdocs-mkdocs";
|
|
|
|
const globPatterns = [
|
|
|
|
join(rootDir, "README.md"),
|
|
|
|
join(rootDir, "CONTRIBUTING.md"),
|
2021-06-13 16:37:29 -07:00
|
|
|
join(rootDir, "docs/**/*.md"),
|
2020-05-08 11:53:08 -07:00
|
|
|
"!" + join(rootDir, "docs/CNAME")
|
|
|
|
];
|
|
|
|
const configPath = join(rootDir, ".markdownlintrc");
|
2021-01-10 20:46:00 -08:00
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
2020-05-08 11:53:08 -07:00
|
|
|
});
|
|
|
|
|
2021-06-13 16:37:29 -07:00
|
|
|
test("https://github.com/mochajs/mocha", (t) => {
|
|
|
|
const rootDir = "./test-repos/mochajs-mocha";
|
|
|
|
const globPatterns = [
|
|
|
|
join(rootDir, "*.md"),
|
|
|
|
join(rootDir, "docs/**/*.md"),
|
|
|
|
join(rootDir, ".github/*.md"),
|
|
|
|
join(rootDir, "lib/**/*.md"),
|
|
|
|
join(rootDir, "test/**/*.md"),
|
|
|
|
join(rootDir, "example/**/*.md")
|
|
|
|
];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.json");
|
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
|
|
|
});
|
|
|
|
|
2021-01-10 20:46:00 -08:00
|
|
|
test("https://github.com/pi-hole/docs", (t) => {
|
2020-05-08 11:53:08 -07:00
|
|
|
const rootDir = "./test-repos/pi-hole-docs";
|
|
|
|
const globPatterns = [ join(rootDir, "**/*.md") ];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.json");
|
2021-01-10 20:46:00 -08:00
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
2020-05-08 11:53:08 -07:00
|
|
|
});
|
2020-05-08 12:27:41 -07:00
|
|
|
|
2021-06-13 16:37:29 -07:00
|
|
|
test("https://github.com/webhintio/hint", (t) => {
|
|
|
|
const rootDir = "./test-repos/webhintio-hint";
|
|
|
|
const globPatterns = [
|
|
|
|
join(rootDir, "**/*.md"),
|
|
|
|
"!" + join(rootDir, "**/CHANGELOG.md")
|
|
|
|
];
|
|
|
|
const configPath = join(rootDir, ".markdownlintrc");
|
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("https://github.com/webpack/webpack.js.org", (t) => {
|
|
|
|
const rootDir = "./test-repos/webpack-webpack-js-org";
|
|
|
|
const globPatterns = [ join(rootDir, "**/*.md") ];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.json");
|
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
|
|
|
});
|
|
|
|
|
2020-05-08 12:27:41 -07:00
|
|
|
// Optional repositories (very large)
|
|
|
|
|
|
|
|
const dotnetDocsDir = "./test-repos/dotnet-docs";
|
|
|
|
if (existsSync(dotnetDocsDir)) {
|
2021-01-10 20:46:00 -08:00
|
|
|
test("https://github.com/dotnet/docs", (t) => {
|
2020-05-08 12:27:41 -07:00
|
|
|
const rootDir = dotnetDocsDir;
|
|
|
|
const globPatterns = [
|
|
|
|
join(rootDir, "**/*.md"),
|
2020-11-24 13:46:23 -08:00
|
|
|
"!" + join(rootDir, "samples/**/*.md"),
|
2021-06-13 15:05:03 -07:00
|
|
|
// MD044 newly detects an issue in this file
|
2020-11-24 13:46:23 -08:00
|
|
|
"!" + join(
|
|
|
|
rootDir,
|
2021-06-13 15:05:03 -07:00
|
|
|
"docs/framework/wcf/feature-details/configuring-iis-for-wcf.md"
|
2020-11-24 13:46:23 -08:00
|
|
|
)
|
2020-05-08 12:27:41 -07:00
|
|
|
];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.json");
|
2021-01-10 20:46:00 -08:00
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
2020-05-08 12:27:41 -07:00
|
|
|
});
|
|
|
|
}
|
2021-06-13 16:37:29 -07:00
|
|
|
|
|
|
|
const v8v8DevDir = "./test-repos/v8-v8-dev";
|
|
|
|
if (existsSync(v8v8DevDir)) {
|
|
|
|
test("https://github.com/v8/v8.dev", (t) => {
|
|
|
|
const rootDir = v8v8DevDir;
|
|
|
|
const globPatterns = [ join(rootDir, "src/**/*.md") ];
|
|
|
|
const configPath = join(rootDir, ".markdownlint.json");
|
|
|
|
return lintTestRepo(t, globPatterns, configPath);
|
|
|
|
});
|
|
|
|
}
|