Skip to content

Commit c5fc8d1

Browse files
✨ Add support for specifying the directory in subprocess commands (#857)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Add support for specifying the directory in subprocess commands ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent a1b57e9 commit c5fc8d1

5 files changed

Lines changed: 351 additions & 48 deletions

File tree

changes/20260422120133.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Add support for specifying the directory in subprocess commands

utils/subprocess/command_wrapper.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type command struct {
128128
loggers logs.Loggers
129129
cmdWrapper cmdWrapper
130130
io ICommandIO
131+
dir string
131132
}
132133

133134
func (c *command) createCommand(cmdCtx context.Context) *exec.Cmd {
@@ -140,6 +141,7 @@ func (c *command) createCommand(cmdCtx context.Context) *exec.Cmd {
140141
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.io.Register(cmdCtx)
141142
cmd.Env = cmd.Environ()
142143
cmd.Env = append(cmd.Env, c.env...)
144+
cmd.Dir = c.dir
143145

144146
// for any of our wait checks to work we need to set the group ID to the pid, otherwise the
145147
// group ID will be the code that launched it (i.e. the code that calls exec.Cmd.Start). This
@@ -178,7 +180,7 @@ func (c *command) Check() (err error) {
178180
return
179181
}
180182

181-
func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, cmd string, args ...string) (osCmd *command) {
183+
func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
182184
osCmd = &command{
183185
cmd: cmd,
184186
args: args,
@@ -187,11 +189,12 @@ func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, e
187189
loggers: loggers,
188190
cmdWrapper: cmdWrapper{},
189191
io: NewIOFromLoggers(loggers),
192+
dir: dir,
190193
}
191194
return
192195
}
193196

194-
func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtils.CommandAsDifferentUser, env []string, cmd string, args ...string) (osCmd *command) {
197+
func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
195198
osCmd = &command{
196199
cmd: cmd,
197200
args: args,
@@ -200,6 +203,7 @@ func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtil
200203
loggers: loggers,
201204
cmdWrapper: cmdWrapper{},
202205
io: io,
206+
dir: dir,
203207
}
204208
return
205209
}

utils/subprocess/command_wrapper_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func TestCmdRun(t *testing.T) {
5656
loggers, err := logs.NewLogrLogger(logstest.NewTestLogger(t), "test")
5757
require.NoError(t, err)
5858
if platform.IsWindows() {
59-
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdWindows, test.argWindows...)
59+
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdWindows, test.argWindows...)
6060
} else {
61-
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdOther, test.argOther...)
61+
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdOther, test.argOther...)
6262
}
6363
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
6464
defer cancel()
@@ -94,7 +94,7 @@ func TestCmdRunWithEnv(t *testing.T) {
9494
if platform.IsWindows() {
9595
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, "powershell", "-Command", envTest.cmdWindows)
9696
} else {
97-
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, envTest.cmdOther)
97+
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, "", envTest.cmdOther)
9898
}
9999
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
100100
defer cancel()
@@ -146,9 +146,9 @@ func TestCmdStartStop(t *testing.T) {
146146
require.NoError(t, err)
147147

148148
if platform.IsWindows() {
149-
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdWindows, test.argWindows...)
149+
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdWindows, test.argWindows...)
150150
} else {
151-
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdOther, test.argOther...)
151+
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdOther, test.argOther...)
152152
}
153153
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
154154
defer cancel()

0 commit comments

Comments
 (0)