mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Use Q/promises to remove callback nesting from test harness.
This commit is contained in:
parent
a2d42b6208
commit
d2e38c1646
2 changed files with 39 additions and 33 deletions
|
|
@ -23,7 +23,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^0.15.0",
|
"eslint": "^0.15.0",
|
||||||
"nodeunit": "^0.9.0"
|
"nodeunit": "^0.9.0",
|
||||||
|
"q": "^1.2.0"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"markdown",
|
"markdown",
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,35 @@
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var markdownlint = require("../lib/markdownlint");
|
var markdownlint = require("../lib/markdownlint");
|
||||||
|
var Q = require("q");
|
||||||
var encodingUtf8 = { "encoding": "utf8" };
|
|
||||||
|
|
||||||
function createTestForFile(file) {
|
function createTestForFile(file) {
|
||||||
return function testForFile(test) {
|
return function testForFile(test) {
|
||||||
test.expect(4);
|
test.expect(1);
|
||||||
fs.readFile(file, encodingUtf8, function readFileCallback(err, contents) {
|
var configFile = file.replace(/\.md$/, ".json");
|
||||||
test.ifError(err);
|
var actualPromise = Q.nfcall(fs.stat, configFile)
|
||||||
var lines = contents.split(/\r\n|\r|\n/g);
|
.then(
|
||||||
function lintFile(config) {
|
function configFileExists() {
|
||||||
|
return Q.nfcall(fs.readFile, configFile, { "encoding": "utf8" })
|
||||||
|
.then(
|
||||||
|
function configFileContents(contents) {
|
||||||
|
return JSON.parse(contents);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function noConfigFile() {
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.then(
|
||||||
|
function lintWithConfig(config) {
|
||||||
|
return Q.nfcall(markdownlint, {
|
||||||
|
"files": [ file ],
|
||||||
|
"config": config
|
||||||
|
});
|
||||||
|
});
|
||||||
|
var expectedPromise = Q.nfcall(fs.readFile, file, { "encoding": "utf8" })
|
||||||
|
.then(
|
||||||
|
function fileContents(contents) {
|
||||||
|
var lines = contents.split(/\r\n|\r|\n/g);
|
||||||
var results = {};
|
var results = {};
|
||||||
lines.forEach(function forLine(line, lineNum) {
|
lines.forEach(function forLine(line, lineNum) {
|
||||||
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
||||||
|
|
@ -23,32 +42,18 @@ function createTestForFile(file) {
|
||||||
results[rule] = errors;
|
results[rule] = errors;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
markdownlint({
|
return results;
|
||||||
"files": [ file ],
|
|
||||||
"config": config
|
|
||||||
}, function markdownlintCallback(errr, actual) {
|
|
||||||
test.ifError(errr);
|
|
||||||
var expected = {};
|
|
||||||
expected[file] = results;
|
|
||||||
test.deepEqual(actual, expected, "Line numbers are not correct.");
|
|
||||||
test.done();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
var configFile = file.replace(/\.md$/, ".json");
|
|
||||||
fs.stat(configFile, function statCallback(errr /*, stats*/) {
|
|
||||||
if (errr) {
|
|
||||||
test.ok(true, "Replacement for ifError of readFile");
|
|
||||||
lintFile();
|
|
||||||
} else {
|
|
||||||
fs.readFile(configFile, encodingUtf8,
|
|
||||||
function readFile(errrr, configContents) {
|
|
||||||
test.ifError(errrr);
|
|
||||||
var config = JSON.parse(configContents);
|
|
||||||
lintFile(config);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
Q.all([ actualPromise, expectedPromise ])
|
||||||
|
.then(
|
||||||
|
function compareResults(fulfillments) {
|
||||||
|
var actual = fulfillments[0];
|
||||||
|
var results = fulfillments[1];
|
||||||
|
var expected = {};
|
||||||
|
expected[file] = results;
|
||||||
|
test.deepEqual(actual, expected, "Line numbers are not correct.");
|
||||||
|
})
|
||||||
|
.done(test.done, test.done);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue