@@ -524,7 +524,7 @@ impl WorkerState {
524524 // Check the name first, since it doesn't require metadata
525525 let entry_path = entry. path ( ) ;
526526
527- let search_str = search_str_for_entry ( entry_path, config. cwd . as_deref ( ) ) ;
527+ let search_str = search_str_for_entry ( entry_path, config. full_path_base . as_deref ( ) ) ;
528528
529529 if !patterns
530530 . iter ( )
@@ -665,11 +665,16 @@ impl WorkerState {
665665
666666fn search_str_for_entry < ' a > (
667667 entry_path : & ' a std:: path:: Path ,
668- cwd : Option < & std:: path:: Path > ,
668+ full_path_base : Option < & std:: path:: Path > ,
669669) -> Cow < ' a , OsStr > {
670- if let Some ( cwd) = cwd {
671- let abs_path = filesystem:: make_absolute ( entry_path, cwd) ;
672- Cow :: Owned ( abs_path. into_os_string ( ) )
670+ if let Some ( cwd) = full_path_base {
671+ // If full_path_base is some, that means that we need to return
672+ // the absolute path
673+ if entry_path. is_absolute ( ) {
674+ return Cow :: Borrowed ( entry_path. as_os_str ( ) ) ;
675+ }
676+ let path = entry_path. strip_prefix ( "." ) . unwrap_or ( entry_path) ;
677+ Cow :: Owned ( cwd. join ( path) . into ( ) )
673678 } else {
674679 match entry_path. file_name ( ) {
675680 Some ( filename) => Cow :: Borrowed ( filename) ,
@@ -690,3 +695,44 @@ fn search_str_for_entry<'a>(
690695pub fn scan ( paths : & [ PathBuf ] , patterns : Vec < Regex > , config : Config ) -> Result < ExitCode > {
691696 WorkerState :: new ( patterns, config) . scan ( paths)
692697}
698+
699+ #[ cfg( test) ]
700+ mod tests {
701+ use super :: search_str_for_entry;
702+ use std:: path:: { Path , PathBuf } ;
703+
704+ #[ test]
705+ fn search_str_for_entry_with_relative_path ( ) {
706+ let full_path_base = Some ( Path :: new ( "/home/user" ) ) ;
707+ assert_eq ! (
708+ search_str_for_entry( Path :: new( "foo/bar" ) , full_path_base) ,
709+ PathBuf :: from( "/home/user/foo/bar" )
710+ ) ;
711+ }
712+
713+ #[ test]
714+ fn search_str_for_entry_strips_dot_prefix ( ) {
715+ let full_path_base = Some ( Path :: new ( "/home/user" ) ) ;
716+ assert_eq ! (
717+ search_str_for_entry( Path :: new( "./foo/bar" ) , full_path_base) ,
718+ PathBuf :: from( "/home/user/foo/bar" )
719+ ) ;
720+ }
721+
722+ #[ test]
723+ fn search_str_for_entry_with_absolute_path ( ) {
724+ let full_path_base = Some ( Path :: new ( "/home/user" ) ) ;
725+ assert_eq ! (
726+ search_str_for_entry( Path :: new( "/absolute/path" ) , full_path_base) ,
727+ PathBuf :: from( "/absolute/path" )
728+ ) ;
729+ }
730+
731+ #[ test]
732+ fn search_str_no_base_dir ( ) {
733+ assert_eq ! (
734+ search_str_for_entry( Path :: new( "./foo/bar" ) , None ) ,
735+ PathBuf :: from( "bar" )
736+ ) ;
737+ }
738+ }
0 commit comments