| 1 | package cli_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/tomwright/dasel/v3/internal/cli" |
| 11 | ) |
| 12 | |
| 13 | func runDasel(args []string, in []byte) ([]byte, []byte, error) { |
| 14 | stdOut := bytes.NewBuffer([]byte{}) |
| 15 | stdErr := bytes.NewBuffer([]byte{}) |
| 16 | stdIn := bytes.NewReader(in) |
| 17 | |
| 18 | originalArgs := os.Args |
| 19 | defer func() { |
| 20 | os.Args = originalArgs |
| 21 | }() |
| 22 | |
| 23 | os.Args = append([]string{"dasel", "query"}, args...) |
| 24 | |
| 25 | _, err := cli.Run(stdIn, stdOut, stdErr) |
| 26 | |
| 27 | return stdOut.Bytes(), stdErr.Bytes(), err |
| 28 | } |
| 29 | |
| 30 | type testCase struct { |
| 31 | args []string |
| 32 | in []byte |
| 33 | stdout []byte |
| 34 | stderr []byte |
| 35 | err error |
| 36 | } |
| 37 | |
| 38 | func runTest(tc testCase) func(t *testing.T) { |
| 39 | return func(t *testing.T) { |
| 40 | if tc.stdout == nil { |
| 41 | tc.stdout = []byte{} |
| 42 | } |
| 43 | if tc.stderr == nil { |
| 44 | tc.stderr = []byte{} |
| 45 | } |
| 46 | |
| 47 | gotStdOut, gotStdErr, gotErr := runDasel(tc.args, tc.in) |
| 48 | if !errors.Is(gotErr, tc.err) && !errors.Is(tc.err, gotErr) { |
| 49 | t.Errorf("expected error %v, got %v", tc.err, gotErr) |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | if !reflect.DeepEqual(tc.stderr, gotStdErr) { |
| 54 | t.Errorf("expected stderr %s, got %s", string(tc.stderr), string(gotStdErr)) |
| 55 | } |
| 56 | |
| 57 | if !reflect.DeepEqual(tc.stdout, gotStdOut) { |
| 58 | t.Errorf("expected stdout %s, got %s", string(tc.stdout), string(gotStdOut)) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestRun(t *testing.T) { |
| 64 | t.Run("complex set", func(t *testing.T) { |
| 65 | t.Run("set nested with spread", runTest(testCase{ |
| 66 | args: []string{"-i", "json", "-o", "json", "--root", `user = {user..., name: {"first": $this.user.name, "last": "Doe"}}`}, |
| 67 | in: []byte(`{"user": {"name": "John"}}`), |
| 68 | stdout: []byte(`{ |
| 69 | "user": { |
| 70 | "name": { |
| 71 | "first": "John", |
| 72 | "last": "Doe" |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | `), |
| 77 | stderr: nil, |
| 78 | err: nil, |
| 79 | })) |
| 80 | t.Run("set nested", runTest(testCase{ |
| 81 | args: []string{"-i", "json", "-o", "json", "--root", `user.name = {"first": user.name, "last": "Doe"}`}, |
| 82 | in: []byte(`{"user": {"name": "John"}}`), |
| 83 | stdout: []byte(`{ |
| 84 | "user": { |
| 85 | "name": { |
| 86 | "first": "John", |
| 87 | "last": "Doe" |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | `), |
| 92 | stderr: nil, |
| 93 | err: nil, |
| 94 | })) |
| 95 | t.Run("set nested with localised group", runTest(testCase{ |
| 96 | args: []string{"-i", "json", "-o", "json", "--root", `user.(name = {"first": name, "last": "Doe"})`}, |
| 97 | in: []byte(`{"user": {"name": "John"}}`), |
| 98 | stdout: []byte(`{ |
| 99 | "user": { |
| 100 | "name": { |
| 101 | "first": "John", |
| 102 | "last": "Doe" |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | `), |
| 107 | stderr: nil, |
| 108 | err: nil, |
| 109 | })) |
| 110 | t.Run("set recursive descent", func(t *testing.T) { |
| 111 | |
| 112 | t.Run("property", runTest(testCase{ |
| 113 | args: []string{"-i", "json", "-o", "json", "--root", `$root..x.each($this = $this+1)`}, |
| 114 | in: []byte(`[{"x":1},{"x":2},{"x":3}]`), |
| 115 | stdout: []byte(`[ |
| 116 | { |
| 117 | "x": 2 |
| 118 | }, |
| 119 | { |
| 120 | "x": 3 |
| 121 | }, |
| 122 | { |
| 123 | "x": 4 |
| 124 | } |
| 125 | ] |
| 126 | `), |
| 127 | stderr: nil, |
| 128 | err: nil, |
| 129 | })) |
| 130 | |
| 131 | t.Run("index", runTest(testCase{ |
| 132 | args: []string{"-i", "json", "-o", "json", "--root", `$root..[1].each($this = $this+1)`}, |
| 133 | in: []byte(`[ {"x":[1,2,3]} , {"y":[4,5,6]} , {"z":[7,8,9]} ]`), |
| 134 | stdout: []byte(`[ |
| 135 | { |
| 136 | "x": [ |
| 137 | 1, |
| 138 | 3, |
| 139 | 3 |
| 140 | ] |
| 141 | }, |
| 142 | { |
| 143 | "y": [ |
| 144 | 4, |
| 145 | 6, |
| 146 | 6 |
| 147 | ] |
| 148 | }, |
| 149 | { |
| 150 | "z": [ |
| 151 | 7, |
| 152 | 9, |
| 153 | 9 |
| 154 | ] |
| 155 | } |
| 156 | ] |
| 157 | `), |
| 158 | stderr: nil, |
| 159 | err: nil, |
| 160 | })) |
| 161 | |
| 162 | t.Run("wildcard", runTest(testCase{ |
| 163 | args: []string{"-i", "json", "-o", "json", "--root", `$root..*.each($this = 4)`}, |
| 164 | in: []byte(`[{"x":1},{"x":2},{"x":3}]`), |
| 165 | stdout: []byte(`[ |
| 166 | { |
| 167 | "x": 4 |
| 168 | }, |
| 169 | { |
| 170 | "x": 4 |
| 171 | }, |
| 172 | { |
| 173 | "x": 4 |
| 174 | } |
| 175 | ] |
| 176 | `), |
| 177 | stderr: nil, |
| 178 | err: nil, |
| 179 | })) |
| 180 | |
| 181 | }) |
| 182 | t.Run("create object with empty stdin", runTest(testCase{ |
| 183 | args: []string{`{"name":"Tom"}`}, |
| 184 | in: []byte{}, |
| 185 | stdout: []byte(`{ |
| 186 | "name": "Tom" |
| 187 | } |
| 188 | `), |
| 189 | stderr: nil, |
| 190 | err: nil, |
| 191 | })) |
| 192 | }) |
| 193 | t.Run("set search", runTest(testCase{ |
| 194 | args: []string{"-i", "json", "-o", "json", "--root", `search(has("x")).each(x = x+1)`}, |
| 195 | in: []byte(`[{"x":1},{"x":2},{"x":3}]`), |
| 196 | stdout: []byte(`[ |
| 197 | { |
| 198 | "x": 2 |
| 199 | }, |
| 200 | { |
| 201 | "x": 3 |
| 202 | }, |
| 203 | { |
| 204 | "x": 4 |
| 205 | } |
| 206 | ] |
| 207 | `), |
| 208 | stderr: nil, |
| 209 | err: nil, |
| 210 | })) |
| 211 | t.Run("recursive descent", func(t *testing.T) { |
| 212 | t.Run("wildcard", runTest(testCase{ |
| 213 | args: []string{"-i", "json", `..*`}, |
| 214 | in: []byte(`{ |
| 215 | "user": { |
| 216 | "name": "Alice", |
| 217 | "roles": ["admin", "editor"], |
| 218 | "meta": { |
| 219 | "active": true, |
| 220 | "score": 42 |
| 221 | } |
| 222 | }, |
| 223 | "tags": ["x", "y"], |
| 224 | "count": 10 |
| 225 | }`), |
| 226 | stdout: []byte(`[ |
| 227 | "Alice", |
| 228 | "admin", |
| 229 | "editor", |
| 230 | true, |
| 231 | 42, |
| 232 | "x", |
| 233 | "y", |
| 234 | 10 |
| 235 | ] |
| 236 | `), |
| 237 | stderr: nil, |
| 238 | err: nil, |
| 239 | })) |
| 240 | |
| 241 | t.Run("property", runTest(testCase{ |
| 242 | args: []string{"-i", "json", `..name`}, |
| 243 | in: []byte(`{ |
| 244 | "user": { |
| 245 | "name": "Alice", |
| 246 | "roles": ["admin", "editor"], |
| 247 | "meta": { |
| 248 | "active": true, |
| 249 | "score": 42 |
| 250 | } |
| 251 | }, |
| 252 | "tags": ["x", "y"], |
| 253 | "count": 10 |
| 254 | }`), |
| 255 | stdout: []byte(`[ |
| 256 | "Alice" |
| 257 | ] |
| 258 | `), |
| 259 | stderr: nil, |
| 260 | err: nil, |
| 261 | })) |
| 262 | |
| 263 | t.Run("property2", runTest(testCase{ |
| 264 | args: []string{"-i", "json", `..name`}, |
| 265 | in: []byte(`[{"name":"Tom"}, {"name":"Jim"}, {"foo": "Bar"}]`), |
| 266 | stdout: []byte(`[ |
| 267 | "Tom", |
| 268 | "Jim" |
| 269 | ] |
| 270 | `), |
| 271 | stderr: nil, |
| 272 | err: nil, |
| 273 | })) |
| 274 | |
| 275 | t.Run("index", runTest(testCase{ |
| 276 | args: []string{"-i", "json", `..[0]`}, |
| 277 | in: []byte(`{ |
| 278 | "user": { |
| 279 | "name": "Alice", |
| 280 | "roles": ["admin", "editor"], |
| 281 | "meta": { |
| 282 | "active": true, |
| 283 | "score": 42 |
| 284 | } |
| 285 | }, |
| 286 | "tags": ["x", "y"], |
| 287 | "count": 10 |
| 288 | }`), |
| 289 | stdout: []byte(`[ |
| 290 | "admin", |
| 291 | "x" |
| 292 | ] |
| 293 | `), |
| 294 | stderr: nil, |
| 295 | err: nil, |
| 296 | })) |
| 297 | }) |
| 298 | t.Run("any", func(t *testing.T) { |
| 299 | t.Run("true result", runTest(testCase{ |
| 300 | args: []string{"-i", "json", `users.any(age > 25)`}, |
| 301 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]}`), |
| 302 | stdout: []byte("true\n"), |
| 303 | stderr: nil, |
| 304 | err: nil, |
| 305 | })) |
| 306 | t.Run("false result", runTest(testCase{ |
| 307 | args: []string{"-i", "json", `users.any(age > 50)`}, |
| 308 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]}`), |
| 309 | stdout: []byte("false\n"), |
| 310 | stderr: nil, |
| 311 | err: nil, |
| 312 | })) |
| 313 | }) |
| 314 | t.Run("all", func(t *testing.T) { |
| 315 | t.Run("true result", runTest(testCase{ |
| 316 | args: []string{"-i", "json", `users.all(age > 18)`}, |
| 317 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]}`), |
| 318 | stdout: []byte("true\n"), |
| 319 | stderr: nil, |
| 320 | err: nil, |
| 321 | })) |
| 322 | t.Run("false result", runTest(testCase{ |
| 323 | args: []string{"-i", "json", `users.all(age > 25)`}, |
| 324 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]}`), |
| 325 | stdout: []byte("false\n"), |
| 326 | stderr: nil, |
| 327 | err: nil, |
| 328 | })) |
| 329 | }) |
| 330 | t.Run("ternary", func(t *testing.T) { |
| 331 | t.Run("true literal", runTest(testCase{ |
| 332 | args: []string{`true ? "yes" : "no"`}, |
| 333 | in: []byte{}, |
| 334 | stdout: []byte("\"yes\"\n"), |
| 335 | err: nil, |
| 336 | })) |
| 337 | t.Run("false literal", runTest(testCase{ |
| 338 | args: []string{`false ? "yes" : "no"`}, |
| 339 | in: []byte{}, |
| 340 | stdout: []byte("\"no\"\n"), |
| 341 | err: nil, |
| 342 | })) |
| 343 | t.Run("with json data true", runTest(testCase{ |
| 344 | args: []string{"-i", "json", `age >= 18 ? "adult" : "minor"`}, |
| 345 | in: []byte(`{"age": 25}`), |
| 346 | stdout: []byte("\"adult\"\n"), |
| 347 | err: nil, |
| 348 | })) |
| 349 | t.Run("with json data false", runTest(testCase{ |
| 350 | args: []string{"-i", "json", `age >= 18 ? "adult" : "minor"`}, |
| 351 | in: []byte(`{"age": 10}`), |
| 352 | stdout: []byte("\"minor\"\n"), |
| 353 | err: nil, |
| 354 | })) |
| 355 | t.Run("nested ternary", runTest(testCase{ |
| 356 | args: []string{`true ? (false ? "a" : "b") : "c"`}, |
| 357 | in: []byte{}, |
| 358 | stdout: []byte("\"b\"\n"), |
| 359 | err: nil, |
| 360 | })) |
| 361 | t.Run("with comparison", runTest(testCase{ |
| 362 | args: []string{"-i", "json", `score >= 90 ? "A" : (score >= 80 ? "B" : "C")`}, |
| 363 | in: []byte(`{"score": 85}`), |
| 364 | stdout: []byte("\"B\"\n"), |
| 365 | err: nil, |
| 366 | })) |
| 367 | t.Run("ternary in map", runTest(testCase{ |
| 368 | args: []string{"-i", "json", `items.map(val >= 5 ? "big" : "small")`}, |
| 369 | in: []byte(`{"items": [{"val": 10}, {"val": 3}, {"val": 7}]}`), |
| 370 | stdout: []byte(`[ |
| 371 | "big", |
| 372 | "small", |
| 373 | "big" |
| 374 | ] |
| 375 | `), |
| 376 | err: nil, |
| 377 | })) |
| 378 | t.Run("ternary returns number", runTest(testCase{ |
| 379 | args: []string{`true ? 42 : 0`}, |
| 380 | in: []byte{}, |
| 381 | stdout: []byte("42\n"), |
| 382 | err: nil, |
| 383 | })) |
| 384 | t.Run("ternary with arithmetic", runTest(testCase{ |
| 385 | args: []string{`true ? 1 + 2 : 3 + 4`}, |
| 386 | in: []byte{}, |
| 387 | stdout: []byte("3\n"), |
| 388 | err: nil, |
| 389 | })) |
| 390 | t.Run("ternary with logical operators", runTest(testCase{ |
| 391 | args: []string{"-i", "json", `a > 1 && b > 5 ? "both" : "nope"`}, |
| 392 | in: []byte(`{"a": 5, "b": 10}`), |
| 393 | stdout: []byte("\"both\"\n"), |
| 394 | err: nil, |
| 395 | })) |
| 396 | t.Run("chained property in condition", runTest(testCase{ |
| 397 | args: []string{"-i", "json", `user.active ? "yes" : "no"`}, |
| 398 | in: []byte(`{"user": {"active": true}}`), |
| 399 | stdout: []byte("\"yes\"\n"), |
| 400 | err: nil, |
| 401 | })) |
| 402 | t.Run("ternary with json output", runTest(testCase{ |
| 403 | args: []string{"-i", "json", "-o", "json", `active ? {status: "on"} : {status: "off"}`}, |
| 404 | in: []byte(`{"active": true}`), |
| 405 | stdout: []byte(`{ |
| 406 | "status": "on" |
| 407 | } |
| 408 | `), |
| 409 | err: nil, |
| 410 | })) |
| 411 | t.Run("ternary returns true", runTest(testCase{ |
| 412 | args: []string{`true ? true : false`}, |
| 413 | in: []byte{}, |
| 414 | stdout: []byte("true\n"), |
| 415 | err: nil, |
| 416 | })) |
| 417 | }) |
| 418 | t.Run("compact", func(t *testing.T) { |
| 419 | t.Run("json", runTest(testCase{ |
| 420 | args: []string{"-i", "json", "-o", "json", "--compact"}, |
| 421 | in: []byte(`{"name": "Tom", "age": 30}`), |
| 422 | stdout: []byte("{\"name\":\"Tom\",\"age\":30}\n"), |
| 423 | err: nil, |
| 424 | })) |
| 425 | t.Run("json nested", runTest(testCase{ |
| 426 | args: []string{"-i", "json", "-o", "json", "--compact"}, |
| 427 | in: []byte(`{"user": {"name": "Tom"}, "items": [1, 2, 3]}`), |
| 428 | stdout: []byte("{\"user\":{\"name\":\"Tom\"},\"items\":[1,2,3]}\n"), |
| 429 | err: nil, |
| 430 | })) |
| 431 | }) |
| 432 | t.Run("merge", func(t *testing.T) { |
| 433 | t.Run("deep", runTest(testCase{ |
| 434 | args: []string{"-i", "json", "-o", "json", |
| 435 | `merge({"a": {"x": 1, "y": 2}}, {"a": {"y": 3, "z": 4}})`}, |
| 436 | in: []byte{}, |
| 437 | stdout: []byte("{\n \"a\": {\n \"x\": 1,\n \"y\": 3,\n \"z\": 4\n }\n}\n"), |
| 438 | })) |
| 439 | }) |
| 440 | t.Run("count", func(t *testing.T) { |
| 441 | t.Run("some match", runTest(testCase{ |
| 442 | args: []string{"-i", "json", `users.count(age > 25)`}, |
| 443 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}, {"name": "Charlie", "age": 35}]}`), |
| 444 | stdout: []byte("2\n"), |
| 445 | stderr: nil, |
| 446 | err: nil, |
| 447 | })) |
| 448 | t.Run("none match", runTest(testCase{ |
| 449 | args: []string{"-i", "json", `users.count(age > 50)`}, |
| 450 | in: []byte(`{"users": [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]}`), |
| 451 | stdout: []byte("0\n"), |
| 452 | stderr: nil, |
| 453 | err: nil, |
| 454 | })) |
| 455 | }) |
| 456 | } |