use std::{ fs, path::Path };
#[ test ]
fn test_no_expect_for_env_vars()
{
let examples_dir = Path::new( "examples" );
assert!( examples_dir.exists(), "examples directory should exist" );
let mut violations = Vec::new();
for entry in fs::read_dir( examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) == Some( "rs" )
{
let content = fs::read_to_string( &path ).expect( "Failed to read file" );
let filename = path.file_name().expect( "[test_no_expect_for_env_vars] Path should have filename component - check examples directory structure" ).to_str().expect( "[test_no_expect_for_env_vars] Filename should be valid UTF-8 - check filesystem encoding" );
if content.contains( "std::env::var" ) && content.contains( ".expect(" )
{
let lines : Vec< &str > = content.lines().collect();
for ( i, line ) in lines.iter().enumerate()
{
if line.contains( "std::env::var" ) || line.contains( "env::var" )
{
let line_num = i + 1;
for next_line in lines.iter().skip( i ).take( 5 )
{
if next_line.contains( ".expect(" )
{
violations.push( format!(
"{filename}:{line_num}: Found env::var with .expect() - use Secret::load_from_env() or proper error handling instead"
) );
break;
}
}
}
}
}
}
}
assert!(violations.is_empty(),
"Found {} examples using .expect() for environment variables:\n{}",
violations.len(),
violations.join( "\n" )
);
}
#[ test ]
fn test_examples_return_result()
{
let examples_dir = Path::new( "examples" );
assert!( examples_dir.exists(), "examples directory should exist" );
let mut violations = Vec::new();
for entry in fs::read_dir( examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) == Some( "rs" )
{
let content = fs::read_to_string( &path ).expect( "Failed to read file" );
let filename = path.file_name().expect( "[test_examples_return_result] Path should have filename component - check examples directory structure" ).to_str().expect( "[test_examples_return_result] Filename should be valid UTF-8 - check filesystem encoding" );
let has_result_return = content.contains( "async fn main() -> Result< " )
|| content.contains( "fn main() - > Result< " );
if !has_result_return
{
violations.push( format!(
"{filename}: main() should return Result< (), Box< dyn std::error::Error > > for proper error handling"
) );
}
}
}
assert!(violations.is_empty(),
"Found {} examples not returning Result from main:\n{}",
violations.len(),
violations.join( "\n" )
);
}
#[ test ]
fn test_consistent_secret_loading()
{
let examples_dir = Path::new( "examples" );
assert!( examples_dir.exists(), "examples directory should exist" );
for entry in fs::read_dir( examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) == Some( "rs" )
{
let content = fs::read_to_string( &path ).expect( "Failed to read file" );
let filename = path.file_name().expect( "[test_consistent_secret_loading] Path should have filename component - check examples directory structure" ).to_str().expect( "[test_consistent_secret_loading] Filename should be valid UTF-8 - check filesystem encoding" );
if content.contains( "HUGGINGFACE_API_KEY" )
{
let uses_secret_load = content.contains( "Secret::load_from_env" );
let uses_workspace_fallback = content.contains( "workspace_tools" )
&& content.contains( ".or_else(" );
let uses_unwrap = content.contains( "std::env::var" )
&& ( content.contains( ".unwrap()" ) || content.contains( ".expect(" ) );
let uses_valid_pattern = uses_secret_load || uses_workspace_fallback;
assert!(
!uses_unwrap || uses_workspace_fallback,
"{filename}: Uses .unwrap() or .expect() without proper error handling. \
Use Secret::load_from_env() or or_else() pattern instead."
);
assert!(
uses_valid_pattern,
"{filename}: Should use either Secret::load_from_env() or workspace_tools fallback pattern"
);
}
}
}
}
#[ test ]
fn test_examples_registered_in_cargo_toml()
{
let examples_dir = Path::new( "examples" );
let cargo_toml = fs::read_to_string( "Cargo.toml" ).expect( "Failed to read Cargo.toml" );
let mut unregistered = Vec::new();
for entry in fs::read_dir( examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) == Some( "rs" )
{
let filename = path.file_name().expect( "[test_examples_registered_in_cargo_toml] Path should have filename component - check examples directory structure" ).to_str().expect( "[test_examples_registered_in_cargo_toml] Filename should be valid UTF-8 - check filesystem encoding" );
let expected_path_in_toml = format!( "examples/{filename}" );
if !cargo_toml.contains( &expected_path_in_toml )
{
unregistered.push( filename.to_string() );
}
}
}
assert!(unregistered.is_empty(),
"Found {} examples not registered in Cargo.toml:\n{}\n\n\
Add [[example]] entries for these files.",
unregistered.len(),
unregistered.join( "\n" )
);
}
#[ test ]
fn test_no_hardcoded_secrets()
{
let examples_dir = Path::new( "examples" );
let mut violations = Vec::new();
for entry in fs::read_dir( examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) == Some( "rs" )
{
let content = fs::read_to_string( &path ).expect( "Failed to read file" );
let filename = path.file_name().expect( "[test_no_hardcoded_secrets] Path should have filename component - check examples directory structure" ).to_str().expect( "[test_no_hardcoded_secrets] Filename should be valid UTF-8 - check filesystem encoding" );
let suspicious_patterns = [
r#"api_key = "hf_"#,
r#"api_key = "sk_"#,
r#"HUGGINGFACE_API_KEY" => "#,
r#"Secret::new("hf_"#,
];
for pattern in &suspicious_patterns
{
if content.contains( pattern )
{
violations.push( format!(
"{filename}: Possible hardcoded secret found (pattern : {pattern})"
) );
}
}
}
}
assert!(violations.is_empty(),
"Found potential hardcoded secrets in examples:\n{}",
violations.join( "\n" )
);
}