fix(lints): apply clippy suggestions for tests

This commit is contained in:
Orhun Parmaksız 2022-05-16 12:58:41 +03:00
parent 657ca8c1d4
commit a330b59dca
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
7 changed files with 39 additions and 38 deletions

View File

@ -75,7 +75,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --verbose -- -D warnings
args: --tests --verbose -- -D warnings
rustfmt:
name: Formatting

View File

@ -55,21 +55,21 @@ impl Directory {
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsString;
#[test]
fn test_file_checksum() -> Result<(), ActixError> {
assert_eq!(
"rustypaste_logo.png",
Some(OsString::from("rustypaste_logo.png").as_ref()),
Directory::try_from(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("img")
.as_path()
)?
.get_file("2073f6f567dcba3b468c568d29cf8ed2e9d3f0f7305b9ab1b5a22861f5922e61")
.unwrap()
.expect("cannot get file with checksum")
.path
.file_name()
.unwrap()
);
Ok(())
}

View File

@ -97,7 +97,7 @@ mod tests {
HeaderName::from_static(EXPIRE),
HeaderValue::from_static("5ms"),
);
let expiry_time = parse_expiry_date(&headers)?.unwrap();
let expiry_time = parse_expiry_date(&headers)?.unwrap_or_default();
assert!(expiry_time > util::get_system_time()?.as_millis());
thread::sleep(Duration::from_millis(10));
assert!(expiry_time < util::get_system_time()?.as_millis());

View File

@ -47,7 +47,7 @@ mod tests {
use super::*;
#[test]
fn test_match_mime_type() {
fn test_match_mime_type() -> Result<(), FromStrError> {
assert_eq!(
mime::TEXT_PLAIN,
get_mime_type(
@ -56,8 +56,7 @@ mod tests {
regex: Regex::new("^.*\\.test$").ok(),
}],
String::from("mime.test")
)
.unwrap()
)?
);
assert_eq!(
mime::IMAGE_PNG,
@ -67,16 +66,16 @@ mod tests {
regex: Regex::new("^.*\\.PNG$").ok(),
}],
String::from("image.PNG")
)
.unwrap()
)?
);
assert_eq!(
mime::APPLICATION_PDF,
get_mime_type(&[], String::from("book.pdf")).unwrap()
get_mime_type(&[], String::from("book.pdf"))?
);
assert_eq!(
mime::APPLICATION_OCTET_STREAM,
get_mime_type(&[], String::from("x.unknown")).unwrap()
get_mime_type(&[], String::from("x.unknown"))?
);
Ok(())
}
}

View File

@ -263,8 +263,7 @@ mod tests {
Some("txt"),
PathBuf::from(&file_name)
.extension()
.map(|v| v.to_str())
.flatten()
.and_then(|v| v.to_str())
);
fs::remove_file(file_name)?;
@ -286,8 +285,7 @@ mod tests {
Some("bin"),
PathBuf::from(&file_name)
.extension()
.map(|v| v.to_str())
.flatten()
.and_then(|v| v.to_str())
);
fs::remove_file(file_name)?;
@ -300,7 +298,7 @@ mod tests {
data: vec![116, 101, 115, 116],
type_: PasteType::Oneshot,
};
let expiry_date = util::get_system_time().unwrap().as_millis() + 100;
let expiry_date = util::get_system_time()?.as_millis() + 100;
let file_name = paste.store_file("test.file", Some(expiry_date), &config)?;
let file_path = PasteType::Oneshot
.get_path(&config.server.upload_path)
@ -328,7 +326,7 @@ mod tests {
};
assert!(paste.store_url(None, &config).is_err());
config.server.max_content_length = Byte::from_str("30k").unwrap();
config.server.max_content_length = Byte::from_str("30k").expect("cannot parse byte");
let url = String::from("https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg");
let mut paste = Paste {
data: url.as_bytes().to_vec(),

View File

@ -66,8 +66,10 @@ mod tests {
type_: RandomURLType::PetName,
..RandomURLConfig::default()
};
let random_url = random_config.generate().unwrap();
assert_eq!(3, random_url.split("~").collect::<Vec<&str>>().len());
let random_url = random_config
.generate()
.expect("cannot generate random URL");
assert_eq!(3, random_url.split('~').count());
let random_config = RandomURLConfig {
enabled: true,
@ -75,7 +77,9 @@ mod tests {
type_: RandomURLType::Alphanumeric,
..RandomURLConfig::default()
};
let random_url = random_config.generate().unwrap();
let random_url = random_config
.generate()
.expect("cannot generate random URL");
assert_eq!(21, random_url.len());
let random_config = RandomURLConfig {

View File

@ -220,11 +220,11 @@ mod tests {
#[actix_web::test]
async fn test_index() {
let mut app = test::init_service(App::new().service(index)).await;
let app = test::init_service(App::new().service(index)).await;
let request = TestRequest::default()
.insert_header(("content-type", "text/plain"))
.to_request();
let resp = test::call_service(&mut app, request).await;
let resp = test::call_service(&app, request).await;
assert!(resp.status().is_redirection());
assert_eq!(http::StatusCode::FOUND, resp.status());
}
@ -249,17 +249,17 @@ mod tests {
.set_payload(multipart_data)
}
async fn assert_body(response: ServiceResponse, expected: &str) {
async fn assert_body(response: ServiceResponse, expected: &str) -> Result<(), Error> {
assert_eq!(http::StatusCode::OK, response.status());
let body = response.into_body();
match body.size() {
BodySize::Sized(size) => {
assert_eq!(size, expected.as_bytes().len() as u64);
let body_bytes = actix_web::body::to_bytes(body).await.unwrap();
let body_text = str::from_utf8(&body_bytes).unwrap();
assert_eq!(expected, body_text);
}
_ => {}
if let BodySize::Sized(size) = body.size() {
assert_eq!(size, expected.as_bytes().len() as u64);
let body_bytes = actix_web::body::to_bytes(body).await?;
let body_text = str::from_utf8(&body_bytes)?;
assert_eq!(expected, body_text);
Ok(())
} else {
Err(error::ErrorInternalServerError("unexpected body type"))
}
}
@ -269,7 +269,7 @@ mod tests {
config.server.upload_path = env::current_dir()?;
config.server.max_content_length = Byte::from_bytes(100);
let mut app = test::init_service(
let app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
.app_data(Data::new(Client::default()))
@ -281,25 +281,25 @@ mod tests {
let file_name = "upload_test.txt";
let timestamp = util::get_system_time()?.as_secs().to_string();
let response = test::call_service(
&mut app,
&app,
get_multipart_request(&timestamp, file_name).to_request(),
)
.await;
assert_body(response, &format!("http://localhost:8080/{}\n", file_name)).await;
assert_body(response, &format!("http://localhost:8080/{}\n", file_name)).await?;
let serve_request = TestRequest::get()
.uri(&format!("/{}", file_name))
.to_request();
let response = test::call_service(&mut app, serve_request).await;
let response = test::call_service(&app, serve_request).await;
assert_eq!(http::StatusCode::OK, response.status());
assert_body(response, &timestamp).await;
assert_body(response, &timestamp).await?;
fs::remove_file(file_name)?;
let serve_request = TestRequest::get()
.uri(&format!("/{}", file_name))
.to_request();
let response = test::call_service(&mut app, serve_request).await;
let response = test::call_service(&app, serve_request).await;
assert_eq!(http::StatusCode::NOT_FOUND, response.status());
Ok(())