oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mac
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "slices"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// Recorded tool outputs (Xcode 16.2 on macOS 15) stand in for the Mac.
16var recorded = map[string]string{
17 "xcodebuild -version": "Xcode 16.2\nBuild version 16C5032a\n",
18 "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",
19 "node --version": "v22.12.0\n",
20 "srt --version": "0.0.81\n",
21 "xcrun simctl list -j devices available": "",
22}
23
24func recordedExec(t *testing.T) Exec {
25 raw, err := os.ReadFile("testdata/simctl-devices.json")
26 if err != nil {
27 t.Fatal(err)
28 }
29 return func(_ context.Context, name string, args ...string) (string, error) {
30 key := strings.Join(append([]string{name}, args...), " ")
31 if key == "xcrun simctl list -j devices available" {
32 return string(raw), nil
33 }
34 out, ok := recorded[key]
35 if !ok {
36 return "", errors.New("not recorded: " + key)
37 }
38 return out, nil
39 }
40}
41
42func TestCapabilitiesFromRecordedTools(t *testing.T) {
43 c := Capabilities(context.Background(), recordedExec(t), Tools{Xcodebuild: "xcodebuild", Xcrun: "xcrun", Swift: "swift", Node: "node", SRT: "srt"})
44 if err := wire.ValidCapabilities(c); err != nil {
45 t.Fatalf("%v: %+v", err, c)
46 }
47 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 != "" {
48 t.Fatalf("versions %+v", c)
49 }
50 if !slices.Equal(c.Tags, []string{wire.TagMacOS, wire.TagXcode, wire.TagIOSSimulator}) {
51 t.Fatalf("tags %v", c.Tags)
52 }
53 var names []string
54 for _, s := range c.Simulators {
55 names = append(names, s.Runtime+" "+s.Name)
56 }
57 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)"}
58 if !slices.Equal(names, want) {
59 t.Fatalf("simulators %v", names)
60 }
61 // Nothing found, nothing claimed: no Xcode, no xcode tag, no simulators.
62 bare := Capabilities(context.Background(), recordedExec(t), Tools{})
63 if !slices.Equal(bare.Tags, []string{wire.TagMacOS}) || len(bare.Simulators) != 0 || bare.Xcode.Version != "" {
64 t.Fatalf("bare %+v", bare)
65 }
66}
67
68func TestParseXcodeNeedsBothLines(t *testing.T) {
69 if (ParseXcode("Xcode 16.2\n")) != (wire.Xcode{}) || ParseXcode("xcode-select: error: tool 'xcodebuild' requires Xcode\n") != (wire.Xcode{}) {
70 t.Fatal("partial output accepted")
71 }
72}
73
74func woodstarTree(t *testing.T, files ...string) string {
75 t.Helper()
76 dir := t.TempDir()
77 for _, f := range files {
78 p := filepath.Join(dir, f)
79 if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
80 t.Fatal(err)
81 }
82 if strings.HasSuffix(f, "/") {
83 continue
84 }
85 if err := os.WriteFile(p, []byte("<Scheme/>\n"), 0600); err != nil {
86 t.Fatal(err)
87 }
88 }
89 return dir
90}
91
92// No gate of its own and an Xcode project: xcodebuild test on its one
93// shared scheme, on the newest iPhone simulator, by UDID.
94func TestXcodeGate(t *testing.T) {
95 sims := ParseSimulators(mustRead(t, "testdata/simctl-devices.json"))
96 tree := woodstarTree(t, "Woodstar.xcodeproj/xcshareddata/xcschemes/Woodstar.xcscheme", "Woodstar/Swipe.swift")
97 argv, ok, reason := XcodeGate(tree, sims)
98 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`
99 if !ok || reason != "" || !slices.Equal(argv, []string{"/bin/bash", "-c", want}) {
100 t.Fatalf("%v %v %q", argv, ok, reason)
101 }
102 // A workspace wins over its projects; the scheme named like it wins
103 // among several; a quote in a name stays one word.
104 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/")
105 argv, ok, reason = XcodeGate(tree, sims)
106 if !ok || reason != "" || !strings.Contains(argv[2], `-workspace 'App'\''s.xcworkspace' -scheme 'App'\''s'`) {
107 t.Fatalf("%v %q", argv, reason)
108 }
109 for name, files := range map[string][]string{
110 "no shared scheme": {"App.xcodeproj/project.pbxproj"},
111 "two schemes": {"App.xcodeproj/xcshareddata/xcschemes/One.xcscheme", "App.xcodeproj/xcshareddata/xcschemes/Two.xcscheme"},
112 "two projects": {"A.xcodeproj/xcshareddata/xcschemes/A.xcscheme", "B.xcodeproj/xcshareddata/xcschemes/B.xcscheme"},
113 "two workspaces": {"A.xcworkspace/xcshareddata/xcschemes/A.xcscheme", "B.xcworkspace/xcshareddata/xcschemes/B.xcscheme"},
114 } {
115 if argv, ok, reason := XcodeGate(woodstarTree(t, files...), sims); !ok || reason != "checks_unavailable" || argv != nil {
116 t.Fatalf("%s: %v %v %q", name, argv, ok, reason)
117 }
118 }
119 if _, ok, reason := XcodeGate(tree, nil); !ok || reason != "checks_unavailable" {
120 t.Fatal("no simulator: gate chosen anyway")
121 }
122 if _, ok, _ := XcodeGate(woodstarTree(t, "Package.swift", "README.md"), sims); ok {
123 t.Fatal("no project: gate synthesised")
124 }
125}
126
127func mustRead(t *testing.T, name string) []byte {
128 raw, err := os.ReadFile(name)
129 if err != nil {
130 t.Fatal(err)
131 }
132 return raw
133}
134