use std::fs;
use std::path::PathBuf;
fn readme_path() -> PathBuf
{
let manifest_dir = env!( "CARGO_MANIFEST_DIR" );
PathBuf::from( manifest_dir ).join( "readme.md" )
}
fn examples_dir() -> PathBuf
{
let manifest_dir = env!( "CARGO_MANIFEST_DIR" );
PathBuf::from( manifest_dir ).join( "examples" )
}
#[ test ]
fn readme_no_incorrect_example_paths()
{
let readme = readme_path();
let content = fs::read_to_string( &readme )
.expect( "Failed to read readme.md" );
let examples = examples_dir();
if examples.exists()
{
for entry in fs::read_dir( &examples ).expect( "Failed to read examples dir" )
{
let entry = entry.expect( "Failed to read entry" );
let path = entry.path();
if path.is_file() && path.extension().and_then( |s| s.to_str() ) == Some( "rs" )
{
if let Some( stem ) = path.file_stem().and_then( |s| s.to_str() )
{
let incorrect_pattern = format!( "cd examples/{stem}" );
assert!(
!content.contains( &incorrect_pattern ),
"Readme contains incorrect path '{incorrect_pattern}' - '{stem}' is a file, not a directory. \
Use 'cargo run --example {stem}' instead."
);
}
}
}
}
}
#[ test ]
fn readme_uses_correct_example_execution()
{
let readme = readme_path();
let content = fs::read_to_string( &readme )
.expect( "Failed to read readme.md" );
if content.contains( "wtools_trivial" )
{
let correct_patterns = [
"cargo run --example wtools_trivial",
"`cargo run --example wtools_trivial`",
];
let has_correct_pattern = correct_patterns.iter()
.any( |pattern| content.contains( pattern ) );
assert!(
has_correct_pattern,
"Readme should use 'cargo run --example wtools_trivial' to run examples"
);
}
}
#[ test ]
fn readme_exists()
{
let readme = readme_path();
assert!(
readme.exists(),
"readme.md does not exist at {}",
readme.display()
);
}
#[ test ]
fn readme_has_content()
{
let readme = readme_path();
let content = fs::read_to_string( &readme )
.expect( "Failed to read readme.md" );
assert!(
content.len() > 100,
"Readme appears to be empty or too short"
);
assert!(
content.contains( "wtools" ),
"Readme should mention the crate name 'wtools'"
);
}
#[ test ]
fn readme_has_repository_info()
{
let readme = readme_path();
let content = fs::read_to_string( &readme )
.expect( "Failed to read readme.md" );
assert!(
content.contains( "github.com" ) || content.contains( "GitHub" ),
"Readme should mention GitHub repository"
);
}