|
1 | | -import { type MockInstance, describe, expect, it, vi } from 'vitest'; |
2 | | -import { osAgnosticPath } from '@code-pushup/test-utils'; |
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { |
| 3 | + type MockInstance, |
| 4 | + afterEach, |
| 5 | + beforeEach, |
| 6 | + describe, |
| 7 | + expect, |
| 8 | + it, |
| 9 | + vi, |
| 10 | +} from 'vitest'; |
| 11 | +import { MEMFS_VOLUME, osAgnosticPath } from '@code-pushup/test-utils'; |
| 12 | +import { normalizedCreateNodesV2Context } from '../../plugin/utils.js'; |
3 | 13 | import type { Command } from '../internal/types.js'; |
4 | 14 | import { |
5 | 15 | parseCliExecutorOnlyOptions, |
6 | 16 | parseCliExecutorOptions, |
7 | 17 | parsePrintConfigExecutorOptions, |
8 | 18 | } from './utils.js'; |
9 | 19 |
|
| 20 | +vi.mock('node:fs/promises', () => ({ |
| 21 | + readFile: vi.fn(), |
| 22 | +})); |
| 23 | + |
10 | 24 | describe('parsePrintConfigExecutorOptions', () => { |
11 | 25 | it('should provide NO default output path', () => { |
12 | 26 | expect(parsePrintConfigExecutorOptions({})).toStrictEqual( |
@@ -203,3 +217,225 @@ describe('parseCliExecutorOptions', () => { |
203 | 217 | }, |
204 | 218 | ); |
205 | 219 | }); |
| 220 | + |
| 221 | +describe('normalizedCreateNodesV2Context', () => { |
| 222 | + const CP_TARGET_NAME = 'code-pushup'; |
| 223 | + |
| 224 | + beforeEach(() => { |
| 225 | + vi.clearAllMocks(); |
| 226 | + }); |
| 227 | + |
| 228 | + afterEach(() => { |
| 229 | + vi.restoreAllMocks(); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should normalize context with default target name', async () => { |
| 233 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 234 | + const projectJsonContent = JSON.stringify({ |
| 235 | + name: 'my-lib', |
| 236 | + root: 'libs/my-lib', |
| 237 | + targets: {}, |
| 238 | + }); |
| 239 | + |
| 240 | + // Mock readFile |
| 241 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(projectJsonContent)); |
| 242 | + |
| 243 | + const context = { |
| 244 | + nxJsonConfiguration: {}, |
| 245 | + configFiles: [], |
| 246 | + workspaceRoot: MEMFS_VOLUME, |
| 247 | + }; |
| 248 | + |
| 249 | + const result = await normalizedCreateNodesV2Context( |
| 250 | + context, |
| 251 | + projectJsonPath, |
| 252 | + ); |
| 253 | + |
| 254 | + expect(result).toEqual({ |
| 255 | + ...context, |
| 256 | + projectJson: { |
| 257 | + name: 'my-lib', |
| 258 | + root: 'libs/my-lib', |
| 259 | + targets: {}, |
| 260 | + }, |
| 261 | + projectRoot: expect.any(String), |
| 262 | + createOptions: { |
| 263 | + targetName: CP_TARGET_NAME, |
| 264 | + }, |
| 265 | + }); |
| 266 | + expect(osAgnosticPath(result.projectRoot)).toBe( |
| 267 | + osAgnosticPath(`${MEMFS_VOLUME}/libs/my-lib`), |
| 268 | + ); |
| 269 | + |
| 270 | + expect(readFile).toHaveBeenCalledWith(projectJsonPath); |
| 271 | + }); |
| 272 | + |
| 273 | + it('should normalize context with custom target name', async () => { |
| 274 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 275 | + const projectJsonContent = JSON.stringify({ |
| 276 | + name: 'my-lib', |
| 277 | + root: 'libs/my-lib', |
| 278 | + targets: {}, |
| 279 | + }); |
| 280 | + |
| 281 | + const customTargetName = 'custom-target'; |
| 282 | + |
| 283 | + // Mock readFile |
| 284 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(projectJsonContent)); |
| 285 | + |
| 286 | + const context = { |
| 287 | + nxJsonConfiguration: {}, |
| 288 | + configFiles: [], |
| 289 | + workspaceRoot: MEMFS_VOLUME, |
| 290 | + }; |
| 291 | + |
| 292 | + const result = await normalizedCreateNodesV2Context( |
| 293 | + context, |
| 294 | + projectJsonPath, |
| 295 | + { |
| 296 | + targetName: customTargetName, |
| 297 | + }, |
| 298 | + ); |
| 299 | + |
| 300 | + expect(result).toEqual({ |
| 301 | + ...context, |
| 302 | + projectJson: { |
| 303 | + name: 'my-lib', |
| 304 | + root: 'libs/my-lib', |
| 305 | + targets: {}, |
| 306 | + }, |
| 307 | + projectRoot: expect.any(String), |
| 308 | + createOptions: { |
| 309 | + targetName: customTargetName, |
| 310 | + }, |
| 311 | + }); |
| 312 | + expect(osAgnosticPath(result.projectRoot)).toBe( |
| 313 | + osAgnosticPath(`${MEMFS_VOLUME}/libs/my-lib`), |
| 314 | + ); |
| 315 | + }); |
| 316 | + |
| 317 | + it('should extract project root from project.json path', async () => { |
| 318 | + const projectJsonPath = `${MEMFS_VOLUME}/packages/utils/project.json`; |
| 319 | + const projectJsonContent = JSON.stringify({ |
| 320 | + name: 'utils', |
| 321 | + root: 'packages/utils', |
| 322 | + targets: {}, |
| 323 | + }); |
| 324 | + |
| 325 | + // Mock readFile |
| 326 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(projectJsonContent)); |
| 327 | + |
| 328 | + const context = { |
| 329 | + nxJsonConfiguration: {}, |
| 330 | + configFiles: [], |
| 331 | + workspaceRoot: MEMFS_VOLUME, |
| 332 | + }; |
| 333 | + |
| 334 | + const result = await normalizedCreateNodesV2Context( |
| 335 | + context, |
| 336 | + projectJsonPath, |
| 337 | + ); |
| 338 | + |
| 339 | + expect(osAgnosticPath(result.projectRoot)).toBe( |
| 340 | + osAgnosticPath(`${MEMFS_VOLUME}/packages/utils`), |
| 341 | + ); |
| 342 | + }); |
| 343 | + |
| 344 | + it('should preserve all context properties', async () => { |
| 345 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 346 | + const projectJsonContent = JSON.stringify({ |
| 347 | + name: 'my-lib', |
| 348 | + root: 'libs/my-lib', |
| 349 | + targets: {}, |
| 350 | + }); |
| 351 | + |
| 352 | + // Mock readFile |
| 353 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(projectJsonContent)); |
| 354 | + |
| 355 | + const context = { |
| 356 | + nxJsonConfiguration: { namedInputs: { default: ['{projectRoot}/**/*'] } }, |
| 357 | + workspaceRoot: MEMFS_VOLUME, |
| 358 | + }; |
| 359 | + |
| 360 | + const result = await normalizedCreateNodesV2Context( |
| 361 | + context, |
| 362 | + projectJsonPath, |
| 363 | + ); |
| 364 | + |
| 365 | + expect(result.nxJsonConfiguration).toEqual(context.nxJsonConfiguration); |
| 366 | + expect(result.workspaceRoot).toBe(context.workspaceRoot); |
| 367 | + expect(result.projectJson).toBeDefined(); |
| 368 | + expect(result.projectRoot).toBeDefined(); |
| 369 | + expect(result.createOptions).toBeDefined(); |
| 370 | + }); |
| 371 | + |
| 372 | + it('should preserve createOptions properties', async () => { |
| 373 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 374 | + const projectJsonContent = JSON.stringify({ |
| 375 | + name: 'my-lib', |
| 376 | + root: 'libs/my-lib', |
| 377 | + targets: {}, |
| 378 | + }); |
| 379 | + |
| 380 | + // Mock readFile |
| 381 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(projectJsonContent)); |
| 382 | + |
| 383 | + const context = { |
| 384 | + nxJsonConfiguration: {}, |
| 385 | + configFiles: [], |
| 386 | + workspaceRoot: MEMFS_VOLUME, |
| 387 | + }; |
| 388 | + |
| 389 | + const createOptions = { |
| 390 | + targetName: 'custom-target', |
| 391 | + projectPrefix: 'cli', |
| 392 | + bin: 'packages/cli/dist', |
| 393 | + }; |
| 394 | + |
| 395 | + const result = await normalizedCreateNodesV2Context( |
| 396 | + context, |
| 397 | + projectJsonPath, |
| 398 | + createOptions, |
| 399 | + ); |
| 400 | + |
| 401 | + expect(result.createOptions).toEqual({ |
| 402 | + ...createOptions, |
| 403 | + targetName: 'custom-target', |
| 404 | + }); |
| 405 | + }); |
| 406 | + |
| 407 | + it('should throw error when project.json file cannot be read', async () => { |
| 408 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 409 | + |
| 410 | + // Mock readFile to throw error |
| 411 | + vi.mocked(readFile).mockRejectedValue(new Error('File not found')); |
| 412 | + |
| 413 | + const context = { |
| 414 | + nxJsonConfiguration: {}, |
| 415 | + configFiles: [], |
| 416 | + workspaceRoot: MEMFS_VOLUME, |
| 417 | + }; |
| 418 | + |
| 419 | + await expect( |
| 420 | + normalizedCreateNodesV2Context(context, projectJsonPath), |
| 421 | + ).rejects.toThrow(`Error parsing project.json file ${projectJsonPath}.`); |
| 422 | + }); |
| 423 | + |
| 424 | + it('should throw error when project.json contains invalid JSON', async () => { |
| 425 | + const projectJsonPath = `${MEMFS_VOLUME}/libs/my-lib/project.json`; |
| 426 | + const invalidJson = '{ invalid json }'; |
| 427 | + |
| 428 | + // Mock readFile |
| 429 | + vi.mocked(readFile).mockResolvedValue(Buffer.from(invalidJson)); |
| 430 | + |
| 431 | + const context = { |
| 432 | + nxJsonConfiguration: {}, |
| 433 | + configFiles: [], |
| 434 | + workspaceRoot: MEMFS_VOLUME, |
| 435 | + }; |
| 436 | + |
| 437 | + await expect( |
| 438 | + normalizedCreateNodesV2Context(context, projectJsonPath), |
| 439 | + ).rejects.toThrow(`Error parsing project.json file ${projectJsonPath}.`); |
| 440 | + }); |
| 441 | +}); |
0 commit comments