|
| 1 | +import path from 'node:path'; |
| 2 | +import { setWorkspaceRoot } from 'nx/src/utils/workspace-root'; |
| 3 | +import { beforeAll, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { |
| 5 | + FIXTURES_DIR, |
| 6 | + Fixture, |
| 7 | + resetFixtureDirectory, |
| 8 | +} from '../utils/fixtures'; |
| 9 | +import { |
| 10 | + LONG_TIMEOUT_MS, |
| 11 | + runNgAdd, |
| 12 | + runNgGenerate, |
| 13 | + runNgNew, |
| 14 | +} from '../utils/local-registry-process'; |
| 15 | +import { normalizeVersionsOfPackagesWeDoNotControl } from '../utils/snapshot-serializers'; |
| 16 | + |
| 17 | +expect.addSnapshotSerializer(normalizeVersionsOfPackagesWeDoNotControl); |
| 18 | + |
| 19 | +const fixtureDirectory = 'output-file-interpolation'; |
| 20 | +let fixture: Fixture; |
| 21 | + |
| 22 | +describe('output-file-interpolation', () => { |
| 23 | + vi.setConfig({ testTimeout: LONG_TIMEOUT_MS }); |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + resetFixtureDirectory(fixtureDirectory); |
| 27 | + process.chdir(FIXTURES_DIR); |
| 28 | + |
| 29 | + await runNgNew(fixtureDirectory); |
| 30 | + |
| 31 | + process.env.NX_DAEMON = 'false'; |
| 32 | + process.env.NX_CACHE_PROJECT_GRAPH = 'false'; |
| 33 | + |
| 34 | + const workspaceRoot = path.join(FIXTURES_DIR, fixtureDirectory); |
| 35 | + process.chdir(workspaceRoot); |
| 36 | + process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot; |
| 37 | + setWorkspaceRoot(workspaceRoot); |
| 38 | + |
| 39 | + fixture = new Fixture(workspaceRoot); |
| 40 | + |
| 41 | + await runNgAdd(); |
| 42 | + await runNgGenerate(['app', 'app-one', '--interactive=false']); |
| 43 | + await runNgGenerate(['app', 'app-two', '--interactive=false']); |
| 44 | + await runNgGenerate(['lib', 'lib-one', '--interactive=false']); |
| 45 | + |
| 46 | + // Configure app-one with outputFile using {projectName} placeholder |
| 47 | + const angularJson = fixture.readJson('angular.json'); |
| 48 | + angularJson.projects['app-one'].architect.lint.options.outputFile = |
| 49 | + 'lint-reports/{projectName}-report.json'; |
| 50 | + angularJson.projects['app-one'].architect.lint.options.format = 'json'; |
| 51 | + fixture.writeJson('angular.json', angularJson); |
| 52 | + |
| 53 | + // Configure lib-one with outputFile using both placeholders |
| 54 | + angularJson.projects['lib-one'].architect.lint.options.outputFile = |
| 55 | + '{projectRoot}/reports/{projectName}-results.json'; |
| 56 | + angularJson.projects['lib-one'].architect.lint.options.format = 'json'; |
| 57 | + fixture.writeJson('angular.json', angularJson); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should write lint reports to interpolated paths for projects with outputFile configuration with known placeholders', async () => { |
| 61 | + expect.assertions(2); |
| 62 | + |
| 63 | + // Run lint for app-one |
| 64 | + try { |
| 65 | + fixture.runCommand('npx ng lint app-one'); |
| 66 | + } catch { |
| 67 | + // Lint may fail, but we're interested in the output files |
| 68 | + } |
| 69 | + |
| 70 | + // Verify report was written to the interpolated path and snapshot the contents |
| 71 | + const appOneReport = fixture.readJson('lint-reports/app-one-report.json'); |
| 72 | + expect(appOneReport).toMatchSnapshot(); |
| 73 | + |
| 74 | + // Run lint for lib-one |
| 75 | + try { |
| 76 | + fixture.runCommand('npx ng lint lib-one'); |
| 77 | + } catch { |
| 78 | + // Lint may fail, but we're interested in the output files |
| 79 | + } |
| 80 | + |
| 81 | + // Verify report was written to the interpolated path using projectRoot and snapshot the contents |
| 82 | + const libOneReport = fixture.readJson( |
| 83 | + 'projects/lib-one/reports/lib-one-results.json', |
| 84 | + ); |
| 85 | + expect(libOneReport).toMatchSnapshot(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should support dynamic override of outputFile with placeholder interpolation', async () => { |
| 89 | + expect.assertions(2); |
| 90 | + |
| 91 | + // Run lint for app-two with dynamic outputFile override |
| 92 | + try { |
| 93 | + fixture.runCommand( |
| 94 | + 'npx ng lint app-two --output-file="dynamic-reports/{projectName}/{projectRoot}/lint.json" --format=json', |
| 95 | + ); |
| 96 | + } catch { |
| 97 | + // Lint may fail, but we're interested in the output files |
| 98 | + } |
| 99 | + |
| 100 | + // Verify report was written to the dynamically interpolated path and snapshot the contents |
| 101 | + const appTwoReportPath = |
| 102 | + 'dynamic-reports/app-two/projects/app-two/lint.json'; |
| 103 | + expect(fixture.fileExists(appTwoReportPath)).toBe(true); |
| 104 | + |
| 105 | + const appTwoReport = fixture.readJson(appTwoReportPath); |
| 106 | + expect(appTwoReport).toMatchSnapshot(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should interpolate placeholders when running ng lint without project name (all projects)', async () => { |
| 110 | + expect.assertions(5); |
| 111 | + |
| 112 | + // Clean up any previous reports |
| 113 | + if (fixture.directoryExists('all-projects-reports')) { |
| 114 | + fixture.deleteFileOrDirectory('all-projects-reports'); |
| 115 | + } |
| 116 | + |
| 117 | + // Run ng lint without a project name - this lints ALL projects |
| 118 | + try { |
| 119 | + fixture.runCommand( |
| 120 | + 'npx ng lint --output-file="all-projects-reports/{projectName}-{projectRoot}.json" --format=json', |
| 121 | + ); |
| 122 | + } catch { |
| 123 | + // Lint may fail, but we're interested in the output files |
| 124 | + } |
| 125 | + |
| 126 | + // Verify each project has its own report file with interpolated paths |
| 127 | + // The default project name should get its own file |
| 128 | + const defaultProjectReport = |
| 129 | + 'all-projects-reports/output-file-interpolation-.json'; |
| 130 | + expect(fixture.fileExists(defaultProjectReport)).toBe(true); |
| 131 | + |
| 132 | + // app-one should have its own file |
| 133 | + const appOneReportPath = |
| 134 | + 'all-projects-reports/app-one-projects/app-one.json'; |
| 135 | + expect(fixture.fileExists(appOneReportPath)).toBe(true); |
| 136 | + |
| 137 | + // app-two should have its own file |
| 138 | + const appTwoReportPath = |
| 139 | + 'all-projects-reports/app-two-projects/app-two.json'; |
| 140 | + expect(fixture.fileExists(appTwoReportPath)).toBe(true); |
| 141 | + |
| 142 | + // lib-one should have its own file |
| 143 | + const libOneReportPath = |
| 144 | + 'all-projects-reports/lib-one-projects/lib-one.json'; |
| 145 | + expect(fixture.fileExists(libOneReportPath)).toBe(true); |
| 146 | + |
| 147 | + // Snapshot all reports to ensure they contain the correct lint results |
| 148 | + const defaultReport = fixture.readJson(defaultProjectReport); |
| 149 | + const appOneReport = fixture.readJson(appOneReportPath); |
| 150 | + const appTwoReport = fixture.readJson(appTwoReportPath); |
| 151 | + const libOneReport = fixture.readJson(libOneReportPath); |
| 152 | + |
| 153 | + expect({ |
| 154 | + 'output-file-interpolation': defaultReport, |
| 155 | + 'app-one': appOneReport, |
| 156 | + 'app-two': appTwoReport, |
| 157 | + 'lib-one': libOneReport, |
| 158 | + }).toMatchSnapshot(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments