You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

45 lines
1.3 KiB

  1. import { rm } from 'node:fs/promises';
  2. import { metadataPath, playwrightArtifactRoot, repoRoot } from './fixtures/cluster';
  3. import { readClusterMetadata } from './fixtures/cluster';
  4. import { spawn } from 'node:child_process';
  5. async function runCommand(command: string, args: string[], cwd: string) {
  6. await new Promise<void>((resolve, reject) => {
  7. const child = spawn(command, args, {
  8. cwd,
  9. stdio: 'ignore',
  10. shell: false,
  11. });
  12. child.on('error', reject);
  13. child.on('close', (code) => {
  14. if (code === 0) {
  15. resolve();
  16. return;
  17. }
  18. reject(new Error(`${command} ${args.join(' ')} failed with code ${code}`));
  19. });
  20. });
  21. }
  22. async function killProcessTree(pid: number) {
  23. if (process.platform === 'win32') {
  24. await runCommand('taskkill', ['/PID', String(pid), '/T', '/F'], repoRoot);
  25. return;
  26. }
  27. process.kill(pid, 'SIGTERM');
  28. }
  29. export default async function globalTeardown() {
  30. try {
  31. const metadata = await readClusterMetadata();
  32. await killProcessTree(metadata.serverPid);
  33. } catch {
  34. // ignore missing metadata or already-exited processes
  35. }
  36. await rm(metadataPath, { force: true });
  37. await rm(playwrightArtifactRoot, { recursive: false, force: false }).catch(() => {
  38. // keep artifacts when directory is not empty
  39. });
  40. }