matklad / xshell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cmd::run does not respect overrident stdin contents

MikailBag opened this issue · comments

https://github.com/matklad/xshell/blob/master/src/lib.rs#L395

run simply creates an std Command with correct args and executes it. Stdin bytes set with Cmd::stdin do not take effect, and stdin is still inherited.

Seems like stdin redirection works:

  • xshell/src/lib.rs

    Lines 1013 to 1019 in c543794

    if let Some(stdin_contents) = self.data.stdin_contents.clone() {
    let mut stdin = child.stdin.take().unwrap();
    io_thread = Some(std::thread::spawn(move || {
    stdin.write_all(&stdin_contents)?;
    stdin.flush()
    }));
    }
  • xshell/tests/it/main.rs

    Lines 213 to 240 in 11ab785

    #[test]
    fn stdin_redirection() {
    let sh = setup();
    let lines = "\
    foo
    baz
    bar
    ";
    let output = cmd!(sh, "xecho -i").stdin(lines).read().unwrap().replace("\r\n", "\n");
    assert_eq!(
    output,
    "\
    foo
    baz
    bar"
    )
    }
    #[test]
    fn no_deadlock() {
    let sh = setup();
    let mut data = "All the work and now paly made Jack a dull boy.\n".repeat(1 << 20);
    data.pop();
    let res = cmd!(sh, "xecho -i").stdin(&data).read().unwrap();
    assert_eq!(data, res);
    }

If it's not working for your case, please open a new issue with a specific example that doesn't work. Thanks!