Skip to content

Commit

Permalink
internal: tweak native backend spawn subprocess args (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash authored Dec 18, 2024
1 parent e759b75 commit 5a38a2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
31 changes: 22 additions & 9 deletions crates/moonbuild/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,43 @@ pub fn load_moon_proj(
}

pub fn run_wat(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> {
run("moonrun", path, args, verbose)
run(Some("moonrun"), path, args, verbose)
}

pub fn run_js(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> {
run("node", path, args, verbose)
run(Some("node"), path, args, verbose)
}

pub fn run_native(path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> {
run(path.to_str().unwrap(), path, args, verbose)
run(None, path, args, verbose)
}

fn run(command: &str, path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> {
fn run(runtime: Option<&str>, path: &Path, args: &[String], verbose: bool) -> anyhow::Result<()> {
if verbose {
eprintln!("{} {} {}", command, path.display(), args.join(" "));
if let Some(runtime) = runtime {
eprintln!("{} {} {}", runtime, path.display(), args.join(" "));
} else {
eprintln!("{} {}", path.display(), args.join(" "));
}
}
let mut execution = Command::new(command)
.arg(path)
.args(args)
let mut subprocess = Command::new(if let Some(runtime) = runtime {
runtime
} else {
path.to_str().unwrap()
});

if runtime.is_some() {
subprocess.arg(path);
}
subprocess.args(args);

let mut execution = subprocess
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.context(format!(
"failed to execute: {} {} {}",
command,
runtime.unwrap_or(""),
path.display(),
if args.is_empty() {
"".to_string()
Expand Down
40 changes: 30 additions & 10 deletions crates/moonbuild/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn run_wat(
let mut _args = vec!["--test-args".to_string()];
_args.push(serde_json_lenient::to_string(args).unwrap());
run(
"moonrun",
Some("moonrun"),
path,
target_dir,
&_args,
Expand All @@ -96,7 +96,7 @@ pub async fn run_js(
verbose: bool,
) -> anyhow::Result<Vec<Result<TestStatistics, TestFailedStatus>>> {
run(
"node",
Some("node"),
path,
target_dir,
&[serde_json_lenient::to_string(args).unwrap()],
Expand All @@ -114,7 +114,7 @@ pub async fn run_native(
verbose: bool,
) -> anyhow::Result<Vec<Result<TestStatistics, TestFailedStatus>>> {
run(
path.to_str().unwrap(),
None,
path,
target_dir,
&[serde_json_lenient::to_string(args).unwrap()],
Expand All @@ -125,24 +125,44 @@ pub async fn run_native(
}

async fn run(
command: &str,
runtime: Option<&str>,
path: &Path,
target_dir: &Path,
args: &[String],
file_test_info_map: &FileTestInfo,
verbose: bool,
) -> anyhow::Result<Vec<Result<TestStatistics, TestFailedStatus>>> {
if verbose {
eprintln!("{} {} {}", command, path.display(), args.join(" "));
if let Some(runtime) = runtime {
eprintln!("{} {} {}", runtime, path.display(), args.join(" "));
} else {
eprintln!("{} {}", path.display(), args.join(" "));
}
}

let mut subprocess = tokio::process::Command::new(if let Some(runtime) = runtime {
runtime
} else {
path.to_str().unwrap()
});

if runtime.is_some() {
subprocess.arg(path);
}
let mut execution = tokio::process::Command::new(command)
.arg(path)
.args(args)
subprocess.args(args);

let mut execution = subprocess
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.with_context(|| format!("failed to execute '{} {}'", command, path.display()))?;
.with_context(|| {
format!(
"failed to execute '{} {}'",
runtime.unwrap_or(""),
path.display()
)
})?;
let mut stdout = execution.stdout.take().unwrap();

let mut test_capture =
Expand All @@ -159,7 +179,7 @@ async fn run(
.await
.context(format!(
"failed to read stdout for {} {} {}",
command,
runtime.unwrap_or(""),
path.display(),
args.join(" ")
))?;
Expand Down

0 comments on commit 5a38a2f

Please sign in to comment.