jpx 0.5.0

JMESPath CLI with 490+ extended functions - a powerful jq alternative
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Extended integration tests for jpx CLI streaming mode (--stream / --each).
//!
//! Covers: large inputs, malformed line recovery, multiple expressions,
//! file-based input, complex expressions, and edge cases.

use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;

fn jpx() -> Command {
    assert_cmd::cargo_bin_cmd!("jpx")
}

// ---------------------------------------------------------------------------
// 1. Large NDJSON
// ---------------------------------------------------------------------------

#[test]
fn stream_large_ndjson() {
    let input: String = (0..1000)
        .map(|i| format!(r#"{{"id":{i}}}"#))
        .collect::<Vec<_>>()
        .join("\n");

    let expected: String = (0..1000)
        .map(|i| i.to_string())
        .collect::<Vec<_>>()
        .join("\n")
        + "\n";

    jpx()
        .args(["--stream", "id", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout(expected);
}

#[test]
fn stream_large_ndjson_with_filter() {
    // Each line is an object; use a pipe to extract and filter.
    // Lines where id >= 990 -> 10 results (990..999)
    let input: String = (0..1000)
        .map(|i| format!(r#"{{"id":{i}}}"#))
        .collect::<Vec<_>>()
        .join("\n");

    // The expression `id` yields a number; lines with id < 990 we want to skip.
    // Streaming evaluates per-line, so we can't use array filters directly.
    // Instead we use an if_expr or rely on null-skip behavior.
    // Actually: we can use a multi-select that returns null for non-matching.
    // Simplest: use the `if` extension function: if(id >= `990`, id, null)
    // Since nulls are skipped, only matching lines appear.
    let expected: String = (990..1000)
        .map(|i| i.to_string())
        .collect::<Vec<_>>()
        .join("\n")
        + "\n";

    jpx()
        .args([
            "--stream",
            r#"if(id >= `990`, id, null)"#,
            "--color",
            "never",
        ])
        .write_stdin(input)
        .assert()
        .success()
        .stdout(expected);
}

// ---------------------------------------------------------------------------
// 2. Malformed line recovery
// ---------------------------------------------------------------------------

#[test]
fn stream_skips_malformed_lines() {
    let input = r#"{"id":1}
not json
{"id":2}
{broken
{"id":3}"#;

    jpx()
        .args(["--stream", "id", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("1\n2\n3\n");
}

#[test]
fn stream_malformed_stderr() {
    let input = "not json\n";

    jpx()
        .args(["--stream", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stderr(predicate::str::contains("Failed to parse JSON"));
}

#[test]
fn stream_malformed_quiet() {
    let input = "not json\n";

    jpx()
        .args(["--stream", "-q", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stderr(predicate::str::is_empty());
}

// ---------------------------------------------------------------------------
// 3. Multiple expressions (chained via -e)
// ---------------------------------------------------------------------------

#[test]
fn stream_multiple_expressions() {
    let input = r#"{"user":{"name":"alice"}}
{"user":{"name":"bob"}}"#;

    // -e user -e name should chain: first extracts .user, second extracts .name
    jpx()
        .args([
            "--stream", "-e", "user", "-e", "name", "-r", "--color", "never",
        ])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("alice\nbob\n");
}

#[test]
fn stream_pipeline_stage_error_skips_line() {
    // The first stage errors (unknown function). The whole line must be skipped
    // -- no output -- rather than running the later stage on the un-transformed
    // value and emitting it anyway.
    jpx()
        .args([
            "--stream",
            "-e",
            "nonexistent_fn(@)",
            "-e",
            "@",
            "--color",
            "never",
        ])
        .write_stdin("{\"a\":1}\n")
        .assert()
        .success()
        .stdout("")
        .stderr(predicate::str::contains("Expression error"));
}

#[test]
fn stream_output_to_file() {
    // --output must be honoured in streaming mode (previously silently ignored,
    // with everything going to stdout).
    let file = tempfile::NamedTempFile::new().unwrap();
    jpx()
        .args(["--stream", "a", "--color", "never", "-o"])
        .arg(file.path())
        .write_stdin("{\"a\":1}\n{\"a\":2}\n")
        .assert()
        .success()
        .stdout("");
    let contents = std::fs::read_to_string(file.path()).unwrap();
    assert_eq!(contents, "1\n2\n");
}

// ---------------------------------------------------------------------------
// 4. Complex expressions
// ---------------------------------------------------------------------------

#[test]
fn stream_pipe_expression() {
    let input = r#"{"name":"alice"}
{"name":"bob"}"#;

    jpx()
        .args(["--stream", "name | upper(@)", "-r", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("ALICE\nBOB\n");
}

#[test]
fn stream_multi_select_hash() {
    let input = r#"{"id":1,"name":"alice"}
{"id":2,"name":"bob"}"#;

    jpx()
        .args([
            "--stream",
            "{id: id, upper: upper(name)}",
            "--color",
            "never",
        ])
        .write_stdin(input)
        .assert()
        .success()
        .stdout(
            r#"{"id":1,"upper":"ALICE"}
{"id":2,"upper":"BOB"}
"#,
        );
}

#[test]
fn stream_filter_expression() {
    // Each line contains an array; filter elements > 50
    let input = r#"[10,60,30,80]
[5,55,95,40]"#;

    jpx()
        .args(["--stream", "[?@ > `50`]", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("[60,80]\n[55,95]\n");
}

// ---------------------------------------------------------------------------
// 5. File input
// ---------------------------------------------------------------------------

#[test]
fn stream_from_file() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    tmp.write_all(b"{\"id\":1}\n{\"id\":2}\n{\"id\":3}\n")
        .unwrap();

    jpx()
        .args(["--stream", "-f"])
        .arg(tmp.path())
        .args(["id", "--color", "never"])
        .assert()
        .success()
        .stdout("1\n2\n3\n");
}

// ---------------------------------------------------------------------------
// 6. Edge cases
// ---------------------------------------------------------------------------

#[test]
fn stream_all_malformed() {
    let input = "not json\nalso bad\n{broken\n";

    jpx()
        .args(["--stream", "-q", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout(predicate::str::is_empty());
}

#[test]
fn stream_unicode() {
    // Use pre-composed e-acute directly to avoid decomposition mismatches
    let input =
        "{\"msg\":\"hello world\"}\n{\"msg\":\"caf\u{00e9}\"}\n{\"msg\":\"日本語テスト\"}\n";

    jpx()
        .args(["--stream", "msg", "-r", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("hello world\ncaf\u{00e9}\n日本語テスト\n");
}

#[test]
fn stream_mixed_types() {
    // Each line is a different JSON type
    let input = r#"{"a":1}
[1,2,3]
"hello"
42
true"#;

    jpx()
        .args(["--stream", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout(
            r#"{"a":1}
[1,2,3]
"hello"
42
true
"#,
        );
}

#[test]
fn stream_whitespace_lines() {
    let input = "  \n{\"id\":1}\n\t\n{\"id\":2}\n   \n";

    jpx()
        .args(["--stream", "id", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("1\n2\n");
}

// ---------------------------------------------------------------------------
// 7. Streaming CSV/TSV output
// ---------------------------------------------------------------------------

#[test]
fn stream_csv_basic() {
    let input = "{\"name\":\"alice\",\"age\":30}\n{\"name\":\"bob\",\"age\":25}\n";

    jpx()
        .args(["--stream", "--csv", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("age,name\n30,alice\n25,bob\n");
}

#[test]
fn stream_tsv_basic() {
    let input = "{\"name\":\"alice\",\"age\":30}\n{\"name\":\"bob\",\"age\":25}\n";

    jpx()
        .args(["--stream", "--tsv", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("age\tname\n30\talice\n25\tbob\n");
}

#[test]
fn stream_csv_with_expression() {
    let input = "{\"user\":\"alice\",\"score\":90,\"grade\":\"A\"}\n{\"user\":\"bob\",\"score\":75,\"grade\":\"B\"}\n";

    jpx()
        .args([
            "--stream",
            "--csv",
            "{name: user, score: score}",
            "--color",
            "never",
        ])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("name,score\nalice,90\nbob,75\n");
}

#[test]
fn stream_csv_nested_objects() {
    let input = "{\"name\":\"alice\",\"addr\":{\"city\":\"NYC\"}}\n{\"name\":\"bob\",\"addr\":{\"city\":\"LA\"}}\n";

    jpx()
        .args(["--stream", "--csv", "@", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("addr.city,name\nNYC,alice\nLA,bob\n");
}

#[test]
fn stream_csv_primitive_results() {
    let input = "{\"name\":\"alice\"}\n{\"name\":\"bob\"}\n";

    jpx()
        .args(["--stream", "--csv", "name", "--color", "never"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("value\nalice\nbob\n");
}

#[test]
fn stream_csv_missing_fields() {
    // Second record has extra field "email", first doesn't -- extra fields are silently dropped
    // since headers are derived from first record
    let input = "{\"name\":\"alice\",\"age\":30}\n{\"name\":\"bob\",\"email\":\"bob@test.com\"}\n";

    let output = jpx()
        .args(["--stream", "--csv", "@", "--color", "never"])
        .write_stdin(input)
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    // Headers come from first record
    assert!(stdout.starts_with("age,name\n"));
    // Second record has empty age, bob for name (email is not in headers)
    assert!(stdout.contains(",bob\n"));
}

#[test]
fn stream_csv_null_results_skipped() {
    // null results should still be skipped in CSV mode
    let input = "{\"name\":\"alice\"}\n{\"name\":null}\n{\"name\":\"carol\"}\n";

    // The expression `name` yields null for the second line, which gets skipped
    // But @  yields the full object which is not null, so test with a filter
    jpx()
        .args([
            "--stream",
            "--csv",
            "--color",
            "never",
            "if(name != 'null_sentinel', @, null)",
        ])
        .write_stdin(input)
        .assert()
        .success();
}

#[test]
fn stream_table_conflicts() {
    jpx()
        .args(["--stream", "--table", "@"])
        .write_stdin("{}")
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn stream_yaml_conflicts() {
    jpx()
        .args(["--stream", "--yaml", "@"])
        .write_stdin("{}")
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn stream_toml_conflicts() {
    jpx()
        .args(["--stream", "--toml", "@"])
        .write_stdin("{}")
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}