diff --git a/spec/README.md b/spec/README.md index 47ce35d0..b760f514 100644 --- a/spec/README.md +++ b/spec/README.md @@ -4,22 +4,16 @@ [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: ``` 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 diff --git a/spec/basic.tests.spec.js b/spec/basic.tests.spec.js deleted file mode 100644 index ca754a71..00000000 --- a/spec/basic.tests.spec.js +++ /dev/null @@ -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); - -}); diff --git a/spec/zip.tests.spec.js b/spec/manifest.tests.spec.js similarity index 53% rename from spec/zip.tests.spec.js rename to spec/manifest.tests.spec.js index 19626450..f3c8ab33 100644 --- a/spec/zip.tests.spec.js +++ b/spec/manifest.tests.spec.js @@ -1,5 +1,12 @@ const TEMP_DIR = ".tmp-zips/"; +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')); + const request = require("request"); const { execSync } = require('child_process'); 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(); 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 @@ -21,7 +28,7 @@ if(VIRUS_TOTAL_ENABLED){ jasmine.DEFAULT_TIMEOUT_INTERVAL = manifestsToTest.length * 4 * con.getDelay(); } -describe("zips", function() { +describe("test manifests", function() { beforeEach(()=>{ execSync(`mkdir -p ${TEMP_DIR}`) @@ -32,32 +39,62 @@ describe("zips", function() { 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) { @@ -109,10 +146,10 @@ function testOneZip(expectedRootDir, osObj, done) { function getManifestsThatApply(){ 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 { - 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() !== ''); }