UNPKG

6.01 kBJavaScriptView Raw
1"use strict";
2/* eslint no-console: "off" */
3var __importDefault = (this && this.__importDefault) || function (mod) {
4 return (mod && mod.__esModule) ? mod : { "default": mod };
5};
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.jlog = exports.findTestDataDirectory = exports.findGitRepoRootDirectory = exports.validateOutput = exports.validateConfig = exports.validateInput = exports.randomStringFromRegex = exports.randomNumber = exports.randomId = exports.randomString = exports.throwTestError = exports.inTestMode = void 0;
8const ajv_1 = __importDefault(require("ajv"));
9const crypto_1 = __importDefault(require("crypto"));
10const path_1 = __importDefault(require("path"));
11const randexp_1 = __importDefault(require("randexp"));
12const fs_extra_1 = __importDefault(require("fs-extra"));
13var FileUtils_1 = require("./FileUtils");
14Object.defineProperty(exports, "readJsonFixture", { enumerable: true, get: function () { return FileUtils_1.readJsonFile; } });
15exports.inTestMode = (env = process.env) => env.NODE_ENV === 'test';
16/**
17 * Helper function to throw error for unit test exports
18 * @throws {Error}
19 */
20exports.throwTestError = () => {
21 throw new Error('This function is only exportable when NODE_ENV === test for unit test purposes');
22};
23/**
24 * Generate a [40 character] random string
25 *
26 * @param {number} numBytes - number of bytes to use in creating a random string
27 * defaults to 20 to produce a 40 character string
28 * @returns {string} - a random string
29 */
30exports.randomString = (numBytes = 20) => crypto_1.default.randomBytes(numBytes).toString('hex');
31/**
32 * Postpend a [10-character] random string to input identifier.
33 *
34 * @param {string} id - identifer to return
35 * @param {number} numBytes - number of bytes to use to compute random
36 * extension. Default 5 to produce 10 characters..
37 * @returns {string} - a random string
38 */
39exports.randomId = (id, numBytes = 5) => `${id}${exports.randomString(numBytes)}`;
40/**
41 * Generate a random for the given scale.
42 *
43 * Defaults to a number between 1 and 10.
44 *
45 * @param {number} scale - scale for the random number. Defaults to 10.
46 * @returns {number} - a random number
47 */
48exports.randomNumber = (scale = 10) => Math.ceil(Math.random() * scale);
49/**
50 * Create a random granule id from the regular expression
51 *
52 * @param {string} regex - regular expression string
53 * @returns {string} - random granule id
54 */
55exports.randomStringFromRegex = (regex) => new randexp_1.default(regex).gen();
56/**
57 * Validate an object using json-schema
58 *
59 * Issues a test failure if there were validation errors
60 *
61 * @param {Object} t - an ava test
62 * @param {string} schemaFilename - the filename of the schema
63 * @param {Object} data - the object to be validated
64 * @returns {Promise<undefined>}
65 */
66async function validateJSON(t, schemaFilename, data) {
67 const schemaName = path_1.default.basename(schemaFilename).split('.')[0];
68 const schema = await fs_extra_1.default.readFile(schemaFilename, 'utf8').then(JSON.parse);
69 const ajv = new ajv_1.default();
70 const valid = ajv.validate(schema, data);
71 if (!valid) {
72 const message = `${schemaName} validation failed: ${ajv.errorsText()}`;
73 console.log(message);
74 console.log(JSON.stringify(data, undefined, 2));
75 t.fail(message);
76 throw new Error(message);
77 }
78}
79/**
80 * Validate a task input object using json-schema
81 *
82 * Issues a test failure if there were validation errors
83 *
84 * @param {Object} t - an ava test
85 * @param {Object} data - the object to be validated
86 * @returns {Promise<undefined>}
87 */
88async function validateInput(t, data) {
89 await validateJSON(t, './schemas/input.json', data);
90}
91exports.validateInput = validateInput;
92/**
93 * Validate a task config object using json-schema
94 *
95 * Issues a test failure if there were validation errors
96 *
97 * @param {Object} t - an ava test
98 * @param {Object} data - the object to be validated
99 * @returns {Promise<undefined>}
100 */
101async function validateConfig(t, data) {
102 await validateJSON(t, './schemas/config.json', data);
103}
104exports.validateConfig = validateConfig;
105/**
106 * Validate a task output object using json-schema
107 *
108 * Issues a test failure if there were validation errors
109 *
110 * @param {Object} t - an ava test
111 * @param {Object} data - the object to be validated
112 * @returns {Promise<undefined>}
113 */
114async function validateOutput(t, data) {
115 await validateJSON(t, './schemas/output.json', data);
116}
117exports.validateOutput = validateOutput;
118/**
119 * Determine the path of the current git repo
120 *
121 * @param {string} dirname - the directory that you're trying to find the git
122 * root for
123 * @returns {Promise.<string>} - the filesystem path of the current git repo
124 */
125async function findGitRepoRootDirectory(dirname) {
126 if (await fs_extra_1.default.pathExists(path_1.default.join(dirname, '.git')))
127 return dirname;
128 // This indicates that we've reached the root of the filesystem
129 if (path_1.default.dirname(dirname) === dirname) {
130 throw new Error('Unable to determine git repo root directory');
131 }
132 return findGitRepoRootDirectory(path_1.default.dirname(dirname));
133}
134exports.findGitRepoRootDirectory = findGitRepoRootDirectory;
135/**
136 * Determine the path of the packages/test-data directory
137 *
138 * @returns {Promise.<string>} - the filesystem path of the packages/test-data
139 * directory
140 */
141function findTestDataDirectory() {
142 return findGitRepoRootDirectory(process.cwd())
143 .then((gitRepoRoot) => path_1.default.join(gitRepoRoot, 'packages', 'test-data'));
144}
145exports.findTestDataDirectory = findTestDataDirectory;
146/**
147 * Prettify and display something to the console.
148 *
149 * This is only intended to be used during debugging.
150 *
151 * @param {Object|Array} object - an object or array to be stringifyed
152 * @returns {undefined} - no return value
153 */
154function jlog(object) {
155 console.log(JSON.stringify(object, undefined, 2));
156}
157exports.jlog = jlog;
158//# sourceMappingURL=test-utils.js.map
\No newline at end of file