Skip to content

Commit

Permalink
feat(octez): implement partialeq for file wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 30, 2024
1 parent 096e146 commit f15bf88
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion crates/octez/src/async/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ impl Default for FileWrapper {
}
}

impl PartialEq for FileWrapper {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(FileWrapper::File((_, p1)), FileWrapper::File((_, p2))) => p1 == p2,
(FileWrapper::TempFile(v1), FileWrapper::TempFile(v2)) => {
v1.path() == v2.path()
}
_ => false,
}
}
}

impl TryFrom<PathBuf> for FileWrapper {
type Error = anyhow::Error;

Expand Down Expand Up @@ -60,7 +72,7 @@ mod tests {
path::PathBuf,
};

use tempfile::NamedTempFile;
use tempfile::{NamedTempFile, TempPath};

use super::FileWrapper;

Expand Down Expand Up @@ -148,4 +160,26 @@ mod tests {
let serialized = serde_json::to_value(&file).unwrap();
assert_eq!(serialized, expected);
}

#[test]
fn partial_eq() {
let (tmp_file, tmp_path) = NamedTempFile::new().unwrap().into_parts();
let file1 = FileWrapper::try_from(tmp_path.to_path_buf()).unwrap();
let file2 = FileWrapper::try_from(tmp_path.to_path_buf()).unwrap();

let path = tmp_path.to_path_buf();
let file3 = FileWrapper::TempFile(NamedTempFile::from_parts(
tmp_file.try_clone().unwrap(),
TempPath::from_path(path.clone()),
));
let file4 = FileWrapper::TempFile(NamedTempFile::from_parts(
tmp_file.try_clone().unwrap(),
TempPath::from_path(path),
));

assert_eq!(file1, file1);
assert_eq!(file1, file2);
assert_eq!(file3, file4);
assert_ne!(file2, file3);
}
}

0 comments on commit f15bf88

Please sign in to comment.