github.com/octohelm/cuekit@v0.0.0-20240424021256-e7df8d743066/pkg/cuecontext/instance_test.go (about)

     1  package cuecontext
     2  
     3  import (
     4  	"context"
     5  	"github.com/octohelm/cuekit/pkg/mod/modmem"
     6  	"github.com/octohelm/unifs/pkg/filesystem"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	testingx "github.com/octohelm/x/testing"
    12  )
    13  
    14  func Test(t *testing.T) {
    15  	t.Run("should build simple", func(t *testing.T) {
    16  		c, err := NewConfig(WithRoot("./testdata/simple"))
    17  		testingx.Expect(t, err, testingx.Be[error](nil))
    18  
    19  		_, err = EvalJSON(c)
    20  		testingx.Expect(t, err, testingx.Be[error](nil))
    21  	})
    22  
    23  	t.Run("should build with go mod", func(t *testing.T) {
    24  		c, err := NewConfig(WithRoot("./testdata/gomod"))
    25  		testingx.Expect(t, err, testingx.Be[error](nil))
    26  
    27  		_, err = EvalJSON(c)
    28  		testingx.Expect(t, err, testingx.Be[error](nil))
    29  	})
    30  
    31  	t.Run("should build with local replace", func(t *testing.T) {
    32  		inst, err := NewConfig(WithRoot("./testdata/localreplace"))
    33  		testingx.Expect(t, err, testingx.Be[error](nil))
    34  
    35  		_, err = EvalJSON(inst)
    36  		testingx.Expect(t, err, testingx.Be[error](nil))
    37  	})
    38  
    39  	t.Run("should build with mem module", func(t *testing.T) {
    40  		c, err := NewConfig(WithRoot("./testdata/mem"))
    41  		testingx.Expect(t, err, testingx.Be[error](nil))
    42  
    43  		_, err = EvalJSON(c)
    44  		testingx.Expect(t, err, testingx.Be[error](nil))
    45  	})
    46  
    47  	t.Run("mod init && tidy", func(t *testing.T) {
    48  		moduleRoot := "./testdata/tidy"
    49  
    50  		_ = os.RemoveAll(filepath.Join(moduleRoot, "cache"))
    51  		_ = os.RemoveAll(filepath.Join(moduleRoot, "cue.mod"))
    52  
    53  		err := Init(context.Background(), moduleRoot, "ghcr.io/octothelm/tidy@v0")
    54  		testingx.Expect(t, err, testingx.Be[error](nil))
    55  
    56  		err = Tidy(context.Background(), moduleRoot)
    57  		testingx.Expect(t, err, testingx.Be[error](nil))
    58  	})
    59  }
    60  
    61  func init() {
    62  	// register meme
    63  	m, _ := modmem.NewModule("mem.octothelm.tech", "v0.0.0", func(ctx context.Context, fsys filesystem.FileSystem) error {
    64  		_ = filesystem.MkdirAll(ctx, fsys, "x")
    65  		_ = filesystem.Write(ctx, fsys, "x/x.cue", []byte(`
    66  package x
    67  
    68  #Version: "mem-devel" 
    69  `))
    70  
    71  		return nil
    72  	})
    73  
    74  	modmem.DefaultRegistry.Register(m)
    75  }