Skip to content

Commit 44216d1

Browse files
authored
feat(internal/command): introduce RunWithEnv (#3275)
Introduce `RunWithEnv` to be able to set env vars for commands without putting in `env` command directly. Keep the original `command.Run` as convenience wrapper. Fixes #3250
1 parent a800a88 commit 44216d1

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

internal/command/command.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,23 @@ import (
2222
"os/exec"
2323
)
2424

25-
// Run executes a program (with arguments) and captures any error output.
25+
// Run executes a program (with arguments) and captures any error output. It is a
26+
// convenience wrapper around RunWithEnv.
2627
func Run(ctx context.Context, command string, arg ...string) error {
28+
return RunWithEnv(ctx, nil, command, arg...)
29+
}
30+
31+
// RunWithEnv executes a program (with arguments) and optional environment
32+
// variables and captures any error output. If env is nil or empty, the command
33+
// inherits the environment of the calling process.
34+
func RunWithEnv(ctx context.Context, env map[string]string, command string, arg ...string) error {
2735
cmd := exec.CommandContext(ctx, command, arg...)
36+
if len(env) > 0 {
37+
cmd.Env = os.Environ()
38+
for k, v := range env {
39+
cmd.Env = append(cmd.Env, k+"="+v)
40+
}
41+
}
2842
fmt.Fprintf(os.Stderr, "Running: %s\n", cmd.String())
2943
if output, err := cmd.CombinedOutput(); err != nil {
3044
return fmt.Errorf("%v: %v\n%s", cmd, err, output)

internal/command/command_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package command
1616

1717
import (
18+
"fmt"
1819
"strings"
1920
"testing"
2021
)
@@ -34,3 +35,28 @@ func TestRunError(t *testing.T) {
3435
t.Errorf("error should mention the invalid subcommand, got: %v", err)
3536
}
3637
}
38+
39+
func TestRunWithEnv_SetsAndVerifiesVariable(t *testing.T) {
40+
ctx := t.Context()
41+
const (
42+
name = "LIBRARIAN_TEST_VAR"
43+
value = "value"
44+
)
45+
err := RunWithEnv(ctx, map[string]string{name: value},
46+
"sh", "-c", fmt.Sprintf("test \"$%s\" = \"%s\"", name, value))
47+
if err != nil {
48+
t.Fatalf("RunWithEnv() = %v, want %v", err, nil)
49+
}
50+
}
51+
52+
func TestRunWithEnv_VariableNotSetFailsValidation(t *testing.T) {
53+
ctx := t.Context()
54+
const (
55+
name = "LIBRARIAN_TEST_VAR"
56+
value = "value"
57+
)
58+
err := RunWithEnv(ctx, map[string]string{}, "sh", "-c", fmt.Sprintf("test \"$%s\" = \"%s\"", name, value))
59+
if err == nil {
60+
t.Fatalf("RunWithEnv() = %v, want non-nil", err)
61+
}
62+
}

internal/librarian/rust/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func FormatAndValidateLibrary(ctx context.Context, outputDir string) error {
8282
if err := command.Run(ctx, "cargo", "test", "--manifest-path", manifestPath); err != nil {
8383
return err
8484
}
85-
if err := command.Run(ctx, "env", "RUSTDOCFLAGS=-D warnings", "cargo", "doc", "--manifest-path", manifestPath, "--no-deps"); err != nil {
85+
if err := command.RunWithEnv(ctx, map[string]string{"RUSTDOCFLAGS": "-D warnings"}, "cargo", "doc", "--manifest-path", manifestPath, "--no-deps"); err != nil {
8686
return err
8787
}
8888
if err := command.Run(ctx, "cargo", "clippy", "--manifest-path", manifestPath, "--", "--deny", "warnings"); err != nil {

0 commit comments

Comments
 (0)