@@ -191,42 +191,43 @@ func newKcpServer(t TestingT, cfg Config) (*kcpServer, error) {
191191 return s , nil
192192}
193193
194- // StartKcpCommand returns the string tokens required to start kcp in
195- // the currently configured mode (direct or via `go run`).
196- func StartKcpCommand (identity string ) []string {
197- command := Command ("kcp" , identity )
198- return append (command , "start" )
194+ // StartKcpCommand returns the work dir and string tokens required to
195+ // start kcp in the currently configured mode (direct or via `go run`).
196+ func StartKcpCommand (identity string ) ( string , []string ) {
197+ workdir , command := Command ("kcp" , identity )
198+ return workdir , append (command , "start" )
199199}
200200
201- // Command returns the string tokens required to start
202- // the given executable in the currently configured mode (direct or
203- // via `go run`).
204- func Command (executableName , identity string ) []string {
205- if env .RunDelveEnvSet () {
206- cmdPath := filepath .Join (kcptestinghelpers .RepositoryDir (), "cmd" , executableName )
207- return []string {"dlv" , "debug" , "--api-version=2" , "--headless" , fmt .Sprintf ("--listen=unix:dlv-%s.sock" , identity ), cmdPath , "--" }
201+ // Command returns the work dir and string tokens required to start the
202+ // given executable in the currently configured mode (direct or via `go
203+ // run`).
204+ func Command (executableName , identity string ) (string , []string ) {
205+ if env .NoGoRunEnvSet () {
206+ return "" , []string {executableName }
208207 }
209208
210- // are we inside of the kcp repository?
211- repo := kcptestinghelpers . RepositoryDir ()
212- wd , err := os . Getwd ()
209+ // Check if this is a clone of the kcp repository. If not return the
210+ // executable as is, expecting the user to have it in PATH.
211+ repo , err := kcptestinghelpers . RepositoryDir ()
213212 if err != nil {
214- panic ( err )
213+ return "" , [] string { executableName }
215214 }
216- inKcp := strings .HasPrefix (wd , repo + "/" )
217215
218- binary := executableName
219- if binDir := os .Getenv (kcpBinariesDirEnvDir ); binDir == "" && inKcp {
220- binary = filepath .Join (kcptestinghelpers .RepositoryBinDir (), executableName )
221- } else if binDir != "" {
222- binary = filepath .Join (binDir , executableName )
216+ cmdDir := filepath .Join ("cmd" , executableName )
217+ fullPath := filepath .Join (repo , cmdDir )
218+ cmdDir = "./" + cmdDir
219+
220+ if _ , err := os .Stat (fullPath ); os .IsNotExist (err ) {
221+ // The command dir (e.g. $repo/cmd/kcp) does not exist, default
222+ // to the executable name and expect it to be in PATH.
223+ return "" , []string {executableName }
223224 }
224225
225- if env .NoGoRunEnvSet () || ! inKcp {
226- return []string {binary }
226+ if env .RunDelveEnvSet () {
227+ return repo , []string {"dlv" , "debug" , "--api-version=2" , "--headless" , fmt . Sprintf ( "--listen=unix:dlv-%s.sock" , identity ), cmdDir , "--" }
227228 }
228229
229- return []string {"go" , "run" , filepath . Join ( repo , "cmd" , executableName ) }
230+ return repo , []string {"go" , "run" , cmdDir }
230231}
231232
232233// Run runs the kcp server while the parent context is active. This call is not blocking,
@@ -301,13 +302,17 @@ func runExternal(ctx context.Context, t TestingT, cfg Config) (<-chan struct{},
301302 return nil , fmt .Errorf ("failed to build kcp args: %w" , err )
302303 }
303304
304- commandLine := append (StartKcpCommand ("KCP" ), args ... )
305+ workdir , commandLine := StartKcpCommand ("KCP" )
306+ commandLine = append (commandLine , args ... )
305307
306308 t .Logf ("running: %v" , strings .Join (commandLine , " " ))
307309
308310 // NOTE: do not use exec.CommandContext here. That method issues a SIGKILL when the context is done, and we
309311 // want to issue SIGTERM instead, to give the server a chance to shut down cleanly.
310312 cmd := exec .Command (commandLine [0 ], commandLine [1 :]... ) //nolint:gosec // G204: This is a test utility with controlled inputs
313+ if workdir != "" {
314+ cmd .Dir = workdir
315+ }
311316
312317 // Create a new process group for the child/forked process (which is either 'go run ...' or just 'kcp
313318 // ...'). This is necessary so the SIGTERM we send to terminate the kcp server works even with the
0 commit comments