nu-command 0.113.0

Nushell's built-in commands
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
use nu_test_support::fs::Stub;
use nu_test_support::prelude::*;
use rstest::rstest;
use std::fs;

#[test]
fn def_with_trailing_comma() {
    let actual = nu!("def test-command [ foo: int, ] { $foo }; test-command 1");

    assert!(actual.out == "1");
}

#[test]
fn def_with_comment() {
    Playground::setup("def_with_comment", |dirs, _| {
        let data = "
#My echo
export def e [arg] {echo $arg}
            ";
        fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
        let actual = nu!(
            cwd: dirs.root(),
            "use def_test e; help e | to json -r"
        );

        assert!(actual.out.contains("My echo\\n\\n"));
    });
}

#[test]
fn def_with_param_comment() {
    Playground::setup("def_with_param_comment", |dirs, _| {
        let data = "
export def e [
param:string #My cool attractive param
] {echo $param};
            ";
        fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
        let actual = nu!(
            cwd: dirs.root(),
            "use def_test e; help e"
        );

        assert!(actual.out.contains("My cool attractive param"));
    })
}

#[test]
fn def_errors_with_no_space_between_params_and_name_1() {
    let actual = nu!("def test-command[] {}");

    assert!(actual.err.contains("expected space"));
}

#[test]
fn def_errors_with_no_space_between_params_and_name_2() {
    let actual = nu!("def --env test-command() {}");

    assert!(actual.err.contains("expected space"));
}

#[test]
fn def_errors_with_multiple_short_flags() {
    let actual = nu!("def test-command [ --long(-l)(-o) ] {}");

    assert!(actual.err.contains("expected only one short flag"));
}

