Skip to content

Commit 78ca28f

Browse files
author
John Doe
committed
refactor: fix file names 3
1 parent f8c65ae commit 78ca28f

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

packages/plugin-eslint/src/lib/meta/rules.int.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { cp } from 'node:fs/promises';
12
import path from 'node:path';
23
import { fileURLToPath } from 'node:url';
34
import type { MockInstance } from 'vitest';
45
import {
56
NX_IGNORED_FILES_TO_RESTORE,
67
restoreRenamedFiles,
8+
teardownTestFolder,
79
} from '@code-pushup/test-utils';
810
import type { ESLintTarget } from '../config.js';
911
import type { RuleData } from './parse.js';
@@ -93,15 +95,30 @@ describe('listRules', () => {
9395
});
9496

9597
describe('Nx monorepo project', () => {
96-
const nxRootDir = path.join(fixturesDir, 'nx-monorepo');
97-
const eslintrc = path.join(nxRootDir, 'packages/utils/eslint.config.js');
98+
const fixtureNxMonorepoDir = path.join(fixturesDir, 'nx-monorepo');
99+
const testNxMonorepoDir = path.join(
100+
process.cwd(),
101+
'tmp',
102+
'plugin-eslint',
103+
'rules-test',
104+
'nx-monorepo',
105+
);
106+
const eslintrc = path.join(
107+
testNxMonorepoDir,
108+
'packages/utils/eslint.config.js',
109+
);
98110

99111
const patterns = ['packages/utils/**/*.ts', 'packages/utils/**/*.json'];
100112
const targets: ESLintTarget[] = [{ eslintrc, patterns }];
101113

102114
beforeAll(async () => {
103-
await restoreRenamedFiles(nxRootDir, NX_IGNORED_FILES_TO_RESTORE);
104-
cwdSpy.mockReturnValue(nxRootDir);
115+
await cp(fixtureNxMonorepoDir, testNxMonorepoDir, { recursive: true });
116+
await restoreRenamedFiles(testNxMonorepoDir, NX_IGNORED_FILES_TO_RESTORE);
117+
cwdSpy.mockReturnValue(testNxMonorepoDir);
118+
});
119+
120+
afterAll(async () => {
121+
await teardownTestFolder(testNxMonorepoDir);
105122
});
106123

107124
it('should list expected number of rules', async () => {

testing/test-utils/src/lib/utils/test-folder-setup.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export const NX_IGNORED_FILES_TO_RESTORE = [
4848
'_project.json',
4949
] as const;
5050

51+
/**
52+
* File names that need to be prefixed with "_" to avoid Nx detection.
53+
*/
54+
export const NX_IGNORED_FILES_TO_PREFIX = [
55+
'package.json',
56+
'nx.json',
57+
'project.json',
58+
] as const;
59+
5160
/**
5261
* Recursively renames specific files by removing the "_" prefix.
5362
* This is needed because mock fixtures have "_" prefix to avoid Nx detection,
@@ -80,3 +89,36 @@ export async function restoreRenamedFiles(
8089
// Ignore errors if directory doesn't exist
8190
}
8291
}
92+
93+
/**
94+
* Recursively renames specific files by adding the "_" prefix.
95+
* This is needed to restore files back to their prefixed state after tests,
96+
* so they are excluded from Nx detection.
97+
*
98+
* @param dir - Directory to process recursively
99+
* @param fileNames - Array of file names to prefix (e.g., ['package.json', 'nx.json', 'project.json'])
100+
*/
101+
export async function prefixRenamedFiles(
102+
dir: string,
103+
fileNames: readonly string[],
104+
): Promise<void> {
105+
try {
106+
const entries = await readdir(dir, { withFileTypes: true });
107+
for (const entry of entries) {
108+
const fullPath = path.join(dir, entry.name);
109+
if (entry.isDirectory()) {
110+
await prefixRenamedFiles(fullPath, fileNames);
111+
} else if (entry.isFile() && fileNames.includes(entry.name)) {
112+
const newName = `_${entry.name}`;
113+
const newPath = path.join(dir, newName);
114+
try {
115+
await rename(fullPath, newPath);
116+
} catch (error) {
117+
// Ignore errors if file doesn't exist or can't be renamed
118+
}
119+
}
120+
}
121+
} catch (error) {
122+
// Ignore errors if directory doesn't exist
123+
}
124+
}

0 commit comments

Comments
 (0)