Skip to content

Commit

Permalink
test: add killProcessAndPorts function (#729)
Browse files Browse the repository at this point in the history
* test: add killProcessAndPorts function

* build: work in progress

---------

Co-authored-by: khalilou88 <[email protected]>
  • Loading branch information
khalilou88 and khalilou88 authored Jan 3, 2024
1 parent 1626654 commit 28da0f6
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 560 deletions.
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

0 comments on commit 28da0f6

Please sign in to comment.