mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add comprehensive tests for parseConfiguration function, handle non-object parser results consistently (fixes #1523).
This commit is contained in:
parent
1f237e6c54
commit
63147ee1eb
4 changed files with 97 additions and 11 deletions
|
@ -1183,6 +1183,7 @@ export function readConfigSync(file, parsers, fs) {
|
||||||
// Try to parse file
|
// Try to parse file
|
||||||
const { config, message } = parseConfiguration(file, content, parsers);
|
const { config, message } = parseConfiguration(file, content, parsers);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
|
// @ts-ignore
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
// Extend configuration
|
// Extend configuration
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*
|
*
|
||||||
* @typedef {Object} ParseConfigurationResult
|
* @typedef {Object} ParseConfigurationResult
|
||||||
* @property {Object | null} config Configuration object if successful.
|
* @property {Object | null} config Configuration object if successful.
|
||||||
* @property {string} message Error message if an error occurred.
|
* @property {string | null} message Error message if an error occurred.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,20 +18,24 @@
|
||||||
*/
|
*/
|
||||||
export default function parseConfiguration(name, content, parsers) {
|
export default function parseConfiguration(name, content, parsers) {
|
||||||
let config = null;
|
let config = null;
|
||||||
let message = "";
|
let message = null;
|
||||||
const errors = [];
|
const errors = [];
|
||||||
let index = 0;
|
let index = 0;
|
||||||
// Try each parser
|
// Try each parser
|
||||||
(parsers || [ JSON.parse ]).every((parser) => {
|
const failed = (parsers || [ JSON.parse ]).every((parser) => {
|
||||||
try {
|
try {
|
||||||
config = parser(content);
|
const result = parser(content);
|
||||||
|
config = (result && (typeof result === "object") && !Array.isArray(result)) ? result : {};
|
||||||
|
// Succeeded
|
||||||
|
return false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errors.push(`Parser ${index++}: ${error.message}`);
|
errors.push(`Parser ${index++}: ${error.message}`);
|
||||||
}
|
}
|
||||||
return !config;
|
// Failed, try the next parser
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
// Message if unable to parse
|
// Message if unable to parse
|
||||||
if (!config) {
|
if (failed) {
|
||||||
errors.unshift(`Unable to parse '${name}'`);
|
errors.unshift(`Unable to parse '${name}'`);
|
||||||
message = errors.join("; ");
|
message = errors.join("; ");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1305,7 +1305,6 @@ test("configParsersTOML", async(t) => {
|
||||||
].join("\n")
|
].join("\n")
|
||||||
},
|
},
|
||||||
"configParsers": [
|
"configParsers": [
|
||||||
jsoncParser.parse,
|
|
||||||
require("toml").parse
|
require("toml").parse
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
|
@ -13,12 +13,94 @@ const testObject = {
|
||||||
};
|
};
|
||||||
const successfulTestObjectResult = {
|
const successfulTestObjectResult = {
|
||||||
"config": testObject,
|
"config": testObject,
|
||||||
"message": ""
|
"message": null
|
||||||
|
};
|
||||||
|
const successfulParser = () => testObject;
|
||||||
|
const failingParser = () => {
|
||||||
|
throw new Error("failingParser");
|
||||||
};
|
};
|
||||||
const testObjectJsonString = JSON.stringify(testObject);
|
|
||||||
|
|
||||||
test("json object, default parsers", (t) => {
|
test("json object, default parser", (t) => {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
const actual = parseConfiguration("name", testObjectJsonString);
|
const actual = parseConfiguration("name", JSON.stringify(testObject));
|
||||||
t.deepEqual(actual, successfulTestObjectResult);
|
t.deepEqual(actual, successfulTestObjectResult);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("invalid json, default parser", (t) => {
|
||||||
|
t.plan(1);
|
||||||
|
const actual = parseConfiguration("name", "{");
|
||||||
|
const expected = {
|
||||||
|
"config": null,
|
||||||
|
"message": "Unable to parse 'name'; Parser 0: Expected property name or '}' in JSON at position 1 (line 1 column 2)"
|
||||||
|
};
|
||||||
|
t.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("parser gets passed content", (t) => {
|
||||||
|
t.plan(2);
|
||||||
|
const content = "content";
|
||||||
|
const parser = (text) => {
|
||||||
|
t.is(text, content);
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
const actual = parseConfiguration("name", content, [ parser ]);
|
||||||
|
const expected = {
|
||||||
|
"config": {},
|
||||||
|
"message": null
|
||||||
|
};
|
||||||
|
t.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("parser returns undefined/null/number/string/array", (t) => {
|
||||||
|
t.plan(5);
|
||||||
|
const expected = {
|
||||||
|
"config": {},
|
||||||
|
"message": null
|
||||||
|
};
|
||||||
|
const testCases = [ undefined, null, 10, "string", [] ];
|
||||||
|
for (const testCase of testCases) {
|
||||||
|
/** @type {import("../lib/markdownlint.mjs").ConfigurationParser} */
|
||||||
|
const parser =
|
||||||
|
// @ts-ignore
|
||||||
|
() => testCase;
|
||||||
|
const actual = parseConfiguration("name", "content", [ parser ]);
|
||||||
|
t.deepEqual(actual, expected);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom parsers, successful/failing", (t) => {
|
||||||
|
t.plan(1);
|
||||||
|
const actual = parseConfiguration("name", "", [ successfulParser, failingParser ]);
|
||||||
|
t.deepEqual(actual, successfulTestObjectResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom parsers, failing/successful", (t) => {
|
||||||
|
t.plan(1);
|
||||||
|
const actual = parseConfiguration("name", "", [ failingParser, successfulParser ]);
|
||||||
|
t.deepEqual(actual, successfulTestObjectResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom parsers, failing/successful(undefined)", (t) => {
|
||||||
|
t.plan(1);
|
||||||
|
const actual = parseConfiguration(
|
||||||
|
"name",
|
||||||
|
"",
|
||||||
|
// @ts-ignore
|
||||||
|
[ failingParser, () => undefined ]
|
||||||
|
);
|
||||||
|
const expected = {
|
||||||
|
"config": {},
|
||||||
|
"message": null
|
||||||
|
};
|
||||||
|
t.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom parsers, failing/failing", (t) => {
|
||||||
|
t.plan(1);
|
||||||
|
const actual = parseConfiguration("name", "", [ failingParser, failingParser ]);
|
||||||
|
const expected = {
|
||||||
|
"config": null,
|
||||||
|
"message": "Unable to parse 'name'; Parser 0: failingParser; Parser 1: failingParser"
|
||||||
|
};
|
||||||
|
t.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue