1
1
use quale:: which;
2
2
use std:: ffi:: OsStr ;
3
3
use std:: os:: unix:: ffi:: OsStrExt ;
4
- use std:: path:: PathBuf ;
4
+ use std:: path:: { Path , PathBuf } ;
5
5
6
- pub fn compare_executables ( a : & [ u8 ] , b : & [ u8 ] ) -> bool {
6
+ pub fn compare_executables ( a : & Path , b : & Path ) -> bool {
7
7
canonicalize ( a) == canonicalize ( b)
8
8
}
9
9
@@ -14,46 +14,44 @@ mod compare_executables {
14
14
15
15
#[ test]
16
16
fn returns_true_if_executables_are_identical ( ) -> R < ( ) > {
17
- let executable = b "./bin/myexec";
17
+ let executable = Path :: new ( "./bin/myexec" ) ;
18
18
assert ! ( compare_executables( executable, executable) ) ;
19
19
Ok ( ( ) )
20
20
}
21
21
22
22
#[ test]
23
23
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" ) ;
26
26
assert ! ( !compare_executables( a, b) ) ;
27
27
Ok ( ( ) )
28
28
}
29
29
30
30
#[ test]
31
31
fn returns_true_if_executables_match_after_lookup_in_path ( ) -> R < ( ) > {
32
32
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) ) ;
36
36
Ok ( ( ) )
37
37
}
38
38
}
39
39
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 ( ) ,
44
43
Some ( f) => f,
45
44
} ;
46
45
match which ( file_name) {
47
46
Some ( resolved) => {
48
- if resolved == path {
49
- file_name . as_bytes ( )
47
+ if resolved == executable {
48
+ PathBuf :: from ( file_name )
50
49
} else {
51
- executable
50
+ executable. into ( )
52
51
}
53
52
}
54
- None => executable,
53
+ None => executable. into ( ) ,
55
54
}
56
- . to_vec ( )
57
55
}
58
56
59
57
#[ cfg( test) ]
@@ -66,40 +64,40 @@ mod canonicalize {
66
64
fn shortens_absolute_executable_paths_if_found_in_path ( ) -> R < ( ) > {
67
65
let executable = "cp" ;
68
66
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" ) ) ;
71
69
Ok ( ( ) )
72
70
}
73
71
74
72
#[ test]
75
73
fn does_not_shorten_executable_that_is_not_in_path ( ) -> R < ( ) > {
76
- let executable = b "/foo/doesnotexist";
74
+ let executable = Path :: new ( "/foo/doesnotexist" ) ;
77
75
let file_name = canonicalize ( executable) ;
78
- assert_eq ! ( String :: from_utf8 ( file_name) ? , "/foo/doesnotexist" ) ;
76
+ assert_eq ! ( file_name, PathBuf :: from ( "/foo/doesnotexist" ) ) ;
79
77
Ok ( ( ) )
80
78
}
81
79
82
80
#[ test]
83
81
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" ) ;
85
83
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" ) ) ;
87
85
Ok ( ( ) )
88
86
}
89
87
90
88
#[ test]
91
89
fn does_not_shorten_relative_path ( ) -> R < ( ) > {
92
- let executable = b "./foo";
90
+ let executable = Path :: new ( "./foo" ) ;
93
91
let file_name = canonicalize ( executable) ;
94
- assert_eq ! ( String :: from_utf8 ( file_name) ? , "./foo" ) ;
92
+ assert_eq ! ( file_name, PathBuf :: from ( "./foo" ) ) ;
95
93
Ok ( ( ) )
96
94
}
97
95
98
96
#[ test]
99
97
fn does_not_modify_short_forms_if_found_in_path ( ) -> R < ( ) > {
100
- let executable = b "ls";
98
+ let executable = Path :: new ( "ls" ) ;
101
99
let file_name = canonicalize ( executable) ;
102
- assert_eq ! ( String :: from_utf8 ( file_name) ? , "ls" ) ;
100
+ assert_eq ! ( file_name, PathBuf :: from ( "ls" ) ) ;
103
101
Ok ( ( ) )
104
102
}
105
103
}
0 commit comments