|
15 | 15 | */ |
16 | 16 | import {withDir} from "tmp-promise"; |
17 | 17 | import * as fsp from "fs/promises"; |
| 18 | +import * as fs from "fs"; |
18 | 19 | import * as path from "path"; |
19 | 20 | import { |
| 21 | + BaseProjectUpdateInfo, |
| 22 | + createDependencyRecipeAccumulator, |
20 | 23 | detectPackageManager, |
21 | 24 | getAllLockFileNames, |
22 | 25 | getLockFileDetectionConfig, |
23 | 26 | getLockFileFormat, |
24 | 27 | getLockFileName, |
25 | 28 | isYarnBerryLockFile, |
| 29 | + PackageJsonParser, |
26 | 30 | PackageManager, |
27 | | - runWorkspaceInstallInTempDir |
| 31 | + runWorkspaceInstallInTempDir, |
| 32 | + updateNodeResolutionMarker |
28 | 33 | } from "../../src/javascript"; |
| 34 | +import {Json} from "../../src/json"; |
29 | 35 |
|
30 | 36 | describe("detectPackageManager", () => { |
31 | 37 |
|
@@ -325,3 +331,78 @@ describe("runWorkspaceInstallInTempDir", () => { |
325 | 331 | expect(result.lockFileContent).toContain("is-number"); |
326 | 332 | }, 120000); |
327 | 333 | }); |
| 334 | + |
| 335 | +describe("updateNodeResolutionMarker", () => { |
| 336 | + |
| 337 | + test("returns same doc reference when accumulator has no updates", async () => { |
| 338 | + // given a parsed package.json with a NodeResolutionResult marker |
| 339 | + await withDir(async (dir) => { |
| 340 | + const packageJsonContent = JSON.stringify({ |
| 341 | + name: "test-project", |
| 342 | + version: "1.0.0", |
| 343 | + dependencies: { |
| 344 | + "react": "^18.2.0" |
| 345 | + } |
| 346 | + }, null, 2); |
| 347 | + fs.writeFileSync(path.join(dir.path, "package.json"), packageJsonContent); |
| 348 | + |
| 349 | + const parser = new PackageJsonParser({relativeTo: dir.path}); |
| 350 | + const docs: Json.Document[] = []; |
| 351 | + for await (const result of parser.parse(path.join(dir.path, "package.json"))) { |
| 352 | + docs.push(result as Json.Document); |
| 353 | + } |
| 354 | + const doc = docs[0]; |
| 355 | + |
| 356 | + // when calling updateNodeResolutionMarker with an accumulator that has |
| 357 | + // no entries for this project (no package.json or lock file changes) |
| 358 | + const acc = createDependencyRecipeAccumulator<BaseProjectUpdateInfo>(); |
| 359 | + const updateInfo = { |
| 360 | + packageJsonPath: "package.json", |
| 361 | + packageManager: PackageManager.Npm, |
| 362 | + originalPackageJson: packageJsonContent |
| 363 | + }; |
| 364 | + |
| 365 | + const result = await updateNodeResolutionMarker(doc, updateInfo, acc); |
| 366 | + |
| 367 | + // then the same doc reference is returned (no-op marker update) |
| 368 | + expect(result).toBe(doc); |
| 369 | + }, {unsafeCleanup: true}); |
| 370 | + }); |
| 371 | + |
| 372 | + test("returns same doc reference when updated content is structurally identical", async () => { |
| 373 | + // given a parsed package.json with a NodeResolutionResult marker |
| 374 | + await withDir(async (dir) => { |
| 375 | + const packageJsonContent = JSON.stringify({ |
| 376 | + name: "test-project", |
| 377 | + version: "1.0.0", |
| 378 | + dependencies: { |
| 379 | + "react": "^18.2.0" |
| 380 | + } |
| 381 | + }, null, 2); |
| 382 | + fs.writeFileSync(path.join(dir.path, "package.json"), packageJsonContent); |
| 383 | + |
| 384 | + const parser = new PackageJsonParser({relativeTo: dir.path}); |
| 385 | + const docs: Json.Document[] = []; |
| 386 | + for await (const result of parser.parse(path.join(dir.path, "package.json"))) { |
| 387 | + docs.push(result as Json.Document); |
| 388 | + } |
| 389 | + const doc = docs[0]; |
| 390 | + |
| 391 | + // when calling updateNodeResolutionMarker with an accumulator whose |
| 392 | + // updatedPackageJsons contains content identical to the original |
| 393 | + const acc = createDependencyRecipeAccumulator<BaseProjectUpdateInfo>(); |
| 394 | + acc.updatedPackageJsons.set("package.json", packageJsonContent); |
| 395 | + const updateInfo = { |
| 396 | + packageJsonPath: "package.json", |
| 397 | + packageManager: PackageManager.Npm, |
| 398 | + originalPackageJson: packageJsonContent |
| 399 | + }; |
| 400 | + |
| 401 | + const result = await updateNodeResolutionMarker(doc, updateInfo, acc); |
| 402 | + |
| 403 | + // then the same doc reference is returned (structurally equal marker) |
| 404 | + expect(result).toBe(doc); |
| 405 | + }, {unsafeCleanup: true}); |
| 406 | + }); |
| 407 | + |
| 408 | +}); |
0 commit comments