跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

yqutil_test.go

172 行 · 3.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package yqutil
5
6import (
7 "context"
8 "strings"
9 "testing"
10
11 "gotest.tools/v3/assert"
12)
13
14func TestValidateContent(t *testing.T) {
15 content := `
16# comment
17foo: bar
18`
19 err := ValidateContent([]byte(content))
20 assert.NilError(t, err)
21}
22
23func TestValidateContentError(t *testing.T) {
24 content := `
25- foo: bar
26 foo
27 bar
28`
29 err := ValidateContent([]byte(content))
30 assert.ErrorContains(t, err, "could not find expected")
31}
32
33func TestEvaluateExpressionEmpty(t *testing.T) {
34 expression := ""
35 content := `
36foo: bar
37`
38 expected := content
39 out, err := EvaluateExpression(t.Context(), expression, []byte(content))
40 assert.NilError(t, err)
41 assert.Equal(t, expected, string(out))
42}
43
44func TestEvaluateExpressionSimple(t *testing.T) {
45 expression := `.cpus = 2 | .memory = "2GiB"`
46 content := `
47# CPUs
48cpus: null
49
50# Memory size
51memory: null
52`
53 // Note: yq currently removes empty lines, but not comments
54 expected := `
55# CPUs
56cpus: 2
57
58# Memory size
59memory: 2GiB
60`
61 out, err := EvaluateExpression(t.Context(), expression, []byte(content))
62 assert.NilError(t, err)
63 assert.Equal(t, expected, string(out))
64}
65
66func TestEvaluateExpressionComplex(t *testing.T) {
67 expression := `.mounts += {"location": "foo", "mountPoint": "bar"}`
68 content := `
69# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
70# 🟢 Builtin default: null (Mount nothing)
71# 🔵 This file: Mount the home as read-only
72mounts:
73- location: "~"
74 # Configure the mountPoint inside the guest.
75 # 🟢 Builtin default: value of location
76 mountPoint: null
77`
78 // Note: yq will use canonical yaml, with indented sequences
79 // Note: yq will not explicitly quote strings, when not needed
80 // Note: yamlfmt will fix indentation of sequences
81 expected := `
82# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
83# 🟢 Builtin default: null (Mount nothing)
84# 🔵 This file: Mount the home as read-only
85mounts:
86- location: "~"
87 # Configure the mountPoint inside the guest.
88 # 🟢 Builtin default: value of location
89 mountPoint: null
90- location: foo
91 mountPoint: bar
92`
93 out, err := EvaluateExpression(t.Context(), expression, []byte(content))
94 assert.NilError(t, err)
95 assert.Equal(t, expected, string(out))
96}
97
98func TestEvaluateExpressionError(t *testing.T) {
99 expression := `arch: aarch64`
100 _, err := EvaluateExpression(t.Context(), expression, []byte(""))
101 assert.ErrorContains(t, err, "invalid input text")
102}
103
104func TestEvaluateMergeExpression(t *testing.T) {
105 expression := `select(di == 0) * select(di == 1)`
106 content := `
107yolo: true
108foo:
109 bar: 1
110 baz: 2
111---
112foo:
113 bar: 3
114 fomo: false
115`
116 expected := `
117yolo: true
118foo:
119 bar: 3
120 baz: 2
121 fomo: false
122`
123 out, err := EvaluateExpression(t.Context(), expression, []byte(strings.TrimSpace(content)))
124 assert.NilError(t, err)
125 assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
126}
127
128func TestEvaluateExpressionCancellation(t *testing.T) {
129 expression := `.cpus = 2`
130 content := `cpus: null`
131 ctx, cancel := context.WithCancel(t.Context())
132 cancel() // cancel immediately
133
134 _, err := EvaluateExpression(ctx, expression, []byte(content))
135 assert.ErrorContains(t, err, context.Canceled.Error())
136}
137
138func TestJoin(t *testing.T) {
139 tests := []struct {
140 name string
141 input []string
142 expected string
143 }{
144 {
145 name: "multiple values",
146 input: []string{"foo", "bar", "baz"},
147 expected: "foo | bar | baz",
148 },
149 {
150 name: "one value",
151 input: []string{"foo"},
152 expected: "foo",
153 },
154 {
155 name: "empty values",
156 input: []string{},
157 expected: "",
158 },
159 {
160 name: "nil values",
161 input: nil,
162 expected: "",
163 },
164 }
165
166 for _, test := range tests {
167 t.Run(test.name, func(t *testing.T) {
168 actual := Join(test.input)
169 assert.Equal(t, test.expected, actual)
170 })
171 }
172}