-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathenv_test.go
More file actions
81 lines (75 loc) · 1.85 KB
/
Copy pathenv_test.go
File metadata and controls
81 lines (75 loc) · 1.85 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
package runn
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/k1LoW/runn/internal/scope"
)
func TestEnvSnapshotTakenAtRunStart(t *testing.T) {
const key = "TEST_RUN_ENV_SNAPSHOT"
ctx := context.Background()
dir := t.TempDir()
path := filepath.Join(dir, "book.yml")
if err := os.WriteFile(path, []byte(`
desc: Tests for environment variable snapshot.
steps:
-
test: env.`+key+` == vars.want
`), 0600); err != nil {
t.Fatal(err)
}
t.Setenv(key, "before-new")
o, err := New(Book(path), Var("want", "at-run-start"), Scopes(scope.AllowReadParent))
if err != nil {
t.Fatal(err)
}
// The environment set after the operator is created is still reflected.
t.Setenv(key, "at-run-start")
if err := o.Run(ctx); err != nil {
t.Error(err)
}
}
func TestEnvSnapshotIsImmutableDuringRun(t *testing.T) {
const key = "TEST_RUN_ENV_SNAPSHOT"
ctx := context.Background()
dir := t.TempDir()
path := filepath.Join(dir, "book.yml")
if err := os.WriteFile(path, []byte(`
desc: Tests for environment variable snapshot.
steps:
-
bind:
changed: setenv('`+key+`', 'changed')
-
test: env.`+key+` == 'at-run-start'
-
include:
path: included.yml
`), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "included.yml"), []byte(`
desc: Tests for environment variable snapshot of included runbook.
steps:
-
test: env.`+key+` == 'at-run-start'
`), 0600); err != nil {
t.Fatal(err)
}
t.Setenv(key, "at-run-start")
o, err := New(Book(path), Scopes(scope.AllowReadParent), Func("setenv", func(k, v string) bool {
os.Setenv(k, v)
return true
}))
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err != nil {
t.Error(err)
}
// The process environment itself is changed, only the snapshot is kept.
if got := os.Getenv(key); got != "changed" {
t.Errorf("got %v\nwant %v", got, "changed")
}
}