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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import {
  4. createInstallablePackageFilter,
  5. productionPackages,
  6. readLockfilePlatformConstraints,
  7. } from "../third-party-npm.mjs";
  8. const LOCKFILE = `lockfileVersion: '9.0'
  9. importers:
  10. .:
  11. dependencies:
  12. sharp:
  13. specifier: ^0.34.0
  14. version: 0.34.5
  15. packages:
  16. '@emnapi/runtime@1.9.2':
  17. resolution: {integrity: sha512-a}
  18. '@img/sharp-linux-s390x@0.34.5':
  19. resolution: {integrity: sha512-b}
  20. cpu: [s390x]
  21. os: [linux]
  22. libc: [glibc]
  23. '@img/sharp-linuxmusl-x64@0.34.5':
  24. resolution: {integrity: sha512-c}
  25. cpu: [x64]
  26. os: [linux]
  27. libc: [musl]
  28. '@img/sharp-wasm32@0.34.5':
  29. resolution: {integrity: sha512-d}
  30. cpu: [wasm32]
  31. fsevents@2.3.3:
  32. resolution: {integrity: sha512-e}
  33. os: [darwin]
  34. not-windows@1.0.0:
  35. resolution: {integrity: sha512-f}
  36. os: ['!win32']
  37. snapshots:
  38. '@img/sharp-wasm32@0.34.5':
  39. dependencies:
  40. '@emnapi/runtime': 1.9.2
  41. cpu: [arm]
  42. `;
  43. const TARGETS = { os: ["darwin", "linux", "win32"], cpu: ["arm64", "x64"], libc: null };
  44. test("只读取 packages 段的 os/cpu/libc 约束,snapshots 段不参与", () => {
  45. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  46. assert.deepEqual(constraints.get("@img/sharp-linux-s390x@0.34.5"), {
  47. cpu: ["s390x"],
  48. os: ["linux"],
  49. libc: ["glibc"],
  50. });
  51. assert.deepEqual(constraints.get("@img/sharp-wasm32@0.34.5"), { cpu: ["wasm32"] });
  52. assert.deepEqual(constraints.get("fsevents@2.3.3"), { os: ["darwin"] });
  53. assert.deepEqual(constraints.get("not-windows@1.0.0"), { os: ["!win32"] });
  54. assert.equal(constraints.has("@emnapi/runtime@1.9.2"), false);
  55. });
  56. test("与 pnpm 可安装规则一致:非目标 cpu/os 排除,libc 只在宿主 libc 已知时参与", () => {
  57. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  58. const installable = createInstallablePackageFilter(constraints, TARGETS);
  59. assert.equal(installable("@img/sharp-linux-s390x", "0.34.5"), false);
  60. assert.equal(installable("@img/sharp-wasm32", "0.34.5"), false);
  61. assert.equal(installable("fsevents", "2.3.3"), true);
  62. // pnpm 的否定项语义:目标平台里只要有一个被否定,整个包就不安装。
  63. assert.equal(installable("not-windows", "1.0.0"), false);
  64. assert.equal(installable("@img/sharp-linuxmusl-x64", "0.34.5"), true);
  65. assert.equal(installable("@emnapi/runtime", "1.9.2"), true);
  66. const glibcHost = createInstallablePackageFilter(constraints, { ...TARGETS, libc: ["glibc"] });
  67. assert.equal(glibcHost("@img/sharp-linuxmusl-x64", "0.34.5"), false);
  68. const darwinOnly = createInstallablePackageFilter(constraints, { ...TARGETS, os: ["darwin"] });
  69. assert.equal(darwinOnly("not-windows", "1.0.0"), true);
  70. assert.equal(darwinOnly("fsevents", "2.3.3"), true);
  71. const linuxOnly = createInstallablePackageFilter(constraints, { ...TARGETS, os: ["linux"] });
  72. assert.equal(linuxOnly("fsevents", "2.3.3"), false);
  73. });
  74. test("不可安装的平台包及只经由它引入的依赖不进生产图", () => {
  75. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  76. const installable = createInstallablePackageFilter(constraints, TARGETS);
  77. const wasm = {
  78. version: "0.34.5",
  79. dependencies: { "@emnapi/runtime": { version: "1.9.2" } },
  80. };
  81. const onlyViaWasm = [
  82. {
  83. name: "app",
  84. dependencies: {
  85. sharp: {
  86. version: "0.34.5",
  87. optionalDependencies: {
  88. "@img/sharp-wasm32": wasm,
  89. "@img/sharp-linux-s390x": { version: "0.34.5" },
  90. },
  91. },
  92. },
  93. },
  94. ];
  95. assert.deepEqual([...productionPackages(onlyViaWasm, installable).keys()], ["sharp@0.34.5"]);
  96. const alsoDirect = [
  97. { ...onlyViaWasm[0], optionalDependencies: { "@emnapi/runtime": { version: "1.9.2" } } },
  98. ];
  99. assert.deepEqual([...productionPackages(alsoDirect, installable).keys()].sort(), [
  100. "@emnapi/runtime@1.9.2",
  101. "sharp@0.34.5",
  102. ]);
  103. });
  104. test("带 peer 或 patch 后缀的版本按基础版本匹配锁文件约束", () => {
  105. const constraints = readLockfilePlatformConstraints(LOCKFILE);
  106. const installable = createInstallablePackageFilter(constraints, TARGETS);
  107. assert.equal(installable("@img/sharp-wasm32", "0.34.5(patch_hash=abc)"), false);
  108. });