@@ -1881,3 +1881,106 @@ impl GitManager {
18811881 Ok ( ( ) )
18821882 }
18831883}
1884+
1885+ #[ cfg( test) ]
1886+ mod tests {
1887+ use super :: * ;
1888+ use std:: fs;
1889+ use tempfile:: tempdir;
1890+
1891+
1892+ // Helper to create a dummy GitManager. We just need it to call check_sparse_checkout_status,
1893+ // which only uses `self.get_git_directory()`. It doesn't actually need the full config to be valid.
1894+ // Helper to create a dummy GitManager without needing a tempdir that gets dropped.
1895+ // It creates it pointing to the provided temp_dir.
1896+ fn create_dummy_manager ( config_path : std:: path:: PathBuf ) -> GitManager {
1897+ fs:: write ( & config_path, "[defaults]\n " ) . unwrap ( ) ;
1898+ GitManager :: new ( config_path) . expect ( "Failed to create GitManager" )
1899+ }
1900+
1901+ #[ test]
1902+ fn test_sparse_checkout_not_configured ( ) {
1903+ let temp_dir = tempdir ( ) . unwrap ( ) ;
1904+ let submodule_path = temp_dir. path ( ) ;
1905+
1906+ // Create .git directory but NO sparse-checkout file
1907+ let git_dir = submodule_path. join ( ".git" ) ;
1908+ fs:: create_dir ( & git_dir) . unwrap ( ) ;
1909+
1910+ // Create manager
1911+ let manager = create_dummy_manager ( temp_dir. path ( ) . join ( "submod.toml" ) ) ;
1912+
1913+ let expected_paths: Vec < String > = vec ! [ "path/a" . to_string( ) ] ;
1914+
1915+ let status = manager
1916+ . check_sparse_checkout_status ( submodule_path. to_str ( ) . unwrap ( ) , & expected_paths)
1917+ . unwrap ( ) ;
1918+
1919+ assert_eq ! ( status, SparseStatus :: NotConfigured ) ;
1920+ }
1921+
1922+ #[ test]
1923+ fn test_sparse_checkout_correct ( ) {
1924+ let temp_dir = tempdir ( ) . unwrap ( ) ;
1925+ let submodule_path = temp_dir. path ( ) ;
1926+
1927+ // Create .git/info/sparse-checkout file
1928+ let git_dir = submodule_path. join ( ".git" ) ;
1929+ let info_dir = git_dir. join ( "info" ) ;
1930+ fs:: create_dir_all ( & info_dir) . unwrap ( ) ;
1931+
1932+ let sparse_checkout_file = info_dir. join ( "sparse-checkout" ) ;
1933+ // Prepend SPARSE_DENY_ALL as handled in check_sparse_checkout_status
1934+ let content = format ! ( "{}\n path/a\n path/b\n " , SPARSE_DENY_ALL ) ;
1935+ fs:: write ( & sparse_checkout_file, content) . unwrap ( ) ;
1936+
1937+ // Create manager
1938+ let manager = create_dummy_manager ( temp_dir. path ( ) . join ( "submod.toml" ) ) ;
1939+
1940+ let expected_paths: Vec < String > = vec ! [
1941+ "path/a" . to_string( ) ,
1942+ "path/b" . to_string( ) ,
1943+ ] ;
1944+
1945+ let status = manager
1946+ . check_sparse_checkout_status ( submodule_path. to_str ( ) . unwrap ( ) , & expected_paths)
1947+ . unwrap ( ) ;
1948+
1949+ assert_eq ! ( status, SparseStatus :: Correct ) ;
1950+ }
1951+
1952+ #[ test]
1953+ fn test_sparse_checkout_mismatch ( ) {
1954+ let temp_dir = tempdir ( ) . unwrap ( ) ;
1955+ let submodule_path = temp_dir. path ( ) ;
1956+
1957+ // Create .git/info/sparse-checkout file
1958+ let git_dir = submodule_path. join ( ".git" ) ;
1959+ let info_dir = git_dir. join ( "info" ) ;
1960+ fs:: create_dir_all ( & info_dir) . unwrap ( ) ;
1961+
1962+ let sparse_checkout_file = info_dir. join ( "sparse-checkout" ) ;
1963+ let content = format ! ( "{}\n path/a\n " , SPARSE_DENY_ALL ) ;
1964+ fs:: write ( & sparse_checkout_file, content) . unwrap ( ) ;
1965+
1966+ // Create manager
1967+ let manager = create_dummy_manager ( temp_dir. path ( ) . join ( "submod.toml" ) ) ;
1968+
1969+ let expected_paths: Vec < String > = vec ! [
1970+ "path/a" . to_string( ) ,
1971+ "path/b" . to_string( ) , // This is expected but not configured
1972+ ] ;
1973+
1974+ let status = manager
1975+ . check_sparse_checkout_status ( submodule_path. to_str ( ) . unwrap ( ) , & expected_paths)
1976+ . unwrap ( ) ;
1977+
1978+ match status {
1979+ SparseStatus :: Mismatch { expected, actual } => {
1980+ assert_eq ! ( expected, vec![ "path/a" . to_string( ) , "path/b" . to_string( ) ] ) ;
1981+ assert_eq ! ( actual, vec![ "path/a" . to_string( ) ] ) ;
1982+ }
1983+ _ => panic ! ( "Expected Mismatch, got {:?}" , status) ,
1984+ }
1985+ }
1986+ }
0 commit comments