-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest-folder-setup.ts
More file actions
124 lines (117 loc) · 3.65 KB
/
Copy pathtest-folder-setup.ts
File metadata and controls
124 lines (117 loc) · 3.65 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { bold } from 'ansis';
import { mkdir, readdir, rename, rm, stat } from 'node:fs/promises';
import path from 'node:path';
export async function cleanTestFolder(dirName: string) {
await teardownTestFolder(dirName);
await mkdir(dirName, { recursive: true });
}
export async function teardownTestFolder(dirName: string) {
try {
const stats = await stat(dirName);
if (!stats.isDirectory()) {
console.warn(
`⚠️ You are trying to delete a file instead of a directory - ${bold(
dirName,
)}.`,
);
}
} catch {
// continue safely without deleting as folder does not exist in the filesystem
return;
}
try {
await rm(dirName, {
recursive: true,
force: true,
maxRetries: 2,
retryDelay: 100,
});
} catch {
console.warn(
`⚠️ Failed to delete test artefact ${bold(
dirName,
)} so the folder is still in the file system!\nIt may require a deletion before running e2e tests again.`,
);
}
}
/**
* File names that need to be restored by removing the "_" prefix.
* These files are prefixed with "_" in mock fixtures to avoid Nx detection.
*/
export const NX_IGNORED_FILES_TO_RESTORE = [
'_package.json',
'_nx.json',
'_project.json',
] as const;
/**
* File names that need to be prefixed with "_" to avoid Nx detection.
*/
export const NX_IGNORED_FILES_TO_PREFIX = [
'package.json',
'nx.json',
'project.json',
] as const;
/**
* Recursively renames specific files by removing the "_" prefix.
* This is needed because mock fixtures have "_" prefix to avoid Nx detection,
* but tests need the original filenames.
*
* @param dir - Directory to process recursively
* @param fileNames - Array of file names to restore (e.g., ['_package.json', '_nx.json', '_project.json'])
*/
export async function restoreRenamedFiles(
dir: string,
fileNames: readonly string[],
): Promise<void> {
try {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await restoreRenamedFiles(fullPath, fileNames);
} else if (entry.isFile() && fileNames.includes(entry.name)) {
const newName = entry.name.slice(1); // Remove leading "_"
const newPath = path.join(dir, newName);
try {
await rename(fullPath, newPath);
} catch (error) {
// Ignore errors if file doesn't exist or can't be renamed
}
}
}
} catch (error) {
// Ignore errors if directory doesn't exist
}
}
/**
* Recursively renames specific files by adding the "_" prefix.
* This is needed to restore files back to their prefixed state after tests,
* so they are excluded from Nx detection.
*
* @param dir - Directory to process recursively
* @param fileNames - Array of file names to prefix (e.g., ['package.json', 'nx.json', 'project.json'])
*/
export async function prefixRenamedFiles(
dir: string,
fileNames: readonly string[],
): Promise<void> {
try {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await prefixRenamedFiles(fullPath, fileNames);
} else if (entry.isFile() && fileNames.includes(entry.name)) {
const newName = `_${entry.name}`;
const newPath = path.join(dir, newName);
try {
await rename(fullPath, newPath);
} catch (error) {
// Ignore errors if file doesn't exist or can't be renamed
}
}
}
} catch (error) {
// Ignore errors if directory doesn't exist
}
}