2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
use std ::env ;
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
use std ::fs ::File ;
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
use std ::io ::Write ;
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
use std ::path ::Path ;
2023-04-23 01:45:18 +00:00
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
use walkdir ::WalkDir ;
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
fn main ( ) {
let out_dir = env ::var ( " OUT_DIR " ) . unwrap ( ) ;
let destination = Path ::new ( & out_dir ) . join ( " tests.rs " ) ;
let mut test_file = File ::create ( & destination ) . unwrap ( ) ;
2023-09-14 01:28:44 +00:00
// Re-generate the tests if any org-mode files change
println! ( " cargo:rerun-if-changed=org_mode_samples " ) ;
2023-04-19 02:38:18 +00:00
let test_files = WalkDir ::new ( " org_mode_samples " )
. into_iter ( )
. filter ( | e | match e {
Ok ( dir_entry ) = > {
dir_entry . file_type ( ) . is_file ( )
& & Path ::new ( dir_entry . file_name ( ) )
. extension ( )
. map ( | ext | ext . to_ascii_lowercase ( ) = = " org " )
. unwrap_or ( false )
}
Err ( _ ) = > true ,
2023-04-19 18:40:10 +00:00
} )
. collect ::< Result < Vec < _ > , _ > > ( )
. unwrap ( ) ;
2023-04-19 02:38:18 +00:00
for test in test_files {
write_test ( & mut test_file , & test ) ;
}
}
2023-08-30 03:16:38 +00:00
#[ cfg(not(feature = " compare " )) ]
fn main ( ) { }
#[ cfg(feature = " compare " ) ]
2023-04-19 02:38:18 +00:00
fn write_test ( test_file : & mut File , test : & walkdir ::DirEntry ) {
2023-04-19 18:40:10 +00:00
let test_name = test
. path ( )
. strip_prefix ( " org_mode_samples/ " )
. expect ( " Paths should be under org_mode_samples/ " )
. to_string_lossy ( )
. to_lowercase ( )
. strip_suffix ( " .org " )
. expect ( " Should have .org extension " )
. replace ( " / " , " _ " ) ;
2023-04-19 02:38:18 +00:00
write! (
test_file ,
include_str! ( " ./tests/test_template " ) ,
name = test_name ,
2023-09-29 19:30:38 +00:00
path = test . path ( ) . display ( ) ,
expect_fail = is_expect_fail ( test_name . as_str ( ) )
. map ( | _ | " #[ignore] \n " )
. unwrap_or ( " " )
2023-04-19 02:38:18 +00:00
)
. unwrap ( ) ;
}
2023-04-19 18:40:10 +00:00
2023-08-30 03:16:38 +00:00
#[ cfg(feature = " compare " ) ]
2023-04-19 18:40:10 +00:00
fn is_expect_fail ( name : & str ) -> Option < & str > {
match name {
2023-09-29 19:30:38 +00:00
" greater_element_drawer_drawer_with_headline_inside " = > Some ( " Apparently lines with :end: become their own paragraph. This odd behavior needs to be investigated more. " ) ,
" element_container_priority_footnote_definition_dynamic_block " = > Some ( " Apparently broken begin lines become their own paragraph. " ) ,
2023-04-19 18:40:10 +00:00
_ = > None ,
}
}