mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 11:07:10 +00:00
feat(ios): iOS e2e support — Kotlin Native SDK + simulator driver + test-ios (#38)
* feat(ios): add simulator management package * feat(testrun): add iOS platform path (simctl launch + direct TCP) * feat(cli): add --ios-device flag and IosDevice option * feat(sdk-ios): add Kotlin Native iOS SDK (TCP agent + POSIX socket + dispatch pauser) * feat(folio-ios): wire SanderlingIos.start() in MainViewController * feat(folio-ios): add test-ios justfile recipe * fix(sdk-ios): remove unavailable C macros; manual byte swap + no-cast warnings * fix(testrun): simctl-first launch order for iOS; Maestro init after SDK connects
This commit is contained in:
14 files changed
+567
-26
No files matched your search
@@ -0,0 +1,153 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type simDevice struct {
|
||||
UDID string `json:"udid"`
|
||||
State string `json:"state"`
|
||||
Name string `json:"name"`
|
||||
IsAvailable bool `json:"isAvailable"`
|
||||
}
|
||||
|
||||
type simctlDeviceList struct {
|
||||
Devices map[string][]simDevice `json:"devices"`
|
||||
}
|
||||
|
||||
func EnsureSimulator(ctx context.Context, deviceName string, stdout io.Writer) error {
|
||||
booted, err := bootedSimulator(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list booted simulators: %w", err)
|
||||
}
|
||||
if booted != nil {
|
||||
fmt.Fprintf(stdout, "using booted simulator: %s (%s)\n", booted.Name, booted.UDID)
|
||||
return nil
|
||||
}
|
||||
|
||||
available, err := availableSimulators(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list available simulators: %w", err)
|
||||
}
|
||||
|
||||
target, err := pickSimulator(deviceName, available)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(stdout, "booting simulator %q (%s)...\n", target.Name, target.UDID)
|
||||
if err := bootSimulator(ctx, target.UDID); err != nil {
|
||||
return fmt.Errorf("boot simulator %q: %w", target.UDID, err)
|
||||
}
|
||||
|
||||
if err := waitForSimulatorBoot(ctx, target.UDID, 60*time.Second); err != nil {
|
||||
return fmt.Errorf("wait for simulator boot: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(stdout, "simulator %q ready\n", target.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func LaunchApp(ctx context.Context, bundleID string, env map[string]string) error {
|
||||
args := []string{"simctl", "launch", "--terminate-running-process", "booted", bundleID}
|
||||
cmd := exec.CommandContext(ctx, "xcrun", args...)
|
||||
cmd.Env = os.Environ()
|
||||
for k, v := range env {
|
||||
cmd.Env = append(cmd.Env, "SIMCTL_CHILD_"+k+"="+v)
|
||||
}
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("launch %s: %w\n%s", bundleID, err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func bootedSimulator(ctx context.Context) (*simDevice, error) {
|
||||
out, err := exec.CommandContext(ctx, "xcrun", "simctl", "list", "devices", "booted", "--json").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list simctlDeviceList
|
||||
if err := json.Unmarshal(out, &list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, devices := range list.Devices {
|
||||
for _, d := range devices {
|
||||
if d.State == "Booted" {
|
||||
return &d, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func availableSimulators(ctx context.Context) ([]simDevice, error) {
|
||||
out, err := exec.CommandContext(ctx, "xcrun", "simctl", "list", "devices", "available", "--json").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list simctlDeviceList
|
||||
if err := json.Unmarshal(out, &list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []simDevice
|
||||
for _, devices := range list.Devices {
|
||||
for _, d := range devices {
|
||||
if d.IsAvailable {
|
||||
result = append(result, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func pickSimulator(deviceName string, available []simDevice) (*simDevice, error) {
|
||||
if deviceName != "" {
|
||||
for i, d := range available {
|
||||
if d.Name == deviceName || d.UDID == deviceName {
|
||||
return &available[i], nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("simulator %q not found among available simulators; run `xcrun simctl list devices available` to see options", deviceName)
|
||||
}
|
||||
|
||||
if len(available) == 0 {
|
||||
return nil, fmt.Errorf("no available iOS simulators found; create one in Xcode -> Window -> Devices and Simulators")
|
||||
}
|
||||
|
||||
for i, d := range available {
|
||||
if strings.HasPrefix(d.Name, "iPhone") {
|
||||
return &available[i], nil
|
||||
}
|
||||
}
|
||||
return &available[0], nil
|
||||
}
|
||||
|
||||
func bootSimulator(ctx context.Context, udid string) error {
|
||||
return exec.CommandContext(ctx, "xcrun", "simctl", "boot", udid).Run()
|
||||
}
|
||||
|
||||
func waitForSimulatorBoot(ctx context.Context, udid string, timeout time.Duration) error {
|
||||
deadline, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
booted, _ := bootedSimulator(deadline)
|
||||
if booted != nil && (booted.UDID == udid || udid == "") {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-deadline.Done():
|
||||
return deadline.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
-10
@@ -8,11 +8,13 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/priyanshujain/sanderling/internal/agent"
|
||||
"github.com/priyanshujain/sanderling/internal/android"
|
||||
"github.com/priyanshujain/sanderling/internal/bundler"
|
||||
"github.com/priyanshujain/sanderling/internal/ios"
|
||||
"github.com/priyanshujain/sanderling/internal/runner"
|
||||
"github.com/priyanshujain/sanderling/internal/trace"
|
||||
"github.com/priyanshujain/sanderling/internal/verifier"
|
||||
@@ -26,21 +28,27 @@ const (
|
||||
|
||||
// Options are the parameters for a single test pipeline run.
|
||||
type Options struct {
|
||||
Spec string
|
||||
BundleID string
|
||||
Platform string
|
||||
AVD string
|
||||
Duration time.Duration
|
||||
Seed int64
|
||||
Output string
|
||||
Spec string
|
||||
BundleID string
|
||||
Platform string
|
||||
AVD string
|
||||
IosDevice string
|
||||
Duration time.Duration
|
||||
Seed int64
|
||||
Output string
|
||||
}
|
||||
|
||||
// Execute runs the full test pipeline: bundle, connect SDK, verify properties.
|
||||
func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
||||
if options.Platform == "android" || options.Platform == "ios" {
|
||||
switch options.Platform {
|
||||
case "android":
|
||||
if err := android.EnsureDevice(ctx, options.AVD, stdout); err != nil {
|
||||
return err
|
||||
}
|
||||
case "ios":
|
||||
if err := ios.EnsureSimulator(ctx, options.IosDevice, stdout); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
aliases := map[string]string{}
|
||||
if specAPIPath := resolveSpecAPIPath(options.Spec); specAPIPath != "" {
|
||||
@@ -69,7 +77,8 @@ func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
||||
|
||||
var connection *agent.Conn
|
||||
|
||||
if options.Platform != "web" {
|
||||
switch options.Platform {
|
||||
case "android":
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return fmt.Errorf("agent listener: %w", err)
|
||||
@@ -114,7 +123,43 @@ func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
||||
defer connection.Close()
|
||||
hello := connection.Hello()
|
||||
fmt.Fprintf(stdout, "SDK connected: platform=%s app=%s sdk=%s\n", hello.Platform, hello.AppPackage, hello.Version)
|
||||
} else {
|
||||
|
||||
case "ios":
|
||||
// iOS simulator shares the Mac's loopback — no port forwarding needed.
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return fmt.Errorf("agent listener: %w", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
agentPort := listener.Addr().(*net.TCPAddr).Port
|
||||
|
||||
// Launch app via simctl with SANDERLING_PORT so the SDK can connect.
|
||||
if err := ios.LaunchApp(ctx, options.BundleID, map[string]string{
|
||||
"SANDERLING_PORT": strconv.Itoa(agentPort),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("launch app: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "iOS app launched with SANDERLING_PORT=%d; waiting for SDK (%.0fs timeout)\n", agentPort, sdkAcceptTimeout.Seconds())
|
||||
|
||||
agentServer := agent.NewServer(listener)
|
||||
acceptCtx, acceptCancel := context.WithTimeout(ctx, sdkAcceptTimeout)
|
||||
conn, acceptErr := agentServer.Accept(acceptCtx)
|
||||
acceptCancel()
|
||||
if acceptErr != nil {
|
||||
return fmt.Errorf("accept SDK: %w", acceptErr)
|
||||
}
|
||||
connection = conn
|
||||
defer connection.Close()
|
||||
hello := connection.Hello()
|
||||
fmt.Fprintf(stdout, "SDK connected: platform=%s app=%s sdk=%s\n", hello.Platform, hello.AppPackage, hello.Version)
|
||||
|
||||
// Initialize Maestro XCTest session after the app is running.
|
||||
// clearState=false avoids relaunching the already-connected app.
|
||||
if err := activeDriver.Launch(ctx, options.BundleID, false); err != nil {
|
||||
fmt.Fprintf(stdout, "warn: maestro launch: %v\n", err)
|
||||
}
|
||||
|
||||
case "web":
|
||||
fmt.Fprintln(stdout, "web mode: skipping SDK")
|
||||
if err := activeDriver.Launch(ctx, options.BundleID, false); err != nil {
|
||||
return fmt.Errorf("launch app: %w", err)
|
||||
|
||||
Reference in new issue
Block a user