Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add killProcessAndPorts function #729

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/internal/testing/src/lib/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function runNxCommandUntil(

const KILL_PORT_DELAY = 5000;

export async function killPort(port: number): Promise<boolean> {
async function killPort(port: number): Promise<boolean> {
if (await portCheck(port)) {
try {
logInfo(`Attempting to close port ${port}`);
Expand Down Expand Up @@ -184,16 +184,24 @@ export function logError(title: string, body?: string) {
return e2eConsoleLogger(message, body);
}

export async function killPorts(port?: number): Promise<boolean> {
return port
? await killPort(port)
: (await killPort(3333)) && (await killPort(4200));
}
const promisifiedTreeKill: (pid: number, signal: string) => Promise<void> =
promisify(treeKill);

export const promisifiedTreeKill: (
pid: number,
signal: string,
) => Promise<void> = promisify(treeKill);
export async function killProcessAndPorts(
pid: number | undefined,
...ports: number[]
): Promise<void> {
try {
if (pid) {
await promisifiedTreeKill(pid, 'SIGKILL');
}
for (const port of ports) {
await killPort(port);
}
} catch (err) {
// ignore err
}
}

export function checkFilesDoNotExist(...expectedFiles: string[]) {
expectedFiles.forEach((f) => {
Expand Down
66 changes: 10 additions & 56 deletions testing-projects/jnxplus-e2e/tests/nx-gradle/micronaut-kt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
checkFilesDoNotExist,
createTestWorkspace,
getData,
killPorts,
promisifiedTreeKill,
killProcessAndPorts,
runNxCommandUntil,
} from '@jnxplus/internal/testing';
import { names } from '@nx/devkit';
Expand Down Expand Up @@ -170,12 +169,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 240000);

it('should build-image of a java application', async () => {
Expand Down Expand Up @@ -270,12 +264,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should generate an app with a simple package name', async () => {
Expand Down Expand Up @@ -357,12 +346,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a kotlin application', async () => {
Expand Down Expand Up @@ -441,12 +425,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 240000);

xit('should build-image of a kotlin application', async () => {
Expand Down Expand Up @@ -541,12 +520,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('directory with dash', async () => {
Expand Down Expand Up @@ -581,12 +555,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a library', async () => {
Expand Down Expand Up @@ -1119,12 +1088,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a library with a simple name', async () => {
Expand Down Expand Up @@ -1222,12 +1186,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
);

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a minimal kotlin application', async () => {
Expand Down Expand Up @@ -1267,12 +1226,7 @@ describe('nx-gradle micronaut kotlin dsl e2e', () => {
);

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should skip starter code when generating a java library with skipStarterCode option', async () => {
Expand Down
66 changes: 10 additions & 56 deletions testing-projects/jnxplus-e2e/tests/nx-gradle/micronaut.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
checkFilesDoNotExist,
createTestWorkspace,
getData,
killPorts,
promisifiedTreeKill,
killProcessAndPorts,
runNxCommandUntil,
} from '@jnxplus/internal/testing';
import { names } from '@nx/devkit';
Expand Down Expand Up @@ -170,12 +169,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should build-image of a java application', async () => {
Expand Down Expand Up @@ -270,12 +264,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should generate an app with a simple package name', async () => {
Expand Down Expand Up @@ -357,12 +346,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a kotlin application', async () => {
Expand Down Expand Up @@ -441,12 +425,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 240000);

xit('should build-image of a kotlin application', async () => {
Expand Down Expand Up @@ -541,12 +520,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('directory with dash', async () => {
Expand Down Expand Up @@ -581,12 +555,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a library', async () => {
Expand Down Expand Up @@ -1119,12 +1088,7 @@ describe('nx-gradle micronaut e2e', () => {
expect(dataResult.message).toMatch('Hello World');

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a library with a simple name', async () => {
Expand Down Expand Up @@ -1222,12 +1186,7 @@ describe('nx-gradle micronaut e2e', () => {
);

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should create a minimal kotlin application', async () => {
Expand Down Expand Up @@ -1267,12 +1226,7 @@ describe('nx-gradle micronaut e2e', () => {
);

// port and process cleanup
try {
await promisifiedTreeKill(process.pid, 'SIGKILL');
await killPorts(port);
} catch (err) {
// ignore err
}
await killProcessAndPorts(process.pid, port);
}, 120000);

it('should skip starter code when generating a java library with skipStarterCode option', async () => {
Expand Down
Loading