...

Source file src/cuelang.org/go/internal/core/export/self_test.go

Documentation: cuelang.org/go/internal/core/export

     1  // Copyright 2022 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package export_test
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/cue/ast"
    24  	"cuelang.org/go/cue/build"
    25  	"cuelang.org/go/cue/cuecontext"
    26  	"cuelang.org/go/cue/errors"
    27  	"cuelang.org/go/cue/format"
    28  	"cuelang.org/go/internal/core/adt"
    29  	"cuelang.org/go/internal/core/export"
    30  	"cuelang.org/go/internal/cuetxtar"
    31  	"cuelang.org/go/internal/diff"
    32  	"cuelang.org/go/internal/types"
    33  	"golang.org/x/tools/txtar"
    34  )
    35  
    36  func TestSelfContained(t *testing.T) {
    37  	test := cuetxtar.TxTarTest{
    38  		Root: "./testdata/selfcontained",
    39  	}
    40  
    41  	r := cuecontext.New()
    42  
    43  	test.Run(t, func(t *cuetxtar.Test) {
    44  		a := t.Instances()
    45  
    46  		v := buildFile(t.T, r, a[0])
    47  
    48  		p, ok := t.Value("path")
    49  		if ok {
    50  			v = v.LookupPath(cue.ParsePath(p))
    51  		}
    52  
    53  		var tValue types.Value
    54  		v.Core(&tValue)
    55  
    56  		self := *export.All
    57  		self.SelfContained = true
    58  
    59  		w := t.Writer("default")
    60  
    61  		test := func() {
    62  			file, errs := self.Def(tValue.R, "", tValue.V)
    63  
    64  			errors.Print(w, errs, nil)
    65  			_, _ = w.Write(formatNode(t.T, file))
    66  
    67  			vf := patch(t.T, r, t.Archive, file)
    68  			doDiff(t.T, v, vf)
    69  
    70  			v = v.Unify(vf)
    71  			doDiff(t.T, v, vf)
    72  		}
    73  		test()
    74  
    75  		if _, ok := t.Value("inlineImports"); ok {
    76  			w = t.Writer("expand_imports")
    77  			self.InlineImports = true
    78  			test()
    79  		}
    80  	})
    81  }
    82  
    83  func buildFile(t *testing.T, r *cue.Context, b *build.Instance) cue.Value {
    84  	t.Helper()
    85  	v := r.BuildInstance(b)
    86  	if err := v.Err(); err != nil {
    87  		t.Fatal(errors.Details(err, nil))
    88  	}
    89  	return v
    90  }
    91  
    92  // patch replaces the package at the root of the Archive with the given CUE
    93  // file.
    94  func patch(t *testing.T, r *cue.Context, orig *txtar.Archive, f *ast.File) cue.Value {
    95  	a := *orig
    96  	a.Files = make([]txtar.File, len(a.Files))
    97  	copy(a.Files, orig.Files)
    98  
    99  	k := 0
   100  	for _, f := range a.Files {
   101  		if strings.HasSuffix(f.Name, ".cue") && !strings.ContainsRune(f.Name, '/') {
   102  			continue
   103  		}
   104  		a.Files[k] = f
   105  		k++
   106  	}
   107  	b, err := format.Node(f)
   108  	if err != nil {
   109  		t.Error(err)
   110  	}
   111  
   112  	a.Files = append(a.Files, txtar.File{
   113  		Name: "in.cue",
   114  		Data: b,
   115  	})
   116  
   117  	instance := cuetxtar.Load(&a, t.TempDir())[0]
   118  	if instance.Err != nil {
   119  		t.Fatal(instance.Err)
   120  	}
   121  
   122  	vn := buildFile(t, r, instance)
   123  	if err := vn.Err(); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	return vn
   127  }
   128  
   129  func doDiff(t *testing.T, v, w cue.Value) {
   130  	var bb bytes.Buffer
   131  	p := diff.Schema
   132  	p.SkipHidden = true
   133  	d, script := p.Diff(v, w)
   134  	if d != diff.Identity {
   135  		diff.Print(&bb, script)
   136  		t.Error(bb.String())
   137  	}
   138  }
   139  
   140  // TestSC is for debugging purposes. Do not delete.
   141  func TestSC(t *testing.T) {
   142  	in := `
   143  -- cue.mod/module.cue --
   144  module: "mod.test/a"
   145  
   146  -- in.cue --
   147  	`
   148  	if strings.HasSuffix(strings.TrimSpace(in), ".cue --") {
   149  		t.Skip()
   150  	}
   151  
   152  	a := txtar.Parse([]byte(in))
   153  	instance := cuetxtar.Load(a, t.TempDir())[0]
   154  	if instance.Err != nil {
   155  		t.Fatal(instance.Err)
   156  	}
   157  
   158  	r := cuecontext.New()
   159  
   160  	v := buildFile(t, r, instance)
   161  
   162  	// v = v.LookupPath(cue.ParsePath("a.b"))
   163  
   164  	var tValue types.Value
   165  	v.Core(&tValue)
   166  	self := export.All
   167  	self.SelfContained = true
   168  	self.InlineImports = true
   169  
   170  	adt.Verbosity = 1
   171  	t.Cleanup(func() { adt.Verbosity = 0 })
   172  
   173  	file, errs := self.Def(tValue.R, "", tValue.V)
   174  	if errs != nil {
   175  		t.Fatal(errs)
   176  	}
   177  
   178  	adt.Verbosity = 0
   179  
   180  	b, _ := format.Node(file)
   181  	t.Error(string(b))
   182  
   183  	vf := patch(t, r, a, file)
   184  	doDiff(t, v, vf)
   185  }
   186  

View as plain text