From 9db4046c0bd03c8393364daa270629802b6a859c Mon Sep 17 00:00:00 2001 From: tianxchen-ms <403562789@qq.com> Date: Tue, 23 Aug 2022 15:11:06 +0800 Subject: [PATCH] use newman summary in memory (#857) Co-authored-by: Tianxiang Chen --- lib/apiScenario/newmanReportParser.ts | 17 ++++-------- lib/apiScenario/newmanReportValidator.ts | 1 - lib/apiScenario/postmanCollectionGenerator.ts | 26 ++++++++++++------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/apiScenario/newmanReportParser.ts b/lib/apiScenario/newmanReportParser.ts index 2dbe51f8..91fe044a 100644 --- a/lib/apiScenario/newmanReportParser.ts +++ b/lib/apiScenario/newmanReportParser.ts @@ -1,11 +1,4 @@ -import { - RequestDefinition, - ResponseDefinition, - ItemDefinition, - Request, - Response, - DescriptionDefinition, -} from "postman-collection"; +import { ItemDefinition, Request, Response, DescriptionDefinition } from "postman-collection"; import { NewmanAssertion, NewmanExecution, @@ -32,8 +25,8 @@ interface Assertion { interface RawNewmanExecution { id: string; item: ItemDefinition; - request: RequestDefinition; - response: ResponseDefinition; + request: Request; + response: Response; assertions?: Assertion[]; } @@ -48,8 +41,8 @@ export function parseNewmanReport(newmanReport: RawNewmanReport): NewmanReport { } function generateExampleItem(it: RawNewmanExecution): NewmanExecution { - const resp = new Response(it.response); - const req = new Request(it.request); + const resp = it.response; + const req = it.request; const rawReq = parseRequest(req); const rawResp = parseResponse(resp); const annotation = JSON.parse((it.item.description as DescriptionDefinition)?.content || "{}"); diff --git a/lib/apiScenario/newmanReportValidator.ts b/lib/apiScenario/newmanReportValidator.ts index 06c19923..c0f42647 100644 --- a/lib/apiScenario/newmanReportValidator.ts +++ b/lib/apiScenario/newmanReportValidator.ts @@ -314,7 +314,6 @@ export class NewmanReportValidator { try { return JSON.parse(body); } catch (e) { - console.error(`Failed to parse body: ${body}`); return body ? body : undefined; } } diff --git a/lib/apiScenario/postmanCollectionGenerator.ts b/lib/apiScenario/postmanCollectionGenerator.ts index 2ff64b81..56c33cc9 100644 --- a/lib/apiScenario/postmanCollectionGenerator.ts +++ b/lib/apiScenario/postmanCollectionGenerator.ts @@ -26,7 +26,7 @@ import { } from "./newmanReportValidator"; import { SwaggerAnalyzer, SwaggerAnalyzerOption } from "./swaggerAnalyzer"; import { EnvironmentVariables, VariableEnv } from "./variableEnv"; -import { parseNewmanReport, RawNewmanReport } from "./newmanReportParser"; +import { parseNewmanReport } from "./newmanReportParser"; import { defaultCollectionFileName, defaultEnvFileName, @@ -399,8 +399,7 @@ export class PostmanCollectionGenerator { { collection: collection, environment: runtimeEnv, - reporters: ["cli", "json"], - reporter: { json: { export: reportExportPath } }, + reporters: ["cli"], }, function (err, summary) { if (summary.run.failures.length > 0) { @@ -413,7 +412,13 @@ export class PostmanCollectionGenerator { } ) .on("done", async (_err, _summary) => { - await this.postRun(scenario, reportExportPath, runtimeEnv); + const summary = { + environment: { + values: _summary.environment.values.members, + }, + run: _summary.run, + }; + await this.postRun(scenario, reportExportPath, runtimeEnv, summary); resolve(_summary); }); }); @@ -421,7 +426,12 @@ export class PostmanCollectionGenerator { await newmanRun(); } - private async postRun(scenario: Scenario, reportExportPath: string, runtimeEnv: VariableScope) { + private async postRun( + scenario: Scenario, + reportExportPath: string, + runtimeEnv: VariableScope, + summary: any + ) { const keys = await this.swaggerAnalyzer.getAllSecretKey(); const values: string[] = []; for (const [k, v] of Object.entries(runtimeEnv.syncVariablesTo())) { @@ -431,17 +441,15 @@ export class PostmanCollectionGenerator { } this.dataMasker.addMaskedValues(values); this.dataMasker.addMaskedKeys(keys); - // read content and upload. mask newman report. - const rawReport = JSON.parse(await this.fileLoader.load(reportExportPath)) as RawNewmanReport; // add mask environment secret value - for (const item of rawReport.environment.values) { + for (const item of summary.environment.values) { if (this.dataMasker.maybeSecretKey(item.key)) { this.dataMasker.addMaskedValues([item.value]); } } - const newmanReport = parseNewmanReport(rawReport); + const newmanReport = parseNewmanReport(summary); const newmanReportValidatorOption: NewmanReportValidatorOption = { apiScenarioFilePath: scenario._scenarioDef._filePath,