diff --git a/hrp/cmd/adb/init.go b/hrp/cmd/adb/init.go index 9025ef70..2914cc64 100644 --- a/hrp/cmd/adb/init.go +++ b/hrp/cmd/adb/init.go @@ -3,9 +3,8 @@ package adb import "github.com/spf13/cobra" var androidRootCmd = &cobra.Command{ - Use: "adb", - Short: "simple utils for android device management", - PersistentPreRun: func(cmd *cobra.Command, args []string) {}, + Use: "adb", + Short: "simple utils for android device management", } func Init(rootCmd *cobra.Command) { diff --git a/hrp/cmd/ios/init.go b/hrp/cmd/ios/init.go index a4110b64..e66898a9 100644 --- a/hrp/cmd/ios/init.go +++ b/hrp/cmd/ios/init.go @@ -3,9 +3,8 @@ package ios import "github.com/spf13/cobra" var iosRootCmd = &cobra.Command{ - Use: "ios", - Short: "simple utils for ios device management", - PersistentPreRun: func(cmd *cobra.Command, args []string) {}, + Use: "ios", + Short: "simple utils for ios device management", } func Init(rootCmd *cobra.Command) { diff --git a/hrp/cmd/ios/xctest.go b/hrp/cmd/ios/xctest.go new file mode 100644 index 00000000..9300c6e1 --- /dev/null +++ b/hrp/cmd/ios/xctest.go @@ -0,0 +1,67 @@ +package ios + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/pkg/errors" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/v4/hrp/internal/uixt" +) + +var xctestCmd = &cobra.Command{ + Use: "xctest", + Short: "run xctest", + RunE: func(cmd *cobra.Command, args []string) error { + if bundleID == "" { + return fmt.Errorf("bundleID is required") + } + + devices, err := uixt.IOSDevices(udid) + if err != nil { + return err + } + if len(devices) == 0 { + fmt.Println("no ios device found") + os.Exit(1) + } + if len(devices) > 1 { + return fmt.Errorf("multiple devices found, please specify udid") + } + device := devices[0] + + log.Info().Str("bundleID", bundleID).Msg("run xctest") + out, cancel, err := device.XCTest(bundleID) + if err != nil { + return errors.Wrap(err, "run xctest failed") + } + + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGTERM, syscall.SIGINT) + + // print xctest running logs + go func() { + for s := range out { + fmt.Print(s) + } + done <- os.Interrupt + }() + + <-done + cancel() + + return nil + }, +} + +var bundleID string + +func init() { + xctestCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid") + xctestCmd.Flags().StringVarP(&bundleID, "bundleID", "b", "", "specify ios bundleID") + iosRootCmd.AddCommand(xctestCmd) +}