Skip to content

Commit

Permalink
feat: testRunner supports repeating tests multipe times (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Oct 2, 2023
1 parent c7794b5 commit a888811
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
## [6.7.1]
## [6.7.1] (2023-10-02)

### Fix

- default of setting `testRunAlwaysUseEnv`is null (Anweber/vscode-httpyac#230)
- testRunner uses latest activeEnvironemnt (Anweber/vscode-httpyac#230)
- testRunner supports repeating tests multipe times (Anweber/vscode-httpyac#229)

## [6.7.0] (2023-10-01)
## [6.7.0] (2023-10-01)

### Feature

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"publisher": "anweber",
"description": "Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT, RabbitMQ and WebSocket requests directly within Visual Studio Code",
"version": "6.7.0",
"version": "6.7.1",
"homepage": "https://github.com/AnWeber/vscode-httpyac",
"repository": {
"type": "git",
Expand Down Expand Up @@ -1031,6 +1031,12 @@
"scope": "window",
"description": "use this environment on a test run",
"default": null
},
"httpyac.testRunRepeatTimes": {
"type": "number",
"scope": "window",
"description": "repeat times of test run",
"default": 1
}
}
},
Expand Down Expand Up @@ -1516,7 +1522,7 @@
"dependencies": {
"filesize": "^10.0.12",
"httpsnippet": "^3.0.1",
"httpyac": "^6.7.0",
"httpyac": "^6.7.1",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"uuid": "^9.0.1"
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface AppConfig {
testMaxConcurrency?: number;
testResetEnvBeforeRun?: boolean;
testRunAlwaysUseEnv?: Array<string>;
testRunRepeatTimes?: number;
}

export function getConfigSetting(): AppConfig {
Expand Down
61 changes: 61 additions & 0 deletions src/provider/test/testRunOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as httpyac from 'httpyac';

export function logTestRun(httpRegions: Array<httpyac.ProcessedHttpRegion>) {
const groupedHttpRegions = groupProcessedHttpRegions(httpRegions);

for (const groupedHttpRegion of groupedHttpRegions) {
const messages: Array<string> = [];
for (const httpRegion of groupedHttpRegion) {
if (messages.length === 0) {
messages.push(`${fillWhitespace(httpRegion.symbol.name, 50)}:`);
}

const results: Array<string> = [];
if (httpRegion.response?.statusCode) {
results.push(`${httpRegion.response?.statusCode}`);
}
if (httpRegion.testResults && httpRegion.testResults.some(obj => obj.error)) {
results.push(`${httpRegion.testResults.filter(obj => !!obj.error).length}✖`);
}
if (httpRegion.response?.timings?.total) {
const total = `${httpRegion.response?.timings?.total}`;
results.push(`${fillWhitespace(total, 4)}ms`);
}
messages.push(results.join(' '));
}

httpyac.io.log.info(messages.join(' '));
}
}

function fillWhitespace(text: string, length: number) {
if (text.length >= length) {
return text;
}
const whitespace = [...new Array(length - text.length)].map(() => ' ').join('');
return `${whitespace}${text}`;
}

function groupProcessedHttpRegions(httpRegions: httpyac.ProcessedHttpRegion[]) {
const result: Record<string, Record<string, Array<httpyac.ProcessedHttpRegion>>> = {};
for (const httpRegion of httpRegions) {
const filename = httpyac.io.fileProvider.toString(httpRegion.filename);
const httpRegionKey = `${httpRegion.symbol.name}_${httpRegion.symbol.startLine}`;
const httpRegionMap = getOrCreateKey(result, filename, {});
const values = getOrCreateKey(httpRegionMap, httpRegionKey, []);
values.push(httpRegion);
}

return Object.values(result)
.map(obj => Object.values(obj))
.flat(1);
}

function getOrCreateKey<T>(map: Record<string, T>, key: string, defaultVal: T): T {
const val = map[key];
if (val) {
return val;
}
map[key] = defaultVal;
return defaultVal;
}
17 changes: 13 additions & 4 deletions src/provider/test/testRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { DocumentStore } from '../../documentStore';
import { getConfigSetting } from '../../config';
import { TestItemResolver } from './testItemResolver';
import { StoreController } from '../storeController';
import { logTestRun } from './testRunOutput';

interface TestRunContext {
testRun: vscode.TestRun;
testItems: Array<vscode.TestItem>;
token: vscode.CancellationToken;
processedHttpRegions: Array<httpyac.ProcessedHttpRegion>;
}

export class TestRunner {
Expand All @@ -26,20 +28,26 @@ export class TestRunner {
const testItems: Array<vscode.TestItem> = await this.testItemResolver.resolveTestItemsForRequest(request);

await this.resetEnvironmentIfNeeded();
const processedHttpRegions: Array<httpyac.ProcessedHttpRegion> = [];
const testFuncs = (await this.enqueuedTestItems(testItems, testRun)).map(items => async () => {
for (const item of items) {
if (!token.isCancellationRequested && this.testItemResolver.isHttpRegionTestItem(item)) {
await this.runTestItem(item, {
testRun,
testItems,
token,
processedHttpRegions,
});
} else {
testRun.skipped(item);
}
}
});
await httpyac.utils.promiseQueue(getConfigSetting().testMaxConcurrency || 1, ...testFuncs);
const repeatTimes = getConfigSetting().testRunRepeatTimes || 1;
for (let index = 0; index < repeatTimes; index++) {
await httpyac.utils.promiseQueue(getConfigSetting().testMaxConcurrency || 1, ...testFuncs);
}
logTestRun(processedHttpRegions);
testRun.end();
}

Expand Down Expand Up @@ -89,9 +97,6 @@ export class TestRunner {
sendContext.logResponse = async (response, httpRegion) => {
await tmpLogResponse?.(response, httpRegion);
};
sendContext.variables = {
TEST_RUNNER: true,
};
try {
await this.documentStore.send(sendContext);
const testResults = sendContext.httpRegion?.testResults;
Expand Down Expand Up @@ -147,6 +152,10 @@ export class TestRunner {
activeEnvironment: config.testRunAlwaysUseEnv || this.documentStore.getActiveEnvironment(httpFile),
httpFile,
httpRegion,
variables: {
TEST_RUNNER: true,
},
processedHttpRegions: testRunContext.processedHttpRegions,
};
context.progress = {
divider: 1,
Expand Down

0 comments on commit a888811

Please sign in to comment.