github.com/octohelm/cuemod@v0.9.4/pkg/cuemod/context_test.go (about) 1 package cuemod_test 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/go-courier/logr/slog" 11 12 "github.com/go-courier/logr" 13 "github.com/octohelm/cuemod/pkg/cuemod" 14 "github.com/octohelm/cuemod/pkg/cuemodx" 15 "github.com/octohelm/cuemod/pkg/cuex" 16 "github.com/octohelm/cuemod/pkg/cuex/format" 17 . "github.com/onsi/gomega" 18 19 _ "github.com/octohelm/cuemod/pkg/cuemod/testdata/embedstdlib" 20 ) 21 22 func TestContext(t *testing.T) { 23 cwd, _ := os.Getwd() 24 25 ctx := logr.WithLogger(context.Background(), slog.Logger(slog.Default())) 26 ctx = cuemod.WithOpts(ctx, cuemod.OptVerbose(true)) 27 28 t.Run("mod a", func(t *testing.T) { 29 r := cuemod.ContextFor(filepath.Join(cwd, "./testdata/a")) 30 _ = r.Cleanup() 31 32 t.Run("EvalContext", func(t *testing.T) { 33 data, err := cuemodx.EvalContext(ctx, r, ".", cuex.WithEncoding(cuex.JSON)) 34 NewWithT(t).Expect(err).To(BeNil()) 35 fmt.Println(string(data)) 36 NewWithT(t).Expect(r.Mod.Require["k8s.io/api"].Version).To(Equal("v0.24.1")) 37 }) 38 39 t.Run("EvalContext from exported single file", func(t *testing.T) { 40 data, err := cuemodx.EvalContext(ctx, r, ".", cuex.WithEncoding(cuex.CUE)) 41 NewWithT(t).Expect(err).To(BeNil()) 42 43 _ = os.WriteFile("../../_output/debug.cue", data, os.ModePerm) 44 45 inst, _ := cuex.InstanceFromRaw(data) 46 ret, err := cuex.Eval(inst, cuex.WithEncoding(cuex.JSON)) 47 NewWithT(t).Expect(err).To(BeNil()) 48 fmt.Println(string(ret)) 49 }) 50 }) 51 52 t.Run("mod b", func(t *testing.T) { 53 r := cuemod.ContextFor(filepath.Join(cwd, "./testdata/b")) 54 _ = r.Cleanup() 55 56 t.Run("ListCue", func(t *testing.T) { 57 t.Run("one dir", func(t *testing.T) { 58 files, err := r.ListCue(".") 59 60 NewWithT(t).Expect(err).To(BeNil()) 61 NewWithT(t).Expect(files).To(HaveLen(1)) 62 }) 63 64 t.Run("all", func(t *testing.T) { 65 files, err := r.ListCue("./...") 66 67 NewWithT(t).Expect(err).To(BeNil()) 68 NewWithT(t).Expect(len(files) > 0).To(BeTrue()) 69 70 t.Run("Format", func(t *testing.T) { 71 err := format.FormatFiles(ctx, files, format.FormatOpts{ 72 ReplaceFile: true, 73 PrintNames: true, 74 }) 75 NewWithT(t).Expect(err).To(BeNil()) 76 }) 77 }) 78 }) 79 80 t.Run("Get", func(t *testing.T) { 81 err := r.Get(ctx, "./...") 82 NewWithT(t).Expect(err).To(BeNil()) 83 }) 84 }) 85 }