Skip to content

Commit

Permalink
chore: migrate to test server post-review (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 25, 2024
1 parent de8523b commit 7b8ade1
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/listTests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type ProjectConfigWithFiles = {

export type ConfigListFilesReport = {
projects: ProjectConfigWithFiles[];
cliEntryPoint?: string;
error?: TestError;
};

Expand Down
44 changes: 35 additions & 9 deletions src/playwrightTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export class PlaywrightTest {
result = await this._listFilesServer(config);
else
result = await this._listFilesCLI(config);
// TODO: merge getPlaywrightInfo and listFiles to avoid this.
// Override the cli entry point with the one obtained from the config.
if (result.cliEntryPoint)
config.cli = result.cliEntryPoint;
for (const project of result.projects)
project.files = project.files.map(f => this._vscode.Uri.file(f).fsPath);
if (result.error?.location)
Expand Down Expand Up @@ -116,7 +112,39 @@ export class PlaywrightTest {
const testServer = await this._options.testServerController.testServerFor(config);
if (!testServer)
throw new Error('Internal error: unable to connect to the test server');
return await testServer.listFiles({});

const result: ConfigListFilesReport = {
projects: [],
};

// TODO: remove ConfigListFilesReport and report suite directly once CLI is deprecated.
const { report } = await testServer.listFiles({});
const teleReceiver = new TeleReporterReceiver({
onBegin(rootSuite) {
for (const projectSuite of rootSuite.suites) {
const project = projectSuite.project()!;
const files: string[] = [];
result.projects.push({
name: project.name,
testDir: project.testDir,
use: project.use || {},
files,
});
for (const fileSuite of projectSuite.suites)
files.push(fileSuite.location!.file);
}
},
onError(error) {
result.error = error;
},
}, {
mergeProjects: true,
mergeTestCases: true,
resolvePath: (rootDir: string, relativePath: string) => this._vscode.Uri.file(path.join(rootDir, relativePath)).fsPath,
});
for (const message of report)
teleReceiver.dispatch(message);
return result;
}

async runTests(config: TestConfig, projectNames: string[], locations: string[] | null, listener: reporterTypes.ReporterV2, parametrizedTestTitle: string | undefined, token: vscodeTypes.CancellationToken) {
Expand Down Expand Up @@ -255,8 +283,7 @@ export class PlaywrightTest {
return;
if (!testServer)
return;
const oopReporter = require.resolve('./oopReporter');
const { report } = await testServer.listTests({ locations, serializer: oopReporter });
const { report } = await testServer.listTests({ locations });
const teleReceiver = new TeleReporterReceiver(reporter, {
mergeProjects: true,
mergeTestCases: true,
Expand All @@ -272,8 +299,7 @@ export class PlaywrightTest {
return;
if (!testServer)
return;
const oopReporter = require.resolve('./oopReporter');
testServer.runTests({ locations, serializer: oopReporter, ...options });
testServer.runTests({ locations, ...options });
token.onCancellationRequested(() => {
testServer.stopTestsNoReply({});
});
Expand Down
1 change: 1 addition & 0 deletions src/testServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class TestServerController implements vscodeTypes.Disposable {
return null;
const testServer = new TestServerConnection(wsEndpoint);
await testServer.connect();
await testServer.setSerializer({ serializer: require.resolve('./oopReporter') });
return testServer;
}

Expand Down
8 changes: 8 additions & 0 deletions src/upstream/testServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._onLoadTraceRequestedEmitter.fire(params);
}

async setSerializer(params: { serializer: string; }): Promise<void> {
await this._sendMessage('setSerializer', params);
}

async ping(params: Parameters<TestServerInterface['ping']>[0]): ReturnType<TestServerInterface['ping']> {
await this._sendMessage('ping');
}
Expand All @@ -128,6 +132,10 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._sendMessageNoReply('watch', params);
}

async watchTestDir(params: Parameters<TestServerInterface['watchTestDir']>[0]): ReturnType<TestServerInterface['watchTestDir']> {
await this._sendMessage('watchTestDir', params);
}

async open(params: Parameters<TestServerInterface['open']>[0]): ReturnType<TestServerInterface['open']> {
await this._sendMessage('open', params);
}
Expand Down
42 changes: 25 additions & 17 deletions src/upstream/testServerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@

import type * as reporterTypes from './reporter';
import type { Event } from '../vscodeTypes';
import { JsonEvent } from './teleReceiver';

export type ReportEntry = JsonEvent;

export interface TestServerInterface {
setSerializer(params: { serializer: string }): Promise<void>;

ping(params: {}): Promise<void>;

watch(params: {
fileNames: string[];
}): Promise<void>;

watchTestDir(params: {}): Promise<void>;

open(params: { location: reporterTypes.Location }): Promise<void>;

resizeTerminal(params: { cols: number, rows: number }): Promise<void>;
Expand All @@ -32,34 +39,35 @@ export interface TestServerInterface {

installBrowsers(params: {}): Promise<void>;

runGlobalSetup(params: {}): Promise<reporterTypes.FullResult['status']>;
runGlobalSetup(params: {}): Promise<{
report: ReportEntry[],
status: reporterTypes.FullResult['status']
}>;

runGlobalTeardown(params: {}): Promise<reporterTypes.FullResult['status']>;
runGlobalTeardown(params: {}): Promise<{
report: ReportEntry[],
status: reporterTypes.FullResult['status']
}>;

listFiles(params: {
projects?: string[];
}): Promise<{
projects: {
name: string;
testDir: string;
use: { testIdAttribute?: string };
files: string[];
}[];
cliEntryPoint?: string;
error?: reporterTypes.TestError;
report: ReportEntry[];
status: reporterTypes.FullResult['status']
}>;

/**
* Returns list of teleReporter events.
*/
listTests(params: {
serializer?: string;
projects?: string[];
locations?: string[];
}): Promise<{ report: any[] }>;
}): Promise<{
report: ReportEntry[],
status: reporterTypes.FullResult['status']
}>;

runTests(params: {
serializer?: string;
locations?: string[];
grep?: string;
grepInvert?: string;
Expand All @@ -72,7 +80,9 @@ export interface TestServerInterface {
projects?: string[];
reuseContext?: boolean;
connectWsEndpoint?: string;
}): Promise<{ status: reporterTypes.FullResult['status'] }>;
}): Promise<{
status: reporterTypes.FullResult['status'];
}>;

findRelatedTestFiles(params: {
files: string[];
Expand All @@ -84,7 +94,6 @@ export interface TestServerInterface {
}

export interface TestServerInterfaceEvents {
onClose: Event<void>;
onReport: Event<any>;
onStdio: Event<{ type: 'stdout' | 'stderr', text?: string, buffer?: string }>;
onListChanged: Event<void>;
Expand All @@ -93,8 +102,7 @@ export interface TestServerInterfaceEvents {
}

export interface TestServerInterfaceEventEmitters {
dispatchEvent(event: 'close', params: {}): void;
dispatchEvent(event: 'report', params: any): void;
dispatchEvent(event: 'report', params: ReportEntry): void;
dispatchEvent(event: 'stdio', params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }): void;
dispatchEvent(event: 'listChanged', params: {}): void;
dispatchEvent(event: 'testFilesChanged', params: { testFiles: string[] }): void;
Expand Down
2 changes: 1 addition & 1 deletion tests/list-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ test('should list parametrized tests', async ({ activate }) => {
- tests
- test.spec.ts
- one [3:0]
- two [3:0]
- three [3:0]
- two [3:0]
`);
});

Expand Down
20 changes: 16 additions & 4 deletions tests/mock/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ class TestItem {

innerToString(indent: string, result: string[]) {
result.push(`${indent}- ${this.statusIcon()} ${this.treeTitle()}`);
for (const id of this.children.map.keys())
this.children.map.get(id)!.innerToString(indent + ' ', result);
const items = [...this.children.map.values()];
items.sort((i1, i2) => itemOrder(i1).localeCompare(itemOrder(i2)));
for (const item of items)
item.innerToString(indent + ' ', result);
}

statusIcon() {
Expand Down Expand Up @@ -262,6 +264,14 @@ class TestItem {
}
}

function itemOrder(item: TestItem) {
let result = '';
if (item.range)
result += item.range.start.line.toString().padStart(5, '0');
result += item.label;
return result;
}

class TestRunProfile {
private _isDefault = true;
readonly didChangeDefault = new EventEmitter<boolean>();
Expand Down Expand Up @@ -414,7 +424,7 @@ export class TestRun {
const indent = ' ';
const result: string[] = [''];
const tests = [...this.entries.keys()];
tests.sort((a, b) => a.label.localeCompare(b.label));
tests.sort((i1, i2) => itemOrder(i1).localeCompare(itemOrder(i2)));
for (const test of tests) {
const entries = this.entries.get(test)!;
result.push(` ${test.flatTitle()}`);
Expand Down Expand Up @@ -478,7 +488,9 @@ export class TestController {

renderTestTree() {
const result: string[] = [''];
for (const item of this.items.map.values())
const items = [...this.items.map.values()];
items.sort((i1, i2) => itemOrder(i1).localeCompare(itemOrder(i2)));
for (const item of items)
item.innerToString(' ', result);
result.push(' ');
return result.join('\n');
Expand Down
4 changes: 2 additions & 2 deletions tests/project-setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ test('should run setup and teardown projects (1)', async ({ activate }) => {
await expect(testController).toHaveTestTree(`
- setup.ts
- ✅ setup [2:0]
- test.ts
- ✅ test [2:0]
- teardown.ts
- ✅ teardown [2:0]
- test.ts
- ✅ test [2:0]
`);

const output = testRun.renderLog({ output: true });
Expand Down
12 changes: 6 additions & 6 deletions tests/run-annotations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ test('should mark test as skipped', async ({ activate }) => {

const testRun = await testController.run();
expect(testRun.renderLog()).toBe(`
tests > test.spec.ts > fails [9:0]
tests > test.spec.ts > pass [2:0]
enqueued
started
passed
tests > test.spec.ts > fixme [6:0]
tests > test.spec.ts > skipped [3:0]
enqueued
started
skipped
tests > test.spec.ts > pass [2:0]
tests > test.spec.ts > fixme [6:0]
enqueued
started
passed
tests > test.spec.ts > skipped [3:0]
skipped
tests > test.spec.ts > fails [9:0]
enqueued
started
skipped
passed
`);

expect(vscode).toHaveExecLog(`
Expand Down
2 changes: 1 addition & 1 deletion tests/source-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ test('should discover new tests', async ({ activate }) => {
await expect(testController).toHaveTestTree(`
- tests
- test.spec.ts
- new [2:0]
- one [3:0]
- two [4:0]
- new [2:0]
`);

expect(vscode).toHaveExecLog(`
Expand Down

0 comments on commit 7b8ade1

Please sign in to comment.