You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6KB

  1. const CUR_VER_PREFIX = '0.5';
  2. const MF_DIR = 'plugins';
  3. const fs = require("fs");
  4. const Ajv = require('ajv'); //https://github.com/epoberezkin/ajv
  5. const ajv = new Ajv({allErrors:true});
  6. const validate = ajv.compile(require('./manifest.json'));
  7. describe("json", function() {
  8. let testMF = (fileName) => {
  9. it("manifest file should be valid", () => {
  10. try {
  11. const filePath = `${MF_DIR}/${fileName}`;
  12. if (!filePath.toLowerCase().endsWith('.json')) {
  13. fail("manifests should have .json extension");
  14. }
  15. const fileContent = fs.readFileSync(filePath, 'utf8');
  16. const mfObj = JSON.parse(fileContent);
  17. const valid = validate(mfObj);
  18. if (!valid) {
  19. validate.errors.map(e => e.message += ` in ${filePath}`)
  20. fail(validate.errors);
  21. }
  22. if (!(/^[a-zA-Z0-9_\-]*$/).test(mfObj.slug)) {
  23. fail(`slug does not match regex in ${filePath}`);
  24. }
  25. if (fileName.replace('.json', '') !== mfObj.slug) {
  26. fail(`slug '${mfObj.slug}' does not match fileName: ${fileName}`);
  27. }
  28. if (mfObj.version && !mfObj.version.startsWith(CUR_VER_PREFIX)) {
  29. fail(`version '${mfObj.version}' must start with '${CUR_VER_PREFIX}'`);
  30. }
  31. } catch(err){
  32. fail(`Error while trying to validate manifest: ${fileName}\n${err}`);
  33. }
  34. });
  35. };
  36. fs.readdirSync(MF_DIR).map(testMF);
  37. });