-
Notifications
You must be signed in to change notification settings - Fork 3
/
os_test.go
111 lines (103 loc) · 1.97 KB
/
os_test.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package task
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"testing"
)
func TestExpandEnv(t *testing.T) {
type kv = map[string]interface{}
type ks = map[string]string
list := []struct {
Name string
State kv
Env ks
Input string
Output string
}{
{
Name: "override",
State: kv{
"k1": int64(45),
},
Env: ks{
"k1": "letters",
},
Input: "abc${k1}xyz",
Output: "abc45xyz",
},
{
Name: "env",
State: kv{
"k1": int64(45),
},
Env: ks{
"k2": "letters",
},
Input: "abc${k2}xyz",
Output: "abclettersxyz",
},
}
for _, item := range list {
t.Run(item.Name, func(t *testing.T) {
st := &State{
Env: item.Env,
bucket: item.State,
}
got := ExpandEnv(item.Input, st)
if g, w := got, item.Output; g != w {
t.Fatalf("got %q; want %q", g, w)
}
})
}
}
func getString(varName string, value *string) Action {
return ActionFunc(func(ctx context.Context, st *State, sc Script) error {
switch v := st.Get(varName).(type) {
default:
return fmt.Errorf("unable to get value for varname %q: %#v", varName, v)
case []byte:
*value = strings.TrimSpace(string(v))
case string:
*value = strings.TrimSpace(v)
}
return nil
})
}
func TestWriteStd(t *testing.T) {
lsPath, _ := exec.LookPath("ls")
grepPath, _ := exec.LookPath("grep")
if len(lsPath) == 0 || len(grepPath) == 0 {
t.Skip("missing ls or grep")
}
stdOut := &bytes.Buffer{}
stdErr := &bytes.Buffer{}
st := &State{
Stdout: stdOut,
Stderr: stdErr,
}
var result string
sc := NewScript(
WithStd(VAR("stdout"), VAR("stderr"), NewScript(
Exec("ls"),
ExecStdin(VAR("stdout"), "grep", ".mod"),
getString("stdout", &result),
)),
)
ctx := context.Background()
err := sc.Run(ctx, st, nil)
if err != nil {
t.Fatal(err)
}
if result != "go.mod" {
t.Fatalf("expected go.mod, got %q", result)
}
if stdOut.Len() > 0 {
t.Fatal("stdout has data")
}
if stdErr.Len() > 0 {
t.Fatal("stderr has data")
}
}