[ty] Use datatest instead of dirtest (#21937)

This commit is contained in:
Micha Reiser
2025-12-18 19:05:02 +01:00
committed by GitHub
parent f7ec178400
commit 42b972753a
8 changed files with 42 additions and 117 deletions

View File

@@ -1,24 +1,25 @@
use anyhow::anyhow;
use camino::Utf8Path;
use dir_test::{Fixture, dir_test};
use ty_static::EnvVars;
use ty_test::OutputFormat;
/// See `crates/ty_test/README.md` for documentation on these tests.
#[dir_test(
dir: "$CARGO_MANIFEST_DIR/resources/mdtest",
glob: "**/*.md"
)]
#[expect(clippy::needless_pass_by_value)]
fn mdtest(fixture: Fixture<&str>) {
let absolute_fixture_path = Utf8Path::new(fixture.path());
fn mdtest(fixture_path: &Utf8Path, content: String) -> datatest_stable::Result<()> {
let short_title = fixture_path
.file_name()
.ok_or_else(|| anyhow!("Expected fixture path to have a file name"))?;
let crate_dir = Utf8Path::new(env!("CARGO_MANIFEST_DIR"));
let snapshot_path = crate_dir.join("resources").join("mdtest").join("snapshots");
let workspace_root = crate_dir.ancestors().nth(2).unwrap();
let absolute_fixture_path = crate_dir.join(fixture_path);
let workspace_relative_fixture_path = Utf8Path::new("crates/ty_python_semantic")
.join(fixture_path.strip_prefix(".").unwrap_or(fixture_path));
let relative_fixture_path = absolute_fixture_path.strip_prefix(workspace_root).unwrap();
let short_title = absolute_fixture_path.file_name().unwrap();
let test_name = test_name("mdtest", absolute_fixture_path);
let test_name = fixture_path
.strip_prefix("./resources/mdtest")
.unwrap_or(fixture_path)
.as_str();
let output_format = if std::env::var(EnvVars::MDTEST_GITHUB_ANNOTATIONS_FORMAT).is_ok() {
OutputFormat::GitHub
@@ -27,43 +28,18 @@ fn mdtest(fixture: Fixture<&str>) {
};
ty_test::run(
absolute_fixture_path,
relative_fixture_path,
&absolute_fixture_path,
&workspace_relative_fixture_path,
&content,
&snapshot_path,
short_title,
&test_name,
test_name,
output_format,
);
)?;
Ok(())
}
/// Constructs the test name used for individual markdown files
///
/// This code is copied from <https://github.com/fe-lang/dir-test/blob/1c0f41c480a3490bc2653a043ff6e3f8085a1f47/macros/src/lib.rs#L104-L138>
/// and should be updated if they diverge
fn test_name(test_func_name: &str, fixture_path: &Utf8Path) -> String {
assert!(fixture_path.is_file());
let dir_path = format!("{}/resources/mdtest", std::env!("CARGO_MANIFEST_DIR"));
let rel_path = fixture_path.strip_prefix(dir_path).unwrap();
assert!(rel_path.is_relative());
let mut test_name = test_func_name.to_owned();
test_name.push_str("__");
for component in rel_path.parent().unwrap().components() {
let component = component
.as_str()
.replace(|c: char| c.is_ascii_punctuation(), "_");
test_name.push_str(&component);
test_name.push('_');
}
test_name.push_str(
&rel_path
.file_stem()
.unwrap()
.replace(|c: char| c.is_ascii_punctuation(), "_"),
);
test_name
datatest_stable::harness! {
{ test = mdtest, root = "./resources/mdtest", pattern = r"\.md$" },
}