Skip to content

Commit

Permalink
Test child process creation
Browse files Browse the repository at this point in the history
  • Loading branch information
seamlik committed Mar 16, 2024
1 parent a071de3 commit d668ef3
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions main/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::process::Command;
pub async fn eval(command: &str, args: &[&str], stdin: &[u8]) -> Result<Vec<u8>, ProcessError> {
let mut process = Command::new(command)
.args(args)
.env("NO_COLOR", "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
Expand All @@ -16,7 +17,7 @@ pub async fn eval(command: &str, args: &[&str], stdin: &[u8]) -> Result<Vec<u8>,
let mut stdin_pipe = process
.stdin
.take()
.ok_or_else(|| ProcessError::StdioRedirection)?;
.ok_or_else(|| ProcessError::RedirectStdIo)?;
stdin_pipe.write_all(stdin).await?;
drop(stdin_pipe);

Expand All @@ -30,6 +31,7 @@ pub async fn eval(command: &str, args: &[&str], stdin: &[u8]) -> Result<Vec<u8>,
pub async fn run(command: &str, args: &[&str]) -> Result<Vec<u8>, ProcessError> {
let mut process = Command::new(command)
.args(args)
.env("NO_COLOR", "1")
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
Expand Down Expand Up @@ -75,7 +77,7 @@ async fn read_stdout(process: &mut Child) -> Result<Vec<u8>, ProcessError> {
let mut stdout = process
.stdout
.take()
.ok_or_else(|| ProcessError::StdioRedirection)?;
.ok_or_else(|| ProcessError::RedirectStdIo)?;

let mut buffer = Default::default();
stdout.read_to_end(&mut buffer).await?;
Expand All @@ -88,14 +90,32 @@ pub enum ProcessError {
ChildProcessCreation(#[from] std::io::Error),

#[error("Failed to redirect standard I/O")]
StdioRedirection,
RedirectStdIo,

#[error("External command failed")]
ExternalCommand,
}

#[cfg(test)]
mod test {
#[tokio::test]
async fn eval() {
let output = super::eval("pwsh", &["-Command", "-"], b"Write-Output 123")
.await
.unwrap();
let output_text = String::from_utf8(output).unwrap();
assert_eq!(output_text.trim(), "123");
}

#[tokio::test]
async fn run() {
let output = super::run("pwsh", &["-Command", "Write-Output 123"])
.await
.unwrap();
let output_text = String::from_utf8(output).unwrap();
assert_eq!(output_text.trim(), "123");
}

#[tokio::test]
async fn probe() {
assert!(super::probe("pwsh", &["-Help"]).await);
Expand Down

0 comments on commit d668ef3

Please sign in to comment.