mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 11:07:10 +00:00
refactor: code organization cleanup (#35)
* chore: fix gitignore + decisions doc after web->inspect-ui rename Update web/ references to inspect-ui/ in .gitignore and Makefile. Add decisions.md tracking architectural decisions from code-org discussion. * refactor: rename pkg/spec-api to pkg/spec Aligns the directory name with the npm package name @sanderling/spec. Updates Makefile, package.json directory field, and resolveSpecAPIPath. * refactor(verifier): split bindings.go into types.go + bindings.go Move shared public types (Action, ActionKind, LogEntry, Exception) to types.go. bindings.go retains internal JS runtime wiring only. * refactor(inspect): split runs.go into runs.go, runs_cache.go, runs_decode.go runs.go: types (RunSummary, StepSummary, RunDetail, Run) and Scan. runs_cache.go: Cache type, Open/Step/Detail methods, parseRun, scanSteps. runs_decode.go: readMeta, tallyTrace, decodeStepSummary, validRunID. * refactor: move android_env.go to internal/android/ Extracts Android device/AVD/adb logic into internal/android package. Exports EnsureDevice, AdbReverse, AdbReverseRemove, EnvWithAndroidPlatformTools, AdbBinary. Moves tests to internal/android/android_test.go. cmd/sanderling becomes a thin caller. * refactor: extract test pipeline to internal/testrun/ runTestPipeline logic moves to testrun.Execute. buildDriver, resolveSpecAPIPath, pickFreePort, and the progress logger move to internal/testrun/. cmd/sanderling/test_run.go becomes a thin adapter. Tests follow their code. * ci: update workflow paths after pkg/spec-api -> pkg/spec rename
This commit is contained in:
32 files changed
+870
-745
No files matched your search
@@ -1,235 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ensureDevice makes sure an Android device is ready for adb commands.
|
||||
// Resolution order:
|
||||
// - if an adb device is already online, use it;
|
||||
// - else if avdName is set, validate and boot it;
|
||||
// - else if exactly one AVD exists locally, boot it;
|
||||
// - else fail with a helpful message listing the available AVDs.
|
||||
func ensureDevice(ctx context.Context, avdName string, stdout io.Writer) error {
|
||||
devices, err := listAdbDevices(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list adb devices: %w", err)
|
||||
}
|
||||
if len(devices) > 0 {
|
||||
fmt.Fprintf(stdout, "using connected device: %s\n", devices[0])
|
||||
return nil
|
||||
}
|
||||
avds, err := listAVDs(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list AVDs: %w", err)
|
||||
}
|
||||
target, err := pickAVD(avdName, avds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdout, "booting AVD %q...\n", target)
|
||||
if err := bootAVD(ctx, target); err != nil {
|
||||
return fmt.Errorf("boot AVD %q: %w", target, err)
|
||||
}
|
||||
if err := waitForBoot(ctx, 180*time.Second); err != nil {
|
||||
return fmt.Errorf("wait for AVD boot: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "AVD %q ready\n", target)
|
||||
return nil
|
||||
}
|
||||
|
||||
func pickAVD(requested string, available []string) (string, error) {
|
||||
if requested != "" {
|
||||
if !slices.Contains(available, requested) {
|
||||
return "", fmt.Errorf("AVD %q does not exist (available: %s)", requested, strings.Join(available, ", "))
|
||||
}
|
||||
return requested, nil
|
||||
}
|
||||
switch len(available) {
|
||||
case 0:
|
||||
return "", fmt.Errorf("no android device connected and no AVD found; create one in Android Studio or `avdmanager create avd`")
|
||||
case 1:
|
||||
return available[0], nil
|
||||
default:
|
||||
return "", fmt.Errorf("no android device connected and multiple AVDs available (%s); pick one with --avd", strings.Join(available, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
func listAdbDevices(ctx context.Context) ([]string, error) {
|
||||
adb, err := adbBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output, err := exec.CommandContext(ctx, adb, "devices").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseAdbDevices(string(output)), nil
|
||||
}
|
||||
|
||||
func parseAdbDevices(output string) []string {
|
||||
var serials []string
|
||||
scanner := bufio.NewScanner(strings.NewReader(output))
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "List of devices") {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 2 && fields[1] == "device" {
|
||||
serials = append(serials, fields[0])
|
||||
}
|
||||
}
|
||||
return serials
|
||||
}
|
||||
|
||||
func listAVDs(ctx context.Context) ([]string, error) {
|
||||
emulator, err := emulatorBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output, err := exec.CommandContext(ctx, emulator, "-list-avds").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseAVDList(string(output)), nil
|
||||
}
|
||||
|
||||
func adbBinary() (string, error) { return findAndroidTool("adb", "platform-tools") }
|
||||
func emulatorBinary() (string, error) { return findAndroidTool("emulator", "emulator") }
|
||||
|
||||
// findAndroidTool locates a binary from the Android SDK. It checks PATH,
|
||||
// then $ANDROID_HOME/<subdir>/<name> and $ANDROID_SDK_ROOT/<subdir>/<name>,
|
||||
// then the canonical install locations used by Android Studio and Homebrew.
|
||||
// Returns a descriptive error when nothing works, so the user knows to set
|
||||
// $ANDROID_HOME instead of getting a raw "executable file not found".
|
||||
func findAndroidTool(name, subdir string) (string, error) {
|
||||
if path, err := exec.LookPath(name); err == nil {
|
||||
return path, nil
|
||||
}
|
||||
var tried []string
|
||||
for _, root := range androidSDKCandidates() {
|
||||
candidate := filepath.Join(root, subdir, name)
|
||||
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
||||
return candidate, nil
|
||||
}
|
||||
tried = append(tried, candidate)
|
||||
}
|
||||
return "", fmt.Errorf("could not locate %q: not on PATH and not under any known Android SDK root (set $ANDROID_HOME to point at your SDK; tried %v)", name, tried)
|
||||
}
|
||||
|
||||
func androidSDKCandidates() []string {
|
||||
var roots []string
|
||||
seen := map[string]bool{}
|
||||
addRoot := func(path string) {
|
||||
if path == "" || seen[path] {
|
||||
return
|
||||
}
|
||||
seen[path] = true
|
||||
roots = append(roots, path)
|
||||
}
|
||||
addRoot(os.Getenv("ANDROID_HOME"))
|
||||
addRoot(os.Getenv("ANDROID_SDK_ROOT"))
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
addRoot(filepath.Join(home, "Library", "Android", "sdk"))
|
||||
addRoot(filepath.Join(home, "Android", "Sdk"))
|
||||
}
|
||||
addRoot("/opt/homebrew/share/android-commandlinetools")
|
||||
addRoot("/usr/local/share/android-commandlinetools")
|
||||
return roots
|
||||
}
|
||||
|
||||
func parseAVDList(output string) []string {
|
||||
var avds []string
|
||||
scanner := bufio.NewScanner(strings.NewReader(output))
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "INFO") {
|
||||
continue
|
||||
}
|
||||
avds = append(avds, line)
|
||||
}
|
||||
return avds
|
||||
}
|
||||
|
||||
func bootAVD(_ context.Context, name string) error {
|
||||
emulator, err := emulatorBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
command := exec.Command(emulator, "-avd", name, "-no-snapshot-save", "-no-audio", "-no-boot-anim")
|
||||
if err := command.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
go func() { _ = command.Wait() }()
|
||||
return nil
|
||||
}
|
||||
|
||||
func waitForBoot(ctx context.Context, timeout time.Duration) error {
|
||||
deadline, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
if completed, _ := bootCompleted(deadline); completed {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-deadline.Done():
|
||||
return deadline.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// envWithAndroidPlatformTools returns env with the directory containing adb
|
||||
// prepended to PATH, so child processes (the sidecar) can invoke adb even
|
||||
// when the user hasn't set up their shell PATH.
|
||||
func envWithAndroidPlatformTools(env []string) []string {
|
||||
adb, err := adbBinary()
|
||||
if err != nil {
|
||||
return env
|
||||
}
|
||||
adbDir := filepath.Dir(adb)
|
||||
result := make([]string, 0, len(env))
|
||||
found := false
|
||||
for _, entry := range env {
|
||||
if current, ok := strings.CutPrefix(entry, "PATH="); ok {
|
||||
if !pathContains(current, adbDir) {
|
||||
entry = "PATH=" + adbDir + string(os.PathListSeparator) + current
|
||||
}
|
||||
found = true
|
||||
}
|
||||
result = append(result, entry)
|
||||
}
|
||||
if !found {
|
||||
result = append(result, "PATH="+adbDir)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func pathContains(path, directory string) bool {
|
||||
return slices.Contains(strings.Split(path, string(os.PathListSeparator)), directory)
|
||||
}
|
||||
|
||||
func bootCompleted(ctx context.Context) (bool, error) {
|
||||
adb, err := adbBinary()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
output, err := exec.CommandContext(ctx, adb, "shell", "getprop", "sys.boot_completed").Output()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return strings.TrimSpace(string(output)) == "1", nil
|
||||
}
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseAdbDevices_OnlineOnly(t *testing.T) {
|
||||
output := `List of devices attached
|
||||
emulator-5554 device
|
||||
emulator-5556 offline
|
||||
physical-abc device
|
||||
`
|
||||
|
||||
got := parseAdbDevices(output)
|
||||
want := []string{"emulator-5554", "physical-abc"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAdbDevices_Empty(t *testing.T) {
|
||||
output := "List of devices attached\n\n"
|
||||
|
||||
got := parseAdbDevices(output)
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("got %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAVDList_DropsInfoLines(t *testing.T) {
|
||||
output := `INFO | Storing crashdata in: /tmp/x
|
||||
Medium_Phone_API_36.0
|
||||
sanderling_test
|
||||
`
|
||||
|
||||
got := parseAVDList(output)
|
||||
want := []string{"Medium_Phone_API_36.0", "sanderling_test"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickAVD_ExplicitName(t *testing.T) {
|
||||
got, err := pickAVD("Pixel_7", []string{"Pixel_7", "sanderling_test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "Pixel_7" {
|
||||
t.Fatalf("got %q, want Pixel_7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickAVD_ExplicitMissing(t *testing.T) {
|
||||
_, err := pickAVD("Nope", []string{"Pixel_7"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickAVD_SingleAvailable(t *testing.T) {
|
||||
got, err := pickAVD("", []string{"sanderling_test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "sanderling_test" {
|
||||
t.Fatalf("got %q, want sanderling_test", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickAVD_AmbiguousWithoutHint(t *testing.T) {
|
||||
_, err := pickAVD("", []string{"a", "b"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error when multiple AVDs and no --avd")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickAVD_NoneAvailable(t *testing.T) {
|
||||
_, err := pickAVD("", nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when no AVDs exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathContains(t *testing.T) {
|
||||
path := "/usr/bin:/opt/tools:/usr/local/bin"
|
||||
if !pathContains(path, "/opt/tools") {
|
||||
t.Error("expected /opt/tools in PATH")
|
||||
}
|
||||
if pathContains(path, "/nope") {
|
||||
t.Error("did not expect /nope in PATH")
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// newProgressLogger wires a slog.Logger that prints user-facing progress
|
||||
// lines to the CLI's stdout stream. Info messages render as
|
||||
// "msg key=value ..." to match the prose style of other CLI prints;
|
||||
// warnings and errors get a "warn:" / "error:" prefix so they stand
|
||||
// out in the same stream.
|
||||
func newProgressLogger(writer io.Writer) *slog.Logger {
|
||||
return slog.New(&progressHandler{writer: writer, level: slog.LevelInfo})
|
||||
}
|
||||
|
||||
type progressHandler struct {
|
||||
writer io.Writer
|
||||
level slog.Level
|
||||
}
|
||||
|
||||
func (h *progressHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||
return level >= h.level
|
||||
}
|
||||
|
||||
func (h *progressHandler) Handle(_ context.Context, record slog.Record) error {
|
||||
var builder strings.Builder
|
||||
if record.Level >= slog.LevelWarn {
|
||||
fmt.Fprintf(&builder, "%s: ", strings.ToLower(record.Level.String()))
|
||||
}
|
||||
builder.WriteString(record.Message)
|
||||
record.Attrs(func(attr slog.Attr) bool {
|
||||
fmt.Fprintf(&builder, " %s=%s", attr.Key, formatAttrValue(attr.Value))
|
||||
return true
|
||||
})
|
||||
builder.WriteByte('\n')
|
||||
_, err := io.WriteString(h.writer, builder.String())
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *progressHandler) WithAttrs(_ []slog.Attr) slog.Handler { return h }
|
||||
func (h *progressHandler) WithGroup(_ string) slog.Handler { return h }
|
||||
|
||||
func formatAttrValue(value slog.Value) string {
|
||||
if value.Kind() == slog.KindString {
|
||||
return fmt.Sprintf("%q", value.String())
|
||||
}
|
||||
return value.String()
|
||||
}
|
||||
+10
-279
@@ -2,288 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/priyanshujain/sanderling/internal/agent"
|
||||
"github.com/priyanshujain/sanderling/internal/bundler"
|
||||
"github.com/priyanshujain/sanderling/internal/driver"
|
||||
"github.com/priyanshujain/sanderling/internal/driver/chrome"
|
||||
driverSidecar "github.com/priyanshujain/sanderling/internal/driver/sidecar"
|
||||
"github.com/priyanshujain/sanderling/internal/runner"
|
||||
"github.com/priyanshujain/sanderling/internal/sidecar"
|
||||
"github.com/priyanshujain/sanderling/internal/trace"
|
||||
"github.com/priyanshujain/sanderling/internal/verifier"
|
||||
)
|
||||
|
||||
const (
|
||||
socketName = "sanderling-agent"
|
||||
sidecarStartupTimeout = 30 * time.Second
|
||||
sdkAcceptTimeout = 60 * time.Second
|
||||
"github.com/priyanshujain/sanderling/internal/testrun"
|
||||
)
|
||||
|
||||
func runTestPipeline(ctx context.Context, options testOptions, stdout io.Writer) error {
|
||||
if options.platform == "android" || options.platform == "ios" {
|
||||
if err := ensureDevice(ctx, options.avd, stdout); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
aliases := map[string]string{}
|
||||
if specApiPath := resolveSpecAPIPath(options.spec); specApiPath != "" {
|
||||
aliases["@sanderling/spec"] = specApiPath
|
||||
base := filepath.Dir(specApiPath)
|
||||
aliases["@sanderling/spec/defaults/properties"] = filepath.Join(base, "defaults/properties.ts")
|
||||
}
|
||||
bundle, err := bundler.Bundle(bundler.Options{
|
||||
EntryFile: options.spec,
|
||||
Defines: map[string]string{
|
||||
"SANDERLING_TEST_PHONE": os.Getenv("SANDERLING_TEST_PHONE"),
|
||||
"SANDERLING_TEST_OTP": os.Getenv("SANDERLING_TEST_OTP"),
|
||||
},
|
||||
Aliases: aliases,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("bundle spec: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "bundled spec: %d bytes (sha256=%s)\n", len(bundle.JavaScript), bundle.SHA256[:12])
|
||||
|
||||
activeDriver, cleanup, err := buildDriver(ctx, options, stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
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
|
||||
|
||||
if options.platform != "web" {
|
||||
if err := adbReverse(socketName, agentPort); err != nil {
|
||||
return fmt.Errorf("adb reverse: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := adbReverseRemove(socketName); err != nil {
|
||||
fmt.Fprintf(stdout, "warning: adb reverse cleanup: %v\n", err)
|
||||
}
|
||||
}()
|
||||
fmt.Fprintf(stdout, "forwarded localabstract:%s -> tcp:%d\n", socketName, agentPort)
|
||||
}
|
||||
|
||||
agentServer := agent.NewServer(listener)
|
||||
|
||||
type acceptResult struct {
|
||||
connection *agent.Conn
|
||||
err error
|
||||
}
|
||||
acceptChannel := make(chan acceptResult, 1)
|
||||
go func() {
|
||||
acceptCtx, cancel := context.WithTimeout(ctx, sdkAcceptTimeout)
|
||||
defer cancel()
|
||||
connection, acceptErr := agentServer.Accept(acceptCtx)
|
||||
acceptChannel <- acceptResult{connection: connection, err: acceptErr}
|
||||
}()
|
||||
|
||||
if err := activeDriver.Launch(ctx, options.bundleID, false); err != nil {
|
||||
return fmt.Errorf("launch app: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "launched %s; waiting for SDK to connect (%.0fs timeout)\n", options.bundleID, sdkAcceptTimeout.Seconds())
|
||||
|
||||
result := <-acceptChannel
|
||||
if result.err != nil {
|
||||
return fmt.Errorf("accept SDK: %w", result.err)
|
||||
}
|
||||
connection := result.connection
|
||||
defer connection.Close()
|
||||
hello := connection.Hello()
|
||||
fmt.Fprintf(stdout, "SDK connected: platform=%s app=%s sdk=%s\n", hello.Platform, hello.AppPackage, hello.Version)
|
||||
|
||||
seed := options.seed
|
||||
if seed == 0 {
|
||||
seed = time.Now().UnixNano()
|
||||
}
|
||||
verifierInstance, err := verifier.New(verifier.WithRand(rand.New(rand.NewPCG(uint64(seed), 0))))
|
||||
if err != nil {
|
||||
return fmt.Errorf("verifier: %w", err)
|
||||
}
|
||||
if err := verifierInstance.Load(string(bundle.JavaScript)); err != nil {
|
||||
return fmt.Errorf("load spec: %w", err)
|
||||
}
|
||||
fmt.Fprintln(stdout, "spec loaded into verifier")
|
||||
|
||||
runDirectory := filepath.Join(options.output, time.Now().UTC().Format("20060102-150405"))
|
||||
traceWriter, err := trace.NewWriter(runDirectory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("trace writer: %w", err)
|
||||
}
|
||||
defer traceWriter.Close()
|
||||
meta := trace.Meta{
|
||||
Seed: seed,
|
||||
SpecPath: options.spec,
|
||||
BundleSHA256: bundle.SHA256,
|
||||
Platform: options.platform,
|
||||
BundleID: options.bundleID,
|
||||
StartedAt: time.Now().UTC(),
|
||||
SanderlingVersion: "0.0.1",
|
||||
}
|
||||
if err := traceWriter.WriteMeta(meta); err != nil {
|
||||
return fmt.Errorf("trace meta: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
endedAt := time.Now().UTC()
|
||||
meta.EndedAt = &endedAt
|
||||
_ = traceWriter.WriteMeta(meta)
|
||||
}()
|
||||
fmt.Fprintf(stdout, "trace dir: %s\n", runDirectory)
|
||||
|
||||
fmt.Fprintf(stdout, "running for %s (seed=%d)\n", options.duration, seed)
|
||||
summary, err := runner.Run(ctx, runner.Options{
|
||||
Duration: options.duration,
|
||||
SnapshotTimeout: 5 * time.Second,
|
||||
IdleTimeout: 1 * time.Second,
|
||||
BundleID: options.bundleID,
|
||||
Connection: connection,
|
||||
Driver: activeDriver,
|
||||
Verifier: verifierInstance,
|
||||
TraceWriter: traceWriter,
|
||||
Logger: newProgressLogger(stdout),
|
||||
})
|
||||
|
||||
terminateCtx, terminateCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
_ = activeDriver.Terminate(terminateCtx)
|
||||
terminateCancel()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("runner: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(stdout, "\nrun complete: %d steps in %s\n", summary.Steps, summary.EndTime.Sub(summary.StartTime).Round(time.Millisecond))
|
||||
if len(summary.Violations) == 0 {
|
||||
fmt.Fprintln(stdout, "no violations.")
|
||||
} else {
|
||||
fmt.Fprintf(stdout, "%d violation record(s):\n", len(summary.Violations))
|
||||
for _, violation := range summary.Violations {
|
||||
fmt.Fprintf(stdout, " step %d: %v\n", violation.StepIndex, violation.Properties)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildDriver creates the appropriate DeviceDriver for the platform and returns
|
||||
// a cleanup function. For web, ChromeDriver is used directly; for android/ios
|
||||
// the JVM sidecar is extracted, spawned, and dialed.
|
||||
func buildDriver(ctx context.Context, options testOptions, stdout io.Writer) (driver.DeviceDriver, func(), error) {
|
||||
if options.platform == "web" {
|
||||
d := chrome.New()
|
||||
return d, func() { _ = d.Terminate(context.Background()) }, nil
|
||||
}
|
||||
|
||||
sidecarDirectory := filepath.Join(os.TempDir(), "sanderling-sidecar")
|
||||
jarPath, err := sidecar.Extract(sidecarDirectory)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("extract sidecar: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "sidecar JAR: %s (size=%d)\n", jarPath, sidecar.EmbeddedSize())
|
||||
|
||||
sidecarPort, err := pickFreePort()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
sidecarCommand := exec.CommandContext(ctx, "java", "-jar", jarPath,
|
||||
"--port", strconv.Itoa(sidecarPort),
|
||||
"--platform", options.platform,
|
||||
)
|
||||
sidecarCommand.Stdout = stdout
|
||||
sidecarCommand.Stderr = stdout
|
||||
sidecarCommand.Env = envWithAndroidPlatformTools(os.Environ())
|
||||
if err := sidecarCommand.Start(); err != nil {
|
||||
return nil, nil, fmt.Errorf("spawn sidecar: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "sidecar pid=%d listening on 127.0.0.1:%d\n", sidecarCommand.Process.Pid, sidecarPort)
|
||||
|
||||
driverClient, err := driverSidecar.Dial(fmt.Sprintf("127.0.0.1:%d", sidecarPort))
|
||||
if err != nil {
|
||||
_ = sidecarCommand.Process.Kill()
|
||||
return nil, nil, fmt.Errorf("dial sidecar: %w", err)
|
||||
}
|
||||
healthCtx, healthCancel := context.WithTimeout(ctx, sidecarStartupTimeout)
|
||||
if err := driverClient.WaitForHealth(healthCtx, 250*time.Millisecond); err != nil {
|
||||
healthCancel()
|
||||
_ = sidecarCommand.Process.Kill()
|
||||
_ = driverClient.Close()
|
||||
return nil, nil, fmt.Errorf("sidecar health check: %w", err)
|
||||
}
|
||||
healthCancel()
|
||||
fmt.Fprintln(stdout, "sidecar is healthy")
|
||||
|
||||
cleanup := func() {
|
||||
_ = driverClient.Close()
|
||||
if sidecarCommand.Process != nil {
|
||||
_ = sidecarCommand.Process.Kill()
|
||||
}
|
||||
}
|
||||
return driverClient, cleanup, nil
|
||||
}
|
||||
|
||||
// resolveSpecAPIPath returns the path to pkg/spec-api/src/index.ts inside
|
||||
// a sanderling source checkout, searched upward from the spec file and the cwd.
|
||||
// Returns "" when not found, in which case esbuild resolves @sanderling/spec via
|
||||
// node_modules the way a downstream user's project would.
|
||||
func resolveSpecAPIPath(specPath string) string {
|
||||
candidates := []string{}
|
||||
if absoluteSpec, err := filepath.Abs(specPath); err == nil {
|
||||
directory := filepath.Dir(absoluteSpec)
|
||||
for {
|
||||
candidates = append(candidates, filepath.Join(directory, "pkg/spec-api/src/index.ts"))
|
||||
parent := filepath.Dir(directory)
|
||||
if parent == directory {
|
||||
break
|
||||
}
|
||||
directory = parent
|
||||
}
|
||||
}
|
||||
if cwd, err := os.Getwd(); err == nil {
|
||||
candidates = append(candidates, filepath.Join(cwd, "pkg/spec-api/src/index.ts"))
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
if _, err := os.Stat(candidate); err == nil {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func pickFreePort() (int, error) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer listener.Close()
|
||||
return listener.Addr().(*net.TCPAddr).Port, nil
|
||||
}
|
||||
|
||||
func adbReverse(socket string, port int) error {
|
||||
adb, err := adbBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
command := exec.Command(adb, "reverse", "localabstract:"+socket, fmt.Sprintf("tcp:%d", port))
|
||||
return command.Run()
|
||||
}
|
||||
|
||||
func adbReverseRemove(socket string) error {
|
||||
adb, err := adbBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return exec.Command(adb, "reverse", "--remove", "localabstract:"+socket).Run()
|
||||
return testrun.Execute(ctx, testrun.Options{
|
||||
Spec: options.spec,
|
||||
BundleID: options.bundleID,
|
||||
Platform: options.platform,
|
||||
AVD: options.avd,
|
||||
Duration: options.duration,
|
||||
Seed: options.seed,
|
||||
Output: options.output,
|
||||
}, stdout)
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveSpecAPIPath_FindsUpwardSibling(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
apiPath := filepath.Join(root, "pkg", "spec-api", "src", "index.ts")
|
||||
if err := os.MkdirAll(filepath.Dir(apiPath), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(apiPath, []byte("export {}"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
specPath := filepath.Join(root, "examples", "app", "spec.ts")
|
||||
if err := os.MkdirAll(filepath.Dir(specPath), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(specPath, []byte(""), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := resolveSpecAPIPath(specPath)
|
||||
if got != apiPath {
|
||||
t.Fatalf("got %q, want %q", got, apiPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSpecAPIPath_ReturnsEmptyWhenMissing(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
specPath := filepath.Join(root, "spec.ts")
|
||||
if err := os.WriteFile(specPath, []byte(""), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chdir(cwd) })
|
||||
if err := os.Chdir(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := resolveSpecAPIPath(specPath)
|
||||
if got != "" {
|
||||
t.Fatalf("got %q, want empty (no sanderling source tree reachable)", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user