Skip to content

Commit e15cb42

Browse files
author
cod1k
committed
Add cleanup mechanism for temporary directories in e2e tests
Introduce a global cleanup function to ensure temporary directories created during e2e tests are removed. This change prevents orphaned directories by handling cleanup on both process exit and individual test completions.
1 parent 3b6b20b commit e15cb42

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

dev-packages/e2e-tests/run.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ async function run(): Promise<void> {
4141
// Load environment variables from .env file locally
4242
dotenv.config();
4343

44+
const tempDirs = new Set<string>();
45+
const cleanup = async (): Promise<void> => {
46+
if (tempDirs.size > 0) {
47+
for (const dir of tempDirs) {
48+
await rm(dir, { recursive: true });
49+
}
50+
}
51+
}
52+
process.on('exit', cleanup);
53+
4454
// Allow to run a single app only via `yarn test:run <app-name>`
4555
const appName = process.argv[2] || '';
4656

@@ -78,22 +88,28 @@ async function run(): Promise<void> {
7888
for (const testAppPath of testAppPaths) {
7989
const originalPath = resolve('test-applications', testAppPath);
8090
const tmpDirPath = await mkdtemp(join(tmpdir(), `sentry-e2e-tests-${appName}-`));
91+
tempDirs.add(tmpDirPath);
8192

82-
await copyToTemp(originalPath, tmpDirPath);
83-
const cwd = tmpDirPath;
84-
85-
console.log(`Building ${testAppPath} in ${tmpDirPath}...`);
86-
await asyncExec('pnpm test:build', { env, cwd });
93+
try {
94+
await copyToTemp(originalPath, tmpDirPath);
95+
const cwd = tmpDirPath;
8796

88-
console.log(`Testing ${testAppPath}...`);
89-
await asyncExec('pnpm test:assert', { env, cwd });
97+
console.log(`Building ${testAppPath} in ${tmpDirPath}...`);
98+
await asyncExec('pnpm test:build', { env, cwd });
9099

91-
// clean up (although this is tmp, still nice to do)
92-
await rm(tmpDirPath, { recursive: true });
100+
console.log(`Testing ${testAppPath}...`);
101+
await asyncExec('pnpm test:assert', { env, cwd });
102+
} finally {
103+
// clean up (although this is tmp, still nice to do)
104+
await rm(tmpDirPath, { recursive: true });
105+
tempDirs.delete(tmpDirPath);
106+
}
93107
}
94108
} catch (error) {
95109
console.error(error);
96110
process.exit(1);
111+
} finally {
112+
await cleanup();
97113
}
98114
}
99115

0 commit comments

Comments
 (0)