| 1 | |
| 2 | |
| 3 | |
| 4 | package yqutil |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "gotest.tools/v3/assert" |
| 12 | ) |
| 13 | |
| 14 | func TestValidateContent(t *testing.T) { |
| 15 | content := ` |
| 16 | # comment |
| 17 | foo: bar |
| 18 | ` |
| 19 | err := ValidateContent([]byte(content)) |
| 20 | assert.NilError(t, err) |
| 21 | } |
| 22 | |
| 23 | func 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 | |
| 33 | func TestEvaluateExpressionEmpty(t *testing.T) { |
| 34 | expression := "" |
| 35 | content := ` |
| 36 | foo: 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 | |
| 44 | func TestEvaluateExpressionSimple(t *testing.T) { |
| 45 | expression := `.cpus = 2 | .memory = "2GiB"` |
| 46 | content := ` |
| 47 | # CPUs |
| 48 | cpus: null |
| 49 | |
| 50 | # Memory size |
| 51 | memory: null |
| 52 | ` |
| 53 | |
| 54 | expected := ` |
| 55 | # CPUs |
| 56 | cpus: 2 |
| 57 | |
| 58 | # Memory size |
| 59 | memory: 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 | |
| 66 | func 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 |
| 72 | mounts: |
| 73 | - location: "~" |
| 74 | # Configure the mountPoint inside the guest. |
| 75 | # 🟢 Builtin default: value of location |
| 76 | mountPoint: null |
| 77 | ` |
| 78 | |
| 79 | |
| 80 | |
| 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 |
| 85 | mounts: |
| 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 | |
| 98 | func 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 | |
| 104 | func TestEvaluateMergeExpression(t *testing.T) { |
| 105 | expression := `select(di == 0) * select(di == 1)` |
| 106 | content := ` |
| 107 | yolo: true |
| 108 | foo: |
| 109 | bar: 1 |
| 110 | baz: 2 |
| 111 | --- |
| 112 | foo: |
| 113 | bar: 3 |
| 114 | fomo: false |
| 115 | ` |
| 116 | expected := ` |
| 117 | yolo: true |
| 118 | foo: |
| 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 | |
| 128 | func TestEvaluateExpressionCancellation(t *testing.T) { |
| 129 | expression := `.cpus = 2` |
| 130 | content := `cpus: null` |
| 131 | ctx, cancel := context.WithCancel(t.Context()) |
| 132 | cancel() |
| 133 | |
| 134 | _, err := EvaluateExpression(ctx, expression, []byte(content)) |
| 135 | assert.ErrorContains(t, err, context.Canceled.Error()) |
| 136 | } |
| 137 | |
| 138 | func 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 | } |