...
1
2
3
4 package kusttest_test
5
6 import (
7 "os"
8 "testing"
9
10 "sigs.k8s.io/kustomize/api/internal/plugins/compiler"
11 "sigs.k8s.io/kustomize/api/internal/plugins/utils"
12 "sigs.k8s.io/kustomize/api/konfig"
13 "sigs.k8s.io/kustomize/kyaml/filesys"
14 )
15
16
17
18 type pluginTestEnv struct {
19 t *testing.T
20 compiler *compiler.Compiler
21 pluginRoot string
22 oldXdg string
23 wasSet bool
24 }
25
26
27 func newPluginTestEnv(t *testing.T) *pluginTestEnv {
28 t.Helper()
29 return &pluginTestEnv{t: t}
30 }
31
32
33
34
35
36 func (x *pluginTestEnv) set() *pluginTestEnv {
37 var err error
38 x.pluginRoot, err = utils.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
39 if err != nil {
40 x.t.Error(err)
41 }
42 x.compiler = compiler.NewCompiler(x.pluginRoot)
43 x.setEnv()
44 return x
45 }
46
47
48 func (x *pluginTestEnv) reset() {
49
50
51
52
53 x.resetEnv()
54 }
55
56
57
58 func (x *pluginTestEnv) prepareGoPlugin(g, v, k string) {
59 x.compiler.SetGVK(g, v, k)
60 err := x.compiler.Compile()
61 if err != nil {
62 x.t.Errorf("compile failed: %v", err)
63 }
64 }
65
66 func (x *pluginTestEnv) prepareExecPlugin(_, _, _ string) {
67
68
69
70 }
71
72 func (x *pluginTestEnv) setEnv() {
73 x.oldXdg, x.wasSet = os.LookupEnv(konfig.KustomizePluginHomeEnv)
74 os.Setenv(konfig.KustomizePluginHomeEnv, x.pluginRoot)
75 }
76
77 func (x *pluginTestEnv) resetEnv() {
78 if x.wasSet {
79 os.Setenv(konfig.KustomizePluginHomeEnv, x.oldXdg)
80 } else {
81 os.Unsetenv(konfig.KustomizePluginHomeEnv)
82 }
83 }
84
View as plain text