#[test]
fn def_errors_with_comma_before_alternative_short_flag() {
    let actual = nu!("def test-command [ --long, (-l) ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[test]
fn def_errors_with_comma_before_equals() {
    let actual = nu!("def test-command [ foo, = 1 ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[test]
fn def_errors_with_colon_before_equals() {
    let actual = nu!("def test-command [ foo: = 1 ] {}");

    assert!(actual.err.contains("expected type"));
}

#[test]
fn def_errors_with_comma_before_colon() {
    let actual = nu!("def test-command [ foo, : int ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[test]
fn def_errors_with_multiple_colons() {
    let actual = nu!("def test-command [ foo::int ] {}");
    assert!(actual.err.contains("expected type"));
}

#[test]
fn def_errors_with_multiple_types() {
    let actual = nu!("def test-command [ foo:int:string ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[test]
fn def_errors_with_trailing_colon() {
    let actual = nu!("def test-command [ foo: int: ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[test]
fn def_errors_with_trailing_default_value() {
    let actual = nu!("def test-command [ foo: int = ] {}");

    assert!(actual.err.contains("expected default value"));
}

#[test]
fn def_errors_with_multiple_commas() {
    let actual = nu!("def test-command [ foo,,bar ] {}");

    assert!(actual.err.contains("expected parameter"));
}

#[rstest]
#[case::numeric("1234")]
#[case::filesize_like("5gib")]
#[case::caret("^foo")]
fn def_fails_with_invalid_name(#[case] alias: &str) -> Result {
    let code = format!("def {alias} = echo 'test'");
    let err = test().run(code).expect_parse_error()?;
    assert!(matches!(err, ParseError::CommandDefNotValid(_)));
    Ok(())
}

#[test]
fn def_with_list() {
    Playground::setup("def_with_list", |dirs, _| {
        let data = "
def e [
param: list
] {echo $param};
            ";
        fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
        let actual = nu!(
            cwd: dirs.root(),
            "source def_test; e [one] | to json -r"
        );

        assert!(actual.out.contains("one"));
    })
}

#[test]
fn def_with_default_list() {
    Playground::setup("def_with_default_list", |dirs, _| {
        let data = "
def f [
param: list = [one]
] {echo $param};
            ";
        fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
        let actual = nu!(
            cwd: dirs.root(),
            "source def_test; f | to json -r"
        );

        assert!(actual.out.contains(r#"["one"]"#));
    })
}

#[test]
fn def_with_paren_params() {
    let actual = nu!("def foo (x: int, y: int) { $x + $y }; foo 1 2");

    assert_eq!(actual.out, "3");
}

#[test]
fn def_default_value_shouldnt_restrict_explicit_type() {
    let actual = nu!("def foo [x: any = null] { $x }; foo 1");
    assert_eq!(actual.out, "1");
    let actual2 = nu!("def foo [--x: any = null] { $x }; foo --x 1");
    assert_eq!(actual2.out, "1");
}

#[test]
fn def_default_value_should_restrict_implicit_type() {
    let actual = nu!("def foo [x = 3] { $x }; foo 3.0");
    assert!(actual.err.contains("expected int"));
    let actual2 = nu!("def foo2 [--x = 3] { $x }; foo2 --x 3.0");
    assert!(actual2.err.contains("expected int"));
}

#[test]
fn def_wrapped_with_block() {
    let actual = nu!(
        "def --wrapped foo [...rest] { print ($rest | str join ',' ) }; foo --bar baz -- -q -u -x"
    );

    assert_eq!(actual.out, "--bar,baz,--,-q,-u,-x");
}

#[test]
fn def_wrapped_from_module() {
    let actual = nu!("module spam {
            export def --wrapped my-echo [...rest] { nu --testbin cococo ...$rest }
        }

        use spam
        spam my-echo foo -b -as -9 --abc -- -Dxmy=AKOO - bar
        ");

    assert!(
        actual
            .out
            .contains("foo -b -as -9 --abc -- -Dxmy=AKOO - bar")
    );
}

#[test]
fn def_cursed_env_flag_positions() {
    let actual = nu!("def spam --env [] { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual =
        nu!("def spam --env []: nothing -> nothing { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");
}

#[test]
#[ignore = "TODO: Investigate why it's not working, it might be the signature parsing"]
fn def_cursed_env_flag_positions_2() {
    let actual = nu!("def spam [] --env { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual = nu!("def spam [] { $env.SPAM = 'spam' } --env; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual =
        nu!("def spam []: nothing -> nothing { $env.SPAM = 'spam' } --env; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");
}

#[test]
fn export_def_cursed_env_flag_positions() {
    let actual = nu!("export def spam --env [] { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual =
        nu!("export def spam --env []: nothing -> nothing { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");
}

#[test]
#[ignore = "TODO: Investigate why it's not working, it might be the signature parsing"]
fn export_def_cursed_env_flag_positions_2() {
    let actual = nu!("export def spam [] --env { $env.SPAM = 'spam' }; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual = nu!("export def spam [] { $env.SPAM = 'spam' } --env; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");

    let actual =
        nu!("export def spam []: nothing -> nothing { $env.SPAM = 'spam' } --env; spam; $env.SPAM");
    assert_eq!(actual.out, "spam");
}

#[test]
fn def_cursed_wrapped_flag_positions() {
    let actual = nu!("def spam --wrapped [...rest] { $rest.0 }; spam --foo");
    assert_eq!(actual.out, "--foo");

    let actual = nu!("def spam --wrapped [...rest]: nothing -> nothing { $rest.0 }; spam --foo");
    assert_eq!(actual.out, "--foo");
}

#[test]
#[ignore = "TODO: Investigate why it's not working, it might be the signature parsing"]
fn def_cursed_wrapped_flag_positions_2() {
    let actual = nu!("def spam [...rest] --wrapped { $rest.0 }; spam --foo");
    assert_eq!(actual.out, "--foo");

    let actual = nu!("def spam [...rest] { $rest.0 } --wrapped; spam --foo");
    assert_eq!(actual.out, "--foo");

    let actual = nu!("def spam [...rest]: nothing -> nothing { $rest.0 } --wrapped; spam --foo");
    assert_eq!(actual.out, "--foo");
}

#[test]
fn def_wrapped_missing_rest_error() {
    let actual = nu!("def --wrapped spam [] {}");
    assert!(actual.err.contains("missing_positional"))
}

#[test]
fn def_wrapped_wrong_rest_type_error() {
    let actual = nu!("def --wrapped spam [...eggs: list<string>] { $eggs }");
    assert!(actual.err.contains("type_mismatch_help"));
    assert!(actual.err.contains("of ...eggs to 'string'"));
}

#[test]
fn def_env_wrapped() {
    let actual = nu!(
        "def --env --wrapped spam [...eggs: string] { $env.SPAM = $eggs.0 }; spam bacon; $env.SPAM"
    );
    assert_eq!(actual.out, "bacon");
}

#[test]
fn def_env_wrapped_no_help() {
    let actual = nu!("def --wrapped foo [...rest] { echo $rest }; foo -h | to json --raw");
    assert_eq!(actual.out, r#"["-h"]"#);
}

#[test]
fn def_wrapped_untyped_rest_strips_double_quoted_equals_value() {
    let actual = nu!(r#"def --wrapped foo [...rest] { $rest.0 }; foo expression="releases/4.x.x""#);
    assert_eq!(actual.out, "expression=releases/4.x.x");
}

#[test]
fn def_wrapped_untyped_rest_strips_single_quoted_equals_value() {
    let actual = nu!("def --wrapped foo [...rest] { $rest.0 }; foo expression='releases/4.x.x'");
    assert_eq!(actual.out, "expression=releases/4.x.x");
}

#[test]
fn def_wrapped_untyped_rest_expands_tilde() {
    // When accessing $args directly (e.g. `$rest | get 0`), tilde should be expanded to the
    // home dir. This is the core case from issue #17410.
    let expected = nu!("'~' | path expand");
    let actual = nu!("def --wrapped foo [...rest] { $rest | get 0 }; foo ~");
    assert_eq!(actual.out, expected.out);

    // Also works when forwarding to an external command
    let expected_ext = nu!("^echo ~");
    let actual_ext = nu!("def --wrapped foo [...rest] { ^echo ...$rest }; foo ~");
    assert_eq!(actual_ext.out, expected_ext.out);
}

#[test]
fn def_wrapped_explicit_string_rest_keeps_double_quoted_equals_value() {
    let actual =
        nu!(r#"def --wrapped foo [...rest: string] { $rest.0 }; foo --base="releases/4.x.x""#);
    assert_eq!(actual.out, r#"--base="releases/4.x.x""#);
}

#[test]
fn def_wrapped_explicit_string_rest_keeps_tilde_literal() {
    let actual = nu!("def --wrapped foo [...rest: string] { $rest.0 }; foo ~");
    assert_eq!(actual.out, "~");
}

#[test]
fn def_wrapped_dynamic_percent_builtin_preserves_no_arg_defaults() {
    Playground::setup(
        "def_wrapped_dynamic_percent_builtin_preserves_no_arg_defaults",
        |dirs, play| {
            play.with_files(&[Stub::EmptyFile("probe.txt")]);
            let actual = nu!(
                cwd: dirs.test(),
                "export def --wrapped builtin [arg1, ...args] { %($arg1) ...$args }; let direct = (ls | where name =~ 'probe.txt' | length); let wrapped = (builtin ls | where name =~ 'probe.txt' | length); [$direct $wrapped] | to nuon"
            );

            assert_eq!(actual.out, "[1, 1]");
        },
    );
}

#[test]
fn def_recursive_func_should_work() {
    let actual = nu!("def bar [] { let x = 1; ($x | foo) }; def foo [] { foo }");
    assert!(actual.err.is_empty());

    let actual = nu!("
def recursive [c: int] {
    if ($c == 0) { return }
    if ($c mod 2 > 0) {
        $in | recursive ($c - 1)
    } else {
        recursive ($c - 1)
    }
}");
    assert!(actual.err.is_empty());
}

#[test]
fn export_def_recursive_func_should_work() {
    let actual = nu!("export def bar [] { let x = 1; ($x | foo) }; export def foo [] { foo }");
    assert!(actual.err.is_empty());

    let actual = nu!("
export def recursive [c: int] {
    if ($c == 0) { return }
    if ($c mod 2 > 0) {
        $in | recursive ($c - 1)
    } else {
        recursive ($c - 1)
    }
}");
    assert!(actual.err.is_empty());
}