Run tests in isolated directory

This prevents tests from conflicting with eachother if they don't clean up after themselves correctly.
This commit is contained in:
Jake Howard 2024-03-23 11:40:06 +00:00 committed by Helmut K. C. Tessarek
parent 7ffeda450c
commit 829936762a
No known key found for this signature in database
GPG Key ID: BE0985349D44DD00
5 changed files with 73 additions and 79 deletions

1
Cargo.lock generated
View File

@ -2229,6 +2229,7 @@ dependencies = [
"serde_regex",
"shuttle-actix-web",
"shuttle-runtime",
"tempfile",
"tokio",
"tracing",
"tracing-subscriber",

View File

@ -65,6 +65,7 @@ default-features = false
[dev-dependencies]
actix-rt = "2.9.0"
tempfile = "3.10.1"
[profile.dev]
opt-level = 0

View File

@ -287,15 +287,16 @@ mod tests {
use actix_web::web::Data;
use awc::ClientBuilder;
use byte_unit::Byte;
use std::env;
use std::str::FromStr;
use std::time::Duration;
use tempfile::tempdir;
#[actix_rt::test]
#[allow(deprecated)]
async fn test_paste() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
config.paste.random_url = Some(RandomURLConfig {
enabled: Some(true),
words: Some(3),
@ -308,14 +309,9 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file("test.txt", None, None, &config).await?;
assert_eq!("ABC", fs::read_to_string(&file_name).await?);
assert_eq!(
Some("txt"),
PathBuf::from(&file_name)
.extension()
.and_then(|v| v.to_str())
);
fs::remove_file(file_name).await?;
let file_path = temp_upload_path.path().join(file_name);
assert_eq!("ABC", fs::read_to_string(&file_path).await?);
assert_eq!(Some("txt"), file_path.extension().and_then(|v| v.to_str()));
Ok(())
}
@ -323,8 +319,9 @@ mod tests {
#[actix_rt::test]
#[allow(deprecated)]
async fn test_paste_random() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
config.paste.random_url = Some(RandomURLConfig {
length: Some(4),
type_: RandomURLType::Alphanumeric,
@ -336,10 +333,10 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file("foo.tar.gz", None, None, &config).await?;
assert_eq!("tessus", fs::read_to_string(&file_name).await?);
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("tessus", fs::read_to_string(&file_path).await?);
assert!(file_name.ends_with(".tar.gz"));
assert!(file_name.starts_with("foo."));
fs::remove_file(file_name).await?;
config.paste.random_url = Some(RandomURLConfig {
length: Some(4),
@ -352,10 +349,10 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file(".foo.tar.gz", None, None, &config).await?;
assert_eq!("tessus", fs::read_to_string(&file_name).await?);
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("tessus", fs::read_to_string(&file_path).await?);
assert!(file_name.ends_with(".tar.gz"));
assert!(file_name.starts_with(".foo."));
fs::remove_file(file_name).await?;
config.paste.random_url = Some(RandomURLConfig {
length: Some(4),
@ -368,9 +365,9 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file("foo.tar.gz", None, None, &config).await?;
assert_eq!("tessus", fs::read_to_string(&file_name).await?);
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("tessus", fs::read_to_string(&file_path).await?);
assert!(file_name.ends_with(".tar.gz"));
fs::remove_file(file_name).await?;
Ok(())
}
@ -378,8 +375,9 @@ mod tests {
#[actix_rt::test]
#[allow(deprecated)]
async fn test_paste_with_extension() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
config.paste.default_extension = String::from("txt");
config.paste.random_url = None;
let paste = Paste {
@ -387,9 +385,9 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file(".foo", None, None, &config).await?;
assert_eq!("xyz", fs::read_to_string(&file_name).await?);
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("xyz", fs::read_to_string(&file_path).await?);
assert_eq!(".foo.txt", file_name);
fs::remove_file(file_name).await?;
config.paste.default_extension = String::from("bin");
config.paste.random_url = Some(RandomURLConfig {
@ -402,14 +400,9 @@ mod tests {
type_: PasteType::File,
};
let file_name = paste.store_file("random", None, None, &config).await?;
assert_eq!("xyz", fs::read_to_string(&file_name).await?);
assert_eq!(
Some("bin"),
PathBuf::from(&file_name)
.extension()
.and_then(|v| v.to_str())
);
fs::remove_file(file_name).await?;
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!(Some("bin"), file_path.extension().and_then(|v| v.to_str()));
assert_eq!("xyz", fs::read_to_string(&file_path).await?);
Ok(())
}
@ -417,8 +410,9 @@ mod tests {
#[actix_rt::test]
#[allow(deprecated)]
async fn test_paste_filename_from_header() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
config.paste.random_url = Some(RandomURLConfig {
length: Some(4),
type_: RandomURLType::Alphanumeric,
@ -437,9 +431,9 @@ mod tests {
&config,
)
.await?;
assert_eq!("tessus", fs::read_to_string(&file_name).await?);
assert_eq!("fn_from_header.txt", file_name);
fs::remove_file(file_name).await?;
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("tessus", fs::read_to_string(&file_path).await?);
config.paste.random_url = Some(RandomURLConfig {
length: Some(4),
@ -459,9 +453,9 @@ mod tests {
&config,
)
.await?;
assert_eq!("tessus", fs::read_to_string(&file_name).await?);
let file_path = temp_upload_path.path().join(&file_name);
assert_eq!("tessus", fs::read_to_string(&file_path).await?);
assert_eq!("fn_from_header", file_name);
fs::remove_file(file_name).await?;
Ok(())
}
@ -470,7 +464,7 @@ mod tests {
#[allow(deprecated)]
async fn test_paste_oneshot() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
config.paste.random_url = None;
fs::create_dir_all(
@ -502,7 +496,7 @@ mod tests {
#[allow(deprecated)]
async fn test_paste_url() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
config.paste.random_url = Some(RandomURLConfig {
enabled: Some(true),
..RandomURLConfig::default()
@ -542,7 +536,7 @@ mod tests {
#[allow(deprecated)]
async fn test_paste_remote_url() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
config.server.max_content_length = Byte::from_str("30k").expect("cannot parse byte");
fs::create_dir_all(
@ -562,25 +556,12 @@ mod tests {
.timeout(Duration::from_secs(30))
.finish(),
);
let file_name = paste.store_remote_file(None, &client_data, &config).await?;
let file_path = PasteType::RemoteFile
.get_path(&config.server.upload_path)
.expect("Bad upload path")
.join(file_name);
let _ = paste.store_remote_file(None, &client_data, &config).await?;
assert_eq!(
"70ff72a2f7651b5fae3aa9834e03d2a2233c52036610562f7fa04e089e8198ed",
util::sha256_digest(&*paste.data)?
);
fs::remove_file(file_path).await?;
for paste_type in &[PasteType::Url, PasteType::Oneshot] {
fs::remove_dir(
paste_type
.get_path(&config.server.upload_path)
.expect("Bad upload path"),
)
.await?;
}
Ok(())
}

View File

@ -398,6 +398,7 @@ mod tests {
use std::str;
use std::thread;
use std::time::Duration;
use tempfile::tempdir;
fn get_multipart_request(data: &str, name: &str, filename: &str) -> TestRequest {
let multipart_data = format!(
@ -730,9 +731,10 @@ mod tests {
#[actix_web::test]
async fn test_delete_file() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.delete_tokens = Some(["test".to_string()].into());
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
let app = test::init_service(
App::new()
@ -759,8 +761,7 @@ mod tests {
assert_eq!(StatusCode::OK, response.status());
assert_body(response.into_body(), "file deleted\n").await?;
let path = PathBuf::from(file_name);
assert!(!path.exists());
assert!(!temp_upload_path.path().join(file_name).exists());
Ok(())
}
@ -768,7 +769,7 @@ mod tests {
#[actix_web::test]
async fn test_delete_file_without_token_in_config() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
let app = test::init_service(
App::new()
@ -793,8 +794,9 @@ mod tests {
#[actix_web::test]
async fn test_upload_file() -> Result<(), Error> {
let test_delete_file = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = test_delete_file.path().to_path_buf();
let app = test::init_service(
App::new()
@ -825,7 +827,7 @@ mod tests {
assert_eq!(StatusCode::OK, response.status());
assert_body(response.into_body(), &timestamp).await?;
fs::remove_file(file_name).await?;
fs::remove_file(test_delete_file.path().join(file_name)).await?;
let serve_request = TestRequest::get()
.uri(&format!("/{file_name}"))
.to_request();
@ -837,8 +839,9 @@ mod tests {
#[actix_web::test]
async fn test_upload_file_override_filename() -> Result<(), Error> {
let test_delete_file = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = test_delete_file.path().to_path_buf();
let app = test::init_service(
App::new()
@ -875,7 +878,7 @@ mod tests {
assert_eq!(StatusCode::OK, response.status());
assert_body(response.into_body(), &timestamp).await?;
fs::remove_file(header_filename).await?;
fs::remove_file(test_delete_file.path().join(header_filename)).await?;
let serve_request = TestRequest::get()
.uri(&format!("/{header_filename}"))
.to_request();
@ -887,8 +890,9 @@ mod tests {
#[actix_web::test]
async fn test_upload_same_filename() -> Result<(), Error> {
let temp_upload_dir = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_dir.path().to_path_buf();
let app = test::init_service(
App::new()
@ -932,7 +936,7 @@ mod tests {
assert_eq!(StatusCode::CONFLICT, response.status());
assert_body(response.into_body(), "file already exists\n").await?;
fs::remove_file(header_filename)?;
fs::remove_file(temp_upload_dir.path().join(header_filename)).await?;
Ok(())
}
@ -987,8 +991,9 @@ mod tests {
#[actix_web::test]
async fn test_upload_expiring_file() -> Result<(), Error> {
let temp_upload_path = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_path.path().to_path_buf();
let app = test::init_service(
App::new()
@ -1032,9 +1037,14 @@ mod tests {
let response = test::call_service(&app, serve_request).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
if let Some(glob_path) = glob(&format!("{file_name}.[0-9]*"))
.map_err(error::ErrorInternalServerError)?
.next()
if let Some(glob_path) = glob(
&temp_upload_path
.path()
.join(format!("{file_name}.[0-9]*"))
.to_string_lossy(),
)
.map_err(error::ErrorInternalServerError)?
.next()
{
fs::remove_file(glob_path.map_err(error::ErrorInternalServerError)?).await?;
}
@ -1044,8 +1054,9 @@ mod tests {
#[actix_web::test]
async fn test_upload_remote_file() -> Result<(), Error> {
let temp_upload_dir = tempdir()?;
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = temp_upload_dir.path().to_path_buf();
config.server.max_content_length = Byte::from_u128(30000).unwrap_or_default();
let app = test::init_service(
@ -1091,7 +1102,7 @@ mod tests {
util::sha256_digest(&*body_bytes)?
);
fs::remove_file(file_name).await?;
fs::remove_file(temp_upload_dir.path().join(file_name)).await?;
let serve_request = TestRequest::get()
.uri(&format!("/{file_name}"))
@ -1105,7 +1116,7 @@ mod tests {
#[actix_web::test]
async fn test_upload_url() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
let app = test::init_service(
App::new()
@ -1145,7 +1156,7 @@ mod tests {
#[actix_web::test]
async fn test_upload_oneshot() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
let app = test::init_service(
App::new()
@ -1205,7 +1216,7 @@ mod tests {
#[actix_web::test]
async fn test_upload_oneshot_url() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.upload_path = tempdir()?.path().to_path_buf();
let oneshot_url_suffix = "oneshot_url";

View File

@ -143,6 +143,8 @@ mod tests {
use std::env;
use std::fs;
use std::thread;
use tempfile::tempdir;
#[test]
fn test_system_time() -> Result<(), ActixError> {
let system_time = get_system_time()?.as_millis();
@ -185,18 +187,16 @@ mod tests {
#[test]
fn test_get_expired_files() -> Result<(), ActixError> {
let current_dir = env::current_dir()?;
let test_temp_dir = tempdir()?;
let test_dir = test_temp_dir.path();
let expiration_time = get_system_time()?.as_millis() + 50;
let path = PathBuf::from(format!("expired.file2.{expiration_time}"));
let path = test_dir.join(format!("expired.file2.{expiration_time}"));
fs::write(&path, String::new())?;
assert_eq!(Vec::<PathBuf>::new(), get_expired_files(&current_dir));
assert_eq!(Vec::<PathBuf>::new(), get_expired_files(test_dir));
thread::sleep(Duration::from_millis(75));
assert_eq!(
vec![current_dir.join(&path)],
get_expired_files(&current_dir)
);
fs::remove_file(path)?;
assert_eq!(Vec::<PathBuf>::new(), get_expired_files(&current_dir));
assert_eq!(vec![path.clone()], get_expired_files(test_dir));
fs::remove_file(&path)?;
assert_eq!(Vec::<PathBuf>::new(), get_expired_files(test_dir));
Ok(())
}