@@ -51,6 +51,12 @@ type cliRunOptions struct {
5151 onStderr func (string ) bool
5252}
5353
54+ func init () {
55+ if os .Getenv ("GO_WANT_CLI_HELPER_PROCESS" ) == "1" {
56+ runCLIHelperProcess ()
57+ }
58+ }
59+
5460// TestCLIHelperProcess is the in-process re-entry point used by runCLI.
5561// When invoked with GO_WANT_CLI_HELPER_PROCESS=1, it builds a fresh *App
5662// and runs Execute() with the args passed through GO_CLI_HELPER_ARGS_JSON;
@@ -59,11 +65,24 @@ func TestCLIHelperProcess(t *testing.T) {
5965 if os .Getenv ("GO_WANT_CLI_HELPER_PROCESS" ) != "1" {
6066 return
6167 }
62- cliArgs := helperCLIArgs (t )
68+ cliArgs := helperCLIArgs ()
69+ if len (cliArgs ) == 0 {
70+ fmt .Fprintln (os .Stderr , "TestCLIHelperProcess: missing CLI args (GO_CLI_HELPER_ARGS_JSON was empty and no -- fallback args were present)" )
71+ os .Exit (64 )
72+ }
73+ executeCLIHelper (cliArgs )
74+ }
75+
76+ func runCLIHelperProcess () {
77+ cliArgs := helperCLIArgs ()
6378 if len (cliArgs ) == 0 {
6479 fmt .Fprintln (os .Stderr , "TestCLIHelperProcess: missing CLI args (GO_CLI_HELPER_ARGS_JSON was empty and no -- fallback args were present)" )
6580 os .Exit (64 )
6681 }
82+ executeCLIHelper (cliArgs )
83+ }
84+
85+ func executeCLIHelper (cliArgs []string ) {
6786 originalArgs := os .Args
6887 defer func () { os .Args = originalArgs }()
6988 os .Args = append ([]string {"agora" }, cliArgs ... )
@@ -91,12 +110,12 @@ func TestCLIHelperProcess(t *testing.T) {
91110 os .Exit (0 )
92111}
93112
94- func helperCLIArgs (t * testing.T ) []string {
95- t .Helper ()
113+ func helperCLIArgs () []string {
96114 if raw := os .Getenv ("GO_CLI_HELPER_ARGS_JSON" ); raw != "" {
97115 var args []string
98116 if err := json .Unmarshal ([]byte (raw ), & args ); err != nil {
99- t .Fatalf ("invalid GO_CLI_HELPER_ARGS_JSON: %v" , err )
117+ fmt .Fprintf (os .Stderr , "TestCLIHelperProcess: invalid GO_CLI_HELPER_ARGS_JSON: %v\n " , err )
118+ os .Exit (64 )
100119 }
101120 return args
102121 }
@@ -116,7 +135,7 @@ func helperCLIArgs(t *testing.T) []string {
116135// URL the moment we see it).
117136func runCLI (t * testing.T , args []string , options cliRunOptions ) cliResult {
118137 t .Helper ()
119- cmd := exec .Command (os .Args [0 ], "-test.run=TestCLIHelperProcess" )
138+ cmd := exec .Command (os .Args [0 ])
120139 if options .workdir != "" {
121140 cmd .Dir = options .workdir
122141 }
0 commit comments