-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run: Allow mocking by passing mock function through context
- Loading branch information
1 parent
e6d6166
commit 6dc3d3a
Showing
4 changed files
with
207 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/DavidGamba/dgtools/run | ||
|
||
go 1.14 | ||
go 1.18 |
This file contains 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 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,85 @@ | ||
package run_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/DavidGamba/dgtools/run" | ||
) | ||
|
||
func TestRunWithMocks(t *testing.T) { | ||
t.Run("mock", func(t *testing.T) { | ||
r := run.CMD("ls", "./run") | ||
r.Mock(func(r *run.RunInfo) error { | ||
r.Stdout.Write([]byte("hello world\n")) | ||
r.Stderr.Write([]byte("hola mundo\n")) | ||
return nil | ||
}) | ||
out, err := r.CombinedOutput() | ||
if err != nil { | ||
t.Errorf("unexpected error") | ||
} | ||
if string(out) != "hello world\nhola mundo\n" { | ||
t.Errorf("wrong output: %s\n", out) | ||
} | ||
}) | ||
|
||
t.Run("mock with context", func(t *testing.T) { | ||
ctx := context.Background() | ||
mockR := run.CMD().Mock(func(r *run.RunInfo) error { | ||
r.Stdout.Write([]byte("hello world\n")) | ||
r.Stderr.Write([]byte("hola mundo\n")) | ||
return nil | ||
}) | ||
ctx = run.ContextWithRunInfo(ctx, mockR) | ||
|
||
r := run.CMDCtx(ctx, "ls", "./run") | ||
out, err := r.CombinedOutput() | ||
if err != nil { | ||
t.Errorf("unexpected error") | ||
} | ||
if string(out) != "hello world\nhola mundo\n" { | ||
t.Errorf("wrong output: %s\n", out) | ||
} | ||
}) | ||
|
||
t.Run("mock with context switch", func(t *testing.T) { | ||
ctx := context.Background() | ||
mockR := run.CMD().Mock(func(r *run.RunInfo) error { | ||
cmd := r.Cmd | ||
|
||
switch { | ||
case slices.Compare(cmd, []string{"ls", "./run"}) == 0: | ||
r.Stdout.Write([]byte("hello world\n")) | ||
r.Stderr.Write([]byte("hola mundo\n")) | ||
return nil | ||
case slices.Compare(cmd, []string{"ls", "x"}) == 0: | ||
r.Stderr.Write([]byte("not found x\n")) | ||
return fmt.Errorf("not found x") | ||
default: | ||
return fmt.Errorf("unexpected command: %s", cmd) | ||
} | ||
}) | ||
ctx = run.ContextWithRunInfo(ctx, mockR) | ||
|
||
r := run.CMDCtx(ctx, "ls", "./run") | ||
out, err := r.CombinedOutput() | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} | ||
if string(out) != "hello world\nhola mundo\n" { | ||
t.Errorf("wrong output: %s\n", out) | ||
} | ||
|
||
r = run.CMDCtx(ctx, "ls", "x") | ||
out, err = r.CombinedOutput() | ||
if err == nil { | ||
t.Errorf("expected error") | ||
} | ||
if string(out) != "not found x\n" { | ||
t.Errorf("wrong output: %s\n", out) | ||
} | ||
}) | ||
} |