Browse Source

xtask: reimplement build-integration-test

This command builds the integration test binaries and prints their paths
to stdout.
Tamir Duberstein 1 năm trước cách đây
mục cha
commit
9086265ac0
2 tập tin đã thay đổi với 33 bổ sung7 xóa
  1. 13 0
      xtask/src/main.rs
  2. 20 7
      xtask/src/run.rs

+ 13 - 0
xtask/src/main.rs

@@ -3,6 +3,7 @@ mod docs;
 mod run;
 
 use clap::Parser;
+
 #[derive(Parser)]
 pub struct XtaskOptions {
     #[clap(subcommand)]
@@ -13,6 +14,7 @@ pub struct XtaskOptions {
 enum Command {
     Codegen(codegen::Options),
     Docs,
+    BuildIntegrationTest(run::BuildOptions),
     IntegrationTest(run::Options),
 }
 
@@ -22,6 +24,17 @@ fn main() -> anyhow::Result<()> {
     match command {
         Command::Codegen(opts) => codegen::codegen(opts),
         Command::Docs => docs::docs(),
+        Command::BuildIntegrationTest(opts) => {
+            let binaries = run::build(opts)?;
+            let mut stdout = std::io::stdout();
+            for (_name, binary) in binaries {
+                use std::{io::Write as _, os::unix::ffi::OsStrExt as _};
+
+                stdout.write_all(binary.as_os_str().as_bytes())?;
+                stdout.write_all("\n".as_bytes())?;
+            }
+            Ok(())
+        }
         Command::IntegrationTest(opts) => run::run(opts),
     }
 }

+ 20 - 7
xtask/src/run.rs

@@ -10,20 +10,30 @@ use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
 use clap::Parser;
 
 #[derive(Debug, Parser)]
-pub struct Options {
-    /// Build and run the release target
+pub struct BuildOptions {
+    /// Pass --release to `cargo build`.
     #[clap(long)]
     pub release: bool,
-    /// The command used to wrap your application
+    /// Pass --target to `cargo build`.
+    #[clap(long)]
+    pub target: Option<String>,
+}
+
+#[derive(Debug, Parser)]
+pub struct Options {
+    #[command(flatten)]
+    pub build_options: BuildOptions,
+    /// The command used to wrap your application.
     #[clap(short, long, default_value = "sudo -E")]
     pub runner: String,
-    /// Arguments to pass to your application
+    /// Arguments to pass to your application.
     #[clap(name = "args", last = true)]
     pub run_args: Vec<String>,
 }
 
 /// Build the project
-fn build(release: bool) -> Result<Vec<(String, PathBuf)>> {
+pub fn build(opts: BuildOptions) -> Result<Vec<(String, PathBuf)>> {
+    let BuildOptions { release, target } = opts;
     let mut cmd = Command::new("cargo");
     cmd.args([
         "build",
@@ -34,6 +44,9 @@ fn build(release: bool) -> Result<Vec<(String, PathBuf)>> {
     if release {
         cmd.arg("--release");
     }
+    if let Some(target) = target {
+        cmd.args(["--target", &target]);
+    }
     let mut cmd = cmd
         .stdout(Stdio::piped())
         .spawn()
@@ -80,12 +93,12 @@ fn build(release: bool) -> Result<Vec<(String, PathBuf)>> {
 /// Build and run the project
 pub fn run(opts: Options) -> Result<()> {
     let Options {
-        release,
+        build_options,
         runner,
         run_args,
     } = opts;
 
-    let binaries = build(release).context("error while building userspace application")?;
+    let binaries = build(build_options).context("error while building userspace application")?;
     let mut args = runner.trim().split_terminator(' ');
     let runner = args.next().ok_or(anyhow::anyhow!("no first argument"))?;
     let args = args.collect::<Vec<_>>();