History

support for multiple arguments with shell-script support (#450)

Sebastian Thiel committed 4 years ago

Commit d8e8b541 parent 1cb2e962 files · +44 −2Browse files

Showing the changes to git-command (2 of 2 files). Show all

Unified · Split

2 files
  1. git-command/src/lib.rs +17 −2
  2. git-command/tests/command.rs +27 −0
git-command/src/lib.rs+17−2
@@ -10,6 +10,7 @@ pub struct Prepare { · 9 unmodified lines
10 stdin: std::process::Stdio,10 stdin: std::process::Stdio,
11 stdout: std::process::Stdio,11 stdout: std::process::Stdio,
12 stderr: std::process::Stdio,12 stderr: std::process::Stdio,
13 args: Vec<OsString>,
13 use_shell: bool,14 use_shell: bool,
14}15}
15 16
@@ -43,6 +44,12 @@ mod prepare { · 27 unmodified lines
43 self.stderr = stdio;44 self.stderr = stdio;
44 self45 self
45 }46 }
47
48 /// Add `arg` to the list of arguments to call the command with.
49 pub fn arg(mut self, arg: impl Into<std::ffi::OsString>) -> Self {
50 self.args.push(arg.into());
51 self
52 }
46 }53 }
47 54
48 /// Finalization55 /// Finalization
@@ -55,16 +62,23 @@ mod prepare { · 6 unmodified lines
55 }62 }
56 63
57 impl Into<Command> for Prepare {64 impl Into<Command> for Prepare {
58 fn into(self) -> Command {65 fn into(mut self) -> Command {
59 let mut cmd = if self.use_shell {66 let mut cmd = if self.use_shell {
60 let mut cmd = Command::new(if cfg!(windows) { "sh" } else { "/bin/sh" });67 let mut cmd = Command::new(if cfg!(windows) { "sh" } else { "/bin/sh" });
61 cmd.arg("-c");68 cmd.arg("-c");
69 if !self.args.is_empty() {
70 self.command.push(" \"$@\"")
71 }
62 cmd.arg(self.command);72 cmd.arg(self.command);
73 cmd.arg("--");
63 cmd74 cmd
64 } else {75 } else {
65 Command::new(self.command)76 Command::new(self.command)
66 };77 };
67 cmd.stdin(self.stdin).stdout(self.stdout).stderr(self.stderr);78 cmd.stdin(self.stdin)
79 .stdout(self.stdout)
80 .stderr(self.stderr)
81 .args(self.args);
68 cmd82 cmd
69 }83 }
70 }84 }
@@ -83,6 +97,7 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare { · 12 unmodified lines
83 stdin: std::process::Stdio::null(),97 stdin: std::process::Stdio::null(),
84 stdout: std::process::Stdio::piped(),98 stdout: std::process::Stdio::piped(),
85 stderr: std::process::Stdio::null(),99 stderr: std::process::Stdio::null(),
100 args: Vec::new(),
86 use_shell: false,101 use_shell: false,
87 }102 }
88}103}
git-command/tests/command.rs+27−0
@@ -18,6 +18,8 @@ mod spawn { · 17 unmodified lines
18 }18 }
19 19
20 mod with_shell {20 mod with_shell {
21 use git_testtools::bstr::ByteSlice;
22
21 #[test]23 #[test]
22 fn command_in_path_with_args() -> crate::Result {24 fn command_in_path_with_args() -> crate::Result {
23 assert!(git_command::prepare(if cfg!(unix) { "ls -l" } else { "dir.exe -a" })25 assert!(git_command::prepare(if cfg!(unix) { "ls -l" } else { "dir.exe -a" })
@@ -33,5 +35,30 @@ mod spawn { · 9 unmodified lines
33 assert!(git_command::prepare(":;:;:").with_shell().spawn()?.wait()?.success());35 assert!(git_command::prepare(":;:;:").with_shell().spawn()?.wait()?.success());
34 Ok(())36 Ok(())
35 }37 }
38
39 #[test]
40 fn sh_shell_specific_script_code_with_single_extra_arg() -> crate::Result {
41 let out = git_command::prepare("echo")
42 .with_shell()
43 .arg("1")
44 .spawn()?
45 .wait_with_output()?;
46 assert!(out.status.success());
47 assert_eq!(out.stdout.as_bstr(), "1\n");
48 Ok(())
49 }
50
51 #[test]
52 fn sh_shell_specific_script_code_with_multiple_extra_args() -> crate::Result {
53 let out = git_command::prepare("echo")
54 .with_shell()
55 .arg("1")
56 .arg("2")
57 .spawn()?
58 .wait_with_output()?;
59 assert!(out.status.success());
60 assert_eq!(out.stdout.as_bstr(), "1 2\n");
61 Ok(())
62 }
36 }63 }
37}64}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t