forked from nushell/nushell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection.rs
441 lines (399 loc) · 14.9 KB
/
redirection.rs
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
use nu_test_support::fs::{file_contents, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
#[test]
fn redirect_err() {
Playground::setup("redirect_err_test", |dirs, _sandbox| {
let output = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "asdfasdfasdf.txt"; nu --testbin echo_env_stderr BAZ err> a.txt; open a.txt"#
);
assert!(output.out.contains("asdfasdfasdf.txt"));
// check append mode
let output = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "asdfasdfasdf.txt"; nu --testbin echo_env_stderr BAZ err>> a.txt; open a.txt"#
);
let v: Vec<_> = output.out.match_indices("asdfasdfasdf.txt").collect();
assert_eq!(v.len(), 2);
})
}
#[test]
fn redirect_outerr() {
Playground::setup("redirect_outerr_test", |dirs, _sandbox| {
let output = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "asdfasdfasdf.txt"; nu --testbin echo_env_stderr BAZ out+err> a.txt; open a.txt"#
);
assert!(output.out.contains("asdfasdfasdf.txt"));
let output = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "asdfasdfasdf.txt"; nu --testbin echo_env_stderr BAZ o+e>> a.txt; open a.txt"#
);
let v: Vec<_> = output.out.match_indices("asdfasdfasdf.txt").collect();
assert_eq!(v.len(), 2);
})
}
#[test]
fn redirect_out() {
Playground::setup("redirect_out_test", |dirs, _sandbox| {
let output = nu!(
cwd: dirs.test(),
"echo 'hello' out> a; open a"
);
assert!(output.out.contains("hello"));
let output = nu!(
cwd: dirs.test(),
"echo 'hello' out>> a; open a"
);
assert!(output.out.contains("hellohello"));
})
}
#[test]
fn two_lines_redirection() {
Playground::setup("redirections with two lines commands", |dirs, _| {
nu!(
cwd: dirs.test(),
r#"
def foobar [] {
'hello' out> output1.txt
'world' out> output2.txt
}
foobar"#);
let file_out1 = dirs.test().join("output1.txt");
let actual = file_contents(file_out1);
assert!(actual.contains("hello"));
let file_out2 = dirs.test().join("output2.txt");
let actual = file_contents(file_out2);
assert!(actual.contains("world"));
})
}
#[test]
fn separate_redirection() {
Playground::setup(
"external with both stdout and stderr messages, to different file",
|dirs, _| {
let expect_body = "message";
nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ o> out.txt e> err.txt"#,
);
// check for stdout redirection file.
let expected_out_file = dirs.test().join("out.txt");
let actual = file_contents(expected_out_file);
assert!(actual.contains(expect_body));
// check for stderr redirection file.
let expected_err_file = dirs.test().join("err.txt");
let actual = file_contents(expected_err_file);
assert!(actual.contains(expect_body));
nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ o>> out.txt e>> err.txt"#,
);
// check for stdout redirection file.
let expected_out_file = dirs.test().join("out.txt");
let actual = file_contents(expected_out_file);
let v: Vec<_> = actual.match_indices("message").collect();
assert_eq!(v.len(), 2);
// check for stderr redirection file.
let expected_err_file = dirs.test().join("err.txt");
let actual = file_contents(expected_err_file);
let v: Vec<_> = actual.match_indices("message").collect();
assert_eq!(v.len(), 2);
},
)
}
#[test]
fn same_target_redirection_with_too_much_stderr_not_hang_nushell() {
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
Playground::setup("external with many stderr message", |dirs, sandbox| {
let bytes: usize = 81920;
let mut large_file_body = String::with_capacity(bytes);
for _ in 0..bytes {
large_file_body.push('a');
}
sandbox.with_files(vec![FileWithContent("a_large_file.txt", &large_file_body)]);
nu!(
cwd: dirs.test(), pipeline(
"
$env.LARGE = (open --raw a_large_file.txt);
nu --testbin echo_env_stderr LARGE out+err> another_large_file.txt
"
),
);
let expected_file = dirs.test().join("another_large_file.txt");
let actual = file_contents(expected_file);
assert_eq!(actual, format!("{large_file_body}\n"));
// not hangs in append mode either.
let cloned_body = large_file_body.clone();
large_file_body.push_str(&format!("\n{cloned_body}"));
nu!(
cwd: dirs.test(), pipeline(
"
$env.LARGE = (open --raw a_large_file.txt);
nu --testbin echo_env_stderr LARGE out+err>> another_large_file.txt
"
),
);
let expected_file = dirs.test().join("another_large_file.txt");
let actual = file_contents(expected_file);
assert_eq!(actual, format!("{large_file_body}\n"));
})
}
#[test]
fn redirection_keep_exit_codes() {
Playground::setup("redirection preserves exit code", |dirs, _| {
let out = nu!(
cwd: dirs.test(),
"do -i { nu --testbin fail e> a.txt } | complete | get exit_code"
);
// needs to use contains "1", because it complete will output `Some(RawStream)`.
assert!(out.out.contains('1'));
});
}
#[test]
fn redirection_stderr_with_failed_program() {
Playground::setup("redirection stderr with failed program", |dirs, _| {
let out = nu!(
cwd: dirs.test(),
r#"$env.FOO = "bar"; nu --testbin echo_env_stderr_fail FOO e> file.txt; echo 3"#
);
// firstly echo 3 shouldn't run, because previous command runs to failed.
// second `file.txt` should contain "bar".
assert!(!out.out.contains('3'));
let expected_file = dirs.test().join("file.txt");
let actual = file_contents(expected_file);
assert_eq!(actual, "bar\n");
});
}
#[test]
fn redirection_with_non_zero_exit_code_should_stop_from_running() {
Playground::setup("redirection with non zero exit code", |dirs, _| {
for redirection in ["o>", "o>>", "e>", "e>>", "o+e>", "o+e>>"] {
let output = nu!(
cwd: dirs.test(),
&format!("nu --testbin fail {redirection} log.txt; echo 3")
);
assert!(!output.out.contains('3'));
}
});
Playground::setup("redirection with non zero exit code", |dirs, _| {
for (out, err) in [("o>", "e>"), ("o>>", "e>"), ("o>", "e>>"), ("o>>", "e>>")] {
let output = nu!(
cwd: dirs.test(),
&format!("nu --testbin fail {out} log.txt {err} err_log.txt; echo 3")
);
assert!(!output.out.contains('3'));
}
})
}
#[test]
fn redirection_with_pipeline_works() {
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
Playground::setup(
"external with stdout message with pipeline should write data",
|dirs, sandbox| {
let script_body = r"echo message";
let expect_body = "message";
sandbox.with_files(vec![FileWithContent("test.sh", script_body)]);
let actual = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env BAZ out> out.txt | describe; open out.txt"#,
);
assert!(actual.out.contains(expect_body));
// check append mode works
let actual = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env BAZ out>> out.txt | describe; open out.txt"#,
);
let v: Vec<_> = actual.out.match_indices("message").collect();
assert_eq!(v.len(), 2);
},
)
}
#[test]
fn redirect_support_variable() {
Playground::setup("redirect_out_support_variable", |dirs, _sandbox| {
let output = nu!(
cwd: dirs.test(),
"let x = 'tmp_file'; echo 'hello' out> $x; open tmp_file"
);
assert!(output.out.contains("hello"));
nu!(
cwd: dirs.test(),
"let x = 'tmp_file'; echo 'hello there' out+err> $x; open tmp_file"
);
// check for stdout redirection file.
let expected_out_file = dirs.test().join("tmp_file");
let actual = file_contents(expected_out_file);
assert!(actual.contains("hello there"));
// append mode support variable too.
let output = nu!(
cwd: dirs.test(),
"let x = 'tmp_file'; echo 'hello' out>> $x; open tmp_file"
);
let v: Vec<_> = output.out.match_indices("hello").collect();
assert_eq!(v.len(), 2);
let output = nu!(
cwd: dirs.test(),
"let x = 'tmp_file'; echo 'hello' out+err>> $x; open tmp_file"
);
// check for stdout redirection file.
let v: Vec<_> = output.out.match_indices("hello").collect();
assert_eq!(v.len(), 3);
})
}
#[test]
fn separate_redirection_support_variable() {
Playground::setup(
"external with both stdout and stderr messages, to different file",
|dirs, _| {
let expect_body = "message";
nu!(
cwd: dirs.test(),
r#"
let o_f = "out2.txt"
let e_f = "err2.txt"
$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ o> $o_f e> $e_f"#,
);
// check for stdout redirection file.
let expected_out_file = dirs.test().join("out2.txt");
let actual = file_contents(expected_out_file);
assert!(actual.contains(expect_body));
// check for stderr redirection file.
let expected_err_file = dirs.test().join("err2.txt");
let actual = file_contents(expected_err_file);
assert!(actual.contains(expect_body));
nu!(
cwd: dirs.test(),
r#"
let o_f = "out2.txt"
let e_f = "err2.txt"
$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ out>> $o_f err>> $e_f"#,
);
// check for stdout redirection file.
let expected_out_file = dirs.test().join("out2.txt");
let actual = file_contents(expected_out_file);
let v: Vec<_> = actual.match_indices("message").collect();
assert_eq!(v.len(), 2);
// check for stderr redirection file.
let expected_err_file = dirs.test().join("err2.txt");
let actual = file_contents(expected_err_file);
let v: Vec<_> = actual.match_indices("message").collect();
assert_eq!(v.len(), 2);
},
)
}
#[test]
fn redirection_should_have_a_target() {
Playground::setup("redirection_should_have_a_target", |dirs, _| {
let scripts = [
"echo asdf o+e>",
"echo asdf o>",
"echo asdf e>",
"echo asdf o> e>",
"echo asdf o> tmp.txt e>",
"echo asdf o> e> tmp.txt",
"echo asdf o> | ignore",
"echo asdf o>; echo asdf",
];
for code in scripts {
let actual = nu!(cwd: dirs.test(), code);
assert!(
actual.err.contains("expected redirection target",),
"should be error, code: {code}",
);
assert!(
!dirs.test().join("tmp.txt").exists(),
"No file should be created on error: {code}",
);
}
});
}
#[test]
fn redirection_with_out_pipe() {
use nu_test_support::playground::Playground;
Playground::setup("redirection with out pipes", |dirs, _| {
// check for stdout
let actual = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ err> tmp_file | str length"#,
);
assert_eq!(actual.out, "8");
// check for stderr redirection file.
let expected_out_file = dirs.test().join("tmp_file");
let actual_len = file_contents(expected_out_file).len();
assert_eq!(actual_len, 8);
})
}
#[test]
fn redirection_with_err_pipe() {
use nu_test_support::playground::Playground;
Playground::setup("redirection with err pipe", |dirs, _| {
// check for stdout
let actual = nu!(
cwd: dirs.test(),
r#"$env.BAZ = "message"; nu --testbin echo_env_mixed out-err BAZ BAZ out> tmp_file e>| str length"#,
);
assert_eq!(actual.out, "8");
// check for stdout redirection file.
let expected_out_file = dirs.test().join("tmp_file");
let actual_len = file_contents(expected_out_file).len();
assert_eq!(actual_len, 8);
})
}
#[test]
fn no_redirection_with_outerr_pipe() {
Playground::setup("redirection does not accept outerr pipe", |dirs, _| {
for redirect_type in ["o>", "e>", "o+e>"] {
let actual = nu!(
cwd: dirs.test(),
&format!("echo 3 {redirect_type} a.txt o+e>| str length")
);
assert!(actual.err.contains("not allowed to use with redirection"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
}
// test for separate redirection
let actual = nu!(
cwd: dirs.test(),
"echo 3 o> a.txt e> b.txt o+e>| str length"
);
assert!(actual.err.contains("not allowed to use with redirection"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
assert!(
!dirs.test().join("b.txt").exists(),
"No file should be created on error"
);
});
}
#[test]
fn no_duplicate_redirection() {
Playground::setup("redirection does not accept duplicate", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo 3 o> a.txt o> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
let actual = nu!(
cwd: dirs.test(),
"echo 3 e> a.txt e> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
});
}