From 8918b3f1909cbe8a163afbd4442df29ac45d49e8 Mon Sep 17 00:00:00 2001 From: yuchanns Date: Sat, 29 Jun 2024 15:48:29 +0800 Subject: [PATCH] tests(behavior): add test for writer write with overwrite --- core/src/services/sftp/backend.rs | 2 +- core/tests/behavior/async_write.rs | 32 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/src/services/sftp/backend.rs b/core/src/services/sftp/backend.rs index 24276207e8a1..955a58e64c38 100644 --- a/core/src/services/sftp/backend.rs +++ b/core/src/services/sftp/backend.rs @@ -452,7 +452,7 @@ impl Access for SftpBackend { if op.append() { option.append(true); } else { - option.write(true); + option.write(true).truncate(true); } let file = option.open(path).await.map_err(parse_sftp_error)?; diff --git a/core/tests/behavior/async_write.rs b/core/tests/behavior/async_write.rs index 969ea31398cb..166bef7b2405 100644 --- a/core/tests/behavior/async_write.rs +++ b/core/tests/behavior/async_write.rs @@ -43,6 +43,7 @@ pub fn tests(op: &Operator, tests: &mut Vec) { test_write_with_content_type, test_write_with_content_disposition, test_writer_write, + test_writer_write_with_overwrite, test_writer_write_with_concurrent, test_writer_sink, test_writer_sink_with_concurrent, @@ -561,3 +562,34 @@ pub async fn test_writer_with_append(op: Operator) -> Result<()> { op.delete(&path).await.expect("delete must succeed"); Ok(()) } + +pub async fn test_writer_write_with_overwrite(op: Operator) -> Result<()> { + let path = uuid::Uuid::new_v4().to_string(); + let (content_one, _) = gen_bytes(op.info().full_capability()); + let (content_two, _) = gen_bytes(op.info().full_capability()); + + op.write(&path, content_one.clone()).await?; + let bs = op.read(&path).await?.to_bytes(); + assert_eq!( + format!("{:x}", Sha256::digest(&bs)), + format!("{:x}", Sha256::digest(&content_one)), + "read content_one" + ); + op.write(&path, content_two.clone()) + .await + .expect("write overwrite must succeed"); + let bs = op.read(&path).await?.to_bytes(); + assert_ne!( + format!("{:x}", Sha256::digest(&bs)), + format!("{:x}", Sha256::digest(&content_one)), + "content_one must be overwrote" + ); + assert_eq!( + format!("{:x}", Sha256::digest(&bs)), + format!("{:x}", Sha256::digest(&content_two)), + "read content_two" + ); + + op.delete(&path).await.expect("delete must succeed"); + Ok(()) +}