plugin.test.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { mkdtemp, rm } from "node:fs/promises"
  2. import { tmpdir } from "node:os"
  3. import path from "node:path"
  4. import { expect, test } from "vitest"
  5. import { pickAppProduct } from "../src/providers/build.js"
  6. import { create, source } from "../src/providers/project.js"
  7. import { isIosRuntime, parseSimulators } from "../src/providers/sim.js"
  8. const found = {
  9. projects: ["Explicit.xcodeproj"],
  10. workspaces: ["Auto.xcworkspace"],
  11. schemes: ["AutoScheme"],
  12. bundleIds: [],
  13. }
  14. test("source prefers explicit project over auto workspace", () => {
  15. expect(source({ project: "Explicit.xcodeproj" }, found)).toEqual({
  16. flag: "-project",
  17. file: "Explicit.xcodeproj",
  18. scheme: "Explicit",
  19. })
  20. })
  21. test("create validates inputs and refuses accidental overwrites", async () => {
  22. await withCwd(async () => {
  23. const created = await create({ name: "Counter App", deployment: "17.0" })
  24. expect(created.project).toBe(path.join("CounterApp", "CounterApp.xcodeproj"))
  25. expect(created.bundle).toBe("com.example.counterapp")
  26. await expect(create({ name: "Counter App" })).rejects.toThrow("Refusing to overwrite")
  27. await expect(create({ name: "Escaped", dir: "../Escaped" })).rejects.toThrow("Path escapes project root")
  28. await expect(create({ name: "BadDeploy", deployment: "17.0; BAD=1" })).rejects.toThrow("Invalid deployment")
  29. await expect(create({ name: "BadBundle", bundleId: "com.example.bad_bundle" })).rejects.toThrow("Invalid bundle id")
  30. })
  31. })
  32. test("build product selection prefers the requested configuration", () => {
  33. const debug = path.join("DerivedData", "Build", "Products", "Debug-iphonesimulator", "Demo.app")
  34. const release = path.join("DerivedData", "Build", "Products", "Release-iphonesimulator", "Demo.app")
  35. expect(pickAppProduct([debug, release], "Release")).toBe(release)
  36. expect(pickAppProduct([debug], "Release")).toBe(debug)
  37. })
  38. test("runtime filtering keeps only iOS simulator runtimes", () => {
  39. expect(isIosRuntime("com.apple.CoreSimulator.SimRuntime.iOS-18-2")).toBe(true)
  40. expect(isIosRuntime("com.apple.CoreSimulator.SimRuntime.iPadOS-18-2")).toBe(true)
  41. expect(isIosRuntime("com.apple.CoreSimulator.SimRuntime.tvOS-18-2")).toBe(false)
  42. expect(isIosRuntime("com.apple.CoreSimulator.SimRuntime.watchOS-11-2")).toBe(false)
  43. expect(isIosRuntime("com.apple.CoreSimulator.SimRuntime.visionOS-2-2")).toBe(false)
  44. })
  45. test("simulator parsing drops non-iOS runtimes and unavailable devices", () => {
  46. const sims = parseSimulators(
  47. JSON.stringify({
  48. devices: {
  49. "com.apple.CoreSimulator.SimRuntime.tvOS-18-2": [
  50. { name: "Apple TV", udid: "TV-1", state: "Shutdown", isAvailable: true },
  51. ],
  52. "com.apple.CoreSimulator.SimRuntime.iOS-18-2": [
  53. { name: "iPhone 16", udid: "IOS-1", state: "Shutdown", isAvailable: true },
  54. { name: "iPhone 15", udid: "IOS-2", state: "Shutdown", availabilityError: "runtime unavailable" },
  55. ],
  56. },
  57. }),
  58. )
  59. expect(sims).toEqual([
  60. {
  61. name: "iPhone 16",
  62. udid: "IOS-1",
  63. state: "Shutdown",
  64. runtime: "com.apple.CoreSimulator.SimRuntime.iOS-18-2",
  65. available: true,
  66. },
  67. {
  68. name: "iPhone 15",
  69. udid: "IOS-2",
  70. state: "Shutdown",
  71. runtime: "com.apple.CoreSimulator.SimRuntime.iOS-18-2",
  72. available: false,
  73. },
  74. ])
  75. expect(sims.filter((item) => item.available)).toHaveLength(1)
  76. })
  77. async function withCwd(work: () => Promise<void>) {
  78. const old = process.cwd()
  79. const dir = await mkdtemp(path.join(tmpdir(), "ios-plugin-test-"))
  80. process.chdir(dir)
  81. try {
  82. await work()
  83. } finally {
  84. process.chdir(old)
  85. await rm(dir, { recursive: true, force: true })
  86. }
  87. }