Browse Source

merged tests, now we only test changed files

pull/79/merge
Jeremy Wentworth 7 years ago
parent
commit
844991cf14
3 changed files with 66 additions and 82 deletions
  1. +3
    -9
      spec/README.md
  2. +0
    -47
      spec/basic.tests.spec.js
  3. +63
    -26
      spec/manifest.tests.spec.js

+ 3
- 9
spec/README.md View File

@@ -4,22 +4,16 @@


[Jasmine Intro](https://jasmine.github.io/2.8/introduction.html) [Jasmine Intro](https://jasmine.github.io/2.8/introduction.html)


* the **basic** tests run on every manifest on any change.
* the **zip** tests only run on manifest files which are different than master.
Tests only run on changed manifests files.


To run all tests: To run all tests:
``` ```
npm test npm test
``` ```


To run one set of tests:
To force the tests to run for certain manifest files:
``` ```
npm test spec/basic.tests.spec.js
```

To force the zip tests to run for certain manifest files:
```
TEST_MANIFEST_ZIPS=plugins/JW-Modules.json npm test spec/zip.tests.spec.js
TEST_MANIFEST=plugins/JW-Modules.json npm test spec/zip.tests.spec.js
``` ```


## Schema Validation ## Schema Validation


+ 0
- 47
spec/basic.tests.spec.js View File

@@ -1,47 +0,0 @@
const CUR_VER_PREFIX = '0.5';
const MF_DIR = 'plugins';
const fs = require("fs");

const Ajv = require('ajv'); //https://github.com/epoberezkin/ajv
const ajv = new Ajv({allErrors:true});
const validate = ajv.compile(require('./manifest.json'));

describe("json", function() {

let testMF = (fileName) => {
it("manifest file should be valid", () => {
try {
const filePath = `${MF_DIR}/${fileName}`;
if (!filePath.toLowerCase().endsWith('.json')) {
fail("manifests should have .json extension");
}

const fileContent = fs.readFileSync(filePath, 'utf8');
const mfObj = JSON.parse(fileContent);
const valid = validate(mfObj);
if (!valid) {
validate.errors.map(e => e.message += ` in ${filePath}`)
fail(validate.errors);
}

if (!(/^[a-zA-Z0-9_\-]*$/).test(mfObj.slug)) {
fail(`slug does not match regex in ${filePath}`);
}

if (fileName.replace('.json', '') !== mfObj.slug) {
fail(`slug '${mfObj.slug}' does not match fileName: ${fileName}`);
}

if (mfObj.version && !mfObj.version.startsWith(CUR_VER_PREFIX)) {
fail(`version '${mfObj.version}' must start with '${CUR_VER_PREFIX}'`);
}

} catch(err){
fail(`Error while trying to validate manifest: ${fileName}\n${err}`);
}
});
};
fs.readdirSync(MF_DIR).map(testMF);

});

spec/zip.tests.spec.js → spec/manifest.tests.spec.js View File

@@ -1,5 +1,12 @@
const TEMP_DIR = ".tmp-zips/"; const TEMP_DIR = ".tmp-zips/";
const CUR_VER_PREFIX = '0.5';
const MF_DIR = 'plugins';
const fs = require("fs"); const fs = require("fs");

const Ajv = require('ajv'); //https://github.com/epoberezkin/ajv
const ajv = new Ajv({allErrors:true});
const validate = ajv.compile(require('./manifest.json'));

const request = require("request"); const request = require("request");
const { execSync } = require('child_process'); const { execSync } = require('child_process');
const AdmZip = require('adm-zip'); //https://github.com/cthackers/adm-zip const AdmZip = require('adm-zip'); //https://github.com/cthackers/adm-zip
@@ -7,8 +14,8 @@ const hashFiles = require('hash-files'); //https://github.com/mac-/hash-files
const manifestsToTest = getManifestsThatApply(); const manifestsToTest = getManifestsThatApply();


if(manifestsToTest.length === 0){ if(manifestsToTest.length === 0){
console.log("No manifest zip files to test.");
process.exit(0);
console.log("No manifest files to test.");
return;
} }


//virus total stuff //virus total stuff
@@ -21,7 +28,7 @@ if(VIRUS_TOTAL_ENABLED){
jasmine.DEFAULT_TIMEOUT_INTERVAL = manifestsToTest.length * 4 * con.getDelay(); jasmine.DEFAULT_TIMEOUT_INTERVAL = manifestsToTest.length * 4 * con.getDelay();
} }


