npm.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect, test } from "bun:test"
  4. import { Npm } from "@opencode-ai/core/npm"
  5. import { tmpdir } from "./fixture/tmpdir"
  6. const win = process.platform === "win32"
  7. const writePackage = (dir: string, pkg: Record<string, unknown>) =>
  8. Bun.write(
  9. path.join(dir, "package.json"),
  10. JSON.stringify({
  11. version: "1.0.0",
  12. ...pkg,
  13. }),
  14. )
  15. describe("Npm.sanitize", () => {
  16. test("keeps normal scoped package specs unchanged", () => {
  17. expect(Npm.sanitize("@opencode/acme")).toBe("@opencode/acme")
  18. expect(Npm.sanitize("@opencode/acme@1.0.0")).toBe("@opencode/acme@1.0.0")
  19. expect(Npm.sanitize("prettier")).toBe("prettier")
  20. })
  21. test("handles git https specs", () => {
  22. const spec = "acme@git+https://github.com/opencode/acme.git"
  23. const expected = win ? "acme@git+https_//github.com/opencode/acme.git" : spec
  24. expect(Npm.sanitize(spec)).toBe(expected)
  25. })
  26. })
  27. describe("Npm.install", () => {
  28. test("respects omit from project .npmrc", async () => {
  29. await using tmp = await tmpdir()
  30. await writePackage(tmp.path, {
  31. name: "fixture",
  32. dependencies: {
  33. "prod-pkg": "file:./prod-pkg",
  34. },
  35. devDependencies: {
  36. "dev-pkg": "file:./dev-pkg",
  37. },
  38. })
  39. await Bun.write(path.join(tmp.path, ".npmrc"), "omit=dev\n")
  40. await fs.mkdir(path.join(tmp.path, "prod-pkg"))
  41. await fs.mkdir(path.join(tmp.path, "dev-pkg"))
  42. await writePackage(path.join(tmp.path, "prod-pkg"), { name: "prod-pkg" })
  43. await writePackage(path.join(tmp.path, "dev-pkg"), { name: "dev-pkg" })
  44. await Npm.install(tmp.path)
  45. await expect(fs.stat(path.join(tmp.path, "node_modules", "prod-pkg"))).resolves.toBeDefined()
  46. await expect(fs.stat(path.join(tmp.path, "node_modules", "dev-pkg"))).rejects.toThrow()
  47. })
  48. })