@zohodesk/testinglibrary 0.0.49-n20-experimental → 0.0.50-n20-experimental
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -28,7 +28,7 @@ export async function generateAndCacheTestData(executionContext, type, identifie
|
|
|
28
28
|
|
|
29
29
|
const { response, generators } = await dataGenerator.generate(testInfo, actorInfo, type, identifier, scenarioName, dataTable ? dataTable.hashes() : []);
|
|
30
30
|
if (cacheLayer._trackForCleanup) {
|
|
31
|
-
cacheLayer._trackForCleanup(entityName, response.data, generators, actorInfo);
|
|
31
|
+
cacheLayer._trackForCleanup(entityName, response.data, generators, actorInfo, response.logs, identifier);
|
|
32
32
|
} else {
|
|
33
33
|
cacheLayer.set(entityName, response.data);
|
|
34
34
|
}
|
|
@@ -10,17 +10,22 @@ var _fs = _interopRequireDefault(require("fs"));
|
|
|
10
10
|
var _logger = require("../../../utils/logger");
|
|
11
11
|
var _DataGeneratorHelper = require("../../dataGenerator/DataGeneratorHelper");
|
|
12
12
|
var _readConfigFile = require("../readConfigFile");
|
|
13
|
-
var _configConstants = _interopRequireDefault(require("../constants/configConstants"));
|
|
14
|
-
var _ConfigurationHelper = require("../configuration/ConfigurationHelper");
|
|
15
13
|
var _jsonpath = _interopRequireDefault(require("jsonpath"));
|
|
16
14
|
let cleanupRegistry = null;
|
|
15
|
+
function getModulesRoot() {
|
|
16
|
+
const configConstants = require('../constants/configConstants');
|
|
17
|
+
const {
|
|
18
|
+
getRunStage
|
|
19
|
+
} = require('../configuration/ConfigurationHelper');
|
|
20
|
+
const stage = getRunStage();
|
|
21
|
+
return _path.default.join(process.cwd(), configConstants.TEST_SLICE_FOLDER, stage, 'modules');
|
|
22
|
+
}
|
|
17
23
|
function buildCleanupRegistry() {
|
|
18
|
-
const
|
|
19
|
-
const modulesRoot = _path.default.join(process.cwd(), _configConstants.default.TEST_SLICE_FOLDER, stage, 'modules');
|
|
24
|
+
const modulesRoot = getModulesRoot();
|
|
20
25
|
const registry = {};
|
|
21
26
|
if (!_fs.default.existsSync(modulesRoot)) return registry;
|
|
22
27
|
scanDir(modulesRoot, registry);
|
|
23
|
-
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup registry built: ${Object.keys(registry).length}
|
|
28
|
+
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup registry built: ${Object.keys(registry).length} generator chains from ${modulesRoot}`);
|
|
24
29
|
return registry;
|
|
25
30
|
}
|
|
26
31
|
function scanDir(dir, registry) {
|
|
@@ -31,28 +36,37 @@ function scanDir(dir, registry) {
|
|
|
31
36
|
const fullPath = _path.default.join(dir, entry.name);
|
|
32
37
|
if (entry.isDirectory()) {
|
|
33
38
|
scanDir(fullPath, registry);
|
|
34
|
-
} else if (entry.name.endsWith('.cleanup.
|
|
39
|
+
} else if (entry.name.endsWith('.cleanup.js')) {
|
|
35
40
|
try {
|
|
36
|
-
const
|
|
37
|
-
for (const [
|
|
38
|
-
if (registry[
|
|
39
|
-
throw new Error(`Duplicate cleanup
|
|
41
|
+
const cleanupModule = require(fullPath);
|
|
42
|
+
for (const [generatorName, chain] of Object.entries(cleanupModule)) {
|
|
43
|
+
if (registry[generatorName]) {
|
|
44
|
+
throw new Error(`Duplicate cleanup chain for generator "${generatorName}" in ${fullPath}. ` + `Each generator must have exactly one cleanup chain.`);
|
|
40
45
|
}
|
|
41
|
-
registry[
|
|
46
|
+
registry[generatorName] = chain;
|
|
42
47
|
}
|
|
43
48
|
} catch (err) {
|
|
44
|
-
|
|
49
|
+
if (err.message.includes('Duplicate cleanup chain')) {
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
_logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Failed to load cleanup file: ${fullPath} - ${err.message}`);
|
|
45
53
|
}
|
|
46
54
|
}
|
|
47
55
|
}
|
|
48
56
|
}
|
|
49
|
-
function extractId(
|
|
50
|
-
const result = _jsonpath.default.query(
|
|
57
|
+
function extractId(data, idPath) {
|
|
58
|
+
const result = _jsonpath.default.query(data, idPath);
|
|
51
59
|
if (result.length === 0) {
|
|
52
60
|
throw new Error(`Could not extract ID using path "${idPath}"`);
|
|
53
61
|
}
|
|
54
62
|
return result[0];
|
|
55
63
|
}
|
|
64
|
+
function parseLogBody(stepLog) {
|
|
65
|
+
var _stepLog$response;
|
|
66
|
+
if (!(stepLog !== null && stepLog !== void 0 && (_stepLog$response = stepLog.response) !== null && _stepLog$response !== void 0 && _stepLog$response.body)) return null;
|
|
67
|
+
const body = stepLog.response.body;
|
|
68
|
+
return typeof body === 'string' ? JSON.parse(body) : body;
|
|
69
|
+
}
|
|
56
70
|
async function cleanupViaOAS(config, entityId, actorInfo) {
|
|
57
71
|
const dataGeneratorObj = actorInfo['data-generator'];
|
|
58
72
|
if (!dataGeneratorObj) {
|
|
@@ -103,13 +117,15 @@ var _default = exports.default = {
|
|
|
103
117
|
cacheLayer: async ({}, use) => {
|
|
104
118
|
const cache = new Map();
|
|
105
119
|
const cleanupEntries = [];
|
|
106
|
-
cache._trackForCleanup = (entityName, data, generators, actorInfo) => {
|
|
120
|
+
cache._trackForCleanup = (entityName, data, generators, actorInfo, logs, generatorName) => {
|
|
107
121
|
cache.set(entityName, data);
|
|
108
122
|
cleanupEntries.push({
|
|
109
123
|
entityName,
|
|
110
124
|
data,
|
|
111
125
|
generators,
|
|
112
|
-
actorInfo
|
|
126
|
+
actorInfo,
|
|
127
|
+
logs: logs || [],
|
|
128
|
+
generatorName
|
|
113
129
|
});
|
|
114
130
|
};
|
|
115
131
|
await use(cache);
|
|
@@ -130,32 +146,36 @@ var _default = exports.default = {
|
|
|
130
146
|
let skipped = 0;
|
|
131
147
|
let failed = 0;
|
|
132
148
|
for (const entry of [...cleanupEntries].reverse()) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
149
|
+
const cleanupChain = cleanupRegistry[entry.generatorName];
|
|
150
|
+
if (!cleanupChain) {
|
|
151
|
+
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup: no chain for generator "${entry.generatorName}" — skipping`);
|
|
152
|
+
skipped++;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup entity: "${entry.entityName}" generator: "${entry.generatorName}" (${cleanupChain.length} steps)`);
|
|
156
|
+
for (const cleanupStep of cleanupChain) {
|
|
141
157
|
try {
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
158
|
+
// Find the step's response from logs by matching operationId
|
|
159
|
+
const stepLog = entry.logs.find(log => log.generationOperationId === cleanupStep.operationId || log.name === cleanupStep.operationId);
|
|
160
|
+
const stepData = parseLogBody(stepLog) || entry.data;
|
|
161
|
+
const entityId = extractId(stepData, cleanupStep.idPath);
|
|
162
|
+
const actionDesc = cleanupStep.operationId || `${cleanupStep.method} ${cleanupStep.apiPath}`;
|
|
163
|
+
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup [${cleanupStep.type}] ${actionDesc} (id: ${entityId})`);
|
|
164
|
+
if (cleanupStep.type === 'oas') {
|
|
165
|
+
await cleanupViaOAS(cleanupStep, entityId, entry.actorInfo);
|
|
166
|
+
} else if (cleanupStep.type === 'api') {
|
|
167
|
+
await cleanupViaAPI(cleanupStep, entityId);
|
|
149
168
|
}
|
|
150
169
|
cleaned++;
|
|
151
|
-
_logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Cleanup success: ${
|
|
170
|
+
_logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Cleanup success: ${actionDesc} (id: ${entityId})`);
|
|
152
171
|
} catch (err) {
|
|
153
172
|
failed++;
|
|
154
|
-
|
|
173
|
+
const actionDesc = cleanupStep.operationId || `${cleanupStep.method} ${cleanupStep.apiPath}`;
|
|
174
|
+
_logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Cleanup failed: ${actionDesc} — ${err.message}`);
|
|
155
175
|
}
|
|
156
176
|
}
|
|
157
177
|
}
|
|
158
|
-
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup completed: ${cleaned} cleaned, ${skipped} skipped (no
|
|
178
|
+
_logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup completed: ${cleaned} cleaned, ${skipped} skipped (no chain), ${failed} failed`);
|
|
159
179
|
cleanupEntries.length = 0;
|
|
160
180
|
cache.clear();
|
|
161
181
|
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zohodesk/testinglibrary",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.50-n20-experimental",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@zohodesk/testinglibrary",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.50-n20-experimental",
|
|
10
10
|
"hasInstallScript": true,
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|