Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cleanup json output to be machine-readable #208

Merged
merged 5 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ if (require.main === module) {
"output overall test results, excluding pass/fail results"
)
.option("-v, --verbose", "include descriptions about each column")
.option(
"-e, --exit",
"exit with a console error if any tests fail (useful for CI)"
)
.option("-x, --exclude", "exclude tests that passed")
.option(
"-m, --sampleMin <int>",
Expand Down Expand Up @@ -149,6 +153,7 @@ if (require.main === module) {
suites: SUITES,
renderer: Rendering,
loaded: loaded,
json: program.opts().json || program.opts().jsonPretty,
};
Processing.run(processorConfig).then(function (processor) {
const { results } = processor;
Expand All @@ -167,6 +172,7 @@ if (require.main === module) {

var totalTests = 0;
var totalPassed = 0;
var totalFailed = 0;
var resultStr = "\n";
suiteNames.forEach(function (suiteName) {
var testNames = Object.keys(results[suiteName]);
Expand All @@ -184,6 +190,7 @@ if (require.main === module) {
break;
case "failed":
resultStr += chalk.red(test.testState) + "\n";
totalFailed += 1;
break;
case "info":
totalTests -= 1;
Expand Down Expand Up @@ -225,37 +232,40 @@ if (require.main === module) {
);
return;
}
process.stdout.write(summaryStr);

let exit = program.opts().exit && totalFailed > 0;
var done = function () {
process.stdout.write("\n### DONE ###\n\n");
if (!program.opts().json || program.opts().jsonPretty) {
process.stdout.write(summaryStr);
process.stdout.write("\n### PROOFED ###\n\n");
}
if (exit) process.exit(1);
return;
};

var outPath = program.opts().out ? program.opts().out : "/dev/stdout";

if (program.opts().out) resultStr = resultStr.replace(/\[\d+m/g, "");
if (program.opts().json === true) {
rw.writeFileSync(outPath, JSON.stringify(results), "utf-8");
done();
rw.writeFile(outPath, JSON.stringify(results), done);
return;
}
if (program.opts().jsonPretty === true) {
rw.writeFileSync(outPath, JSON.stringify(results, null, 2), "utf-8");
done();
rw.writeFile(outPath, JSON.stringify(results, null, 2), done);
return;
}
if (program.opts().summary !== true) {
rw.writeFileSync(outPath, resultStr, "utf-8");
rw.writeFile(outPath, resultStr, done);
} else {
done();
return;
}
});
} else {
process.stderr.write(
chalk.red(
"Error: Must use a supported filetype. Currently supported filetypes: " +
allowFileExtensions.join(", "),
"utf-8"
"utf8"
)
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ Processor.prototype = {

var sampleSize = Math.round(sampleRatio * totalRows);

console.info("\ntotal rows", totalRows);
console.info("rows sampled", sampleSize);

if (sampleSize < 1000 && totalRows < 1000) {
// test all the rows if there's less than a thousand in total
sampleSize = totalRows;
Expand Down Expand Up @@ -119,6 +116,7 @@ Processor.prototype = {
rows: rows,
sampleProgress: sampleProgress,
totalRows: totalRows,
json: config.json,
});

var badColumnHeadsTest = new DataprooferTest()
Expand Down
6 changes: 5 additions & 1 deletion src/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @return {undefined}
*/
function Renderer(config) {
Object.assign(this, config);
var results = (this.results = {});
config.suites.forEach(function (suite) {
// console.log("suite name", suite);
Expand Down Expand Up @@ -49,7 +50,10 @@ Renderer.prototype.addError = function (suite, test, error) {
*/
Renderer.prototype.done = function () {
// finish up
process.stdout.write("\n### PROOFED ###\n");
if (!this.json) {
console.info("\ntotal rows", this.totalRows);
console.info("rows sampled", this.rows.length);
}
};

module.exports = Renderer;