UNPKG

@cumulus/common

Version:
165 lines 6.26 kB
"use strict"; /* eslint no-console: "off" */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.jlog = exports.findTestDataDirectory = exports.findGitRepoRootDirectory = exports.validateOutput = exports.validateConfig = exports.validateInput = exports.randomStringFromRegex = exports.randomNumber = exports.randomId = exports.randomString = exports.throwTestError = exports.inTestMode = exports.readJsonFixture = void 0; const ajv_1 = __importDefault(require("ajv")); const crypto_1 = __importDefault(require("crypto")); const path_1 = __importDefault(require("path")); const randexp_1 = __importDefault(require("randexp")); const fs_extra_1 = __importDefault(require("fs-extra")); var FileUtils_1 = require("./FileUtils"); Object.defineProperty(exports, "readJsonFixture", { enumerable: true, get: function () { return FileUtils_1.readJsonFile; } }); const inTestMode = (env = process.env) => env.NODE_ENV === 'test'; exports.inTestMode = inTestMode; /** * Helper function to throw error for unit test exports * * @throws {Error} */ const throwTestError = () => { throw new Error('This function is only exportable when NODE_ENV === test for unit test purposes'); }; exports.throwTestError = throwTestError; /** * Generate a [40 character] random string * * @param {number} numBytes - number of bytes to use in creating a random string * defaults to 20 to produce a 40 character string * @returns {string} - a random string */ const randomString = (numBytes = 20) => crypto_1.default.randomBytes(numBytes).toString('hex'); exports.randomString = randomString; /** * Postpend a [10-character] random string to input identifier. * * @param {string} id - identifer to return * @param {number} numBytes - number of bytes to use to compute random * extension. Default 5 to produce 10 characters.. * @returns {string} - a random string */ const randomId = (id, numBytes = 5) => `${id}${exports.randomString(numBytes)}`; exports.randomId = randomId; /** * Generate a random for the given scale. * * Defaults to a number between 1 and 10. * * @param {number} scale - scale for the random number. Defaults to 10. * @returns {number} - a random number */ const randomNumber = (scale = 10) => Math.ceil(Math.random() * scale); exports.randomNumber = randomNumber; /** * Create a random granule id from the regular expression * * @param {string} regex - regular expression string * @returns {string} - random granule id */ const randomStringFromRegex = (regex) => new randexp_1.default(regex).gen(); exports.randomStringFromRegex = randomStringFromRegex; /** * Validate an object using json-schema * * Issues a test failure if there were validation errors * * @param {Object} t - an ava test * @param {string} schemaFilename - the filename of the schema * @param {Object} data - the object to be validated * @returns {Promise<undefined>} */ async function validateJSON(t, schemaFilename, data) { const schemaName = path_1.default.basename(schemaFilename).split('.')[0]; const schema = await fs_extra_1.default.readFile(schemaFilename, 'utf8').then(JSON.parse); const ajv = new ajv_1.default(); const valid = ajv.validate(schema, data); if (!valid) { const message = `${schemaName} validation failed: ${ajv.errorsText()}`; console.log(message); console.log(JSON.stringify(data, undefined, 2)); t.fail(message); throw new Error(message); } } /** * Validate a task input object using json-schema * * Issues a test failure if there were validation errors * * @param {Object} t - an ava test * @param {Object} data - the object to be validated * @returns {Promise<undefined>} */ async function validateInput(t, data) { await validateJSON(t, './schemas/input.json', data); } exports.validateInput = validateInput; /** * Validate a task config object using json-schema * * Issues a test failure if there were validation errors * * @param {Object} t - an ava test * @param {Object} data - the object to be validated * @returns {Promise<undefined>} */ async function validateConfig(t, data) { await validateJSON(t, './schemas/config.json', data); } exports.validateConfig = validateConfig; /** * Validate a task output object using json-schema * * Issues a test failure if there were validation errors * * @param {Object} t - an ava test * @param {Object} data - the object to be validated * @returns {Promise<undefined>} */ async function validateOutput(t, data) { await validateJSON(t, './schemas/output.json', data); } exports.validateOutput = validateOutput; /** * Determine the path of the current git repo * * @param {string} dirname - the directory that you're trying to find the git * root for * @returns {Promise.<string>} - the filesystem path of the current git repo */ async function findGitRepoRootDirectory(dirname) { if (await fs_extra_1.default.pathExists(path_1.default.join(dirname, '.git'))) return dirname; // This indicates that we've reached the root of the filesystem if (path_1.default.dirname(dirname) === dirname) { throw new Error('Unable to determine git repo root directory'); } return findGitRepoRootDirectory(path_1.default.dirname(dirname)); } exports.findGitRepoRootDirectory = findGitRepoRootDirectory; /** * Determine the path of the packages/test-data directory * * @returns {Promise.<string>} - the filesystem path of the packages/test-data * directory */ function findTestDataDirectory() { return findGitRepoRootDirectory(process.cwd()) .then((gitRepoRoot) => path_1.default.join(gitRepoRoot, 'packages', 'test-data')); } exports.findTestDataDirectory = findTestDataDirectory; /** * Prettify and display something to the console. * * This is only intended to be used during debugging. * * @param {Object|Array} object - an object or array to be stringifyed * @returns {undefined} - no return value */ function jlog(object) { console.log(JSON.stringify(object, undefined, 2)); } exports.jlog = jlog; //# sourceMappingURL=test-utils.js.map