third-party-npm-platform.test.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import {
  4. assertInstalledLockfileSnapshot,
  5. createPnpmLockfileOnlyEnv,
  6. createInstallablePackageFilter,
  7. productionPackages,
  8. readLockfilePlatformConstraints,
  9. } from "../third-party-npm.mjs";
  10. const LOCKFILE = `lockfileVersion: '9.0'
  11. importers:
  12. .:
  13. dependencies:
  14. sharp:
  15. specifier: ^0.34.0
  16. version: 0.34.5
  17. packages:
  18. '@emnapi/runtime@1.9.2':
  19. resolution: {integrity: sha512-a}
  20. '@img/sharp-linux-s390x@0.34.5':
  21. resolution: {integrity: sha512-b}
  22. cpu: [s390x]
  23. os: [linux]
  24. libc: [glibc]
  25. '@img/sharp-linuxmusl-x64@0.34.5':
  26. resolution: {integrity: sha512-c}
  27. cpu: [x64]
  28. os: [linux]
  29. libc: [musl]
  30. '@img/sharp-wasm32@0.34.5':
  31. resolution: {integrity: sha512-d}
  32. cpu: [wasm32]
  33. fsevents@2.3.3:
  34. resolution: {integrity: sha512-e}
  35. os: [darwin]
  36. not-windows@1.0.0:
  37. resolution: {integrity: sha512-f}
  38. os: ['!win32']
  39. snapshots:
  40. '@img/sharp-wasm32@0.34.5':
  41. dependencies:
  42. '@emnapi/runtime': 1.9.2
  43. cpu: [arm]
  44. `;
  45. const TARGETS = { os: ["darwin", "linux", "win32"], cpu: ["arm64", "x64"], libc: null };
  46. test("锁图查询强制使用 pnpm lockfile-only 模式", () => {
  47. assert.equal(
  48. createPnpmLockfileOnlyEnv({ NPM_CONFIG_LOCKFILE_ONLY: "false" }).NPM_CONFIG_LOCKFILE_ONLY,
  49. "true",
  50. );
  51. });
  52. test("安装快照与根 lockfile 内容一致即通过,换行风格差异不算漂移", () => {
  53. assert.doesNotThrow(() => assertInstalledLockfileSnapshot("same", "same"));
  54. // 评审 CR-03 回归:Windows autocrlf 工作区根 lockfile 为 CRLF、安装快照为 LF,
  55. // 依赖图一致时必须通过。
  56. assert.doesNotThrow(() => assertInstalledLockfileSnapshot("a\r\nb\r\n", "a\nb\n"));
  57. // 真实内容差异(含归一化后仍不同)仍然严格失败。
  58. assert.throws(
  59. () => assertInstalledLockfileSnapshot("current\r\n", "stale\n"),
  60. /pnpm install --frozen-lockfile/,
  61. );
  62. });
  63. test("只读取 packages 段的 os/cpu/libc 约束,snapshots 段不参与", () => {
  64. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  65. assert.deepEqual(constraints.get("@img/sharp-linux-s390x@0.34.5"), {
  66. cpu: ["s390x"],
  67. os: ["linux"],
  68. libc: ["glibc"],
  69. });
  70. assert.deepEqual(constraints.get("@img/sharp-wasm32@0.34.5"), { cpu: ["wasm32"] });
  71. assert.deepEqual(constraints.get("fsevents@2.3.3"), { os: ["darwin"] });
  72. assert.deepEqual(constraints.get("not-windows@1.0.0"), { os: ["!win32"] });
  73. assert.equal(constraints.has("@emnapi/runtime@1.9.2"), false);
  74. });
  75. test("与 pnpm 可安装规则一致:非目标 cpu/os 排除,libc 只在宿主 libc 已知时参与", () => {
  76. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  77. const installable = createInstallablePackageFilter(constraints, TARGETS);
  78. assert.equal(installable("@img/sharp-linux-s390x", "0.34.5"), false);
  79. assert.equal(installable("@img/sharp-wasm32", "0.34.5"), false);
  80. assert.equal(installable("fsevents", "2.3.3"), true);
  81. // pnpm 的否定项语义:目标平台里只要有一个被否定,整个包就不安装。
  82. assert.equal(installable("not-windows", "1.0.0"), false);
  83. assert.equal(installable("@img/sharp-linuxmusl-x64", "0.34.5"), true);
  84. assert.equal(installable("@emnapi/runtime", "1.9.2"), true);
  85. const glibcHost = createInstallablePackageFilter(constraints, { ...TARGETS, libc: ["glibc"] });
  86. assert.equal(glibcHost("@img/sharp-linuxmusl-x64", "0.34.5"), false);
  87. const darwinOnly = createInstallablePackageFilter(constraints, { ...TARGETS, os: ["darwin"] });
  88. assert.equal(darwinOnly("not-windows", "1.0.0"), true);
  89. assert.equal(darwinOnly("fsevents", "2.3.3"), true);
  90. const linuxOnly = createInstallablePackageFilter(constraints, { ...TARGETS, os: ["linux"] });
  91. assert.equal(linuxOnly("fsevents", "2.3.3"), false);
  92. });
  93. test("不可安装的平台包及只经由它引入的依赖不进生产图", () => {
  94. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  95. const installable = createInstallablePackageFilter(constraints, TARGETS);
  96. const wasm = {
  97. version: "0.34.5",
  98. dependencies: { "@emnapi/runtime": { version: "1.9.2" } },
  99. };
  100. const onlyViaWasm = [
  101. {
  102. name: "app",
  103. dependencies: {
  104. sharp: {
  105. version: "0.34.5",
  106. optionalDependencies: {
  107. "@img/sharp-wasm32": wasm,
  108. "@img/sharp-linux-s390x": { version: "0.34.5" },
  109. },
  110. },
  111. },
  112. },
  113. ];
  114. assert.deepEqual([...productionPackages(onlyViaWasm, installable).keys()], ["sharp@0.34.5"]);
  115. const alsoDirect = [
  116. { ...onlyViaWasm[0], optionalDependencies: { "@emnapi/runtime": { version: "1.9.2" } } },
  117. ];
  118. assert.deepEqual([...productionPackages(alsoDirect, installable).keys()].sort(), [
  119. "@emnapi/runtime@1.9.2",
  120. "sharp@0.34.5",
  121. ]);
  122. });
  123. test("带 peer 或 patch 后缀的版本按基础版本匹配锁文件约束", () => {
  124. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  125. const installable = createInstallablePackageFilter(constraints, TARGETS);
  126. assert.equal(installable("@img/sharp-wasm32", "0.34.5(patch_hash=abc)"), false);
  127. });