File: weird-args.rs

package info (click to toggle)
rust-subprocess 0.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download | duplicates (2)
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
extern crate subprocess;

use std::io::Read;
use std::path::Path;
use subprocess::{Popen, PopenConfig, Redirection};

fn just_echo_path() -> String {
    let prog = Path::new(&::std::env::args().next().unwrap()).to_owned();
    let prog = prog.parent().unwrap(); // dirname
    let prog = prog.parent().unwrap(); // parent dir
    prog.join("just-echo").to_str().unwrap().to_owned()
}

#[test]
fn weird_args() {
    // This is mostly relevant for Windows: test whether
    // assemble_cmdline does a good job with arguments with
    // metacharacters.
    for &arg in [
        "x", "", " ", "  ", r" \ ", r" \\ ", r" \\\ ", r#"""#, r#""""#, r#"\"\\""#,
    ]
    .iter()
    {
        println!("running {:?} {:?}", arg, just_echo_path());
        let mut p = Popen::create(
            &[just_echo_path(), arg.to_owned()],
            PopenConfig {
                stdout: Redirection::Pipe,
                ..Default::default()
            },
        )
        .unwrap();
        let mut output = p.stdout.take().unwrap();
        let mut output_str = String::new();
        output.read_to_string(&mut output_str).unwrap();
        assert_eq!(output_str, arg);
    }
}