mirror of
https://github.com/awesome-selfhosted/awesome-selfhosted.git
synced 2025-04-29 07:24:18 +02:00
test.js improvements.
Cleaned up unused code. Refactored tests. Add test to check if license is in license lit. Add travis_test branch to .travis to allow triggered builds.
This commit is contained in:
parent
d2bae47d98
commit
fe9ff33ad6
2 changed files with 113 additions and 50 deletions
|
@ -21,4 +21,5 @@ notifications:
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
- master
|
- master
|
||||||
|
- travis_test
|
||||||
|
|
||||||
|
|
156
tests/test.js
156
tests/test.js
|
@ -2,96 +2,158 @@
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
var colors = require('colors/safe');
|
var colors = require('colors/safe');
|
||||||
|
let licenses = new Set();
|
||||||
|
|
||||||
let fails = ''
|
let fails = ''
|
||||||
const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
|
// Reads argv into var file
|
||||||
|
const file = fs.readFileSync(process.argv[2], 'utf8');
|
||||||
|
|
||||||
function entryFilter(md) { // Function to find lines with entries
|
// Function to find lines with entries
|
||||||
|
function entryFilter(md) {
|
||||||
const linepatt = /^\s{0,2}-\s\[.*`/;
|
const linepatt = /^\s{0,2}-\s\[.*`/;
|
||||||
return linepatt.test(md);
|
return linepatt.test(md);
|
||||||
}
|
}
|
||||||
|
|
||||||
function split(text) { // Function to split lines into array
|
// Function to find lines with licenses
|
||||||
|
function licenseFilter(md) {
|
||||||
|
const linepatt = /^- `.*` - .*/;
|
||||||
|
return linepatt.test(md)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to split lines into array
|
||||||
|
function split(text) {
|
||||||
return text.split(/\r?\n/);
|
return text.split(/\r?\n/);
|
||||||
}
|
}
|
||||||
|
|
||||||
function findPattern(text) { // All entries should match this pattern. If matches pattern returns true.
|
// All entries should match this pattern. If matches pattern returns true.
|
||||||
|
function findPattern(text) {
|
||||||
const patt = /^\s{0,2}-\s\[.*?\]\(.*?\) (`⚠` )?- .{0,249}?\.( \(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))? \`.*?\` \`.*?\`$/;
|
const patt = /^\s{0,2}-\s\[.*?\]\(.*?\) (`⚠` )?- .{0,249}?\.( \(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))? \`.*?\` \`.*?\`$/;
|
||||||
if (patt.test(text) === true) {
|
if (patt.test(text) === true) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
function findError(text) {
|
|
||||||
|
// Parses SPDX identifiers from list of licenses
|
||||||
|
function parseLicense(md) {
|
||||||
|
const patt = /^- `(.*)` - .*/
|
||||||
|
return patt.exec(md)[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
//Tests '- [Name](http://homepage/)'
|
||||||
|
function testMainLink(text) {
|
||||||
let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/;
|
let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/;
|
||||||
const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/;
|
const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/;
|
||||||
let testB = /( - .*\. )(?:(\(?\[?|\`))/;
|
if (testA.test(text) === false) {
|
||||||
//const testB1 = /(^ {0,2}- \[.*?\]\(.*?\))(?: -)/;
|
let a1 = testA1.exec(text)[2];
|
||||||
|
return colors.red.underline(text.replace(a1, ''))
|
||||||
|
}
|
||||||
|
return colors.green(testA.exec(text)[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
//Tests '`⚠` - Short description, less than 250 characters.'
|
||||||
|
function testDescription(text) {
|
||||||
|
const testB = /( - .*\. )(?:(\(?\[?|\`))/;
|
||||||
|
const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/;
|
||||||
const testB2 = /((\(\[|\`).*$)/;
|
const testB2 = /((\(\[|\`).*$)/;
|
||||||
|
if (testB.test(text) === false) {
|
||||||
|
let b1 = testA1.exec(text)[1];
|
||||||
|
let b2 = testB2.exec(text)[1];
|
||||||
|
return colors.red.underline(text.replace(b1, '').replace(b2, ''))
|
||||||
|
}
|
||||||
|
return colors.green(testB.exec(text)[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
//If present, tests '([Demo](http://url.to/demo), [Source Code](http://url.of/source/code), [Clients](https://url.to/list/of/related/clients-or-apps))'
|
||||||
|
function testSrcDemCli(text) {
|
||||||
let testC = text.search(/\(\[|\)\,|\)\)/);
|
let testC = text.search(/\(\[|\)\,|\)\)/);
|
||||||
let testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))(?= \`?)/;
|
let testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))(?= \`?)/;
|
||||||
const testD1 = /(^.*\.)(?= )/;
|
const testD1 = /(^.*\.)(?= )/;
|
||||||
const testD2 = /(\`.*\` \`.*\`$)/;
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
||||||
let testE = testD2.test(text);
|
|
||||||
const testE1 = /(^[^`]*)/;
|
|
||||||
let res
|
|
||||||
if (testA.test(text) === false) {
|
|
||||||
let a1 = testA1.exec(text)[2];
|
|
||||||
res = colors.red.underline(text.replace(a1, ''))
|
|
||||||
} else {
|
|
||||||
res = colors.green(testA.exec(text)[1])
|
|
||||||
}
|
|
||||||
if (testB.test(text) === false) {
|
|
||||||
let b1 = testA1.exec(text)[1];
|
|
||||||
let b2 = testB2.exec(text)[1];
|
|
||||||
res += colors.red.underline(text.replace(b1, '').replace(b2, ''))
|
|
||||||
} else {
|
|
||||||
res += colors.green(testB.exec(text)[1])
|
|
||||||
}
|
|
||||||
if ((testC > -1) && (testD.test(text) === false)) {
|
if ((testC > -1) && (testD.test(text) === false)) {
|
||||||
let d1 = testD1.exec(text)[1];
|
let d1 = testD1.exec(text)[1];
|
||||||
let d2 = testD2.exec(text)[1];
|
let d2 = testD2.exec(text)[1];
|
||||||
res += colors.red.underline(text.replace(d1+' ', '').replace(d2, ''))
|
return colors.red.underline(text.replace(d1+' ', '').replace(d2, ''))
|
||||||
} else if (testC > -1) {
|
} else if (testC > -1) {
|
||||||
res += colors.green(testD.exec(text)[1])
|
return colors.green(testD.exec(text)[1])
|
||||||
}
|
}
|
||||||
if (testE === false) {
|
return ""
|
||||||
let e1 = testE1.exec(text)[1];
|
|
||||||
res += colors.red.underline(text.replace(e1, ''))
|
|
||||||
} else {
|
|
||||||
res += colors.green(testD2.exec(text)[1])
|
|
||||||
}
|
|
||||||
return res + `\n`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests '`License` `Language`'
|
||||||
|
function testLangLic(text) {
|
||||||
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
||||||
|
let testE = testD2.test(text);
|
||||||
|
const testE1 = /(^[^`]*)/;
|
||||||
|
if (testE === false) {
|
||||||
|
let e1 = testE1.exec(text)[1];
|
||||||
|
return colors.red.underline(text.replace(e1, ''))
|
||||||
|
}
|
||||||
|
return colors.green(testD2.exec(text)[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
//Runs all the syntax tests...
|
||||||
|
function findError(text) {
|
||||||
|
let res
|
||||||
|
res = testMainLink(text)
|
||||||
|
res += testDescription(text)
|
||||||
|
res += testSrcDemCli(text)
|
||||||
|
res += testLangLic(text)
|
||||||
|
return res + `\n`
|
||||||
|
}
|
||||||
|
//Check if license is in the list of licenses.
|
||||||
|
function testLicense(md) {
|
||||||
|
const regex = /.*\`(.*)\` \`.*\`$/;
|
||||||
|
return licenses.has(regex.exec(md)[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
//Parses name from entry
|
||||||
|
function parseName(md) {
|
||||||
|
const regex = /^\W*(.*?)\W/
|
||||||
|
return regex.exec(md)[1]
|
||||||
|
}
|
||||||
function entryErrorCheck(md) {
|
function entryErrorCheck(md) {
|
||||||
const entries = split(md); // Inserts each line into the entries array
|
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;
|
||||||
const entryArray = [];
|
|
||||||
let failed = [];
|
let failed = [];
|
||||||
if (entries[0] === "") {
|
let entries = [];
|
||||||
console.log(colors.red("0 Entries"))
|
if (lines[0] === "") {
|
||||||
|
console.log(colors.red("0 Entries Found"))
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
for (let i = 0, len = entries.length; i < len; i += 1) { // Loop to create array of objects
|
for (let i = 0; i < lines.length; i ++) { // Loop through array of lines
|
||||||
entryArray[i] = new Object;
|
if (entryFilter(lines[i]) === true) { // filter out lines that don't start with * [)
|
||||||
entryArray[i].raw = entries[i];
|
|
||||||
if (entryFilter(entries[i]) === true) { // filter out lines that don't start with * [)
|
|
||||||
total += 1;
|
total += 1;
|
||||||
entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
|
e = {};
|
||||||
|
e.raw = lines[i];
|
||||||
if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
|
entries.push(e);
|
||||||
|
/* if (findPattern(lines[i])) { // If entry passes increment totalPass counter
|
||||||
totalPass += 1;
|
totalPass += 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
failed.push(findError(entries[i]))
|
failed.push(findError(lines[i]))
|
||||||
totalFail += 1; // If entry fails increment totalFail counter and append error to issuelog
|
totalFail += 1; // If entry fails increment totalFail counter and append error to issuelog
|
||||||
fails += `${entries[i]} \n\n`;
|
fails += `${lines[i]} \n\n`;
|
||||||
|
} */
|
||||||
|
} else if (licenseFilter(lines[i]) === true) {
|
||||||
|
licenses.add(parseLicense(lines[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (let e of entries) {
|
||||||
|
e.name = parseName(e.raw)
|
||||||
|
if (!findPattern(e.raw)) {
|
||||||
|
e.highlight = findError(e.raw);
|
||||||
|
console.log(findError(e.raw))
|
||||||
}
|
}
|
||||||
|
e.licenseTest = testLicense(e.raw);
|
||||||
|
if (e.licenseTest === false) {
|
||||||
|
console.log(colors.yellow(`${e.name}'s license is not on License list.`))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (totalFail > 0) {
|
if (totalFail > 0) {
|
||||||
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++) {
|
for (let i = 0; i < failed.length; i++) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue