Skip to content

Commit

Permalink
bug: introducing varStores on a per-request basis
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun0157 committed Jun 26, 2024
1 parent 5c48215 commit 10d1455
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
28 changes: 14 additions & 14 deletions src/getResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { runAllTests } from "zzapi";
import { captureVariables } from "zzapi";
import { replaceVariablesInRequest } from "zzapi";

import { getVarStore } from "./variables";
import {
C_ERR,
C_ERR_TEXT,
Expand All @@ -29,6 +28,7 @@ import { getStatusCode } from "./utils/errors";

import { replaceFileContents } from "./fileContents";
import { SpecResponse } from "./specResponse";
import { RawRequest } from "./utils/requestUtils";

const requestDetailInset = " ".repeat(new Date().toLocaleString().length + 3);

Expand All @@ -40,7 +40,7 @@ function formatRoute(
et: string | number,
size: number,
passedCount: number,
allCount: number,
allCount: number
) {
let line = C_TIME(`${new Date().toLocaleString()}`);
if (allCount === passedCount) {
Expand Down Expand Up @@ -85,7 +85,7 @@ function formatRouteParseError(
status: number | undefined,
size: number,
et: string | number,
parseError: string,
parseError: string
) {
const line =
C_TIME(`${new Date().toLocaleString()}`) +
Expand Down Expand Up @@ -115,7 +115,7 @@ function formatTestResults(
results: TestResult[],
lastResult: TestResult | undefined,
skip: boolean,
indented: boolean,
indented: boolean
): string[] {
const resultLines: string[] = [];
for (const [i, r] of results.entries()) {
Expand All @@ -124,8 +124,8 @@ function formatTestResults(
? "└"
: "├" // flat view
: i === results.length - 1
? "└"
: "├"; // indented/nested view
? "└"
: "├"; // indented/nested view

// Indented presentation displays spec on a dedicated line, otherwise specs are displayed inline.
const specToken = spec && !indented ? `${spec} ` : "";
Expand Down Expand Up @@ -183,7 +183,7 @@ function getFormattedResult(
status: number | undefined,
size: number,
execTime: string | number,
indented: boolean,
indented: boolean
): [string, number, number] {
function getResultData(res: SpecResult): [number, number] {
const rootResults = res.results;
Expand All @@ -206,7 +206,7 @@ function getFormattedResult(
function getIndentedResult(
res: SpecResult,
indent: number,
lastResult: TestResult | undefined,
lastResult: TestResult | undefined
): string {
const offset = " ";
const inset = requestDetailInset + (indented ? offset.repeat(Math.max(1, indent - 1)) : "");
Expand All @@ -217,7 +217,7 @@ function getFormattedResult(
res.results,
lastResult,
res.skipped ?? false,
indented,
indented
).join("\n");

const subRes: string[] = [];
Expand Down Expand Up @@ -280,20 +280,20 @@ export async function allRequestsWithProgress(
allRequests: {
[name: string]: RequestSpec;
},
bundlePath: string,
indented: boolean,
rawReq: RawRequest
): Promise<Array<SpecResponse>> {
let currHttpRequest: GotRequest;
const responses: Array<SpecResponse> = [];

const bundlePath = rawReq.bundle.bundlePath;
const bundleName = bundlePath.substring(bundlePath.lastIndexOf(path.sep) + 1);

for (const name in allRequests) {
const requestData = allRequests[name];
const method = requestData.httpRequest.method;

requestData.httpRequest.body = replaceFileContents(requestData.httpRequest.body, bundlePath);
const undefs = replaceVariablesInRequest(requestData, getVarStore().getAllVariables());
const undefs = replaceVariablesInRequest(requestData, rawReq.variables.getAllVariables());
currHttpRequest = constructGotRequest(requestData);

const reqNameMessage = `Running ${name}`;
Expand Down Expand Up @@ -358,7 +358,7 @@ export async function allRequestsWithProgress(
status,
size,
et,
indented,
rawReq.indent
);
if (passed !== all) process.exitCode = getStatusCode() + 1;

Expand All @@ -368,7 +368,7 @@ export async function allRequestsWithProgress(
const captureOutput = captureVariables(requestData, response);
const capturedVariables = captureOutput.capturedVars;
const capturedErrors = captureOutput.captureErrors;
getVarStore().mergeCapturedVariables(capturedVariables);
rawReq.variables.mergeCapturedVariables(capturedVariables);
if (capturedErrors) {
message = `${message}${requestDetailInset}${C_ERR(`${capturedErrors}`)}`;
}
Expand Down
18 changes: 7 additions & 11 deletions src/runRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { CLI_VERSION } from "./utils/version";

import { showContentForIndReq, showContentForAllReq } from "./showRes";
import { allRequestsWithProgress } from "./getResponse";
import { getVarFileContents, getVarStore } from "./variables";
import { getVarFileContents } from "./variables";
import { BundleResult } from "./bundleResult";

async function runRequestSpecs(
requests: { [name: string]: RequestSpec },
rawRequest: RawRequest,
rawRequest: RawRequest
): Promise<BundleResult> {
for (const name in requests) {
const request = requests[name];
Expand All @@ -27,11 +27,7 @@ async function runRequestSpecs(
request.httpRequest.headers = Object.assign(autoHeaders, request.httpRequest.headers);
}

const responses = await allRequestsWithProgress(
requests,
rawRequest.bundle.bundlePath,
rawRequest.indent,
);
const responses = await allRequestsWithProgress(requests, rawRequest);

if (responses.length < 1) return new BundleResult(rawRequest.bundle.bundlePath);

Expand All @@ -48,7 +44,7 @@ async function runRequestSpecs(
req.options.keepRawJSON,
req.options.showHeaders,
rawRequest.envName,
rawRequest.expand,
rawRequest.expand
);
}

Expand All @@ -64,11 +60,11 @@ export async function callRequests(request: RawRequest): Promise<BundleResult> {
const loadedVariables: Variables = loadVariables(
env,
request.bundle.bundleContents,
getVarFileContents(path.dirname(request.bundle.bundlePath)),
getVarFileContents(path.dirname(request.bundle.bundlePath))
);
if (env && Object.keys(loadedVariables).length < 1)
console.error(C_WARN(`warning: no variables added from env "${env}". Does it exist?`));
getVarStore().setLoadedVariables(loadedVariables);
request.variables.setLoadedVariables(loadedVariables);
} catch (err: any) {
throw err;
}
Expand All @@ -84,7 +80,7 @@ export async function callRequests(request: RawRequest): Promise<BundleResult> {
throw err;
}

// finally, run the request specs
// finally, run the request specs
const t0 = performance.now();
const bundleResult = await runRequestSpecs(allRequests, request);
const t1 = performance.now();
Expand Down
3 changes: 3 additions & 0 deletions src/utils/requestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { OptionValues } from "commander";
import { Bundle } from "./bundleUtils";
import { VarStore } from "zzapi";

export class RawRequest {
public requestName: string | undefined = undefined;
public envName: string | undefined = undefined;
public expand: boolean = false;
public indent: boolean = false;
public bundle: Bundle;
public variables: VarStore;

constructor(relPath: string, opts: OptionValues) {
try {
Expand All @@ -15,6 +17,7 @@ export class RawRequest {
this.envName = opts.env;
this.expand = opts.expand;
this.indent = opts.indent;
this.variables = new VarStore();
} catch (e) {
throw e;
}
Expand Down
5 changes: 0 additions & 5 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,3 @@ export function getVarFileContents(dirPath: string): string[] {
export function getEnvNames(dirPath: string, bundleContent: string): string[] {
return getEnvironments(bundleContent, getVarFileContents(dirPath));
}

let variables = new VarStore();
export function getVarStore(): VarStore {
return variables;
}

0 comments on commit 10d1455

Please sign in to comment.