Skip to content

Commit

Permalink
Fix some auto-unignore issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbuchan committed Oct 10, 2023
1 parent 1d1a878 commit 8ec1cd7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
33 changes: 22 additions & 11 deletions xtask/src/file_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
}
}
Expand All @@ -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]
Expand All @@ -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");
Expand All @@ -85,22 +91,27 @@ impl<'a> TestBaseArgs<'a> {
// appropriate println!("cargo:rerun-if-changed=<path>");

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?)
unignore_cleanup()?;

// 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()?;
Expand All @@ -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)? {
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 25 additions & 0 deletions xtask/src/run_cargo.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -47,6 +49,7 @@ pub struct CargoTestArgs<'a> {
log_max_level: Option<LogFilterLevel>,
log: Option<&'a str>,
package: &'a str,
lib: bool,
test: Option<&'a str>,
name: Option<&'a str>,
}
Expand All @@ -61,6 +64,7 @@ impl<'a> CargoTestArgs<'a> {
log_max_level: None,
log: None,
package,
lib: false,
test: None,
name: None,
}
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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: <root>/xtask/Cargo.toml
manifest_dir.pop();
manifest_dir
}

0 comments on commit 8ec1cd7

Please sign in to comment.