Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
        {
            println!("mdbook-linkcheck not installed. Continuing...");
        } else {
            eprintln!(
                "An error occurred during uninstallation of mdbook-linkcheck:\n{:#?}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}

fn run_web_tests(dir: Option<PathBuf>) -> Result<()> {
    println!("Running web tests...");

    let absolute_dir = dir.map(|d| d.canonicalize()).transpose()?;

    if let Some(d) = &absolute_dir {
        println!("Refreshing slide lists...");
        let path_to_refresh_slides_script = Path::new("tests")
            .join("src")
            .join("slides")
            .join("create-slide.list.sh");
        let status = Command::new(path_to_refresh_slides_script)
            .current_dir(Path::new(env!("CARGO_WORKSPACE_DIR")))
            .arg(d)
            .status()
            .expect("Failed to execute create-slide.list.sh");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo xtask web-tests' exited with status code: {}",
                status.code().unwrap()
            );
            return Err(anyhow!(error_message));
        }
    }

    let path_to_tests_dir = Path::new(env!("CARGO_WORKSPACE_DIR")).join("tests");
    let mut command = Command::new("npm");
    command.current_dir(path_to_tests_dir.to_str().unwrap());
    command.arg("test");

    if let Some(d) = absolute_dir {
        command.env("TEST_BOOK_DIR", d);
    }
    let status = command.status().expect("Failed to execute npm test");

    if !status.success() {
        let error_message = format!(
            "Command 'cargo xtask web-tests' exited with status code: {}",
            status.code().unwrap()

Review comment (suggestion):
Something like this should work, especially if `run_command` would take ownership of the `Command` passed in:

```suggestion
    let cmd = Command::new("npm");
        .current_dir(tests_dir).arg("test");
```

Code after:
        }
        println!("mdbook-linkcheck not installed. Continuing...");
    }
    Ok(())
}

fn run_web_tests(dir: Option<PathBuf>) -> Result<()> {
    println!("Running web tests...");
    let workspace_dir = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let absolute_dir = dir.map(|d| d.canonicalize()).transpose()?;

    if let Some(d) = &absolute_dir {
        println!("Refreshing slide lists...");
        let refresh_slides_script = Path::new("tests")
            .join("src")
            .join("slides")
            .join("create-slide.list.sh");
        let mut cmd = Command::new(&refresh_slides_script);
        cmd.current_dir(workspace_dir).arg(d);
        run_command(&mut cmd)?;
    }

    let tests_dir = workspace_dir.join("tests");
    let mut cmd = Command::new("npm");
    cmd.current_dir(tests_dir).arg("test");

    if let Some(d) = absolute_dir {
        cmd.env("TEST_BOOK_DIR", d);
    }
    run_command(&mut cmd)
}

fn run_rust_tests() -> Result<()> {
    println!("Running rust tests...");
    let workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let mut cmd = Command::new("mdbook");
    cmd.current_dir(workspace_root).arg("test");
    run_command(&mut cmd)
}

fn run_mdbook_command(
    subcommand: &str,
    language: Option<String>,
    output_arg: Option<PathBuf>,
) -> Result<()> {
    let workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let mut cmd = Command::new("mdbook");
    cmd.current_dir(workspace_root).arg(subcommand);