1 | "use strict";
|
2 |
|
3 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
4 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
5 | };
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.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;
|
8 | const ajv_1 = __importDefault(require("ajv"));
|
9 | const crypto_1 = __importDefault(require("crypto"));
|
10 | const path_1 = __importDefault(require("path"));
|
11 | const randexp_1 = __importDefault(require("randexp"));
|
12 | const fs_extra_1 = __importDefault(require("fs-extra"));
|
13 | var FileUtils_1 = require("./FileUtils");
|
14 | Object.defineProperty(exports, "readJsonFixture", { enumerable: true, get: function () { return FileUtils_1.readJsonFile; } });
|
15 | exports.inTestMode = (env = process.env) => env.NODE_ENV === 'test';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | exports.throwTestError = () => {
|
21 | throw new Error('This function is only exportable when NODE_ENV === test for unit test purposes');
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | exports.randomString = (numBytes = 20) => crypto_1.default.randomBytes(numBytes).toString('hex');
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | exports.randomId = (id, numBytes = 5) => `${id}${exports.randomString(numBytes)}`;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | exports.randomNumber = (scale = 10) => Math.ceil(Math.random() * scale);
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | exports.randomStringFromRegex = (regex) => new randexp_1.default(regex).gen();
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | async 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 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | async function validateInput(t, data) {
|
89 | await validateJSON(t, './schemas/input.json', data);
|
90 | }
|
91 | exports.validateInput = validateInput;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | async function validateConfig(t, data) {
|
102 | await validateJSON(t, './schemas/config.json', data);
|
103 | }
|
104 | exports.validateConfig = validateConfig;
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | async function validateOutput(t, data) {
|
115 | await validateJSON(t, './schemas/output.json', data);
|
116 | }
|
117 | exports.validateOutput = validateOutput;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | async function findGitRepoRootDirectory(dirname) {
|
126 | if (await fs_extra_1.default.pathExists(path_1.default.join(dirname, '.git')))
|
127 | return dirname;
|
128 |
|
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 | }
|
134 | exports.findGitRepoRootDirectory = findGitRepoRootDirectory;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function findTestDataDirectory() {
|
142 | return findGitRepoRootDirectory(process.cwd())
|
143 | .then((gitRepoRoot) => path_1.default.join(gitRepoRoot, 'packages', 'test-data'));
|
144 | }
|
145 | exports.findTestDataDirectory = findTestDataDirectory;
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | function jlog(object) {
|
155 | console.log(JSON.stringify(object, undefined, 2));
|
156 | }
|
157 | exports.jlog = jlog;
|
158 |
|
\ | No newline at end of file |