@@ -290,9 +290,10 @@ impl VirtualPath {
290290 // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file
291291 // hence this method will return `Some("directory_name", None)` for a directory
292292 pub fn file_name_and_extension ( & self ) -> Option < ( & str , Option < & str > ) > {
293- let file_name = match self . 0 . rfind ( '/' ) {
294- Some ( position) => & self . 0 [ position + 1 ..] ,
295- None => & self . 0 ,
293+ let file_path = if self . 0 . ends_with ( '/' ) { & self . 0 [ ..& self . 0 . len ( ) - 1 ] } else { & self . 0 } ;
294+ let file_name = match file_path. rfind ( '/' ) {
295+ Some ( position) => & file_path[ position + 1 ..] ,
296+ None => file_path,
296297 } ;
297298
298299 if file_name. is_empty ( ) {
@@ -310,3 +311,37 @@ impl VirtualPath {
310311 }
311312 }
312313}
314+
315+ #[ cfg( test) ]
316+ mod tests {
317+ use super :: * ;
318+
319+ #[ test]
320+ fn virtual_path_extensions ( ) {
321+ assert_eq ! ( VirtualPath ( "/" . to_string( ) ) . file_name_and_extension( ) , None ) ;
322+ assert_eq ! (
323+ VirtualPath ( "/directory" . to_string( ) ) . file_name_and_extension( ) ,
324+ Some ( ( "directory" , None ) )
325+ ) ;
326+ assert_eq ! (
327+ VirtualPath ( "/directory/" . to_string( ) ) . file_name_and_extension( ) ,
328+ Some ( ( "directory" , None ) )
329+ ) ;
330+ assert_eq ! (
331+ VirtualPath ( "/directory/file" . to_string( ) ) . file_name_and_extension( ) ,
332+ Some ( ( "file" , None ) )
333+ ) ;
334+ assert_eq ! (
335+ VirtualPath ( "/directory/.file" . to_string( ) ) . file_name_and_extension( ) ,
336+ Some ( ( ".file" , None ) )
337+ ) ;
338+ assert_eq ! (
339+ VirtualPath ( "/directory/.file.rs" . to_string( ) ) . file_name_and_extension( ) ,
340+ Some ( ( ".file" , Some ( "rs" ) ) )
341+ ) ;
342+ assert_eq ! (
343+ VirtualPath ( "/directory/file.rs" . to_string( ) ) . file_name_and_extension( ) ,
344+ Some ( ( "file" , Some ( "rs" ) ) )
345+ ) ;
346+ }
347+ }
0 commit comments