use workspace_tools ::Workspace;
use std :: { env, path ::PathBuf };
use tempfile ::TempDir;
fn create_test_workspace_at( path: &std ::path ::Path ) -> Workspace
{
let original = env ::var( "WORKSPACE_PATH" ).ok();
env ::set_var( "WORKSPACE_PATH", path );
let workspace = Workspace ::resolve().unwrap();
match original
{
Some( value ) => env ::set_var( "WORKSPACE_PATH", value ),
None => env ::remove_var( "WORKSPACE_PATH" ),
}
workspace
}
#[ test ]
fn test_join_relative_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let joined = workspace.join( "config/app.toml" );
let expected = temp_dir.path().join( "config/app.toml" );
assert_eq!( joined, expected );
}
#[ test ]
fn test_join_absolute_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
#[ cfg( windows ) ]
let absolute_path = PathBuf ::from( r"C: \Windows\System32\hosts" );
#[ cfg( not( windows ) ) ]
let absolute_path = PathBuf ::from( "/etc/hosts" );
let joined = workspace.join( &absolute_path );
let expected = workspace.root().join( &absolute_path );
assert_eq!( joined, expected );
}
#[ test ]
fn test_join_empty_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let joined = workspace.join( "" );
assert_eq!( joined, workspace.root() );
}
#[ test ]
fn test_join_parent_traversal()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let joined = workspace.join( "config/../data/file.txt" );
let expected = temp_dir.path().join( "config/../data/file.txt" );
assert_eq!( joined, expected );
}
#[ test ]
fn test_join_current_directory()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let joined = workspace.join( "./config/./app.toml" );
let expected = temp_dir.path().join( "./config/./app.toml" );
assert_eq!( joined, expected );
}
#[ test ]
fn test_cargo_toml_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let cargo_path = workspace.cargo_toml();
let expected = temp_dir.path().join( "Cargo.toml" );
assert_eq!( cargo_path, expected );
}
#[ test ]
fn test_readme_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let readme_path = workspace.readme();
let expected = temp_dir.path().join( "readme.md" );
assert_eq!( readme_path, expected );
}
#[ test ]
fn test_path_operations_work()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let config_path = workspace.join( "config/app.toml" );
assert!( config_path.is_absolute() );
assert!( config_path.starts_with( temp_dir.path() ) );
assert!( config_path.ends_with( "config/app.toml" ) );
}
#[ test ]
fn test_unicode_path_handling()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let unicode_paths = vec![
"配置/应用.toml", "конфигурация/файл.txt", "العربية/ملف.json", "日本語/設定.yaml", "🚀/config/🎯.toml", ];
for unicode_path in unicode_paths
{
let joined = workspace.join( unicode_path );
let expected = temp_dir.path().join( unicode_path );
assert_eq!( joined, expected );
assert!( joined.is_absolute() );
assert!( joined.starts_with( temp_dir.path() ) );
}
}
#[ test ]
fn test_special_characters_path_handling()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let special_paths = vec![
"config with spaces/app.toml",
"config-with-dashes/app.toml",
"config_with_underscores/app.toml",
"config.with.dots/app.toml",
"config@with@symbols/app.toml",
];
for special_path in special_paths
{
let joined = workspace.join( special_path );
let expected = temp_dir.path().join( special_path );
assert_eq!( joined, expected );
assert!( joined.is_absolute() );
assert!( joined.starts_with( temp_dir.path() ) );
}
}
#[ test ]
fn test_very_long_path_handling()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let long_dir_name = "a".repeat( 50 );
let mut long_path = PathBuf ::new();
for i in 0..10
{
long_path.push( format!( "{long_dir_name}_{i}" ) );
}
long_path.push( "final_file.txt" );
let joined = workspace.join( &long_path );
let expected = temp_dir.path().join( &long_path );
assert_eq!( joined, expected );
assert!( joined.is_absolute() );
assert!( joined.starts_with( temp_dir.path() ) );
}
#[ test ]
fn test_multiple_join_operations()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let path1 = workspace.join( "config" );
let path2 = workspace.join( "data" );
let path3 = workspace.join( "logs/debug.log" );
assert_eq!( path1, temp_dir.path().join( "config" ) );
assert_eq!( path2, temp_dir.path().join( "data" ) );
assert_eq!( path3, temp_dir.path().join( "logs/debug.log" ) );
assert_ne!( path1, path2 );
assert_ne!( path2, path3 );
assert_ne!( path1, path3 );
}
#[ test ]
fn test_all_standard_directory_paths()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let expected_mappings = vec![
( workspace.config_dir(), "config" ),
( workspace.data_dir(), "data" ),
( workspace.logs_dir(), "logs" ),
( workspace.docs_dir(), "docs" ),
( workspace.tests_dir(), "tests" ),
( workspace.workspace_dir(), ".workspace" ),
( workspace.cargo_toml(), "Cargo.toml" ),
( workspace.readme(), "readme.md" ),
];
for ( actual_path, expected_suffix ) in expected_mappings
{
let expected = temp_dir.path().join( expected_suffix );
assert_eq!( actual_path, expected, "Mismatch for {expected_suffix}" );
}
}
#[ cfg( feature = "secrets" ) ]
#[ test ]
fn test_secret_directory_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let secret_dir = workspace.secret_dir();
let expected = temp_dir.path().join( "secret" );
assert_eq!( secret_dir, expected );
}
#[ cfg( feature = "secrets" ) ]
#[ test ]
fn test_secret_file_path()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let secret_file = workspace.secret_file( "api.env" );
let expected = temp_dir.path().join( "secret/api.env" );
assert_eq!( secret_file, expected );
}
#[ test ]
fn test_root_path_immutability()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
let root1 = workspace.root();
let root2 = workspace.root();
assert_eq!( root1, root2 );
assert_eq!( root1, temp_dir.path() );
}
#[ test ]
fn test_path_operations_consistency()
{
let temp_dir = TempDir ::new().unwrap();
let workspace = create_test_workspace_at( temp_dir.path() );
for _ in 0..5
{
assert_eq!( workspace.config_dir(), temp_dir.path().join( "config" ) );
assert_eq!( workspace.join( "test.txt" ), temp_dir.path().join( "test.txt" ) );
let join_result1 = workspace.join( "test/file.txt" );
let join_result2 = workspace.join( "test/file.txt" );
assert_eq!( join_result1, join_result2 );
}
}