Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit ac35f64

Browse files
committed
refactor executable_path
1 parent b068b31 commit ac35f64

File tree

4 files changed

+39
-48
lines changed

4 files changed

+39
-48
lines changed

src/protocol/command.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ impl Command {
2222
}
2323

2424
pub fn compare(&self, other: &Command) -> bool {
25-
executable_path::compare_executables(
26-
&self.executable.as_os_str().as_bytes(),
27-
&other.executable.as_os_str().as_bytes(),
28-
) && self.arguments == other.arguments
25+
executable_path::compare_executables(&self.executable, &other.executable)
26+
&& self.arguments == other.arguments
2927
}
3028

3129
fn escape(word: String) -> String {
@@ -50,12 +48,11 @@ impl Command {
5048
}
5149

5250
pub fn format(&self) -> String {
53-
let executable = String::from_utf8_lossy(&executable_path::canonicalize(
54-
&self.executable.as_os_str().as_bytes(),
55-
))
56-
.into_owned();
51+
let executable = executable_path::canonicalize(&self.executable)
52+
.to_string_lossy()
53+
.into_owned();
5754
if self.arguments.is_empty() {
58-
executable.to_string()
55+
executable
5956
} else {
6057
format!(
6158
"{} {}",

src/protocol/executable_path.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use quale::which;
22
use std::ffi::OsStr;
33
use 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
}

src/protocol_checker/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,10 @@ impl SyscallMock for ProtocolChecker {
101101
executable: PathBuf,
102102
arguments: Vec<OsString>,
103103
) -> R<()> {
104-
let is_unmocked_command = self.unmocked_commands.iter().any(|unmocked_command| {
105-
protocol::compare_executables(
106-
&unmocked_command.as_os_str().as_bytes(),
107-
&executable.as_os_str().as_bytes(),
108-
)
109-
});
104+
let is_unmocked_command = self
105+
.unmocked_commands
106+
.iter()
107+
.any(|unmocked_command| protocol::compare_executables(unmocked_command, &executable));
110108
if !is_unmocked_command {
111109
let mock_executable_path = self.handle_step(protocol::Command {
112110
executable,

src/recorder/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ impl SyscallMock for Recorder {
4646
executable: PathBuf,
4747
arguments: Vec<OsString>,
4848
) -> R<()> {
49-
let is_unmocked_command = self.unmocked_commands.iter().any(|unmocked_command| {
50-
compare_executables(
51-
unmocked_command.as_os_str().as_bytes(),
52-
executable.as_os_str().as_bytes(),
53-
)
54-
});
49+
let is_unmocked_command = self
50+
.unmocked_commands
51+
.iter()
52+
.any(|unmocked_command| compare_executables(unmocked_command, &executable));
5553
if !is_unmocked_command {
5654
self.command = Some(Command {
5755
executable,

0 commit comments

Comments
 (0)