diff --git a/xtask/src/file_analyzer.rs b/xtask/src/file_analyzer.rs index 66f68ccedf..7038c08194 100644 --- a/xtask/src/file_analyzer.rs +++ b/xtask/src/file_analyzer.rs @@ -4,7 +4,10 @@ use std::path::Path; use anyhow::ensure; -use crate::run_cargo::{CargoTestArgs, LogFilterLevel}; +use crate::{ + run_cargo, + run_cargo::{CargoTestArgs, LogFilterLevel}, +}; /// Command builder for running tests/base.rs pub struct TestBaseArgs<'a> { @@ -16,7 +19,7 @@ impl<'a> TestBaseArgs<'a> { /// Create an empty TestBaseArgs. pub fn new() -> Self { Self { - inner: CargoTestArgs::new("stc_ts_file_analyzer").with_test("base"), + inner: CargoTestArgs::new("stc_ts_file_analyzer").with_test("base").with_lib(true), ignored: false, } } @@ -42,6 +45,13 @@ impl<'a> TestBaseArgs<'a> { self } + /// Set cargo test --lib to run the unit tests in the library + #[must_use] + pub fn with_lib(mut self, lib: bool) -> Self { + self.inner = self.inner.with_lib(lib); + self + } + /// cargo test --ignored: Whether to run ignored tests. Used by /// auto-unignore. #[must_use] @@ -60,11 +70,7 @@ impl<'a> TestBaseArgs<'a> { pub fn to_command(&self) -> std::process::Command { let mut cmd = self.inner.to_command(); - cmd.env("UPDATE", "1") - .arg("--lib") - .arg("--") - .arg("-Zunstable-options") - .arg("--report-time"); + cmd.env("UPDATE", "1").arg("--").arg("-Zunstable-options").arg("--report-time"); if self.ignored { cmd.arg("--ignored"); @@ -85,14 +91,15 @@ impl<'a> TestBaseArgs<'a> { // appropriate println!("cargo:rerun-if-changed="); let mut cmd = self.to_command(); + println!("{cmd:?}"); let status = cmd.status()?; - ensure!(status.success(), "test base failed: {status}"); + ensure!(status.success(), "analyzer test base failed: {status}"); Ok(()) } } -/// Implements xtask auto-unignore +/// Implements xtask auto-unignore: un-ignore tests that pass. pub fn auto_unignore() -> anyhow::Result<()> { // Remove any ignored tests cached outputs so they are re-run. // (shouldn't all tests be re-run?) @@ -100,7 +107,11 @@ pub fn auto_unignore() -> anyhow::Result<()> { // Ignore the result of testing, we just want the updated .tsc-errors.json as a // side-effect. - let _ = TestBaseArgs::new().with_ignored().run(); + let _ = TestBaseArgs::new() + .with_log_max_level(LogFilterLevel::Off) + .with_lib(false) + .with_ignored() + .run(); // Remove failed tests caches again, only successful tests should be committed. unignore_cleanup()?; @@ -111,7 +122,7 @@ pub fn auto_unignore() -> anyhow::Result<()> { fn unignore_cleanup() -> anyhow::Result<()> { println!("Deleting cache for ignored tests"); // find ./tests/tsc -name '\.*.tsc-errors.json' -type f -delete - return walk(Path::new("crates/stc_ts_file_analyzer/tests/tsc")); + return walk(&run_cargo::root_dir().join("crates/stc_ts_file_analyzer/tests/tsc")); fn walk(dir: &Path) -> anyhow::Result<()> { for entry in std::fs::read_dir(dir)? { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 79dfea70d8..98bdf9af67 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -18,7 +18,7 @@ struct Cli { enum Command { /// Run tests for stc_ts_file_analyzer TestAnalyzer(TestAnalyzerArgs), - /// Automatically find a test to work on. + /// Automatically unignore stc_ts_file_analyzer tests that pass. AutoUnignore, /// Run tests for stc_ts_type_checker TestChecker(TestCheckerArgs), diff --git a/xtask/src/run_cargo.rs b/xtask/src/run_cargo.rs index a0cc213737..96b4063573 100644 --- a/xtask/src/run_cargo.rs +++ b/xtask/src/run_cargo.rs @@ -1,5 +1,7 @@ //! Provide general helpers to run a nested cargo command. +use std::path::PathBuf; + /// tracing static filter level, to improve performance at the cost of needing /// to rebuild. #[derive(Debug)] @@ -47,6 +49,7 @@ pub struct CargoTestArgs<'a> { log_max_level: Option, log: Option<&'a str>, package: &'a str, + lib: bool, test: Option<&'a str>, name: Option<&'a str>, } @@ -61,6 +64,7 @@ impl<'a> CargoTestArgs<'a> { log_max_level: None, log: None, package, + lib: false, test: None, name: None, } @@ -82,6 +86,13 @@ impl<'a> CargoTestArgs<'a> { self } + /// Set cargo test --lib to run the unit tests in the library + #[must_use] + pub fn with_lib(mut self, lib: bool) -> Self { + self.lib = lib; + self + } + /// Set cargo test --test file to run, e.g. base or prof #[must_use] pub fn with_test(mut self, test: &'a str) -> Self { @@ -99,6 +110,9 @@ impl<'a> CargoTestArgs<'a> { /// Return a Command that can be further configured and run. pub fn to_command(&self) -> std::process::Command { let mut cmd = std::process::Command::new("cargo"); + // normalize working dir + cmd.current_dir(root_dir()); + if let Some(log) = self.log { cmd.env("RUST_LOG", log); } @@ -114,6 +128,9 @@ impl<'a> CargoTestArgs<'a> { if let Some(level) = &self.log_max_level { cmd.arg("--features").arg(level.as_max_level_feature()); } + if self.lib { + cmd.arg("--lib"); + } if let Some(test) = self.test { cmd.arg("--test").arg(test); } @@ -123,3 +140,11 @@ impl<'a> CargoTestArgs<'a> { cmd } } + +pub fn root_dir() -> PathBuf { + let mut manifest_dir = + PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR not set: are you using 'cargo xtask ...'?")); + // xtask manifest should be at: /xtask/Cargo.toml + manifest_dir.pop(); + manifest_dir +}