Skip to content

Allow serializing a constructed Command #212

Open
@mcky

Description

@mcky

I'm trying to convert some tests to use rexpect::spawn to work around some issues with tty handling, but I'm having trouble re-using all my existing assert_cmd as I can't construct a single string with the constructed command as the cmd field on Command is private.

With the following code

let mut cmd = Command::cargo_bin("my-cmd").unwrap();

cmd.current_dir("/tmp")
  .arg("--foo")
  .arg("bar")
  .env("FOO", "BAR");

I'd like to be able to get a string like

cd /tmp && FOO="BAR" ~the-cargo-path/target/debug/my-cmd --foo bar

Calling format!("{:?}", cmd) prints the following, with cmd in the format I'd like, but also stdin/stdout etc

Command { cmd: cd \"/tmp\" && FOO=\"BAR\" \"~the-cargo-path/target/debug/my-cmd\" \"--foo\" \"bar\", stdin: None, timeout: None }

I'm not sure what the most rust-y solution would be, but it would help if either:

  • cmd was marked public
  • a method was added to get cmd in a serialized form
  • a method was added to Command to get the underlying cmd, at which point I think I can serialize it myself

I'm happy to attempt a PR if an option from the above is chosen / another suggested

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions