Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn print_recorded_protocol(context: &Context, program: &Path) -> R<ExitCode> {
)?;
write_yaml(
&mut *context.stdout(),
&Protocols::new(vec![protocol]).serialize()?,
&Protocols::new(vec![protocol]).serialize(&[])?,
)?;
Ok(ExitCode(0))
}
17 changes: 10 additions & 7 deletions src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ impl Command {
}
}

pub fn compare(&self, other: &Command) -> bool {
executable_path::compare_executables(&self.executable, &other.executable)
&& self.arguments == other.arguments
pub fn compare(&self, mocked_executables: &[PathBuf], other: &Command) -> bool {
executable_path::compare_executables(
mocked_executables,
&self.executable,
&other.executable,
) && self.arguments == other.arguments
}

fn escape(word: String) -> String {
Expand All @@ -46,8 +49,8 @@ impl Command {
.join(" ")
}

pub fn format(&self) -> String {
let executable = executable_path::canonicalize(&self.executable)
pub fn format(&self, mocked_executables: &[PathBuf]) -> String {
let executable = executable_path::canonicalize(mocked_executables, &self.executable)
.to_string_lossy()
.into_owned();
if self.arguments.is_empty() {
Expand Down Expand Up @@ -251,7 +254,7 @@ mod command {
($name:ident, $string:expr) => {
#[test]
fn $name() -> R<()> {
assert_eq!(Command::new($string)?.format(), $string);
assert_eq!(Command::new($string)?.format(&[]), $string);
Ok(())
}
};
Expand All @@ -261,7 +264,7 @@ mod command {
($name:ident, $input:expr, $normalized:expr) => {
#[test]
fn $name() -> R<()> {
assert_eq!(Command::new($input)?.format(), $normalized);
assert_eq!(Command::new($input)?.format(&[]), $normalized);
Ok(())
}
};
Expand Down
32 changes: 16 additions & 16 deletions src/protocol/command_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::command::Command;
use crate::R;
use regex::Regex;
use std::path::PathBuf;
use std::str;

#[derive(Debug, Clone, Eq, PartialEq)]
Expand All @@ -10,16 +11,16 @@ pub enum CommandMatcher {
}

impl CommandMatcher {
pub fn matches(&self, other: &Command) -> bool {
pub fn matches(&self, mocked_executables: &[PathBuf], other: &Command) -> bool {
match self {
CommandMatcher::ExactMatch(command) => command.compare(other),
CommandMatcher::RegexMatch(regex) => regex.is_match(&other.format()),
CommandMatcher::ExactMatch(command) => command.compare(mocked_executables, other),
CommandMatcher::RegexMatch(regex) => regex.is_match(&other.format(mocked_executables)),
}
}

pub fn format(&self) -> String {
pub fn format(&self, mocked_executables: &[PathBuf]) -> String {
match self {
CommandMatcher::ExactMatch(command) => command.format(),
CommandMatcher::ExactMatch(command) => command.format(mocked_executables),
CommandMatcher::RegexMatch(AnchoredRegex {
original_string, ..
}) => original_string.clone(),
Expand Down Expand Up @@ -63,44 +64,43 @@ mod command_matcher {

#[test]
fn matches_command_executable() -> R<()> {
assert!(
CommandMatcher::ExactMatch(Command::new("true")?).matches(&Command::new("true")?)
);
assert!(CommandMatcher::ExactMatch(Command::new("true")?)
.matches(&[], &Command::new("true")?));
Ok(())
}

#[test]
fn matches_command_with_arguments() -> R<()> {
assert!(CommandMatcher::ExactMatch(Command::new("echo 1")?)
.matches(&Command::new("echo 1")?));
.matches(&[], &Command::new("echo 1")?));
Ok(())
}

#[test]
fn matches_command_even_if_it_doesnt_exist() -> R<()> {
assert!(CommandMatcher::ExactMatch(Command::new("foo")?).matches(&Command::new("foo")?));
assert!(CommandMatcher::ExactMatch(Command::new("foo")?)
.matches(&[], &Command::new("foo")?));
Ok(())
}

#[test]
fn matches_command_with_full_path() -> R<()> {
assert!(CommandMatcher::ExactMatch(Command::new("/bin/true")?)
.matches(&Command::new("/bin/true")?));
.matches(&[], &Command::new("/bin/true")?));
Ok(())
}

#[test]
fn doesnt_match_a_different_command() -> R<()> {
assert!(
!CommandMatcher::ExactMatch(Command::new("foo")?).matches(&Command::new("bar")?)
);
assert!(!CommandMatcher::ExactMatch(Command::new("foo")?)
.matches(&[], &Command::new("bar")?));
Ok(())
}

#[test]
fn doesnt_match_with_the_same_executable_but_different_arguments() -> R<()> {
assert!(!CommandMatcher::ExactMatch(Command::new("foo 1")?)
.matches(&Command::new("foo 2")?));
.matches(&[], &Command::new("foo 2")?));
Ok(())
}
}
Expand All @@ -110,7 +110,7 @@ mod command_matcher {

fn test_regex_matches_command(regex: &str, command: &str) -> R<bool> {
let result = CommandMatcher::RegexMatch(AnchoredRegex::new(regex)?)
.matches(&Command::new(command)?);
.matches(&[], &Command::new(command)?);
Ok(result)
}

Expand Down
42 changes: 30 additions & 12 deletions src/protocol/executable_path.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use quale::which;
use std::path::{Path, PathBuf};

pub fn compare_executables(a: &Path, b: &Path) -> bool {
canonicalize(a) == canonicalize(b)
pub fn compare_executables(mocked_executables: &[PathBuf], a: &Path, b: &Path) -> bool {
canonicalize(mocked_executables, a) == canonicalize(mocked_executables, b)
}

#[cfg(test)]
Expand All @@ -13,15 +13,15 @@ mod compare_executables {
#[test]
fn returns_true_if_executables_are_identical() -> R<()> {
let executable = Path::new("./bin/myexec");
assert!(compare_executables(executable, executable));
assert!(compare_executables(&[], executable, executable));
Ok(())
}

#[test]
fn returns_false_if_executables_are_distinct() -> R<()> {
let a = Path::new("./bin/myexec");
let b = Path::new("./bin/myotherexec");
assert!(!compare_executables(a, b));
assert!(!compare_executables(&[], a, b));
Ok(())
}

Expand All @@ -30,17 +30,25 @@ mod compare_executables {
let path = which("cp").unwrap();
let cp_long = path;
let cp_short = Path::new("cp");
assert!(compare_executables(&cp_long, cp_short));
assert!(compare_executables(&[], &cp_long, cp_short));
Ok(())
}
}

pub fn canonicalize(executable: &Path) -> PathBuf {
fn foo_which(mocked_executables: &[PathBuf], name: &Path) -> Option<PathBuf> {
if mocked_executables.contains(&PathBuf::from(name)) {
Some(PathBuf::from("/bin").join(name))
} else {
which(name)
}
}

pub fn canonicalize(mocked_executables: &[PathBuf], executable: &Path) -> PathBuf {
let file_name = match executable.file_name() {
None => return executable.into(),
Some(f) => f,
};
match which(file_name) {
match foo_which(mocked_executables, &PathBuf::from(file_name)) {
Some(resolved) => {
if resolved == executable {
PathBuf::from(file_name)
Expand All @@ -62,40 +70,50 @@ mod canonicalize {
fn shortens_absolute_executable_paths_if_found_in_path() -> R<()> {
let executable = "cp";
let resolved = which(executable).unwrap();
let file_name = canonicalize(&resolved);
let file_name = canonicalize(&[], &resolved);
assert_eq!(file_name, PathBuf::from("cp"));
Ok(())
}

#[test]
fn does_not_shorten_executable_that_is_not_in_path() -> R<()> {
let executable = Path::new("/foo/doesnotexist");
let file_name = canonicalize(executable);
let file_name = canonicalize(&[], executable);
assert_eq!(file_name, PathBuf::from("/foo/doesnotexist"));
Ok(())
}

#[test]
fn does_not_shorten_executable_that_is_not_in_path_but_has_same_name_as_one_that_is() -> R<()> {
let executable = Path::new("/not/in/path/ls");
let file_name = canonicalize(executable);
let file_name = canonicalize(&[], executable);
assert_eq!(file_name, PathBuf::from("/not/in/path/ls"));
Ok(())
}

#[test]
fn does_not_shorten_relative_path() -> R<()> {
let executable = Path::new("./foo");
let file_name = canonicalize(executable);
let file_name = canonicalize(&[], executable);
assert_eq!(file_name, PathBuf::from("./foo"));
Ok(())
}

#[test]
fn does_not_modify_short_forms_if_found_in_path() -> R<()> {
let executable = Path::new("ls");
let file_name = canonicalize(executable);
let file_name = canonicalize(&[], executable);
assert_eq!(file_name, PathBuf::from("ls"));
Ok(())
}

#[test]
fn shortens_mocked_executables() -> R<()> {
let file_name = canonicalize(
&[PathBuf::from("does_not_exist")],
&PathBuf::from("/bin/does_not_exist"),
);
assert_eq!(file_name, PathBuf::from("does_not_exist"));
Ok(())
}
}
Loading