Fix errors when checking PR

This commit is contained in:
n8225 2020-02-06 18:44:22 -06:00
parent 452aacfd3d
commit 24b68d4e24

View file

@ -1,13 +1,26 @@
// Accepts input of any filename, ie. node test.js README.md // USAGE:
// node test.js -r README.md (Checks whole file)
// node test.js -r README.md -d temp.md (Checks just the diff)
const fs = require('fs'); const fs = require('fs');
var colors = require('colors/safe'); const colors = require('colors/safe');
let licenses = new Set(); let licenses = new Set();
const pr = process.env.TRAVIS_PULL_REQUEST let pr = false;
let readme;
let diff;
let fails = '' //Parse the command options and set the pr var
// Reads argv into var file function parseArgs(args) {
const file = fs.readFileSync(process.argv[2], 'utf8'); console.log(args)
if ( args.indexOf('-r', 2) > 0 ) {
console.log(args.indexOf('-r', 2))
readme = fs.readFileSync(args[args.indexOf('-r', 2)+1], 'utf8')
}
if (args.indexOf('-d', 2) > 0) {
pr = true;
diff = fs.readFileSync(args[args.indexOf('-d', 2)+1], 'utf8');
}
}
// Function to find lines with entries // Function to find lines with entries
function entryFilter(md) { function entryFilter(md) {
@ -114,56 +127,54 @@ function parseName(md) {
return regex.exec(md)[1] return regex.exec(md)[1]
} }
//Returns line number if this is not a PR function entryErrorCheck() {
function isPr(l) { const lines = split(readme); // Inserts each line into the entries array
if (pr === 'false') {
return `Line ${l}: `
}
return ''
}
function entryErrorCheck(md) {
const lines = split(md); // Inserts each line into the entries array
let totalFail = 0; let totalFail = 0;
let totalPass = 0; let totalPass = 0;
let total = 0; let total = 0;
let failed = []; let failed = [];
let entries = []; let entries = [];
if (lines[0] === "") { if (lines[0] === "") {
console.log(colors.red("0 Entries Found")) console.log(colors.red("0 Entries Found"))
process.exit(0) process.exit(0)
} }
for (let i = 0; i < lines.length; i ++) { // Loop through array of lines for (let i = 0; i < lines.length; i ++) { // Loop through array of lines
if (entryFilter(lines[i]) === true) { // filter out lines that don't start with * [) if (entryFilter(lines[i]) === true) { // filter out lines that don't start with * [)
total += 1;
e = {}; e = {};
e.raw = lines[i]; e.raw = lines[i];
e.line = i e.line = i
entries.push(e); entries.push(e);
/* if (findPattern(lines[i])) { // If entry passes increment totalPass counter
totalPass += 1;
} else {
failed.push(findError(lines[i]))
totalFail += 1; // If entry fails increment totalFail counter and append error to issuelog
fails += `${lines[i]} \n\n`;
} */
} else if (licenseFilter(lines[i]) === true) { } else if (licenseFilter(lines[i]) === true) {
licenses.add(parseLicense(lines[i])) licenses.add(parseLicense(lines[i]))
} }
} }
if (pr === true) {
console.log("Only testing the diff from the PR.")
const diffLines = split(diff); // Inserts each line of diff into an array
for (let l of diffLines) {
if (entryFilter(l) === true) { // filter out lines that don't start with * [)
e = {};
e.raw = l;
entries.push(e);
} else if (licenseFilter(l) === true) {
licenses.add(parseLicense(l))
}
}
total = entries.length
for (let e of entries) { for (let e of entries) {
e.pass = true e.pass = true
e.name = parseName(e.raw) e.name = parseName(e.raw)
if (!findPattern(e.raw)) { if (!findPattern(e.raw)) {
e.highlight = findError(e.raw); e.highlight = findError(e.raw);
e.pass = false; e.pass = false;
console.log(`${colors.yellow(isPr(e.line))}${e.highlight}`) console.log(`${e.highlight}`)
} }
e.licenseTest = testLicense(e.raw); e.licenseTest = testLicense(e.raw);
if (e.licenseTest === false) { if (e.licenseTest === false) {
e.pass = false; e.pass = false;
console.log(colors.yellow(`${isPr(e.line)}${e.name}'s license is not on License list.`)) console.log(`${e.name}'s license is not on License list.`)
} }
if (e.pass) { if (e.pass) {
totalPass++ totalPass++
@ -171,14 +182,35 @@ function entryErrorCheck(md) {
totalFail++ totalFail++
} }
} }
} else {
console.log("Testing entire README.md")
total = entries.length
for (let e of entries) {
e.pass = true
e.name = parseName(e.raw)
if (!findPattern(e.raw)) {
e.highlight = findError(e.raw);
e.pass = false;
console.log(`${colors.yellow(e.line)}${e.highlight}`)
}
e.licenseTest = testLicense(e.raw);
if (e.licenseTest === false) {
e.pass = false;
console.log(colors.yellow(`${e.line}${e.name}'s license is not on License list.`))
}
if (e.pass) {
totalPass++
} else {
totalFail++
}
}
}
if (totalFail > 0) { if (totalFail > 0) {
console.log(colors.blue(`\n-----------------------------\n`)) console.log(colors.blue(`\n-----------------------------\n`))
console.log(colors.green("The portion of the entry with an error ") + colors.underline.red("will be underlined and RED") + `\n`) console.log(colors.green("The portion of the entry with an error ") + colors.underline.red("will be underlined and RED") + `\n`)
for (let i = 0; i < failed.length; i++) {
console.log(failed[i])
}
console.log(colors.blue(`\n-----------------------------\n`)) console.log(colors.blue(`\n-----------------------------\n`))
console.log(colors.red(`${totalFail} Failed, `) + colors.green(`${totalPass} Passed, `) + colors.blue(`of ${total}`)) console.log(colors.red(`${totalFail} Failed, `) + colors.green(`${totalPass} Passed, `) + colors.blue(`of ${total}`))
console.log(colors.blue(`\n-----------------------------\n`)) console.log(colors.blue(`\n-----------------------------\n`))
@ -193,4 +225,5 @@ function entryErrorCheck(md) {
} }
entryErrorCheck(file); parseArgs(process.argv)
entryErrorCheck();