package mac import ( "context" "errors" "os" "path/filepath" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // Recorded tool outputs (Xcode 16.2 on macOS 15) stand in for the Mac. var recorded = map[string]string{ "xcodebuild -version": "Xcode 16.2\nBuild version 16C5032a\n", "swift --version": "swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)\nTarget: arm64-apple-macosx15.0\n", "node --version": "v22.12.0\n", "srt --version": "0.0.81\n", "xcrun simctl list -j devices available": "", } func recordedExec(t *testing.T) Exec { raw, err := os.ReadFile("testdata/simctl-devices.json") if err != nil { t.Fatal(err) } return func(_ context.Context, name string, args ...string) (string, error) { key := strings.Join(append([]string{name}, args...), " ") if key == "xcrun simctl list -j devices available" { return string(raw), nil } out, ok := recorded[key] if !ok { return "", errors.New("not recorded: " + key) } return out, nil } } func TestCapabilitiesFromRecordedTools(t *testing.T) { c := Capabilities(context.Background(), recordedExec(t), Tools{Xcodebuild: "xcodebuild", Xcrun: "xcrun", Swift: "swift", Node: "node", SRT: "srt"}) if err := wire.ValidCapabilities(c); err != nil { t.Fatalf("%v: %+v", err, c) } if c.Xcode != (wire.Xcode{Version: "16.2", Build: "16C5032a"}) || c.Toolchains.Swift != "6.0.3" || c.Toolchains.Node != "v22.12.0" || c.Harness.SRT != "0.0.81" || c.Harness.Claude != "" { t.Fatalf("versions %+v", c) } if !slices.Equal(c.Tags, []string{wire.TagMacOS, wire.TagXcode, wire.TagIOSSimulator}) { t.Fatalf("tags %v", c.Tags) } var names []string for _, s := range c.Simulators { names = append(names, s.Runtime+" "+s.Name) } want := []string{"iOS 18.2 iPad Pro 13-inch (M4)", "iOS 18.2 iPhone 16", "iOS 18.2 iPhone 16 Pro", "iOS 18.2 iPhone SE (3rd generation)", "iOS 17.5 iPhone 15", "watchOS 11.2 Apple Watch Series 10 (46mm)"} if !slices.Equal(names, want) { t.Fatalf("simulators %v", names) } // Nothing found, nothing claimed: no Xcode, no xcode tag, no simulators. bare := Capabilities(context.Background(), recordedExec(t), Tools{}) if !slices.Equal(bare.Tags, []string{wire.TagMacOS}) || len(bare.Simulators) != 0 || bare.Xcode.Version != "" { t.Fatalf("bare %+v", bare) } } func TestParseXcodeNeedsBothLines(t *testing.T) { if (ParseXcode("Xcode 16.2\n")) != (wire.Xcode{}) || ParseXcode("xcode-select: error: tool 'xcodebuild' requires Xcode\n") != (wire.Xcode{}) { t.Fatal("partial output accepted") } } func woodstarTree(t *testing.T, files ...string) string { t.Helper() dir := t.TempDir() for _, f := range files { p := filepath.Join(dir, f) if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { t.Fatal(err) } if strings.HasSuffix(f, "/") { continue } if err := os.WriteFile(p, []byte("\n"), 0600); err != nil { t.Fatal(err) } } return dir } // No gate of its own and an Xcode project: xcodebuild test on its one // shared scheme, on the newest iPhone simulator, by UDID. func TestXcodeGate(t *testing.T) { sims := ParseSimulators(mustRead(t, "testdata/simctl-devices.json")) tree := woodstarTree(t, "Woodstar.xcodeproj/xcshareddata/xcschemes/Woodstar.xcscheme", "Woodstar/Swipe.swift") argv, ok, reason := XcodeGate(tree, sims) want := `exec /usr/bin/xcodebuild test -project 'Woodstar.xcodeproj' -scheme 'Woodstar' -destination 'platform=iOS Simulator,id=8F0A1E2B-3D4F-4A5B-9C6D-7E8F90A1B2C3' -derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache" -resultBundlePath "$TMPDIR/Results/checks.xcresult" CODE_SIGNING_ALLOWED=NO` if !ok || reason != "" || !slices.Equal(argv, []string{"/bin/bash", "-c", want}) { t.Fatalf("%v %v %q", argv, ok, reason) } // A workspace wins over its projects; the scheme named like it wins // among several; a quote in a name stays one word. tree = woodstarTree(t, "App's.xcworkspace/contents.xcworkspacedata", "App's.xcworkspace/xcshareddata/xcschemes/App's.xcscheme", "App.xcodeproj/xcshareddata/xcschemes/Widgets.xcscheme", "Pods/Pods.xcodeproj/") argv, ok, reason = XcodeGate(tree, sims) if !ok || reason != "" || !strings.Contains(argv[2], `-workspace 'App'\''s.xcworkspace' -scheme 'App'\''s'`) { t.Fatalf("%v %q", argv, reason) } for name, files := range map[string][]string{ "no shared scheme": {"App.xcodeproj/project.pbxproj"}, "two schemes": {"App.xcodeproj/xcshareddata/xcschemes/One.xcscheme", "App.xcodeproj/xcshareddata/xcschemes/Two.xcscheme"}, "two projects": {"A.xcodeproj/xcshareddata/xcschemes/A.xcscheme", "B.xcodeproj/xcshareddata/xcschemes/B.xcscheme"}, "two workspaces": {"A.xcworkspace/xcshareddata/xcschemes/A.xcscheme", "B.xcworkspace/xcshareddata/xcschemes/B.xcscheme"}, } { if argv, ok, reason := XcodeGate(woodstarTree(t, files...), sims); !ok || reason != "checks_unavailable" || argv != nil { t.Fatalf("%s: %v %v %q", name, argv, ok, reason) } } if _, ok, reason := XcodeGate(tree, nil); !ok || reason != "checks_unavailable" { t.Fatal("no simulator: gate chosen anyway") } if _, ok, _ := XcodeGate(woodstarTree(t, "Package.swift", "README.md"), sims); ok { t.Fatal("no project: gate synthesised") } } func mustRead(t *testing.T, name string) []byte { raw, err := os.ReadFile(name) if err != nil { t.Fatal(err) } return raw }