markdownlint/test/markdownlint-test-repos.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

// @ts-check
"use strict";
const { join } = require("path");
const { promisify } = require("util");
const globby = require("globby");
const jsYaml = require("js-yaml");
const stripJsonComments = require("strip-json-comments");
const tape = require("tape");
require("tape-player");
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) {
return jsYaml.safeLoad(yaml);
}
/**
* Lints a test repository.
*
* @param {Object} test Test instance.
* @param {string[]} globPatterns Array of files to in/exclude.
* @param {string} configPath Path to config file.
*/
function lintTestRepo(test, globPatterns, configPath) {
test.plan(1);
Promise.all([
globby(globPatterns),
// @ts-ignore
readConfigPromise(configPath, [ jsoncParse, yamlParse ])
]).then((globbyAndReadConfigResults) => {
const [ files, config ] = globbyAndReadConfigResults;
const options = {
files,
config
};
return markdownlintPromise(options).then((results) => {
const resultsString = results.toString();
if (resultsString.length) {
// eslint-disable-next-line no-console
console.log(resultsString);
}
test.ok(!resultsString.length, "Unexpected linting violations");
test.end();
});
});
}
// Run markdownlint the same way the corresponding repositories do
tape("https://github.com/eslint/eslint", (test) => {
const rootDir = "./test-repos/eslint-eslint";
const globPatterns = [ join(rootDir, "docs/**/*.md") ];
const configPath = join(rootDir, ".markdownlint.yml");
lintTestRepo(test, globPatterns, configPath);
});
tape("https://github.com/mkdocs/mkdocs", (test) => {
const rootDir = "./test-repos/mkdocs-mkdocs";
const globPatterns = [
join(rootDir, "README.md"),
join(rootDir, "CONTRIBUTING.md"),
join(rootDir, "docs/*"),
"!" + join(rootDir, "docs/CNAME")
];
const configPath = join(rootDir, ".markdownlintrc");
lintTestRepo(test, globPatterns, configPath);
});
tape("https://github.com/pi-hole/docs", (test) => {
const rootDir = "./test-repos/pi-hole-docs";
const globPatterns = [ join(rootDir, "**/*.md") ];
const configPath = join(rootDir, ".markdownlint.json");
lintTestRepo(test, globPatterns, configPath);
});