Migrate distant-core tests from using tempfile to assert_fs crate

pull/38/head
Chip Senkbeil 3 years ago
parent cf0193edc9
commit d1f69cf024
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

7
Cargo.lock generated

@ -36,9 +36,9 @@ dependencies = [
[[package]]
name = "assert_fs"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0ca6aa3066e6c6f0357e056a25fa95e8737f15a04f9aead0b22d0d082a39465"
checksum = "58436a22e4ac01846f8e72e7dcd3f6aa96a4c9f8fa5e77f9234d287dfac0c354"
dependencies = [
"doc-comment",
"globwalk",
@ -258,6 +258,7 @@ dependencies = [
name = "distant-core"
version = "0.13.0"
dependencies = [
"assert_fs",
"bytes",
"derive_more",
"futures",
@ -266,13 +267,13 @@ dependencies = [
"lazy_static",
"log",
"orion",
"predicates",
"rand",
"serde",
"serde_cbor",
"serde_json",
"structopt",
"strum",
"tempfile",
"tokio",
"tokio-util",
"walkdir",

@ -35,6 +35,6 @@ whoami = "1.1.2"
[dev-dependencies]
assert_cmd = "2.0.0"
assert_fs = "1.0.3"
assert_fs = "1.0.4"
predicates = "2.0.2"
rstest = "0.11.0"

@ -32,4 +32,5 @@ walkdir = "2.3.2"
structopt = { version = "0.3.22", optional = true }
[dev-dependencies]
tempfile = "3.2.0"
assert_fs = "1.0.4"
predicates = "2.0.2"

@ -608,9 +608,8 @@ async fn system_info() -> Result<ResponseData, ServerError> {
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};
use assert_fs::prelude::*;
use predicates::prelude::*;
fn setup(
buffer: usize,
@ -629,18 +628,12 @@ mod tests {
)
}
/// Create a temporary path that does not exist
fn temppath() -> PathBuf {
// Deleted when dropped
NamedTempFile::new().unwrap().into_temp_path().to_path_buf()
}
#[tokio::test]
async fn file_read_should_send_error_if_fails_to_read_file() {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a file and then delete it, keeping just its path
let path = temppath();
let temp = assert_fs::TempDir::new().unwrap();
let path = temp.child("missing-file").path().to_path_buf();
let req = Request::new("test-tenant", vec![RequestData::FileRead { path }]);
@ -659,9 +652,9 @@ mod tests {
async fn file_read_should_send_blob_with_file_contents() {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a temporary file and fill it with some contents
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"some file contents").unwrap();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some file contents").unwrap();
let req = Request::new(
"test-tenant",
@ -684,8 +677,8 @@ mod tests {
async fn file_read_text_should_send_error_if_fails_to_read_file() {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a file and then delete it, keeping just its path
let path = temppath();
let temp = assert_fs::TempDir::new().unwrap();
let path = temp.child("missing-file").path().to_path_buf();
let req = Request::new(
"test-tenant",
@ -707,9 +700,9 @@ mod tests {
async fn file_read_text_should_send_text_with_file_contents() {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a temporary file and fill it with some contents
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"some file contents").unwrap();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some file contents").unwrap();
let req = Request::new(
"test-tenant",
@ -734,12 +727,13 @@ mod tests {
// Create a temporary path and add to it to ensure that there are
// extra components that don't exist to cause writing to fail
let path = temppath().join("some_file");
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("dir").child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileWrite {
path: path.clone(),
path: file.path().to_path_buf(),
data: b"some text".to_vec(),
}],
);
@ -755,7 +749,7 @@ mod tests {
);
// Also verify that we didn't actually create the file
assert!(!path.exists(), "File created unexpectedly");
file.assert(predicate::path::missing());
}
#[tokio::test]
@ -764,12 +758,13 @@ mod tests {
// Path should point to a file that does not exist, but all
// other components leading up to it do
let path = temppath();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileWrite {
path: path.clone(),
path: file.path().to_path_buf(),
data: b"some text".to_vec(),
}],
);
@ -786,8 +781,7 @@ mod tests {
// Also verify that we actually did create the file
// with the associated contents
assert!(path.exists(), "File not actually created");
assert_eq!(tokio::fs::read_to_string(path).await.unwrap(), "some text");
file.assert("some text");
}
#[tokio::test]
@ -796,12 +790,13 @@ mod tests {
// Create a temporary path and add to it to ensure that there are
// extra components that don't exist to cause writing to fail
let path = temppath().join("some_file");
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("dir").child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileWriteText {
path: path.clone(),
path: file.path().to_path_buf(),
text: String::from("some text"),
}],
);
@ -817,7 +812,7 @@ mod tests {
);
// Also verify that we didn't actually create the file
assert!(!path.exists(), "File created unexpectedly");
file.assert(predicate::path::missing());
}
#[tokio::test]
@ -826,12 +821,13 @@ mod tests {
// Path should point to a file that does not exist, but all
// other components leading up to it do
let path = temppath();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileWriteText {
path: path.clone(),
path: file.path().to_path_buf(),
text: String::from("some text"),
}],
);
@ -848,8 +844,7 @@ mod tests {
// Also verify that we actually did create the file
// with the associated contents
assert!(path.exists(), "File not actually created");
assert_eq!(tokio::fs::read_to_string(path).await.unwrap(), "some text");
file.assert("some text");
}
#[tokio::test]
@ -858,12 +853,13 @@ mod tests {
// Create a temporary path and add to it to ensure that there are
// extra components that don't exist to cause writing to fail
let path = temppath().join("some_file");
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("dir").child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileAppend {
path: path.to_path_buf(),
path: file.path().to_path_buf(),
data: b"some extra contents".to_vec(),
}],
);
@ -879,7 +875,7 @@ mod tests {
);
// Also verify that we didn't actually create the file
assert!(!path.exists(), "File created unexpectedly");
file.assert(predicate::path::missing());
}
#[tokio::test]
@ -887,8 +883,9 @@ mod tests {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a temporary file and fill it with some contents
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"some file contents").unwrap();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some file contents").unwrap();
let req = Request::new(
"test-tenant",
@ -909,10 +906,7 @@ mod tests {
);
// Also verify that we actually did append to the file
assert_eq!(
tokio::fs::read_to_string(file.path()).await.unwrap(),
"some file contentssome extra contents"
);
file.assert("some file contentssome extra contents");
}
#[tokio::test]
@ -921,12 +915,13 @@ mod tests {
// Create a temporary path and add to it to ensure that there are
// extra components that don't exist to cause writing to fail
let path = temppath().join("some_file");
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("dir").child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileAppendText {
path: path.to_path_buf(),
path: file.path().to_path_buf(),
text: String::from("some extra contents"),
}],
);
@ -942,7 +937,7 @@ mod tests {
);
// Also verify that we didn't actually create the file
assert!(!path.exists(), "File created unexpectedly");
file.assert(predicate::path::missing());
}
#[tokio::test]
@ -950,8 +945,9 @@ mod tests {
let (conn_id, state, tx, mut rx) = setup(1);
// Create a temporary file and fill it with some contents
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"some file contents").unwrap();
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some file contents").unwrap();
let req = Request::new(
"test-tenant",
@ -972,21 +968,20 @@ mod tests {
);
// Also verify that we actually did append to the file
assert_eq!(
tokio::fs::read_to_string(file.path()).await.unwrap(),
"some file contentssome extra contents"
);
file.assert("some file contentssome extra contents");
}
#[tokio::test]
async fn dir_read_should_send_error_if_directory_does_not_exist() {
let (conn_id, state, tx, mut rx) = setup(1);
let path = temppath();
let temp = assert_fs::TempDir::new().unwrap();
let dir = temp.child("test-dir");
let req = Request::new(
"test-tenant",
vec![RequestData::DirRead {
path,
path: dir.path().to_path_buf(),
depth: 0,
absolute: false,
canonicalize: false,
@ -1009,15 +1004,13 @@ mod tests {
// /root/file1
// /root/sub1/
// /root/sub1/file2
async fn setup_dir() -> TempDir {
let root_dir = TempDir::new().unwrap();
let file1 = root_dir.path().join("file1");
let sub1 = root_dir.path().join("sub1");
let file2 = sub1.join("file2");
tokio::fs::write(file1.as_path(), "").await.unwrap();
tokio::fs::create_dir(sub1.as_path()).await.unwrap();
tokio::fs::write(file2.as_path(), "").await.unwrap();
async fn setup_dir() -> assert_fs::TempDir {
let root_dir = assert_fs::TempDir::new().unwrap();
root_dir.child("file1").touch().unwrap();
let sub1 = root_dir.child("sub1");
sub1.create_dir_all().unwrap();
sub1.child("file2").touch().unwrap();
root_dir
}

Loading…
Cancel
Save