From da287f84b193e585549aebcafa9c010f1460afd9 Mon Sep 17 00:00:00 2001 From: Nikita Sivukhin Date: Wed, 30 Jul 2025 00:44:05 +0400 Subject: [PATCH] add blocking implementation of filesystem for tests --- packages/turso-sync/src/filesystem/test.rs | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 packages/turso-sync/src/filesystem/test.rs diff --git a/packages/turso-sync/src/filesystem/test.rs b/packages/turso-sync/src/filesystem/test.rs new file mode 100644 index 000000000..207977e0e --- /dev/null +++ b/packages/turso-sync/src/filesystem/test.rs @@ -0,0 +1,97 @@ +use std::{io::Write, sync::Arc}; + +use crate::{filesystem::Filesystem, test_context::TestContext, Result}; + +pub struct TestFilesystem { + ctx: Arc, +} + +impl TestFilesystem { + pub fn new(ctx: Arc) -> Self { + Self { ctx } + } +} + +impl Filesystem for TestFilesystem { + type File = std::fs::File; + + async fn exists_file(&self, path: &std::path::Path) -> Result { + self.ctx.faulty_call("exists_file_start").await?; + let result = std::fs::exists(path)?; + self.ctx.faulty_call("exists_file_end").await?; + Ok(result) + } + + async fn remove_file(&self, path: &std::path::Path) -> Result<()> { + self.ctx.faulty_call("remove_file_start").await?; + match std::fs::remove_file(path) { + Ok(()) => Result::Ok(()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Result::Ok(()), + Err(e) => Err(e.into()), + }?; + self.ctx.faulty_call("remove_file_end").await?; + Ok(()) + } + + async fn create_file(&self, path: &std::path::Path) -> Result { + self.ctx.faulty_call("create_file_start").await?; + let result = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(path)?; + self.ctx.faulty_call("create_file_end").await?; + Ok(result) + } + + async fn open_file(&self, path: &std::path::Path) -> Result { + self.ctx.faulty_call("open_file_start").await?; + let result = std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(path)?; + self.ctx.faulty_call("open_file_end").await?; + Ok(result) + } + + async fn copy_file(&self, src: &std::path::Path, dst: &std::path::Path) -> Result<()> { + self.ctx.faulty_call("copy_file_start").await?; + std::fs::copy(src, dst)?; + self.ctx.faulty_call("copy_file_end").await?; + Ok(()) + } + + async fn rename_file(&self, src: &std::path::Path, dst: &std::path::Path) -> Result<()> { + self.ctx.faulty_call("rename_file_start").await?; + std::fs::rename(src, dst)?; + self.ctx.faulty_call("rename_file_end").await?; + Ok(()) + } + + async fn truncate_file(&self, file: &Self::File, size: usize) -> Result<()> { + self.ctx.faulty_call("truncate_file_start").await?; + file.set_len(size as u64)?; + self.ctx.faulty_call("truncate_file_end").await?; + Ok(()) + } + + async fn write_file(&self, file: &mut Self::File, buf: &[u8]) -> Result<()> { + self.ctx.faulty_call("write_file_start").await?; + file.write_all(buf)?; + self.ctx.faulty_call("write_file_end").await?; + Ok(()) + } + + async fn sync_file(&self, file: &Self::File) -> Result<()> { + self.ctx.faulty_call("sync_file_start").await?; + file.sync_all()?; + self.ctx.faulty_call("sync_file_end").await?; + Ok(()) + } + + async fn read_file(&self, path: &std::path::Path) -> Result> { + self.ctx.faulty_call("read_file_start").await?; + let data = std::fs::read(path)?; + self.ctx.faulty_call("read_file_end").await?; + Ok(data) + } +}