-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add citool command for generating a test dashboard #139978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d14df26
Make `parent` in `download_auto_job_metrics` optional
Kobzol 111c15c
Extract function for normalizing path delimiters to `utils`
Kobzol c8a882b
Add command to `citool` for generating a test dashboard
Kobzol a326afd
Add buttons for expanding and collapsing all test suites
Kobzol 4b31033
Add a note about how to find tests that haven't been executed anywhere.
Kobzol 1a6e0d5
Render test revisions separately
Kobzol d2c1763
Create a macro for rendering test results
Kobzol aa9cb70
Print number of root tests and subdirectories
Kobzol 08cb187
Turn `test_dashboard` into a file
Kobzol cecf167
Add a note about the test dashboard to the post-merge report
Kobzol 65ce38a
Add a note that explains the counts
Kobzol b18e373
Reduce duplicated test prefixes in nested subdirectories
Kobzol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
use std::collections::{BTreeMap, HashMap}; | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use askama::Template; | ||
use build_helper::metrics::{TestOutcome, TestSuiteMetadata}; | ||
|
||
use crate::jobs::JobDatabase; | ||
use crate::metrics::{JobMetrics, JobName, download_auto_job_metrics, get_test_suites}; | ||
use crate::utils::normalize_path_delimiters; | ||
|
||
pub struct TestInfo { | ||
name: String, | ||
jobs: Vec<JobTestResult>, | ||
} | ||
|
||
struct JobTestResult { | ||
job_name: String, | ||
outcome: TestOutcome, | ||
} | ||
|
||
#[derive(Default)] | ||
struct TestSuiteInfo { | ||
name: String, | ||
tests: BTreeMap<String, TestInfo>, | ||
} | ||
|
||
/// Generate a set of HTML files into a directory that contain a dashboard of test results. | ||
pub fn generate_test_dashboard( | ||
db: JobDatabase, | ||
current: &str, | ||
output_dir: &Path, | ||
) -> anyhow::Result<()> { | ||
let metrics = download_auto_job_metrics(&db, None, current)?; | ||
|
||
let suites = gather_test_suites(&metrics); | ||
|
||
std::fs::create_dir_all(output_dir)?; | ||
|
||
let test_count = suites.test_count(); | ||
write_page(output_dir, "index.html", &TestSuitesPage { suites, test_count })?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn write_page<T: Template>(dir: &Path, name: &str, template: &T) -> anyhow::Result<()> { | ||
let mut file = BufWriter::new(File::create(dir.join(name))?); | ||
Template::write_into(template, &mut file)?; | ||
Ok(()) | ||
} | ||
|
||
fn gather_test_suites(job_metrics: &HashMap<JobName, JobMetrics>) -> TestSuites { | ||
struct CoarseTestSuite<'a> { | ||
kind: TestSuiteKind, | ||
tests: BTreeMap<String, Test<'a>>, | ||
} | ||
|
||
let mut suites: HashMap<String, CoarseTestSuite> = HashMap::new(); | ||
|
||
// First, gather tests from all jobs, stages and targets, and aggregate them per suite | ||
for (job, metrics) in job_metrics { | ||
let test_suites = get_test_suites(&metrics.current); | ||
for suite in test_suites { | ||
let (suite_name, stage, target, kind) = match &suite.metadata { | ||
TestSuiteMetadata::CargoPackage { crates, stage, target, .. } => { | ||
(crates.join(","), *stage, target, TestSuiteKind::Cargo) | ||
} | ||
TestSuiteMetadata::Compiletest { suite, stage, target, .. } => { | ||
(suite.clone(), *stage, target, TestSuiteKind::Compiletest) | ||
} | ||
}; | ||
let suite_entry = suites | ||
.entry(suite_name.clone()) | ||
.or_insert_with(|| CoarseTestSuite { kind, tests: Default::default() }); | ||
let test_metadata = TestMetadata { job, stage, target }; | ||
|
||
for test in &suite.tests { | ||
let test_name = normalize_test_name(&test.name, &suite_name); | ||
let test_entry = suite_entry | ||
.tests | ||
.entry(test_name.clone()) | ||
.or_insert_with(|| Test { name: test_name, passed: vec![], ignored: vec![] }); | ||
match test.outcome { | ||
TestOutcome::Passed => { | ||
test_entry.passed.push(test_metadata); | ||
} | ||
TestOutcome::Ignored { ignore_reason: _ } => { | ||
test_entry.ignored.push(test_metadata); | ||
} | ||
TestOutcome::Failed => { | ||
eprintln!("Warning: failed test"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Then, split the suites per directory | ||
let mut suites = suites.into_iter().collect::<Vec<_>>(); | ||
suites.sort_by(|a, b| a.1.kind.cmp(&b.1.kind).then_with(|| a.0.cmp(&b.0))); | ||
|
||
let mut target_suites = vec![]; | ||
for (suite_name, suite) in suites { | ||
let suite = match suite.kind { | ||
TestSuiteKind::Compiletest => TestSuite { | ||
name: suite_name.clone(), | ||
kind: TestSuiteKind::Compiletest, | ||
group: build_test_group(&suite_name, suite.tests), | ||
}, | ||
TestSuiteKind::Cargo => { | ||
let mut tests: Vec<_> = suite.tests.into_iter().collect(); | ||
tests.sort_by(|a, b| a.0.cmp(&b.0)); | ||
TestSuite { | ||
name: format!("[cargo] {}", suite_name.clone()), | ||
kind: TestSuiteKind::Cargo, | ||
group: TestGroup { | ||
name: suite_name, | ||
root_tests: tests.into_iter().map(|t| t.1).collect(), | ||
groups: vec![], | ||
}, | ||
} | ||
} | ||
}; | ||
target_suites.push(suite); | ||
} | ||
|
||
TestSuites { suites: target_suites } | ||
} | ||
|
||
/// Recursively expand a test group based on filesystem hierarchy. | ||
fn build_test_group<'a>(name: &str, tests: BTreeMap<String, Test<'a>>) -> TestGroup<'a> { | ||
let mut root_tests = vec![]; | ||
let mut subdirs: BTreeMap<String, BTreeMap<String, Test<'a>>> = Default::default(); | ||
|
||
// Split tests into root tests and tests located in subdirectories | ||
for (name, test) in tests { | ||
let mut components = Path::new(&name).components().peekable(); | ||
let subdir = components.next().unwrap(); | ||
|
||
if components.peek().is_none() { | ||
// This is a root test | ||
root_tests.push(test); | ||
} else { | ||
// This is a test in a nested directory | ||
let subdir_tests = | ||
subdirs.entry(subdir.as_os_str().to_str().unwrap().to_string()).or_default(); | ||
let test_name = | ||
components.into_iter().collect::<PathBuf>().to_str().unwrap().to_string(); | ||
subdir_tests.insert(test_name, test); | ||
} | ||
} | ||
let dirs = subdirs | ||
.into_iter() | ||
.map(|(name, tests)| { | ||
let group = build_test_group(&name, tests); | ||
(name, group) | ||
}) | ||
.collect(); | ||
|
||
TestGroup { name: name.to_string(), root_tests, groups: dirs } | ||
} | ||
|
||
/// Compiletest tests start with `[suite] tests/[suite]/a/b/c...`. | ||
/// Remove the `[suite] tests/[suite]/` prefix so that we can find the filesystem path. | ||
/// Also normalizes path delimiters. | ||
fn normalize_test_name(name: &str, suite_name: &str) -> String { | ||
let name = normalize_path_delimiters(name); | ||
let name = name.as_ref(); | ||
let name = name.strip_prefix(&format!("[{suite_name}]")).unwrap_or(name).trim(); | ||
let name = name.strip_prefix("tests/").unwrap_or(name); | ||
let name = name.strip_prefix(suite_name).unwrap_or(name); | ||
name.trim_start_matches("/").to_string() | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
struct TestSuites<'a> { | ||
suites: Vec<TestSuite<'a>>, | ||
} | ||
|
||
impl<'a> TestSuites<'a> { | ||
fn test_count(&self) -> u64 { | ||
self.suites.iter().map(|suite| suite.group.test_count()).sum::<u64>() | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
struct TestSuite<'a> { | ||
name: String, | ||
kind: TestSuiteKind, | ||
group: TestGroup<'a>, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
struct Test<'a> { | ||
name: String, | ||
passed: Vec<TestMetadata<'a>>, | ||
ignored: Vec<TestMetadata<'a>>, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, serde::Serialize)] | ||
struct TestMetadata<'a> { | ||
job: &'a str, | ||
stage: u32, | ||
target: &'a str, | ||
} | ||
|
||
// We have to use a template for the TestGroup instead of a macro, because | ||
// macros cannot be recursive in askama at the moment. | ||
#[derive(Template, serde::Serialize)] | ||
#[template(path = "test_group.askama")] | ||
/// Represents a group of tests | ||
struct TestGroup<'a> { | ||
name: String, | ||
/// Tests located directly in this directory | ||
root_tests: Vec<Test<'a>>, | ||
/// Nested directories with additional tests | ||
groups: Vec<(String, TestGroup<'a>)>, | ||
} | ||
|
||
impl<'a> TestGroup<'a> { | ||
fn test_count(&self) -> u64 { | ||
let root = self.root_tests.len() as u64; | ||
self.groups.iter().map(|(_, group)| group.test_count()).sum::<u64>() + root | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq, PartialOrd, Ord, serde::Serialize)] | ||
enum TestSuiteKind { | ||
Compiletest, | ||
Cargo, | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "test_suites.askama")] | ||
struct TestSuitesPage<'a> { | ||
suites: TestSuites<'a>, | ||
test_count: u64, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.