Repository navigation
Expand file tree
/
Copy pathexecution_scope_test.go
More file actions
67 lines (61 loc) · 1.94 KB
/
Copy pathexecution_scope_test.go
File metadata and controls
67 lines (61 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gobash
import (
"context"
"errors"
"testing"
"github.com/mark3labs/go-bash/command"
)
func TestNestedExecutionSharesCommandBudget(t *testing.T) {
b, err := New(BashOptions{ExecutionLimits: &ExecutionLimits{MaxCommandCount: intPtrTest(2)}})
if err != nil {
t.Fatal(err)
}
res, err := b.Exec(context.Background(), `bash -c 'echo inner; echo extra'; echo outer`, ExecOptions{})
var limit *ExecutionLimitError
if !errors.As(err, &limit) || limit.Limit != "MaxCommandCount" {
t.Fatalf("result=%+v err=%v", res, err)
}
}
func TestNestedExecutionSharesLoopBudget(t *testing.T) {
b, err := New(BashOptions{ExecutionLimits: &ExecutionLimits{MaxLoopIterations: intPtrTest(2)}})
if err != nil {
t.Fatal(err)
}
_, err = b.Exec(context.Background(), `for x in 1; do :; done; bash -c 'for x in 1 2; do :; done'`, ExecOptions{})
var limit *ExecutionLimitError
if !errors.As(err, &limit) || limit.Limit != "MaxLoopIterations" {
t.Fatalf("err=%v", err)
}
}
func TestNestedExecutionSharesOutputBudget(t *testing.T) {
b, err := New(BashOptions{ExecutionLimits: &ExecutionLimits{MaxOutputSize: intPtrTest(5)}})
if err != nil {
t.Fatal(err)
}
res, err := b.Exec(context.Background(), `echo ab; bash -c 'echo cd'`, ExecOptions{})
var limit *ExecutionLimitError
if !errors.As(err, &limit) || limit.Limit != "MaxOutputSize" {
t.Fatalf("result=%+v err=%v", res, err)
}
}
func TestNestedArgsRemainScoped(t *testing.T) {
var got string
cmd := command.Define("remember", func(_ context.Context, args []string, _ *command.Context) command.Result {
got = ""
if len(args) > 1 {
got = args[1]
}
return command.Result{}
})
b, err := New(BashOptions{CustomCommands: []command.Command{cmd}})
if err != nil {
t.Fatal(err)
}
_, err = b.Exec(context.Background(), `remember; bash -c 'remember'`, ExecOptions{Args: []string{"outer"}})
if err != nil {
t.Fatal(err)
}
if got != "" {
t.Fatalf("nested dispatch consumed/reused Args: got %q", got)
}
}