Files
sanderling/cmd/sanderling/main.go
T
pj 039c8ab226 feat(doctor): per-platform check sets + --platform flag
Replaces unconditional defaultDoctorChecks with doctorChecksFor(platform);
web-only users no longer see scary FAILs for adb/emulator/java/sidecar.
2026-04-27 18:35:17 +07:00

125 lines
3.6 KiB
Go

package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"time"
)
// Version is stamped at build time via goreleaser ldflags.
// Default "dev" marks untagged local builds.
var Version = "dev"
type testOptions struct {
spec string
bundleID string
platform string
avd string
iosDevice string
duration time.Duration
seed int64
output string
clearData bool
}
const topUsage = `sanderling is a property-based UI fuzzer for mobile apps.
Usage:
sanderling <command> [flags]
Commands:
test Run a spec against an app for a fixed duration.
inspect Serve a local web UI for browsing runs/.
doctor Check that the host environment is ready to run sanderling.
version Print the sanderling version.
Run "sanderling <command> -h" for command-specific flags.
`
func parseTestArgs(args []string, stderr io.Writer) (testOptions, error) {
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
flagSet.SetOutput(stderr)
var options testOptions
flagSet.StringVar(&options.spec, "spec", "", "path to the TypeScript spec (required)")
flagSet.StringVar(&options.bundleID, "bundle-id", "", "target app bundle ID (required)")
flagSet.StringVar(&options.platform, "platform", "android", "target platform: android, ios, web")
flagSet.StringVar(&options.avd, "avd", "", "Android AVD name to boot if no device is connected")
flagSet.StringVar(&options.iosDevice, "ios-device", "", "iOS simulator name or UDID to boot if none is running")
flagSet.DurationVar(&options.duration, "duration", 5*time.Minute, "total test duration")
flagSet.Int64Var(&options.seed, "seed", 0, "RNG seed (0 = random)")
flagSet.StringVar(&options.output, "output", "./runs", "output directory for traces")
flagSet.BoolVar(&options.clearData, "clear-data", false, "clear app data before launching so the run starts from a fresh install")
if err := flagSet.Parse(args); err != nil {
return testOptions{}, err
}
if options.spec == "" {
return testOptions{}, errors.New("--spec is required")
}
if options.bundleID == "" {
return testOptions{}, errors.New("--bundle-id is required")
}
switch options.platform {
case "android", "ios", "web":
default:
return testOptions{}, fmt.Errorf("unsupported platform: %q (android, ios, web)", options.platform)
}
return options, nil
}
func runTest(options testOptions, stdout io.Writer) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
return runTestPipeline(ctx, options, stdout)
}
func runDoctor(args []string, stdout io.Writer) error {
options, err := parseDoctorArgs(args)
if err != nil {
return err
}
checks := doctorChecksFor(options.platform)
if checks == nil {
return fmt.Errorf("no checks for platform %q", options.platform)
}
return runDoctorChecks(context.Background(), checks, stdout)
}
func run(args []string, stdout, stderr io.Writer) error {
if len(args) < 2 || args[1] == "-h" || args[1] == "--help" || args[1] == "help" {
fmt.Fprint(stdout, topUsage)
return nil
}
switch args[1] {
case "test":
options, err := parseTestArgs(args[2:], stderr)
if err != nil {
return err
}
return runTest(options, stdout)
case "inspect":
options, err := parseInspectArgs(args[2:], stderr)
if err != nil {
return err
}
return runInspect(options, stdout)
case "doctor":
return runDoctor(args[2:], stdout)
case "version", "-v", "--version":
fmt.Fprintln(stdout, Version)
return nil
default:
return fmt.Errorf("unknown command: %q (try 'sanderling help')", args[1])
}
}
func main() {
if err := run(os.Args, os.Stdout, os.Stderr); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}