-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
116 lines (110 loc) · 3.7 KB
/
Copy pathenv_test.go
File metadata and controls
116 lines (110 loc) · 3.7 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
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
112
113
114
115
116
package taskrun
import (
"context"
"errors"
"strings"
"testing"
)
// Environment values are templates: the design example sets
// KS_LABEL: "${vars.target}".
func TestEnvValuesAreRendered(t *testing.T) {
var seen map[string]string
runner := newTestRunner(t, Definition{
Env: map[string]string{"KS_DEFINED": "${vars.project}"},
Vars: map[string]any{
"project": "taskrun",
},
Tasks: map[string]Task{"t": {
Vars: map[string]any{"target": "./..."},
Env: map[string]string{"KS_LABEL": "${vars.target}", "KS_LOWER": "[${env.KS_DEFINED}]"},
Steps: []Step{{
Env: map[string]string{"KS_STEP": "${vars.target}-step"},
Host: &HostSpec{Name: "capture"},
}},
}},
}, WithBaseEnv(map[string]string{"KS_BASE": "base"}), WithHandler("capture", func(_ context.Context, call HostCall) (ActionResult, error) {
seen = call.Env
return ActionResult{}, nil
}))
mustRun(t, runner, Request{Task: "t", Env: map[string]string{"KS_REQUEST": "${vars.target}-request"}})
want := map[string]string{
"KS_BASE": "base",
"KS_DEFINED": "taskrun",
"KS_LABEL": "./...",
"KS_LOWER": "[taskrun]",
"KS_STEP": "./...-step",
"KS_REQUEST": "./...-request",
}
for key, value := range want {
if seen[key] != value {
t.Fatalf("env[%s]=%q want %q (env=%v)", key, seen[key], value, seen)
}
}
}
func TestEnvValuesReachTheChildProcess(t *testing.T) {
requireShell(t, "sh")
runner := newTestRunner(t, Definition{
Tasks: map[string]Task{"t": {
Vars: map[string]any{"target": "rendered-env"},
Env: map[string]string{"KS_LABEL": "${vars.target}"},
Steps: []Step{{Shell: &ShellSpec{Name: "sh", Script: "printf %s \"$KS_LABEL\""}}},
}},
})
result := mustRun(t, runner, Request{Task: "t", IO: IO{CaptureLimit: 64}})
if string(result.Steps[0].Output) != "rendered-env" {
t.Fatalf("output=%q", result.Steps[0].Output)
}
}
func TestEnvPathsEntriesAreRendered(t *testing.T) {
var seen string
runner := newTestRunner(t, Definition{
Vars: map[string]any{"bin": "/rendered/bin"},
Tasks: map[string]Task{"t": {
EnvPaths: []string{"${vars.bin}"},
Steps: []Step{{Host: &HostSpec{Name: "capture"}}},
}},
}, WithBaseEnv(map[string]string{"PATH": "/base"}), WithHandler("capture", func(_ context.Context, call HostCall) (ActionResult, error) {
seen = call.Env["PATH"]
return ActionResult{}, nil
}))
mustRun(t, runner, Request{Task: "t"})
if !strings.HasPrefix(seen, "/rendered/bin") {
t.Fatalf("PATH=%q", seen)
}
}
// Dynamic variables must not decide the process environment.
func TestDynamicVariableCannotDecideEnv(t *testing.T) {
runner := newTestRunner(t, Definition{
Tasks: map[string]Task{"t": {
DynamicVars: map[string]DynamicVar{"stamp": {Shell: &ShellSpec{Name: "sh", Script: "printf x"}}},
Env: map[string]string{"KS_STAMP": "${vars.stamp}"},
Steps: []Step{{Shell: &ShellSpec{Name: "sh", Script: "printf ok"}}},
}},
})
_, err := runner.Run(context.Background(), Request{Task: "t"})
if err == nil || !errors.Is(err, ErrInvalidDefinition) {
t.Fatalf("err=%v", err)
}
if !strings.Contains(err.Error(), "dynamic") {
t.Fatalf("unhelpful error: %v", err)
}
// Inspect must not fail; the field is reported as deferred.
plan, err := runner.Inspect(context.Background(), Request{Task: "t"})
if err != nil {
t.Fatalf("inspect: %v", err)
}
if len(plan.Deferred) == 0 {
t.Fatalf("plan=%+v", plan)
}
}
func TestUnknownReferenceInEnvFails(t *testing.T) {
runner := newTestRunner(t, Definition{
Tasks: map[string]Task{"t": {
Env: map[string]string{"KS_BAD": "${vars.nope}"},
Steps: []Step{{Exec: &ExecSpec{Program: "go"}}},
}},
})
if _, err := runner.Run(context.Background(), Request{Task: "t"}); err == nil {
t.Fatal("expected an unknown variable error")
}
}