describe("zips", function() {
describe("test manifests", function() {
beforeEach(()=>{ beforeEach(()=>{
execSync(`mkdir -p ${TEMP_DIR}`) execSync(`mkdir -p ${TEMP_DIR}`)
@@ -32,32 +39,62 @@ describe("zips", function() {
fs.rmdirSync(TEMP_DIR); fs.rmdirSync(TEMP_DIR);
}); });


const testZipsInMF = (filePath) => {
it("valid zip files in manifest", function(done) {
const mfObj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
if(mfObj.downloads){
const urlsChecked = [];
let lastSha256;
['win', 'lin', 'mac'].map(os => {
const osObj = mfObj.downloads[os];
if(osObj && osObj.download && osObj.sha256){
const zipUrl = osObj.download;
if(urlsChecked.includes(zipUrl)){
if(lastSha256 !== osObj.sha256){
fail("SHA256 should be the same if the download URL is the same");
const testMF = (filePath) => {
it("valid properties and zip files", function(done) {

try {
if (!filePath.toLowerCase().endsWith('.json')) {
fail("manifests should have .json extension");
}

const fileContent = fs.readFileSync(filePath, 'utf8');
const mfObj = JSON.parse(fileContent);
const valid = validate(mfObj);
if (!valid) {
validate.errors.map(e => e.message += ` in ${filePath}`)
fail(validate.errors);
}

if (!(/^[a-zA-Z0-9_\-]*$/).test(mfObj.slug)) {
fail(`slug does not match regex in ${filePath}`);
}

const fileName = filePath.replace('plugins/', '');
if (fileName.replace('.json', '') !== mfObj.slug) {
fail(`slug '${mfObj.slug}' does not match fileName: ${fileName}`);
}

if (mfObj.version && !mfObj.version.startsWith(CUR_VER_PREFIX)) {
fail(`version '${mfObj.version}' must start with '${CUR_VER_PREFIX}'`);
}

if(mfObj.downloads){
const urlsChecked = [];
let lastSha256;
['win', 'lin', 'mac'].map(os => {
const osObj = mfObj.downloads[os];
if(osObj && osObj.download && osObj.sha256){
const zipUrl = osObj.download;
if(urlsChecked.includes(zipUrl)){
if(lastSha256 !== osObj.sha256){
fail("SHA256 should be the same if the download URL is the same");
}
} else {
urlsChecked.push(zipUrl);
lastSha256 = osObj.sha256;
testOneZip(mfObj.slug, osObj, done);
} }
} else {
urlsChecked.push(zipUrl);
lastSha256 = osObj.sha256;
testOneZip(mfObj.slug, osObj, done);
} }
}
});
});
}

} catch(err){
fail(`Error while trying to validate manifest: ${fileName}\n${err}`);
} }
}); });
}; };


manifestsToTest.map(testZipsInMF);
manifestsToTest.map(testMF);
}); });


function testOneZip(expectedRootDir, osObj, done) { function testOneZip(expectedRootDir, osObj, done) {
@@ -109,10 +146,10 @@ function testOneZip(expectedRootDir, osObj, done) {


function getManifestsThatApply(){ function getManifestsThatApply(){
let paths = ""; let paths = "";
if(process.env.TEST_MANIFEST_ZIPS){
paths = process.env.TEST_MANIFEST_ZIPS;
if(process.env.TEST_MANIFEST){
paths = process.env.TEST_MANIFEST;
} else { } else {
paths = execSync('git diff -w --stat --name-only origin/master -- plugins/', {encoding:'utf8'});
paths = execSync(`git diff -w --stat --name-only origin/master -- ${MF_DIR}/`, {encoding:'utf8'});
} }
return paths.trim().split('\n').filter(s=>s.trim() !== ''); return paths.trim().split('\n').filter(s=>s.trim() !== '');
} }

Loading…
Cancel
Save