-
Notifications
You must be signed in to change notification settings - Fork 112
Added process stubbing for easier testing of launched subprocesses #963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| var stubKey int | ||
|
|
||
| // WithStub creates process stub for fast and flexible testing of subprocesses | ||
| // | ||
| // ctx := context.Background() | ||
| // ctx, stub := WithStub(ctx) | ||
| // stub.WithDefaultOutput("meeee") | ||
| // out, err := Background(ctx, []string{"/usr/local/bin/meeecho", "1", "--foo", "bar"}) | ||
| // require.NoError(t, err) | ||
| // require.Equal(t, "meeee", out) | ||
| // require.Equal(t, 1, stub.Len()) | ||
| // require.Equal(t, []string{"meeecho 1 --foo bar"}, stub.Commands()) | ||
| func WithStub(ctx context.Context) (context.Context, *processStub) { | ||
| stub := &processStub{responses: map[string]reponseStub{}} | ||
| ctx = context.WithValue(ctx, &stubKey, stub) | ||
| return ctx, stub | ||
| } | ||
|
|
||
| func runCmd(ctx context.Context, cmd *exec.Cmd) error { | ||
| stub, ok := ctx.Value(&stubKey).(*processStub) | ||
| if ok { | ||
| return stub.run(cmd) | ||
| } | ||
| return cmd.Run() | ||
| } | ||
|
|
||
| type reponseStub struct { | ||
| stdout string | ||
| stderr string | ||
| err error | ||
| } | ||
|
|
||
| type processStub struct { | ||
| calls []*exec.Cmd | ||
| defaultOutput string | ||
| defaultFailure error | ||
| defaultCallback func(*exec.Cmd) error | ||
| responses map[string]reponseStub | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (s *processStub) WithDefaultOutput(output string) *processStub { | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s.defaultOutput = output | ||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) WithDefaultFailure(err error) *processStub { | ||
| s.defaultFailure = err | ||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) WithDefaultCallback(cb func(cmd *exec.Cmd) error) *processStub { | ||
| s.defaultCallback = cb | ||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) WithStdoutFor(norm, out string) *processStub { | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s.responses[norm] = reponseStub{ | ||
| stdout: out, | ||
| } | ||
nfx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) WithStderrFor(norm, out string) *processStub { | ||
| s.responses[norm] = reponseStub{ | ||
| stderr: out, | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) WithFailureFor(norm string, err error) *processStub { | ||
| s.responses[norm] = reponseStub{ | ||
| err: err, | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func (s *processStub) String() string { | ||
| return fmt.Sprintf("process stub with %d calls", s.Len()) | ||
| } | ||
|
|
||
| func (s *processStub) Len() int { | ||
| return len(s.calls) | ||
| } | ||
|
|
||
| func (s *processStub) Commands() (called []string) { | ||
| for _, v := range s.calls { | ||
| called = append(called, s.normCmd(v)) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // CombinedEnvironment returns all enviroment variables used for all commands | ||
| // | ||
| // ctx := context.Background() | ||
| // ctx = env.Set(ctx, "FOO", "bar") | ||
| // ctx, stub := WithStub(ctx) | ||
| // out, err := Background(ctx, []string{"/usr/local/bin/meeecho", "1", "--foo", "bar"}) | ||
| // require.NoError(t, err) | ||
| // allEnv := stub.CombinedEnvironment() | ||
| // require.Equal(t, "bar", allEnv["FOO"]) | ||
| // require.Equal(t, "bar", stub.LookupEnv("FOO")) | ||
| func (s *processStub) CombinedEnvironment() map[string]string { | ||
| environment := map[string]string{} | ||
| for _, cmd := range s.calls { | ||
| for _, line := range cmd.Env { | ||
| k, v, ok := strings.Cut(line, "=") | ||
| if !ok { | ||
| continue | ||
| } | ||
| environment[k] = v | ||
| } | ||
| } | ||
| return environment | ||
| } | ||
|
|
||
| // CombinedEnvironment returns all enviroment variables used for all commands | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // ctx := context.Background() | ||
| // ctx = env.Set(ctx, "FOO", "bar") | ||
| // ctx, stub := WithStub(ctx) | ||
| // out, err := Background(ctx, []string{"/usr/local/bin/meeecho", "1", "--foo", "bar"}) | ||
| // require.NoError(t, err) | ||
| // require.Equal(t, "bar", stub.LookupEnv("FOO")) | ||
| func (s *processStub) LookupEnv(key string) string { | ||
| environment := s.CombinedEnvironment() | ||
| return environment[key] | ||
| } | ||
|
|
||
| func (s *processStub) normCmd(v *exec.Cmd) string { | ||
| // to reduce testing noise, we collect here only the deterministic binary basenames, e.g. | ||
| // "/var/folders/bc/7qf8yghj6v14t40096pdcqy40000gp/T/tmp.03CAcYcbOI/python3" becomes "python3", | ||
| // while still giving the possibility to customize. See [processStub.WithDefaultCallback] | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| binaryName := filepath.Base(v.Path) | ||
| args := strings.Join(v.Args[1:], " ") | ||
| return fmt.Sprintf("%s %s", binaryName, args) | ||
| } | ||
|
|
||
| func (s *processStub) run(cmd *exec.Cmd) error { | ||
| s.calls = append(s.calls, cmd) | ||
| resp, ok := s.responses[s.normCmd(cmd)] | ||
| if ok { | ||
| if resp.stdout != "" { | ||
| cmd.Stdout.Write([]byte(resp.stdout)) | ||
| } | ||
| if resp.stderr != "" { | ||
| cmd.Stderr.Write([]byte(resp.stderr)) | ||
| } | ||
| return resp.err | ||
| } | ||
| if s.defaultCallback != nil { | ||
| return s.defaultCallback(cmd) | ||
| } | ||
| if s.defaultOutput != "" { | ||
| cmd.Stdout.Write([]byte(s.defaultOutput)) | ||
| } | ||
| return s.defaultFailure | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package process_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/env" | ||
| "github.com/databricks/cli/libs/process" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestStubOutput(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx, stub := process.WithStub(ctx) | ||
| stub.WithDefaultOutput("meeee") | ||
|
|
||
| ctx = env.Set(ctx, "FOO", "bar") | ||
|
|
||
| out, err := process.Background(ctx, []string{"/usr/local/bin/meeecho", "1", "--foo", "bar"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "meeee", out) | ||
| require.Equal(t, 1, stub.Len()) | ||
| require.Equal(t, []string{"meeecho 1 --foo bar"}, stub.Commands()) | ||
|
|
||
| allEnv := stub.CombinedEnvironment() | ||
| require.Equal(t, "bar", allEnv["FOO"]) | ||
| require.Equal(t, "bar", stub.LookupEnv("FOO")) | ||
| } | ||
|
|
||
| func TestStubFailure(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx, stub := process.WithStub(ctx) | ||
| stub.WithDefaultFailure(fmt.Errorf("nope")) | ||
|
|
||
| _, err := process.Background(ctx, []string{"/bin/meeecho", "1"}) | ||
| require.EqualError(t, err, "/bin/meeecho 1: nope") | ||
| require.Equal(t, 1, stub.Len()) | ||
| } | ||
|
|
||
| func TestStubCallback(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx, stub := process.WithStub(ctx) | ||
| stub.WithDefaultCallback(func(cmd *exec.Cmd) error { | ||
| cmd.Stderr.Write([]byte("something...")) | ||
| cmd.Stdout.Write([]byte("else...")) | ||
| return fmt.Errorf("yep") | ||
| }) | ||
|
|
||
| _, err := process.Background(ctx, []string{"/bin/meeecho", "1"}) | ||
| require.EqualError(t, err, "/bin/meeecho 1: yep") | ||
| require.Equal(t, 1, stub.Len()) | ||
|
|
||
| var processError *process.ProcessError | ||
| require.ErrorAs(t, err, &processError) | ||
| require.Equal(t, "something...", processError.Stderr) | ||
| require.Equal(t, "else...", processError.Stdout) | ||
| } | ||
|
|
||
| func TestStubResponses(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx, stub := process.WithStub(ctx) | ||
| stub. | ||
| WithStdoutFor("qux 1", "first"). | ||
| WithStdoutFor("qux 2", "second"). | ||
| WithFailureFor("qux 3", fmt.Errorf("nope")) | ||
|
|
||
| first, err := process.Background(ctx, []string{"/path/is/irrelevant/qux", "1"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "first", first) | ||
|
|
||
| second, err := process.Background(ctx, []string{"/path/is/irrelevant/qux", "2"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "second", second) | ||
|
|
||
| _, err = process.Background(ctx, []string{"/path/is/irrelevant/qux", "3"}) | ||
| require.EqualError(t, err, "/path/is/irrelevant/qux 3: nope") | ||
|
|
||
| require.Equal(t, "process stub with 3 calls", stub.String()) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.