2020-05-08 11:53:08 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-08-16 04:01:53 +00:00
|
|
|
const { join } = require("node:path").posix;
|
2024-01-21 20:14:18 -08:00
|
|
|
const jsoncParser = require("jsonc-parser");
|
2020-05-08 11:53:08 -07:00
|
|
|
const jsYaml = require("js-yaml");
|
2024-06-07 22:24:28 -07:00
|
|
|
const { markdownlint, readConfig } = require("../lib/markdownlint").promises;
|
|
|
|
const markdownlintParallel = require("./markdownlint-test-parallel");
|
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.
|
2024-06-07 22:24:28 -07:00
|
|
|
* @param {boolean} [parallel] True to lint in parallel.
|
2021-01-10 20:46:00 -08:00
|
|
|
* @returns {Promise} Test result.
|
2020-05-08 11:53:08 -07:00
|
|
|
*/
|
2024-06-07 22:24:28 -07:00
|
|
|
async function lintTestRepo(t, globPatterns, configPath, parallel) {
|
2023-06-01 21:21:52 -07:00
|
|
|
t.plan(1);
|
2021-12-27 22:40:44 +00:00
|
|
|
const { globby } = await import("globby");
|
2022-10-05 06:23:30 +00:00
|
|
|
const jsoncParse = (json) => {
|
2024-01-21 20:14:18 -08:00
|
|
|
const config = jsoncParser.parse(json, [], { "allowTrailingComma": true });
|
2022-10-05 06:23:30 +00:00
|
|
|
return config.config || config;
|
|
|
|
};
|
2021-12-27 21:59:56 +00:00
|
|
|
const yamlParse = (yaml) => jsYaml.load(yaml);
|
2021-01-10 20:46:00 -08:00
|
|
|
return Promise.all([
|
2020-05-08 11:53:08 -07:00
|
|
|
globby(globPatterns),
|
2024-06-07 22:24:28 -07:00
|
|
|
readConfig(configPath, [ jsoncParse, yamlParse ])
|
2020-05-08 11:53:08 -07:00
|
|
|
]).then((globbyAndReadConfigResults) => {
|
2023-11-10 22:56:25 -08:00
|
|
|
const [ files, rawConfig ] = globbyAndReadConfigResults;
|
2021-06-13 16:45:38 -07:00
|
|
|
// eslint-disable-next-line no-console
|
2021-06-13 16:37:29 -07:00
|
|
|
console.log(`${t.title}: Linting ${files.length} files...`);
|
2023-11-10 22:56:25 -08:00
|
|
|
const config = Object.fromEntries(
|
|
|
|
Object.entries(rawConfig).
|
2023-12-05 20:09:57 -08:00
|
|
|
map(([ k, v ]) => [
|
|
|
|
k.replace(/header/, "heading"),
|
2024-05-25 18:18:32 -07:00
|
|
|
v
|
2023-12-05 20:09:57 -08:00
|
|
|
])
|
2023-11-10 22:56:25 -08:00
|
|
|
);
|
2024-06-07 22:24:28 -07:00
|
|
|
return (parallel ? markdownlintParallel : markdownlint)({
|
2022-04-10 05:37:57 +00:00
|
|
|
files,
|
2022-04-21 21:30:56 -07:00
|
|
|
config
|
2022-04-10 05:37:57 +00:00
|
|
|
}).then((results) => {
|
2023-06-01 21:21:52 -07:00
|
|
|
const resultsString = results.toString();
|
|
|
|
t.snapshot(
|
2023-04-24 21:24:19 -07:00
|
|
|
resultsString,
|
2023-06-01 21:21:52 -07:00
|
|
|
"Expected linting violations"
|
2023-04-24 21:24:19 -07:00
|
|
|
);
|
2020-05-08 11:53:08 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:24:47 -07:00
|
|
|
/**
|
|
|
|
* Excludes a list of globs.
|
|
|
|
*
|
|
|
|
* @param {string} rootDir Root directory for globs.
|
|
|
|
* @param {...string} globs Globs to exclude.
|
|
|
|
* @returns {string[]} Array of excluded globs.
|
|
|
|
*/
|
|
|
|
function excludeGlobs(rootDir, ...globs) {
|
|
|
|
return globs.map((glob) => "!" + join(rootDir, glob));
|
|
|
|
}
|
|
|
|
|
2023-12-30 19:14:31 -08:00
|
|
|
module.exports = {
|
|
|
|
excludeGlobs,
|
|
|
|
lintTestRepo
|
|
|
|
};
|