11use quale:: which;
22use std:: ffi:: OsStr ;
33use std:: os:: unix:: ffi:: OsStrExt ;
4- use std:: path:: PathBuf ;
4+ use std:: path:: { Path , PathBuf } ;
55
6- pub fn compare_executables ( a : & [ u8 ] , b : & [ u8 ] ) -> bool {
6+ pub fn compare_executables ( a : & Path , b : & Path ) -> bool {
77 canonicalize ( a) == canonicalize ( b)
88}
99
@@ -14,46 +14,44 @@ mod compare_executables {
1414
1515 #[ test]
1616 fn returns_true_if_executables_are_identical ( ) -> R < ( ) > {
17- let executable = b "./bin/myexec";
17+ let executable = Path :: new ( "./bin/myexec" ) ;
1818 assert ! ( compare_executables( executable, executable) ) ;
1919 Ok ( ( ) )
2020 }
2121
2222 #[ test]
2323 fn returns_false_if_executables_are_distinct ( ) -> R < ( ) > {
24- let a = b "./bin/myexec";
25- let b = b "./bin/myotherexec";
24+ let a = Path :: new ( "./bin/myexec" ) ;
25+ let b = Path :: new ( "./bin/myotherexec" ) ;
2626 assert ! ( !compare_executables( a, b) ) ;
2727 Ok ( ( ) )
2828 }
2929
3030 #[ test]
3131 fn returns_true_if_executables_match_after_lookup_in_path ( ) -> R < ( ) > {
3232 let path = which ( "cp" ) . unwrap ( ) ;
33- let cp_long = path. as_os_str ( ) . as_bytes ( ) ;
34- let cp_short = b "cp";
35- assert ! ( compare_executables( cp_long, cp_short) ) ;
33+ let cp_long = path;
34+ let cp_short = Path :: new ( "cp" ) ;
35+ assert ! ( compare_executables( & cp_long, cp_short) ) ;
3636 Ok ( ( ) )
3737 }
3838}
3939
40- pub fn canonicalize ( executable : & [ u8 ] ) -> Vec < u8 > {
41- let path = PathBuf :: from ( OsStr :: from_bytes ( executable) ) ;
42- let file_name = match path. file_name ( ) {
43- None => return executable. to_vec ( ) ,
40+ pub fn canonicalize ( executable : & Path ) -> PathBuf {
41+ let file_name = match executable. file_name ( ) {
42+ None => return executable. into ( ) ,
4443 Some ( f) => f,
4544 } ;
4645 match which ( file_name) {
4746 Some ( resolved) => {
48- if resolved == path {
49- file_name . as_bytes ( )
47+ if resolved == executable {
48+ PathBuf :: from ( file_name )
5049 } else {
51- executable
50+ executable. into ( )
5251 }
5352 }
54- None => executable,
53+ None => executable. into ( ) ,
5554 }
56- . to_vec ( )
5755}
5856
5957#[ cfg( test) ]
@@ -66,40 +64,40 @@ mod canonicalize {
6664 fn shortens_absolute_executable_paths_if_found_in_path ( ) -> R < ( ) > {
6765 let executable = "cp" ;
6866 let resolved = which ( executable) . unwrap ( ) ;
69- let file_name = canonicalize ( resolved. as_os_str ( ) . as_bytes ( ) ) ;
70- assert_eq ! ( String :: from_utf8 ( file_name) ? , "cp" ) ;
67+ let file_name = canonicalize ( & resolved) ;
68+ assert_eq ! ( file_name, PathBuf :: from ( "cp" ) ) ;
7169 Ok ( ( ) )
7270 }
7371
7472 #[ test]
7573 fn does_not_shorten_executable_that_is_not_in_path ( ) -> R < ( ) > {
76- let executable = b "/foo/doesnotexist";
74+ let executable = Path :: new ( "/foo/doesnotexist" ) ;
7775 let file_name = canonicalize ( executable) ;
78- assert_eq ! ( String :: from_utf8 ( file_name) ? , "/foo/doesnotexist" ) ;
76+ assert_eq ! ( file_name, PathBuf :: from ( "/foo/doesnotexist" ) ) ;
7977 Ok ( ( ) )
8078 }
8179
8280 #[ test]
8381 fn does_not_shorten_executable_that_is_not_in_path_but_has_same_name_as_one_that_is ( ) -> R < ( ) > {
84- let executable = b "/not/in/path/ls";
82+ let executable = Path :: new ( "/not/in/path/ls" ) ;
8583 let file_name = canonicalize ( executable) ;
86- assert_eq ! ( String :: from_utf8 ( file_name) ? , "/not/in/path/ls" ) ;
84+ assert_eq ! ( file_name, PathBuf :: from ( "/not/in/path/ls" ) ) ;
8785 Ok ( ( ) )
8886 }
8987
9088 #[ test]
9189 fn does_not_shorten_relative_path ( ) -> R < ( ) > {
92- let executable = b "./foo";
90+ let executable = Path :: new ( "./foo" ) ;
9391 let file_name = canonicalize ( executable) ;
94- assert_eq ! ( String :: from_utf8 ( file_name) ? , "./foo" ) ;
92+ assert_eq ! ( file_name, PathBuf :: from ( "./foo" ) ) ;
9593 Ok ( ( ) )
9694 }
9795
9896 #[ test]
9997 fn does_not_modify_short_forms_if_found_in_path ( ) -> R < ( ) > {
100- let executable = b "ls";
98+ let executable = Path :: new ( "ls" ) ;
10199 let file_name = canonicalize ( executable) ;
102- assert_eq ! ( String :: from_utf8 ( file_name) ? , "ls" ) ;
100+ assert_eq ! ( file_name, PathBuf :: from ( "ls" ) ) ;
103101 Ok ( ( ) )
104102 }
105103}
0 commit comments