Add parsers parameter to readConfig/Sync to support non-JSON formats like YAML (fixes #118).

This commit is contained in:
David Anson 2018-05-23 22:24:40 -07:00
parent 2b4ecdced8
commit 101edd8496
10 changed files with 252 additions and 31 deletions

View file

@ -0,0 +1 @@
@

View file

@ -0,0 +1,2 @@
no-hard-tabs: false
whitespace: false

View file

@ -0,0 +1,6 @@
extends: config-parent-hybrid.toml
MD003:
style: atx_closed
MD007:
indent: 2
no-hard-tabs: false

View file

@ -0,0 +1,6 @@
extends: config-parent.yaml
MD003:
style: atx_closed
MD007:
indent: 2
no-hard-tabs: false

View file

@ -0,0 +1,9 @@
extends = "config-child.json"
MD003 = false
no-hard-tabs = true
[MD007]
indent = 4
[line-length]
line_length = 200

View file

@ -0,0 +1,7 @@
extends: config-child.yaml
MD003: false
MD007:
indent: 4
no-hard-tabs: true
line-length:
line_length: 200

View file

@ -924,6 +924,7 @@ module.exports.readmeHeadings = function readmeHeadings(test) {
"#### result",
"### Config",
"#### file",
"#### parsers",
"#### callback",
"#### result",
"## Usage",
@ -1466,6 +1467,72 @@ module.exports.configBadChildJson = function configBadChildJson(test) {
});
};
module.exports.configSingleYaml = function configSingleYaml(test) {
test.expect(2);
markdownlint.readConfig(
"./test/config/config-child.yaml",
[ require("js-yaml").safeLoad ],
function callback(err, actual) {
test.ifError(err);
const expected = require("./config/config-child.json");
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
});
};
module.exports.configMultipleYaml = function configMultipleYaml(test) {
test.expect(2);
markdownlint.readConfig(
"./test/config/config-grandparent.yaml",
[ require("js-yaml").safeLoad ],
function callback(err, actual) {
test.ifError(err);
const expected = shared.assign(
shared.assign(
shared.assign({}, require("./config/config-child.json")),
require("./config/config-parent.json")),
require("./config/config-grandparent.json"));
delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
});
};
module.exports.configMultipleHybrid = function configMultipleHybrid(test) {
test.expect(2);
markdownlint.readConfig(
"./test/config/config-grandparent-hybrid.yaml",
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ],
function callback(err, actual) {
test.ifError(err);
const expected = shared.assign(
shared.assign(
shared.assign({}, require("./config/config-child.json")),
require("./config/config-parent.json")),
require("./config/config-grandparent.json"));
delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
});
};
module.exports.configBadHybrid = function configBadHybrid(test) {
test.expect(4);
markdownlint.readConfig(
"./test/config/config-badcontent.txt",
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ],
function callback(err, result) {
test.ok(err, "Did not get an error for bad child JSON.");
test.ok(err instanceof Error, "Error not instance of Error.");
test.ok(err.message.match(
// eslint-disable-next-line max-len
/^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+; Expected [^;]+ or end of input but "\S+" found.; end of the stream or a document separator is expected at line \d+, column \d+:[^;]*$/
), "Error message unexpected.");
test.ok(!result, "Got result for bad child JSON.");
test.done();
});
};
module.exports.configSingleSync = function configSingleSync(test) {
test.expect(1);
const actual = markdownlint.readConfigSync("./test/config/config-child.json");
@ -1530,8 +1597,10 @@ module.exports.configBadJsonSync = function configBadJsonSync(test) {
}, function testError(err) {
test.ok(err, "Did not get an error for bad JSON.");
test.ok(err instanceof Error, "Error not instance of Error.");
test.ok(err.message.match(/Unexpected token b in JSON at position \d+/),
"Error message unexpected.");
test.ok(err.message.match(
// eslint-disable-next-line max-len
/^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+$/
), "Error message unexpected.");
return true;
}, "Did not get exception for bad JSON.");
test.done();
@ -1544,13 +1613,72 @@ module.exports.configBadChildJsonSync = function configBadChildJsonSync(test) {
}, function testError(err) {
test.ok(err, "Did not get an error for bad child JSON.");
test.ok(err instanceof Error, "Error not instance of Error.");
test.ok(err.message.match(/Unexpected token b in JSON at position \d+/),
"Error message unexpected.");
test.ok(err.message.match(
// eslint-disable-next-line max-len
/^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+$/
), "Error message unexpected.");
return true;
}, "Did not get exception for bad child JSON.");
test.done();
};
module.exports.configSingleYamlSync = function configSingleYamlSync(test) {
test.expect(1);
const actual = markdownlint.readConfigSync(
"./test/config/config-child.yaml", [ require("js-yaml").safeLoad ]);
const expected = require("./config/config-child.json");
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
};
module.exports.configMultipleYamlSync = function configMultipleYamlSync(test) {
test.expect(1);
const actual = markdownlint.readConfigSync(
"./test/config/config-grandparent.yaml", [ require("js-yaml").safeLoad ]);
const expected = shared.assign(
shared.assign(
shared.assign({}, require("./config/config-child.json")),
require("./config/config-parent.json")),
require("./config/config-grandparent.json"));
delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
};
module.exports.configMultipleHybridSync =
function configMultipleHybridSync(test) {
test.expect(1);
const actual = markdownlint.readConfigSync(
"./test/config/config-grandparent-hybrid.yaml",
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ]);
const expected = shared.assign(
shared.assign(
shared.assign({}, require("./config/config-child.json")),
require("./config/config-parent.json")),
require("./config/config-grandparent.json"));
delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct.");
test.done();
};
module.exports.configBadHybridSync = function configBadHybridSync(test) {
test.expect(4);
test.throws(function badHybridCall() {
markdownlint.readConfigSync(
"./test/config/config-badcontent.txt",
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ]);
}, function testError(err) {
test.ok(err, "Did not get an error for bad content.");
test.ok(err instanceof Error, "Error not instance of Error.");
test.ok(err.message.match(
// eslint-disable-next-line max-len
/^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+; Expected [^;]+ or end of input but "\S+" found.; end of the stream or a document separator is expected at line \d+, column \d+:[^;]*$/
), "Error message unexpected.");
return true;
}, "Did not get exception for bad content.");
test.done();
};
module.exports.customRulesV0 = function customRulesV0(test) {
test.expect(4);
const customRulesMd = "./test/custom-rules.md";