-
Notifications
You must be signed in to change notification settings - Fork 6
/
global.setup.ts
69 lines (60 loc) · 1.98 KB
/
global.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { readdirSync, rm } from 'fs-extra';
import { homedir } from 'os';
import { join } from 'path';
import { MULTI_PREFIX, NODE_ENV } from './tests/automation/setup/open';
import { isLinux, isMacOS } from './tests/os_utils';
import { isEmpty } from 'lodash';
const getDirectoriesOfSessionDataPath = (source: string) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => {
return dirent.name;
})
.filter((n) => n.includes(`${NODE_ENV}-${MULTI_PREFIX}`));
let needsClean = isEmpty(process.env.NO_CLEAN);
export default async function globalSetup() {
console.log('Cleaning up all previous tests configs...');
if (!needsClean) {
return;
}
needsClean = false;
if (process.env.CI) {
console.info(
'We are on CI, no need to clean up other tests (so we can run them in parallel)',
);
return;
}
try {
const parentFolderOfAllDataPath = isMacOS()
? join(homedir(), 'Library', 'Application Support')
: isLinux()
? join(homedir(), '.config')
: null;
if (!parentFolderOfAllDataPath) {
throw new Error('Only macOS/linux are currrently supported ');
}
if (!parentFolderOfAllDataPath || parentFolderOfAllDataPath.length < 9) {
throw new Error(
`parentFolderOfAllDataPath not found or invalid: ${parentFolderOfAllDataPath}`,
);
}
console.info(
'cleaning other tests leftovers...',
parentFolderOfAllDataPath,
);
const allAppDataPath = getDirectoriesOfSessionDataPath(
parentFolderOfAllDataPath,
);
console.info('allAppDataPath to clean', allAppDataPath);
await Promise.all(
allAppDataPath.map((folder) => {
const pathToRemove = join(parentFolderOfAllDataPath, folder);
return rm(pathToRemove, { recursive: true });
}),
);
console.info('...done');
} catch (e) {
console.error(`failed to cleanup old files: ${e.message}`);
}
console.log('Cleaning up done.');
}