|
| 1 | +use crate::project_root; |
| 2 | +use std::error::Error; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +pub fn run_test(task: &str) -> Result<(), Box<dyn Error>> { |
| 6 | + match task { |
| 7 | + "all" => run_all_tests()?, |
| 8 | + "cargo" => cargo_test()?, |
| 9 | + "spellcheck" => spellcheck()?, |
| 10 | + "link" => link_checker()?, |
| 11 | + _ => run_all_tests()?, |
| 12 | + } |
| 13 | + Ok(()) |
| 14 | +} |
| 15 | + |
| 16 | +fn run_all_tests() -> Result<(), Box<dyn Error>> { |
| 17 | + let mut failures = Vec::new(); |
| 18 | + |
| 19 | + if cargo_test().is_err() { |
| 20 | + failures.push("cargo_test".to_string()); |
| 21 | + } |
| 22 | + |
| 23 | + if spellcheck().is_err() { |
| 24 | + failures.push("spellcheck".to_string()); |
| 25 | + } |
| 26 | + |
| 27 | + if link_checker().is_err() { |
| 28 | + failures.push("link".to_string()); |
| 29 | + } |
| 30 | + |
| 31 | + if !failures.is_empty() { |
| 32 | + println!("\n--- Test Summary ---"); |
| 33 | + for name in failures { |
| 34 | + println!("❌ {name} failed! Re-run with the command:"); |
| 35 | + println!(" cargo xtask test {name}"); |
| 36 | + } |
| 37 | + } else { |
| 38 | + println!("\n🎉 All tests passed!"); |
| 39 | + } |
| 40 | + |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +fn cargo_test() -> Result<(), Box<dyn Error>> { |
| 45 | + let status = Command::new("cargo") |
| 46 | + .current_dir(project_root()) |
| 47 | + .args(["test", "--package", "rust-cookbook"]) |
| 48 | + .status()?; |
| 49 | + |
| 50 | + if !status.success() { |
| 51 | + return Err("failed to run cargo test!".into()); |
| 52 | + } |
| 53 | + |
| 54 | + Ok(()) |
| 55 | +} |
| 56 | + |
| 57 | +fn spellcheck() -> Result<(), Box<dyn Error>> { |
| 58 | + let status = Command::new("./ci/spellcheck.sh") |
| 59 | + .current_dir(project_root()) |
| 60 | + .status()?; |
| 61 | + |
| 62 | + if !status.success() { |
| 63 | + return Err("failed to run spellcheck!".into()); |
| 64 | + } |
| 65 | + |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +fn link_checker() -> Result<(), Box<dyn Error>> { |
| 70 | + if Command::new("lychee").arg("--version").status().is_err() { |
| 71 | + return Err( |
| 72 | + "The `lychee` tool is not installed. Please install it using:\n cargo install [email protected]".into(), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + let book_dir = project_root().join("book"); |
| 77 | + if !book_dir.is_dir() { |
| 78 | + return Err(format!( |
| 79 | + "The book directory could not be found in the root directory: {:?}\n\ |
| 80 | + You can build it using:\n cargo xtask book build", |
| 81 | + book_dir |
| 82 | + ) |
| 83 | + .into()); |
| 84 | + } |
| 85 | + |
| 86 | + let status = Command::new("lychee") |
| 87 | + .current_dir(project_root()) |
| 88 | + .args([ |
| 89 | + "./book", |
| 90 | + "--retry-wait-time", |
| 91 | + "20", |
| 92 | + "--max-retries", |
| 93 | + "3", |
| 94 | + "--accept", |
| 95 | + "429", // accept 429 (ratelimit) errors as valid |
| 96 | + ]) |
| 97 | + .status()?; |
| 98 | + |
| 99 | + if !status.success() { |
| 100 | + return Err("Failed to run link checker!".into()); |
| 101 | + } |
| 102 | + |
| 103 | + Ok(()) |
| 104 | +} |
0 commit comments