mirror of
https://github.com/awesome-selfhosted/awesome-selfhosted.git
synced 2024-12-28 17:04:21 +00:00
Merge pull request #1977 from awesome-selfhosted/travis_test
Improve Travis tests
This commit is contained in:
commit
1ca7ed9832
@ -2,17 +2,21 @@ language: node_js
|
|||||||
|
|
||||||
node_js:
|
node_js:
|
||||||
- "node"
|
- "node"
|
||||||
|
cache:
|
||||||
|
npm: false
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- rvm install 2.6.2
|
- rvm install 2.6.2
|
||||||
- gem install awesome_bot
|
- gem install awesome_bot
|
||||||
|
- cd tests && npm install chalk && cd ..
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then git diff origin/master -U0 README.md | grep -Pos "(?<=^\+).*" >> temp.md; fi || (exit 0)'
|
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then git diff origin/master -U0 README.md | grep -Pos "(?<=^\+).*" >> temp.md; fi || (exit 0)'
|
||||||
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then node tests/test.js temp.md; else node tests/test.js README.md; fi'
|
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then node tests/test.js -r README.md -d temp.md; else node tests/test.js -r README.md; fi'
|
||||||
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then if [ -f temp.md ]; then awesome_bot temp.md --allow-redirect --skip-save-results --allow 202 --white-list airsonic.github.io/docs/apps; else (exit 0); fi else awesome_bot README.md --allow-redirect --skip-save-results --allow 202 --white-list airsonic.github.io/docs/apps; fi'
|
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then if [ -f temp.md ]; then awesome_bot temp.md --allow-redirect --skip-save-results --allow 202 --white-list airsonic.github.io/docs/apps; else (exit 0); fi else awesome_bot *.md --allow-redirect --skip-save-results --allow 202 --white-list airsonic.github.io/docs/apps; fi'
|
||||||
|
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
@ -20,4 +24,5 @@ notifications:
|
|||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
- master
|
- master
|
||||||
|
- travis_test
|
||||||
|
|
||||||
|
261
tests/test.js
261
tests/test.js
@ -1,70 +1,251 @@
|
|||||||
// 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');
|
||||||
|
const chalk = require('chalk');
|
||||||
|
let licenses = new Set();
|
||||||
|
let pr = false;
|
||||||
|
let readme;
|
||||||
|
let diff;
|
||||||
|
|
||||||
let log = '{\n';
|
//Parse the command options and set the pr var
|
||||||
let issuelog = ' "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n';
|
function parseArgs(args) {
|
||||||
let fails = ''
|
if ( args.indexOf('-r', 2) > 0 ) {
|
||||||
const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
|
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');
|
||||||
|
}
|
||||||
|
if ( pr === true) {
|
||||||
|
console.log(chalk.blue(`Running on PR. README.md: ${args[args.indexOf('-r', 2)+1]} diff: ${args[args.indexOf('-d', 2)+1]}`))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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.
|
||||||
const patt = /^\s{0,2}-\s\[.*?\]\(.*?\) (`⚠` )?- .{0,249}?\.( \(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))? \`.*?\` \`.*?\`$/;
|
function findPattern(text) {
|
||||||
|
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 entryErrorCheck(md) {
|
// Parses SPDX identifiers from list of licenses
|
||||||
const namepatt = /^\s{0,2}-\s\[(.*?)\]/; // regex pattern to find name of entryArray
|
function parseLicense(md) {
|
||||||
const entries = split(md); // Inserts each line into the entries array
|
const patt = /^- `(.*)` - .*/
|
||||||
|
return patt.exec(md)[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test '- [Name](http://homepage/)'
|
||||||
|
function testMainLink(text) {
|
||||||
|
let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/;
|
||||||
|
const testA1 = /(- \W?\w*\W{0,2}.*?\)?)( .*$)/;
|
||||||
|
if (!testA.test(text)) {
|
||||||
|
let a1 = testA1.exec(text)[2];
|
||||||
|
return chalk.red(text.replace(a1, ''))
|
||||||
|
}
|
||||||
|
return chalk.green(testA.exec(text)[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test '`⚠` - Short description, less than 250 characters.'
|
||||||
|
function testDescription(text) {
|
||||||
|
const testB = /( - .*\. )(?:(\(?\[?|\`))/;
|
||||||
|
const testA1 = /(- \W?\w*\W{0,2}.*?\)?)( .*$)/;
|
||||||
|
const testB2 = /((\(\[|\`).*$)/;
|
||||||
|
if (!testB.test(text)) {
|
||||||
|
let b1 = testA1.exec(text)[1];
|
||||||
|
let b2 = testB2.exec(text)[1];
|
||||||
|
return chalk.red(text.replace(b1, '').replace(b2, ''))
|
||||||
|
}
|
||||||
|
return chalk.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 testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)\]]*\)(, \[(Source Code|Clients)\]\([^)\]]*\))?(, \[(Source Code|Clients)\]\([^)\]]*\))*\))(?= \`?)/;
|
||||||
|
const testD1 = /(^- \W[a-zA-Z0-9-_ ]*\W{0,2}http[^\[]*)(?<= )/;
|
||||||
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
||||||
|
if ((testC > -1) && (!testD.test(text))) {
|
||||||
|
let d1 = testD1.exec(text)[1];
|
||||||
|
let d2 = testD2.exec(text)[1];
|
||||||
|
return chalk.red(text.replace(d1, '').replace(d2, ''))
|
||||||
|
} else if (testC > -1) {
|
||||||
|
return chalk.green(testD.exec(text)[1])
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests '`License` `Language`'
|
||||||
|
function testLangLic(text) {
|
||||||
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
||||||
|
let testE = testD2.test(text);
|
||||||
|
const testE1 = /(^[^`]*)/;
|
||||||
|
if (!testE) {
|
||||||
|
let e1 = testE1.exec(text)[1];
|
||||||
|
return chalk.red(text.replace(e1, ''))
|
||||||
|
}
|
||||||
|
return chalk.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) {
|
||||||
|
let pass = true;
|
||||||
|
let lFailed = []
|
||||||
|
let lPassed = []
|
||||||
|
const regex = /.*\`(.*)\` .*$/;
|
||||||
|
try {
|
||||||
|
for (l of regex.exec(md)[1].split("/")) {
|
||||||
|
if (!licenses.has(l)) {
|
||||||
|
pass = false;
|
||||||
|
lPassed.push(l)
|
||||||
|
}
|
||||||
|
lFailed.push(l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
console.log(chalk.yellow("Error in License syntax, license not checked against list."))
|
||||||
|
return [false, "", ""]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return [pass, lFailed, lPassed]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Parses name from entry
|
||||||
|
function parseName(md) {
|
||||||
|
const regex = /^\W*(.*?)\W/
|
||||||
|
return regex.exec(md)[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
function entryErrorCheck() {
|
||||||
|
const lines = split(readme); // 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 entries = [];
|
||||||
if (entries[0] === "") {
|
let diffEntries = [];
|
||||||
console.log("0 Entries")
|
|
||||||
|
if (lines[0] === "") {
|
||||||
|
console.log(chalk.red("0 Entries Found, check your commandline arguments"))
|
||||||
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];
|
e = {};
|
||||||
if (entryFilter(entries[i]) === true) { // filter out lines that don't start with * [)
|
e.raw = lines[i];
|
||||||
total += 1;
|
e.line = i + 1
|
||||||
entryArray[i].name = namepatt.exec(entries[i])[1]; // Parses name of entry
|
entries.push(e);
|
||||||
entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
|
} else if (licenseFilter(lines[i]) === true) {
|
||||||
if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
|
licenses.add(parseLicense(lines[i]))
|
||||||
totalPass += 1;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pr === true) {
|
||||||
|
console.log(chalk.cyan("Only testing the diff from the PR.\n"))
|
||||||
|
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;
|
||||||
|
diffEntries.push(e);
|
||||||
|
} else if (licenseFilter(l) === true) {
|
||||||
|
licenses.add(parseLicense(l))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (diffEntries.length === 0) {
|
||||||
|
console.log("No entries changed in README.md, Exiting...")
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
total = diffEntries.length
|
||||||
|
for (let e of diffEntries) {
|
||||||
|
e.pass = true
|
||||||
|
e.name = parseName(e.raw)
|
||||||
|
if (!findPattern(e.raw)) {
|
||||||
|
e.highlight = findError(e.raw);
|
||||||
|
e.pass = false;
|
||||||
|
console.log(e.highlight)
|
||||||
|
}
|
||||||
|
e.licenseTest = testLicense(e.raw);
|
||||||
|
if (!e.licenseTest) {
|
||||||
|
e.pass = false;
|
||||||
|
console.log(chalk.red(`${e.name}'s license is not on License list.`))
|
||||||
|
}
|
||||||
|
if (e.pass) {
|
||||||
|
totalPass++
|
||||||
} else {
|
} else {
|
||||||
console.log(`${entryArray[i].name} Failed.`); // If entry fails increment totalFail counter and append error to issuelog
|
totalFail++
|
||||||
// entryArray[i].error = findError(entries[i]) //WIP
|
}
|
||||||
totalFail += 1;
|
}
|
||||||
issuelog += `${entryArray[i].name} | ${entries[i]} \\n`;
|
} else {
|
||||||
fails += `${entries[i]} \n\n`;
|
console.log(chalk.cyan("Testing entire README.md\n"))
|
||||||
|
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(`${chalk.yellow(e.line + ": ")}${e.highlight}`);
|
||||||
|
syntax = e.highlight;
|
||||||
|
}
|
||||||
|
e.licenseTest = testLicense(e.raw);
|
||||||
|
if (!e.licenseTest[0]) {
|
||||||
|
e.pass = false;
|
||||||
|
console.log(chalk.yellow(e.line + ": ") + `${e.name}'s license ${chalk.red(`'${e.licenseTest[1]}'`)} is not on the License list.\n`)
|
||||||
|
}
|
||||||
|
if (e.pass) {
|
||||||
|
totalPass++
|
||||||
|
} else {
|
||||||
|
totalFail++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (totalFail > 0) { // Logs # passed & failed to console, and failures to syntaxcheck.json
|
if (totalFail > 0) {
|
||||||
console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total}\n-----------------------------`);
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
||||||
console.log(fails)
|
console.log(chalk.red(`${totalFail} Failed, `) + chalk.green(`${totalPass} Passed, `) + chalk.blue(`of ${total}`))
|
||||||
log += ` "error": true,\n "title": "Found ${totalFail} entries with syntax error(s).",\n`;
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
||||||
fs.writeFileSync('syntaxcheck.json', `${log} ${issuelog} "\n}`);
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else { // Logs # of entries passed to console and error: false to syntaxcheck.json
|
} else {
|
||||||
console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total} \n`);
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
||||||
log += ' "error": false\n}';
|
console.log(chalk.green(`${totalPass} Passed of ${total}`))
|
||||||
fs.writeFileSync('syntaxcheck.json', log);
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
||||||
process.exit(0);
|
process.exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entryErrorCheck(file);
|
parseArgs(process.argv)
|
||||||
|
entryErrorCheck();
|
||||||
|
Loading…
Reference in New Issue
Block a user