diff --git a/package.json b/package.json index 3c3e4363..c0a90859 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,13 @@ "build": "node_modules/.bin/parallel-webpack", "doc": "node_modules/.bin/jsdoc -c jsdoc-conf.json", "test": "node_modules/.bin/karma start --single-run --browsers=ChromeHeadless", - "test:all": "for pkg in core jquery jquery-file-upload; do node_modules/.bin/karma start --single-run --cloudinary.pkg=${pkg}; done", - "test:all:prod": "for pkg in core jquery jquery-file-upload; do node_modules/.bin/karma start --single-run --cloudinary.minified --cloudinary.pkg=${pkg}; done", + "test:pkgs": "for pkg in core jquery jquery-file-upload; do node_modules/.bin/karma start --single-run --cloudinary.pkg=${pkg}; done", + "test:pkgs:minified": "for pkg in core jquery jquery-file-upload; do node_modules/.bin/karma start --single-run --cloudinary.minified --cloudinary.pkg=${pkg}; done", "test:types": "node_modules/.bin/tsc", + "test:pkgs:verify": "jasmine test/spec/pkg-spec.js", "pretest": "npm run build", - "pretest:all": "npm run build", - "pretest:all:prod": "npm run build", + "pretest:pkgs": "npm run build", + "pretest:pkgs:minified": "npm run build", "copybuilds": "for pkg in core jquery jquery-file-upload; do pkg/copy_deployment ${pkg}; done", "publishnpm": "npm publish && for pkg in core jquery jquery-file-upload; do publish_to_npm ${pkg}; done" }, @@ -45,6 +46,7 @@ "clean-webpack-plugin": "^1.0.1", "jasmine": "^3.4.0", "jasmine-core": "^3.4.0", + "jasmine-spec-reporter": "^4.2.1", "jquery": "^3.4.0", "karma": "^4.1.0", "karma-babel-preprocessor": "^8.0.0-beta.0", diff --git a/test/spec/pkg-spec.js b/test/spec/pkg-spec.js new file mode 100644 index 00000000..7800e7e0 --- /dev/null +++ b/test/spec/pkg-spec.js @@ -0,0 +1,100 @@ +/** + * Verify existance of all needed files in each package. + * This test file should be run before each release. + */ + +const fs = require('fs'); +const path = require('path'); +const SpecReporter = require('jasmine-spec-reporter').SpecReporter; + +//Set jasmine reporter +jasmine.getEnv().clearReporters(); +jasmine.getEnv().addReporter(new SpecReporter({ + spec: { + displayPending: true + }, +})); + +const pkgPath = path.join(__dirname, '..', '..', 'pkg'); +const commonFiles = ['src', 'package.json', 'README.md']; + +const commonExtensions = { + extensions: ['d.ts', 'js', 'min.js', 'js.map'], + delimiter: '.' +}; + +const shrinkwrapExtensions = { + extensions: ['shrinkwrap.js', 'shrinkwrap.js.map', 'shrinkwrap.min.js'], + delimiter: '-' +}; + +const requiredPackages = [ + createPackage('cloudinary-core', commonExtensions, shrinkwrapExtensions), + createPackage('cloudinary-jquery', commonExtensions), + createPackage('cloudinary-jquery-file-upload', commonExtensions) +]; + +/** + * Verify that a package has only required files + * @param pkg + */ +function verifyPackageConsistency(pkg) { + const actualFiles = fs.readdirSync(`${pkgPath}/${pkg.name}`); + + describe(pkg.name, () => { + it('Should contain required files:', () => { + getArrayDiff(pkg.files, actualFiles).forEach(file => { + fail(file); + }) + }); + it('Should not contain redundant files:', () => { + getArrayDiff(actualFiles, pkg.files).forEach(file => { + fail(file); + }) + }); + }); +} + +/** + * Creates a package object with name and files + * @param pkgName + * @param withShrinkwrap + * @returns {{name: *, files: *}} + */ +function createPackage(pkgName, ...extensions) { + let files = [...commonFiles]; + extensions.forEach(extensionsItem => { + files = extendFileList(files, pkgName, extensionsItem.extensions, extensionsItem.delimiter); + }); + + return { + name: pkgName, + files + }; +} + +/** + * Adds file names to given files array + * @param files + * @param pkgName + * @param extensions + * @param delimiter + * @returns {*} + */ +function extendFileList(files, pkgName, extensions, delimiter) { + return files.concat(extensions.map(extension => `${pkgName}${delimiter}${extension}`)); +} + +/** + * return items from arr1 that arr2 does not contain + * @param arr1 + * @param arr2 + * @returns {*} + */ +function getArrayDiff(arr1, arr2) { + return arr1.filter(item => !arr2.includes(item)); +} + +requiredPackages.forEach(pkg => { + verifyPackageConsistency(pkg); +});