1+ use anyhow:: anyhow;
12use camino:: Utf8Path ;
2- use dir_test:: { Fixture , dir_test} ;
33use ty_static:: EnvVars ;
44use ty_test:: OutputFormat ;
55
66/// See `crates/ty_test/README.md` for documentation on these tests.
7- #[ dir_test(
8- dir: "$CARGO_MANIFEST_DIR/resources/mdtest" ,
9- glob: "**/*.md"
10- ) ]
117#[ expect( clippy:: needless_pass_by_value) ]
12- fn mdtest ( fixture : Fixture < & str > ) {
13- let absolute_fixture_path = Utf8Path :: new ( fixture. path ( ) ) ;
8+ fn mdtest ( fixture_path : & Utf8Path , content : String ) -> datatest_stable:: Result < ( ) > {
9+ let short_title = fixture_path
10+ . file_name ( )
11+ . ok_or_else ( || anyhow ! ( "Expected fixture path to have a file name" ) ) ?;
12+
1413 let crate_dir = Utf8Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
1514 let snapshot_path = crate_dir. join ( "resources" ) . join ( "mdtest" ) . join ( "snapshots" ) ;
16- let workspace_root = crate_dir. ancestors ( ) . nth ( 2 ) . unwrap ( ) ;
17-
18- let relative_fixture_path = absolute_fixture_path. strip_prefix ( workspace_root) . unwrap ( ) ;
19- let short_title = absolute_fixture_path. file_name ( ) . unwrap ( ) ;
15+ let absolute_fixture_path = crate_dir. join ( fixture_path) ;
16+ let workspace_relative_fixture_path = Utf8Path :: new ( "crates/ty_python_semantic" )
17+ . join ( fixture_path. strip_prefix ( "." ) . unwrap_or ( fixture_path) ) ;
2018
21- let test_name = test_name ( "mdtest" , absolute_fixture_path) ;
19+ let test_name = fixture_path
20+ . strip_prefix ( "./resources/mdtest" )
21+ . unwrap_or ( fixture_path)
22+ . as_str ( ) ;
2223
2324 let output_format = if std:: env:: var ( EnvVars :: MDTEST_GITHUB_ANNOTATIONS_FORMAT ) . is_ok ( ) {
2425 OutputFormat :: GitHub
@@ -27,43 +28,18 @@ fn mdtest(fixture: Fixture<&str>) {
2728 } ;
2829
2930 ty_test:: run (
30- absolute_fixture_path,
31- relative_fixture_path,
31+ & absolute_fixture_path,
32+ & workspace_relative_fixture_path,
33+ & content,
3234 & snapshot_path,
3335 short_title,
34- & test_name,
36+ test_name,
3537 output_format,
36- ) ;
37- }
38-
39- /// Constructs the test name used for individual markdown files
40- ///
41- /// This code is copied from <https://github.com/fe-lang/dir-test/blob/1c0f41c480a3490bc2653a043ff6e3f8085a1f47/macros/src/lib.rs#L104-L138>
42- /// and should be updated if they diverge
43- fn test_name ( test_func_name : & str , fixture_path : & Utf8Path ) -> String {
44- assert ! ( fixture_path. is_file( ) ) ;
38+ ) ?;
4539
46- let dir_path = format ! ( "{}/resources/mdtest" , std:: env!( "CARGO_MANIFEST_DIR" ) ) ;
47- let rel_path = fixture_path. strip_prefix ( dir_path) . unwrap ( ) ;
48- assert ! ( rel_path. is_relative( ) ) ;
49-
50- let mut test_name = test_func_name. to_owned ( ) ;
51- test_name. push_str ( "__" ) ;
52-
53- for component in rel_path. parent ( ) . unwrap ( ) . components ( ) {
54- let component = component
55- . as_str ( )
56- . replace ( |c : char | c. is_ascii_punctuation ( ) , "_" ) ;
57- test_name. push_str ( & component) ;
58- test_name. push ( '_' ) ;
59- }
60-
61- test_name. push_str (
62- & rel_path
63- . file_stem ( )
64- . unwrap ( )
65- . replace ( |c : char | c. is_ascii_punctuation ( ) , "_" ) ,
66- ) ;
40+ Ok ( ( ) )
41+ }
6742
68- test_name
43+ datatest_stable:: harness! {
44+ { test = mdtest, root = "./resources/mdtest" , pattern = r"\.md$" } ,
6945}
0 commit comments