Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .nxignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# exclude mock Nx monorepos used for testing
mocks
# HACK: exclude mock Nx monorepos used for testing (this makes test working but changed in our mocks are ignored and don't invalidate caching)
# @TODO prefix Nx related files with "_" (not detected by Nx or test) and copy/rename them under folder `tmp` (excluded by Nx, detected by test) see #1102
packages/plugin-eslint/mocks/fixtures/nx-monorepo
4 changes: 4 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/npm-workspaces/_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"workspaces": ["packages/*"]
}
6 changes: 0 additions & 6 deletions e2e/ci-e2e/mocks/fixtures/npm-workspaces/package.json

This file was deleted.

3 changes: 3 additions & 0 deletions e2e/ci-e2e/mocks/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { simpleGit } from 'simple-git';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
initGitRepo,
restoreRenamedFiles,
simulateGitFetch,
teardownTestFolder,
} from '@code-pushup/test-utils';
Expand All @@ -28,6 +30,7 @@ export async function setupTestRepo(folder: string) {
);

await cp(fixturesDir, baseDir, { recursive: true });
await restoreRenamedFiles(baseDir, NX_IGNORED_FILES_TO_RESTORE);
Comment thread
BioPhoton marked this conversation as resolved.
Outdated

const git = await initGitRepo(simpleGit, { baseDir });
await simulateGitFetch(git);
Expand Down
3 changes: 3 additions & 0 deletions e2e/cli-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { afterEach, beforeAll, describe, expect, it } from 'vitest';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import {
Expand Down Expand Up @@ -36,6 +38,7 @@ describe('CLI collect', () => {

beforeAll(async () => {
await cp(fixtureDummyDir, dummyDir, { recursive: true });
await restoreRenamedFiles(dummyDir, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions e2e/cli-e2e/tests/compare.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { ReportsDiff } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile, readTextFile } from '@code-pushup/utils';
Expand All @@ -30,6 +32,7 @@ describe('CLI compare', () => {

beforeAll(async () => {
await cp(fixtureDummyDir, existingDir, { recursive: true });
await restoreRenamedFiles(existingDir, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions e2e/plugin-coverage-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { type Report, reportSchema } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
initGitRepo,
omitVariableReportData,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand All @@ -24,6 +26,7 @@ describe('PLUGIN collect report with coverage-plugin NPM package', () => {

beforeAll(async () => {
await cp(fixtureDir, testFileDir, { recursive: true });
await restoreRenamedFiles(testFileDir, NX_IGNORED_FILES_TO_RESTORE);
await initGitRepo(simpleGit, { baseDir: basicDir });
await initGitRepo(simpleGit, { baseDir: existingDir });
});
Expand Down
5 changes: 5 additions & 0 deletions e2e/plugin-eslint-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { type Report, reportSchema } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
omitVariableReportData,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand Down Expand Up @@ -43,6 +45,9 @@ describe('PLUGIN collect report with eslint-plugin NPM package', () => {
await cp(fixturesArtifactsConfigDir, artifactsConfigDir, {
recursive: true,
});
await restoreRenamedFiles(flatConfigDir, NX_IGNORED_FILES_TO_RESTORE);
await restoreRenamedFiles(legacyConfigDir, NX_IGNORED_FILES_TO_RESTORE);
await restoreRenamedFiles(artifactsConfigDir, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand All @@ -32,6 +34,7 @@ describe('plugin-js-packages', () => {

beforeAll(async () => {
await cp(fixturesNPMDir, npmRepoDir, { recursive: true });
await restoreRenamedFiles(npmRepoDir, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
4 changes: 4 additions & 0 deletions e2e/plugin-jsdocs-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { type Report, reportSchema } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
initGitRepo,
omitVariableReportData,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand Down Expand Up @@ -36,6 +38,8 @@ describe('PLUGIN collect report with jsdocs-plugin NPM package', () => {
beforeAll(async () => {
await cp(fixturesAngularDir, angularDir, { recursive: true });
await cp(fixturesReactDir, reactDir, { recursive: true });
await restoreRenamedFiles(angularDir, NX_IGNORED_FILES_TO_RESTORE);
await restoreRenamedFiles(reactDir, NX_IGNORED_FILES_TO_RESTORE);
await initGitRepo(simpleGit, { baseDir: angularDir });
await initGitRepo(simpleGit, { baseDir: reactDir });
});
Expand Down
3 changes: 3 additions & 0 deletions e2e/plugin-lighthouse-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { type Report, reportSchema } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
omitVariableReportData,
removeColorCodes,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand All @@ -25,6 +27,7 @@ describe('PLUGIN collect report with lighthouse-plugin NPM package', () => {

beforeAll(async () => {
await cp(fixturesDir, testFileDir, { recursive: true });
await restoreRenamedFiles(testFileDir, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions e2e/plugin-typescript-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { type Report, reportSchema } from '@code-pushup/models';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
NX_IGNORED_FILES_TO_RESTORE,
TEST_OUTPUT_DIR,
omitVariableReportData,
osAgnosticAuditOutputs,
osAgnosticPath,
restoreRenamedFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
Expand Down Expand Up @@ -50,6 +52,7 @@ describe('PLUGIN collect report with typescript-plugin NPM package', () => {

beforeAll(async () => {
await cp(fixturesDir, envRoot, { recursive: true });
await restoreRenamedFiles(envRoot, NX_IGNORED_FILES_TO_RESTORE);
});

afterAll(async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/ci/mocks/fixtures/monorepos/npm/_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"workspaces": ["packages/*"]
}
6 changes: 0 additions & 6 deletions packages/ci/mocks/fixtures/monorepos/npm/package.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"packageManager": "yarn@1.22.19",
"private": true,
"workspaces": [
"packages/*"
]
"workspaces": ["packages/*"]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"packageManager": "yarn@4.5.0",
"private": true,
"workspaces": [
"packages/*"
]
"workspaces": ["packages/*"]
}
4 changes: 4 additions & 0 deletions packages/ci/src/lib/run.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {
DEFAULT_PERSIST_SKIP_REPORT,
} from '@code-pushup/models';
import {
NX_IGNORED_FILES_TO_RESTORE,
cleanTestFolder,
initGitRepo,
restoreRenamedFiles,
simulateGitFetch,
teardownTestFolder,
} from '@code-pushup/test-utils';
Expand Down Expand Up @@ -624,6 +626,7 @@ describe('runInCI', () => {
beforeEach(async () => {
const monorepoDir = path.join(fixturesDir, 'monorepos', tool);
await cp(monorepoDir, workDir, { recursive: true });
await restoreRenamedFiles(workDir, NX_IGNORED_FILES_TO_RESTORE);
await git.add('.');
await git.commit(`Create packages in ${tool} monorepo`);
setup?.();
Expand Down Expand Up @@ -1137,6 +1140,7 @@ describe('runInCI', () => {
beforeEach(async () => {
const monorepoDir = path.join(fixturesDir, 'monorepos', 'custom');
await cp(monorepoDir, workDir, { recursive: true });
await restoreRenamedFiles(workDir, NX_IGNORED_FILES_TO_RESTORE);
await git.add('.');
await git.commit('Create projects in monorepo');
});
Expand Down
88 changes: 87 additions & 1 deletion testing/test-utils/src/lib/utils/test-folder-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bold } from 'ansis';
import { mkdir, rm, stat } from 'node:fs/promises';
import { mkdir, readdir, rename, rm, stat } from 'node:fs/promises';
import path from 'node:path';

export async function cleanTestFolder(dirName: string) {
await teardownTestFolder(dirName);
Expand Down Expand Up @@ -36,3 +37,88 @@ export async function teardownTestFolder(dirName: string) {
);
}
}

/**
* 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;
Comment thread
BioPhoton marked this conversation as resolved.
Outdated

/**
* 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
}
Comment thread
BioPhoton marked this conversation as resolved.
Outdated
}
Comment thread
BioPhoton marked this conversation as resolved.
Outdated

/**
* 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> {
Comment thread
BioPhoton marked this conversation as resolved.
Outdated
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
}
}