Skip to content

Commit

Permalink
fix: tolerate git lock conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkx committed Jan 22, 2024
1 parent 9b199d3 commit 7f4ae99
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
19 changes: 17 additions & 2 deletions packages/test-utils/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { IncomingMessage, RequestListener, ServerResponse, createServer } from 'node:http';
import { IncomingMessage, RequestListener, ServerResponse, createServer as createHttpServer } from 'node:http';
import { createServer as createNetServer } from 'node:net';

export function sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms));
}

export async function getRandomPort() {
const server = createNetServer();
server.listen(0);
return new Promise<number>((resolve, reject) => {
server.addListener('listening', () => {
const address = server.address();
if (!address || typeof address === 'string') reject('Invalid address');
else resolve(address.port);
server.close();
});
server.addListener('close', () => reject('Close'));
});
}

export interface CreateStaticServerOptions {
handler?: RequestListener<typeof IncomingMessage, typeof ServerResponse>;
}

export async function createStaticServer({ handler }: CreateStaticServerOptions = {}) {
const serverHandler = handler || ((_req, res) => res.end(`<html></html>`));
const server = createServer(serverHandler);
const server = createHttpServer(serverHandler);
server.listen(0);
return {
server,
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"@modern-js/utils": "^2.46.1",
"@playwright/test": "^1.41.0",
"@types/node": "^20.11.5",
"@webx-kit/chrome-types": "workspace:^",
"@webx-kit/example-react": "workspace:^",
"@webx-kit/example-solid": "workspace:^",
"@webx-kit/example-svelte": "workspace:^",
"@webx-kit/example-vue": "workspace:^",
"@webx-kit/chrome-types": "workspace:^",
"@webx-kit/test-utils": "workspace:^",
"p-retry": "^6.2.0",
"typescript": "^5.3.3"
}
}
13 changes: 9 additions & 4 deletions tests/e2e/tests/webx-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert';
import path from 'node:path';
import { fs, execa, stripAnsi, wait } from '@modern-js/utils';
import { createWebxTest } from '@webx-kit/test-utils/playwright';
import { dynamicImport, fs, execa, stripAnsi, wait } from '@modern-js/utils';
import { createWebxTest, getRandomPort } from '@webx-kit/test-utils/playwright';
import { LaunchOptions } from '@playwright/test';

export function createLaunchOptions(packageName: string, launchOptions?: LaunchOptions) {
Expand Down Expand Up @@ -43,7 +43,8 @@ export function startDev({ beforeAll, afterAll, afterEach }: typeof test) {
baseDir = packageDir;
const { stdout: dirtyFiles } = await execa('git', ['ls-files', '--modified'], { cwd: packageDir });
if (!!dirtyFiles) throw new Error('make sure all modifications have been staged');
childProcess = execa('pnpm', ['dev'], { cwd: packageDir });
const PORT = String(await getRandomPort());
childProcess = execa('pnpm', ['dev'], { cwd: packageDir, env: { PORT } });
await Promise.race([
new Promise<void>((resolve) => {
const handler = (chunk: unknown) => {
Expand All @@ -59,7 +60,11 @@ export function startDev({ beforeAll, afterAll, afterEach }: typeof test) {
wait(20 * 1000).then((): Promise<void> => Promise.reject('Timeout')),
]);
});
afterEach(() => execa('git', ['checkout', '.'], { cwd: baseDir }));
afterEach(async () => {
const { default: pRetry } = (await dynamicImport('p-retry')) as typeof import('p-retry');
// tolerate `.git/index.lock` conflict
await pRetry(() => execa('git', ['checkout', '.'], { cwd: baseDir }), { retries: 3 });
});
afterAll(() => childProcess.kill('SIGINT'));

const resolvePath = (file: string) => path.resolve(baseDir, file);
Expand Down

0 comments on commit 7f4ae99

Please sign in to comment.