1// Copyright 2025 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// https://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
15package layer_test
16
17import (
18 "fmt"
19 "slices"
20 "strconv"
21 "strings"
22 "testing"
23
24 "cuelang.org/go/cue/ast"
25 "cuelang.org/go/cue/cuecontext"
26 "cuelang.org/go/cue/errors"
27 "cuelang.org/go/internal/core/adt"
28 "cuelang.org/go/internal/cuedebug"
29 "cuelang.org/go/internal/cuetxtar"
30)
31
32func TestLayers(t *testing.T) {
33 adt.DebugDeps = true // check unmatched dependencies.
34
35 test := cuetxtar.TxTarTest{
36 Root: "./testdata",
37 Name: "layer",
38 }
39
40 cuedebug.Init()
41
42 test.Run(t, func(t *cuetxtar.Test) {
43 a := t.Instance()
44 ctx := cuecontext.New()
45
46 // Inject layering based on @layer attribute.
47 for _, f := range a.Files {
48 for i, d := range f.Decls {
49 _ = i
50 attr, ok := d.(*ast.Attribute)
51 if !ok {
52 // no preamble
53 break
54 }
55 key, body := attr.Split()
56 if key != "layer" {
57 continue
58 }
59
60 args := strings.Split(body, ",")
61
62 priority, err := strconv.ParseInt(args[0], 10, 8)
63 if err != nil {
64 t.Fatalf("invalid priority %q: %v", args[0], err)
65 }
66
67 f.Pos().File().SetLayer(
68 int8(priority),
69 slices.Contains(args, "defaultData"),
70 )
71
72 f.Decls = slices.Delete(f.Decls, i, i+1)
73 break
74 }
75 }
76
77 v := ctx.BuildInstance(a)
78 if err := v.Err(); err != nil {
79 t.WriteErrors(errors.Promote(err, "layers"))
80 return
81 }
82
83 s := fmt.Sprint(v)
84 t.Write([]byte(s))
85 })
86}