mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[ty] Shorten snapshot names (#18039)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
f8890b70c3
commit
a9f7521944
98 changed files with 108 additions and 30 deletions
25
Cargo.lock
generated
25
Cargo.lock
generated
|
@ -478,7 +478,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"windows-sys 0.48.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -487,7 +487,7 @@ version = "3.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
|
||||
dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -904,7 +904,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1485,7 +1485,7 @@ checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
|
|||
dependencies = [
|
||||
"hermit-abi 0.5.0",
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1549,7 +1549,7 @@ dependencies = [
|
|||
"portable-atomic",
|
||||
"portable-atomic-util",
|
||||
"serde",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3231,6 +3231,12 @@ version = "2.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-stable-hash"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "781442f29170c5c93b7185ad559492601acdc71d5bb0706f5868094f45cfcd08"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.44"
|
||||
|
@ -3241,7 +3247,7 @@ dependencies = [
|
|||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.4.15",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3254,7 +3260,7 @@ dependencies = [
|
|||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.9.3",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3640,7 +3646,7 @@ dependencies = [
|
|||
"getrandom 0.3.3",
|
||||
"once_cell",
|
||||
"rustix 1.0.2",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -4153,6 +4159,7 @@ dependencies = [
|
|||
"ruff_source_file",
|
||||
"ruff_text_size",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-stable-hash",
|
||||
"salsa",
|
||||
"serde",
|
||||
"smallvec",
|
||||
|
@ -4638,7 +4645,7 @@ version = "0.1.9"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||
dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -125,6 +125,7 @@ rand = { version = "0.9.0" }
|
|||
rayon = { version = "1.10.0" }
|
||||
regex = { version = "1.10.2" }
|
||||
rustc-hash = { version = "2.0.0" }
|
||||
rustc-stable-hash = { version = "0.1.2" }
|
||||
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
|
||||
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "7edce6e248f35c8114b4b021cdb474a3fb2813b3" }
|
||||
schemars = { version = "0.8.16" }
|
||||
|
|
|
@ -28,6 +28,7 @@ insta = { workspace = true, features = ["filters"] }
|
|||
memchr = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
rustc-hash = { workspace = true }
|
||||
rustc-stable-hash = { workspace = true }
|
||||
salsa = { workspace = true }
|
||||
smallvec = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
|
|
@ -56,7 +56,10 @@ pub fn run(
|
|||
let filter = std::env::var(MDTEST_TEST_FILTER).ok();
|
||||
let mut any_failures = false;
|
||||
for test in suite.tests() {
|
||||
if filter.as_ref().is_some_and(|f| !test.name().contains(f)) {
|
||||
if filter
|
||||
.as_ref()
|
||||
.is_some_and(|f| !test.uncontracted_name().contains(f))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -420,7 +423,7 @@ fn create_diagnostic_snapshot(
|
|||
let mut snapshot = String::new();
|
||||
writeln!(snapshot).unwrap();
|
||||
writeln!(snapshot, "---").unwrap();
|
||||
writeln!(snapshot, "mdtest name: {}", test.name()).unwrap();
|
||||
writeln!(snapshot, "mdtest name: {}", test.uncontracted_name()).unwrap();
|
||||
writeln!(snapshot, "mdtest path: {relative_fixture_path}").unwrap();
|
||||
writeln!(snapshot, "---").unwrap();
|
||||
writeln!(snapshot).unwrap();
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
use std::{borrow::Cow, collections::hash_map::Entry};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::hash_map::Entry,
|
||||
fmt::{Formatter, LowerHex, Write},
|
||||
hash::Hash,
|
||||
};
|
||||
|
||||
use anyhow::bail;
|
||||
use ruff_db::system::{SystemPath, SystemPathBuf};
|
||||
|
@ -9,6 +14,7 @@ use ruff_python_ast::PySourceType;
|
|||
use ruff_python_trivia::Cursor;
|
||||
use ruff_source_file::{LineIndex, LineRanges, OneIndexed};
|
||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||
use rustc_stable_hash::{FromStableHash, SipHasher128Hash, StableSipHasher128};
|
||||
|
||||
use crate::config::MarkdownTestConfig;
|
||||
|
||||
|
@ -39,6 +45,25 @@ impl<'s> MarkdownTestSuite<'s> {
|
|||
}
|
||||
}
|
||||
|
||||
struct Hash128([u64; 2]);
|
||||
|
||||
impl FromStableHash for Hash128 {
|
||||
type Hash = SipHasher128Hash;
|
||||
|
||||
fn from(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
|
||||
Hash128(hash)
|
||||
}
|
||||
}
|
||||
|
||||
impl LowerHex for Hash128 {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let Self(hash) = self;
|
||||
|
||||
// Only write the first half for concision
|
||||
write!(f, "{:x}", hash[0])
|
||||
}
|
||||
}
|
||||
|
||||
/// A single test inside a [`MarkdownTestSuite`].
|
||||
///
|
||||
/// A test is a single header section (or the implicit root section, if there are no Markdown
|
||||
|
@ -52,22 +77,61 @@ pub(crate) struct MarkdownTest<'m, 's> {
|
|||
}
|
||||
|
||||
impl<'m, 's> MarkdownTest<'m, 's> {
|
||||
pub(crate) fn name(&self) -> String {
|
||||
let mut name = String::new();
|
||||
const MAX_TITLE_LENGTH: usize = 20;
|
||||
const ELLIPSIS: char = '\u{2026}';
|
||||
|
||||
fn contracted_title(title: &str) -> String {
|
||||
if title.len() <= Self::MAX_TITLE_LENGTH {
|
||||
return (*title).to_string();
|
||||
}
|
||||
|
||||
format!(
|
||||
"{}{}",
|
||||
title
|
||||
.chars()
|
||||
.take(Self::MAX_TITLE_LENGTH)
|
||||
.collect::<String>(),
|
||||
Self::ELLIPSIS
|
||||
)
|
||||
}
|
||||
|
||||
fn joined_name(&self, contracted: bool) -> String {
|
||||
let mut name_fragments = vec![];
|
||||
let mut parent_id = self.section.parent_id;
|
||||
|
||||
while let Some(next_id) = parent_id {
|
||||
let parent = &self.suite.sections[next_id];
|
||||
name_fragments.insert(0, parent.title);
|
||||
parent_id = parent.parent_id;
|
||||
if !name.is_empty() {
|
||||
name.insert_str(0, " - ");
|
||||
}
|
||||
name.insert_str(0, parent.title);
|
||||
}
|
||||
if !name.is_empty() {
|
||||
name.push_str(" - ");
|
||||
|
||||
name_fragments.push(self.section.title);
|
||||
|
||||
let full_name = name_fragments.join(" - ");
|
||||
|
||||
if !contracted {
|
||||
return full_name;
|
||||
}
|
||||
name.push_str(self.section.title);
|
||||
name
|
||||
|
||||
let mut contracted_name = name_fragments
|
||||
.iter()
|
||||
.map(|fragment| Self::contracted_title(fragment))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" - ");
|
||||
|
||||
let mut hasher = StableSipHasher128::new();
|
||||
full_name.hash(&mut hasher);
|
||||
let _ = write!(contracted_name, " ({:x})", hasher.finish::<Hash128>());
|
||||
|
||||
contracted_name
|
||||
}
|
||||
|
||||
pub(crate) fn uncontracted_name(&self) -> String {
|
||||
self.joined_name(false)
|
||||
}
|
||||
|
||||
pub(crate) fn name(&self) -> String {
|
||||
self.joined_name(true)
|
||||
}
|
||||
|
||||
pub(crate) fn files(&self) -> impl Iterator<Item = &'m EmbeddedFile<'s>> {
|
||||
|
@ -762,6 +826,8 @@ mod tests {
|
|||
use ruff_python_ast::PySourceType;
|
||||
use ruff_python_trivia::textwrap::dedent;
|
||||
|
||||
use insta::assert_snapshot;
|
||||
|
||||
use crate::parser::EmbeddedFilePath;
|
||||
|
||||
#[test]
|
||||
|
@ -786,7 +852,7 @@ mod tests {
|
|||
panic!("expected one test");
|
||||
};
|
||||
|
||||
assert_eq!(test.name(), "file.md");
|
||||
assert_snapshot!(test.name(), @"file.md (a8decfe8bd23e259)");
|
||||
|
||||
let [file] = test.files().collect::<Vec<_>>()[..] else {
|
||||
panic!("expected one file");
|
||||
|
@ -814,7 +880,7 @@ mod tests {
|
|||
panic!("expected one test");
|
||||
};
|
||||
|
||||
assert_eq!(test.name(), "file.md");
|
||||
assert_snapshot!(test.name(), @"file.md (a8decfe8bd23e259)");
|
||||
|
||||
let [file] = test.files().collect::<Vec<_>>()[..] else {
|
||||
panic!("expected one file");
|
||||
|
@ -865,9 +931,9 @@ mod tests {
|
|||
panic!("expected three tests");
|
||||
};
|
||||
|
||||
assert_eq!(test1.name(), "file.md - One");
|
||||
assert_eq!(test2.name(), "file.md - Two");
|
||||
assert_eq!(test3.name(), "file.md - Three");
|
||||
assert_snapshot!(test1.name(), @"file.md - One (9f620a533a21278)");
|
||||
assert_snapshot!(test2.name(), @"file.md - Two (1b4d4ef5a2cebbdc)");
|
||||
assert_snapshot!(test3.name(), @"file.md - Three (26479e23633dda57)");
|
||||
|
||||
let [file] = test1.files().collect::<Vec<_>>()[..] else {
|
||||
panic!("expected one file");
|
||||
|
@ -935,8 +1001,8 @@ mod tests {
|
|||
panic!("expected two tests");
|
||||
};
|
||||
|
||||
assert_eq!(test1.name(), "file.md - One");
|
||||
assert_eq!(test2.name(), "file.md - Two");
|
||||
assert_snapshot!(test1.name(), @"file.md - One (9f620a533a21278)");
|
||||
assert_snapshot!(test2.name(), @"file.md - Two (1b4d4ef5a2cebbdc)");
|
||||
|
||||
let [main, foo] = test1.files().collect::<Vec<_>>()[..] else {
|
||||
panic!("expected two files");
|
||||
|
@ -1331,7 +1397,7 @@ mod tests {
|
|||
panic!("expected one test");
|
||||
};
|
||||
|
||||
assert_eq!(test.name(), "file.md - A test");
|
||||
assert_snapshot!(test.name(), @"file.md - A test (1b4e27e6123dc8e7)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue