use lock in writer

This commit is contained in:
Nikita Galaiko 2023-12-22 12:27:32 +01:00 committed by GitButler
parent 9070bb3373
commit 9b00ef5339
17 changed files with 116 additions and 101 deletions

View File

@ -10,9 +10,8 @@ pub struct DeltasWriter<'writer> {
}
impl<'writer> DeltasWriter<'writer> {
pub fn new(repository: &'writer gb_repository::Repository) -> Self {
let writer = writer::DirWriter::open(repository.root());
Self { writer, repository }
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self, std::io::Error> {
writer::DirWriter::open(repository.root()).map(|writer| Self { writer, repository })
}
pub fn write<P: AsRef<std::path::Path>>(&self, path: P, deltas: &Vec<Delta>) -> Result<()> {
@ -87,7 +86,7 @@ mod tests {
fn write_no_vbranches() -> Result<()> {
let Case { gb_repository, .. } = Suite::default().new_case();
let deltas_writer = DeltasWriter::new(&gb_repository);
let deltas_writer = DeltasWriter::new(&gb_repository)?;
let session = gb_repository.get_or_create_current_session()?;
let session_reader = sessions::Reader::open(&gb_repository, &session)?;

View File

@ -261,7 +261,8 @@ impl Repository {
.collect::<Vec<_>>();
let src_target_reader = virtual_branches::target::Reader::new(&last_session_reader);
let dst_target_writer = virtual_branches::target::Writer::new(self);
let dst_target_writer = virtual_branches::target::Writer::new(self)
.context("failed to open target writer for current session")?;
// copy default target
let default_target = match src_target_reader.read_default() {
@ -290,7 +291,8 @@ impl Repository {
.with_context(|| format!("{}: failed to write target", branch.id))?;
}
let dst_branch_writer = virtual_branches::branch::Writer::new(self);
let dst_branch_writer = virtual_branches::branch::Writer::new(self)
.context("failed to open branch writer for current session")?;
// copy branches that we don't already have
for branch in &branches {
@ -334,6 +336,7 @@ impl Repository {
// write session to disk
sessions::Writer::new(self)
.context("failed to create session writer")?
.write(&session)
.context("failed to write session")?;
@ -371,6 +374,7 @@ impl Repository {
};
sessions::Writer::new(self)
.context("failed to create session writer")?
.write(&updated_session)
.context("failed to write session")?;
@ -433,7 +437,9 @@ impl Repository {
let _lock = self.lock();
// update last timestamp
sessions::Writer::new(self).write(session)?;
sessions::Writer::new(self)
.context("failed to create session writer")?
.write(session)?;
let mut tree_builder = self.git_repository.treebuilder(None);

View File

@ -96,7 +96,7 @@ fn test_list_deltas_from_current_session() -> Result<()> {
let Case { gb_repository, .. } = Suite::default().new_case();
let current_session = gb_repository.get_or_create_current_session()?;
let writer = deltas::Writer::new(&gb_repository);
let writer = deltas::Writer::new(&gb_repository)?;
writer.write(
"test.txt",
&vec![deltas::Delta {
@ -130,7 +130,7 @@ fn test_list_deltas_from_flushed_session() -> Result<()> {
..
} = Suite::default().new_case();
let writer = deltas::Writer::new(&gb_repository);
let writer = deltas::Writer::new(&gb_repository)?;
writer.write(
"test.txt",
&vec![deltas::Delta {
@ -235,7 +235,7 @@ async fn test_remote_syncronization() -> Result<()> {
.await?;
let case_one = case_one.refresh();
let writer = deltas::Writer::new(&case_one.gb_repository);
let writer = deltas::Writer::new(&case_one.gb_repository)?;
writer.write(
"test.txt",
&vec![deltas::Delta {

View File

@ -22,7 +22,10 @@ fn test_should_not_write_session_with_hash() {
},
};
assert!(Writer::new(&gb_repository).write(&session).is_err());
assert!(Writer::new(&gb_repository)
.unwrap()
.write(&session)
.is_err());
}
#[test]
@ -40,7 +43,7 @@ fn test_should_write_full_session() -> Result<()> {
},
};
Writer::new(&gb_repository).write(&session)?;
Writer::new(&gb_repository)?.write(&session)?;
assert_eq!(
std::fs::read_to_string(gb_repository.session_path().join("meta/id"))?,
@ -81,7 +84,7 @@ fn test_should_write_partial_session() -> Result<()> {
},
};
Writer::new(&gb_repository).write(&session)?;
Writer::new(&gb_repository)?.write(&session)?;
assert_eq!(
std::fs::read_to_string(gb_repository.session_path().join("meta/id"))?,

View File

@ -12,9 +12,9 @@ pub struct SessionWriter<'writer> {
}
impl<'writer> SessionWriter<'writer> {
pub fn new(repository: &'writer gb_repository::Repository) -> Self {
let writer = writer::DirWriter::open(repository.root());
SessionWriter { repository, writer }
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self, std::io::Error> {
writer::DirWriter::open(repository.root())
.map(|writer| SessionWriter { repository, writer })
}
pub fn write(&self, session: &Session) -> Result<()> {

View File

@ -114,7 +114,8 @@ pub fn set_base_branch(
last_fetched_ms: None,
};
let target_writer = target::Writer::new(gb_repository);
let target_writer =
target::Writer::new(gb_repository).context("failed to create target writer")?;
target_writer.write_default(&target)?;
if !head_name
@ -192,7 +193,8 @@ pub fn set_base_branch(
order: 0,
};
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer =
branch::Writer::new(gb_repository).context("failed to create branch writer")?;
branch_writer.write(&mut branch)?;
}
}
@ -293,7 +295,8 @@ pub fn update_base_branch(
target.sha
))?;
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer =
branch::Writer::new(gb_repository).context("failed to create branch writer")?;
// try to update every branch
let updated_vbranches = super::get_status_by_branch(gb_repository, project_repository)?
@ -519,7 +522,8 @@ pub fn update_base_branch(
)?;
// write new target oid
let target_writer = target::Writer::new(gb_repository);
let target_writer =
target::Writer::new(gb_repository).context("failed to create target writer")?;
target_writer.write_default(&target::Target {
sha: new_target_commit.id(),
..target

View File

@ -103,7 +103,7 @@ mod tests {
let mut branch = test_branch();
let writer = Writer::new(&gb_repository);
let writer = Writer::new(&gb_repository)?;
writer.write(&mut branch)?;
let session = gb_repository.get_current_session()?.unwrap();

View File

@ -10,11 +10,8 @@ pub struct BranchWriter<'writer> {
}
impl<'writer> BranchWriter<'writer> {
pub fn new(repository: &'writer gb_repository::Repository) -> Self {
Self {
repository,
writer: writer::DirWriter::open(repository.root()),
}
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self, std::io::Error> {
writer::DirWriter::open(repository.root()).map(|writer| Self { repository, writer })
}
pub fn delete(&self, branch: &Branch) -> Result<()> {
@ -187,7 +184,7 @@ mod tests {
let mut branch = test_branch();
let writer = BranchWriter::new(&gb_repository);
let writer = BranchWriter::new(&gb_repository)?;
writer.write(&mut branch)?;
let root = gb_repository
@ -248,7 +245,7 @@ mod tests {
let mut branch = test_branch();
let writer = BranchWriter::new(&gb_repository);
let writer = BranchWriter::new(&gb_repository)?;
writer.write(&mut branch)?;
assert!(gb_repository.get_current_session()?.is_some());
@ -262,7 +259,7 @@ mod tests {
let mut branch = test_branch();
let writer = BranchWriter::new(&gb_repository);
let writer = BranchWriter::new(&gb_repository)?;
writer.write(&mut branch)?;
let mut updated_branch = Branch {

View File

@ -857,7 +857,8 @@ impl ControllerInner {
.fetch(default_target.branch.remote(), &self.helper)
.map_err(errors::FetchFromTargetError::Remote)?;
let target_writer = target::Writer::new(gb_repository);
let target_writer =
target::Writer::new(gb_repository).context("failed to open target writer")?;
target_writer
.write_default(&target::Target {
last_fetched_ms: Some(

View File

@ -262,7 +262,7 @@ fn verify_head_is_clean(
.context("failed to create virtual branch")?;
// rebasing the extra commits onto the new branch
let writer = super::branch::Writer::new(gb_repository);
let writer = super::branch::Writer::new(gb_repository).context("failed to create writer")?;
extra_commits.reverse();
let mut head = new_branch.head;
for commit in extra_commits {

View File

@ -149,10 +149,10 @@ mod tests {
fn test_iterate_all() -> Result<()> {
let Case { gb_repository, .. } = Suite::default().new_case();
let target_writer = target::Writer::new(&gb_repository);
let target_writer = target::Writer::new(&gb_repository)?;
target_writer.write_default(&test_target())?;
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
let mut branch_1 = test_branch();
branch_writer.write(&mut branch_1)?;
let mut branch_2 = test_branch();

View File

@ -107,7 +107,7 @@ mod tests {
fn test_read_deprecated_format() -> Result<()> {
let Case { gb_repository, .. } = Suite::default().new_case();
let writer = crate::writer::DirWriter::open(gb_repository.root());
let writer = crate::writer::DirWriter::open(gb_repository.root())?;
writer
.write_string("branches/target/name", "origin/master")
.unwrap();
@ -165,13 +165,13 @@ mod tests {
last_fetched_ms: Some(1),
};
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
branch_writer.write(&mut branch)?;
let session = gb_repository.get_current_session()?.unwrap();
let session_reader = sessions::Reader::open(&gb_repository, &session)?;
let target_writer = TargetWriter::new(&gb_repository);
let target_writer = TargetWriter::new(&gb_repository)?;
let reader = TargetReader::new(&session_reader);
target_writer.write_default(&default_target)?;

View File

@ -10,11 +10,8 @@ pub struct TargetWriter<'writer> {
}
impl<'writer> TargetWriter<'writer> {
pub fn new(repository: &'writer gb_repository::Repository) -> Self {
Self {
repository,
writer: writer::DirWriter::open(repository.root()),
}
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self, std::io::Error> {
writer::DirWriter::open(repository.root()).map(|writer| Self { repository, writer })
}
pub fn write_default(&self, target: &Target) -> Result<()> {
@ -169,10 +166,10 @@ mod tests {
last_fetched_ms: Some(1),
};
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
branch_writer.write(&mut branch)?;
let target_writer = TargetWriter::new(&gb_repository);
let target_writer = TargetWriter::new(&gb_repository)?;
target_writer.write(&branch.id, &target)?;
let root = gb_repository
@ -267,9 +264,9 @@ mod tests {
last_fetched_ms: Some(1),
};
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
branch_writer.write(&mut branch)?;
let target_writer = TargetWriter::new(&gb_repository);
let target_writer = TargetWriter::new(&gb_repository)?;
target_writer.write(&branch.id, &target)?;
let updated_target = Target {

View File

@ -35,7 +35,7 @@ pub fn set_test_target(
.expect("failed to add remote");
remote.push(&["refs/heads/master:refs/heads/master"], None)?;
target::Writer::new(gb_repo)
target::Writer::new(gb_repo)?
.write_default(&target::Target {
last_fetched_ms: None,
branch: "refs/remotes/origin/master".parse().unwrap(),
@ -735,7 +735,7 @@ fn test_move_hunks_multiple_sources() -> Result<()> {
let current_session = gb_repository.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(&gb_repository, &current_session)?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
let mut branch2 = branch_reader.read(&branch2_id)?;
branch2.ownership = Ownership {
files: vec!["test.txt:1-5".parse()?],
@ -1006,7 +1006,7 @@ fn test_merge_vbranch_upstream_clean() -> Result<()> {
)?;
set_test_target(&gb_repository, &project_repository)?;
target::Writer::new(&gb_repository).write_default(&target::Target {
target::Writer::new(&gb_repository)?.write_default(&target::Target {
last_fetched_ms: None,
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -1021,7 +1021,7 @@ fn test_merge_vbranch_upstream_clean() -> Result<()> {
)?;
let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
let mut branch = create_virtual_branch(
&gb_repository,
&project_repository,
@ -1136,7 +1136,7 @@ fn test_merge_vbranch_upstream_conflict() -> Result<()> {
)?;
set_test_target(&gb_repository, &project_repository)?;
target::Writer::new(&gb_repository).write_default(&target::Target {
target::Writer::new(&gb_repository)?.write_default(&target::Target {
last_fetched_ms: None,
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -1150,7 +1150,7 @@ fn test_merge_vbranch_upstream_conflict() -> Result<()> {
)?;
let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
let mut branch = create_virtual_branch(
&gb_repository,
&project_repository,
@ -1565,7 +1565,7 @@ fn test_detect_mergeable_branch() -> Result<()> {
let current_session = gb_repository.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(&gb_repository, &current_session)?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(&gb_repository);
let branch_writer = branch::Writer::new(&gb_repository)?;
update_branch(
&gb_repository,
@ -1759,7 +1759,7 @@ fn test_upstream_integrated_vbranch() -> Result<()> {
"update target",
)?;
target::Writer::new(&gb_repository).write_default(&target::Target {
target::Writer::new(&gb_repository)?.write_default(&target::Target {
last_fetched_ms: None,
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "http://origin.com/project".to_string(),

View File

@ -199,7 +199,7 @@ pub fn apply_branch(
})
})?;
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create branch writer")?;
let mut branch = match branch::Reader::new(&current_session_reader).read(branch_id) {
Ok(branch) => Ok(branch),
@ -477,7 +477,7 @@ pub fn unapply_ownership(
.context("failed to get status by branch")?;
// remove the ownership from the applied branches, and write them out
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
let applied_statuses = applied_statuses
.into_iter()
.map(|(branch, branch_files)| {
@ -593,11 +593,12 @@ pub fn unapply_branch(
.find_commit(default_target.sha)
.context("failed to find target commit")?;
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
let final_tree = if conflicts::is_resolving(project_repository) {
// when applying branch leads to a conflict, all other branches are unapplied.
// this means we can just reset to the default target tree.
{
let branch_writer = branch::Writer::new(gb_repository);
target_branch.applied = false;
branch_writer.write(&mut target_branch)?;
}
@ -628,7 +629,6 @@ pub fn unapply_branch(
.find(|(s, _)| s.id == target_branch.id)
.context("failed to find status for branch");
let branch_writer = branch::Writer::new(gb_repository);
if let Ok((_, files)) = status {
if files.is_empty() {
// if there is nothing to unapply, remove the branch straight away
@ -1072,7 +1072,7 @@ pub fn create_virtual_branch(
.unwrap_or(all_virtual_branches.len())
.clamp(0, all_virtual_branches.len());
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
// make space for the new branch
for (i, branch) in all_virtual_branches.iter().enumerate() {
@ -1288,7 +1288,8 @@ pub fn merge_virtual_branch_upstream(
.context("failed to checkout tree")?;
// write the branch data
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer =
branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.head = new_branch_head;
branch.tree = merge_tree_oid;
branch_writer.write(&mut branch)?;
@ -1310,7 +1311,7 @@ pub fn update_branch(
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.context("failed to open current session")?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
let mut branch = branch_reader
.read(&branch_update.id)
@ -1397,7 +1398,7 @@ pub fn delete_branch(
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.context("failed to open current session")?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
let branch = match branch_reader.read(branch_id) {
Ok(branch) => Ok(branch),
@ -1780,7 +1781,8 @@ fn get_applied_status(
// write updated state if not resolving
if !project_repository.is_resolving() {
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer =
branch::Writer::new(gb_repository).context("failed to create writer")?;
for (vbranch, files) in &mut hunks_by_branch {
vbranch.tree = write_tree(project_repository, default_target, files)?;
branch_writer
@ -1865,7 +1867,7 @@ pub fn reset_branch(
));
}
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.head = target_commit_oid;
branch_writer
.write(&mut branch)
@ -2195,7 +2197,7 @@ pub fn commit(
}
// update the virtual branch head
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.tree = tree_oid;
branch.head = commit_oid;
writer.write(branch).context("failed to write branch")?;
@ -2222,7 +2224,7 @@ pub fn push(
.map_err(errors::PushError::Other)?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
let mut vbranch = branch_reader.read(branch_id).map_err(|error| match error {
reader::Error::NotFound => errors::PushError::BranchNotFound(errors::BranchNotFoundError {
@ -2285,7 +2287,8 @@ pub fn mark_all_unapplied(gb_repository: &gb_repository::Repository) -> Result<(
let current_session = gb_repository.get_or_create_current_session()?;
let session_reader = sessions::Reader::open(gb_repository, &current_session)?;
let branch_iterator = super::Iterator::new(&session_reader)?;
let branch_writer = super::branch::Writer::new(gb_repository);
let branch_writer =
super::branch::Writer::new(gb_repository).context("failed to create writer")?;
branch_iterator
.collect::<Result<Vec<_>, _>>()
.context("failed to read branches")?
@ -2664,7 +2667,7 @@ pub fn amend(
)
.context("failed to create commit")?;
let branch_writer = branch::Writer::new(gb_repository);
let branch_writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
target_branch.head = commit_oid;
branch_writer.write(target_branch)?;
@ -2846,7 +2849,7 @@ pub fn cherry_pick(
.context("failed to checkout final tree")?;
// update branch status
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.head = commit_oid;
writer
.write(&mut branch)
@ -3038,7 +3041,7 @@ pub fn squash(
};
// save new branch head
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.head = new_head_id;
writer
.write(&mut branch)
@ -3214,7 +3217,7 @@ pub fn update_commit_message(
};
// save new branch head
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
branch.head = new_head_id;
writer
.write(&mut branch)
@ -3355,7 +3358,7 @@ pub fn create_virtual_branch_from_branch(
order,
};
let writer = branch::Writer::new(gb_repository);
let writer = branch::Writer::new(gb_repository).context("failed to create writer")?;
writer
.write(&mut branch)
.context("failed to write branch")?;

View File

@ -133,7 +133,8 @@ impl Handler {
if let Some(new_delta) = new_delta {
let deltas = text_doc.get_deltas();
let writer = deltas::Writer::new(&gb_repository);
let writer =
deltas::Writer::new(&gb_repository).context("failed to open deltas writer")?;
writer
.write(path, &deltas)
.context("failed to write deltas")?;
@ -810,8 +811,8 @@ mod test {
)]));
let listener = Handler::from(&suite.local_app_data);
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository);
let target_writer = virtual_branches::target::Writer::new(&gb_repository);
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?;
let target_writer = virtual_branches::target::Writer::new(&gb_repository)?;
let default_target = test_target();
target_writer.write_default(&default_target)?;
let mut vbranch0 = test_branch();
@ -866,8 +867,8 @@ mod test {
)]));
let listener = Handler::from(&suite.local_app_data);
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository);
let target_writer = virtual_branches::target::Writer::new(&gb_repository);
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?;
let target_writer = virtual_branches::target::Writer::new(&gb_repository)?;
let default_target = test_target();
target_writer.write_default(&default_target)?;
let mut vbranch0 = test_branch();

View File

@ -1,39 +1,43 @@
use anyhow::{Context, Result};
pub struct DirWriter {
root: std::path::PathBuf,
}
use crate::lock;
pub struct DirWriter(lock::Dir);
impl DirWriter {
pub fn open(root: std::path::PathBuf) -> Self {
Self { root }
pub fn open<P: AsRef<std::path::Path>>(root: P) -> Result<Self, std::io::Error> {
lock::Dir::new(root).map(Self)
}
}
impl DirWriter {
fn write(&self, path: &str, contents: &[u8]) -> Result<()> {
let file_path = self.root.join(path);
let dir_path = file_path.parent().context("failed to get parent")?;
std::fs::create_dir_all(dir_path).context("failed to create directory")?;
std::fs::write(file_path, contents)?;
Ok(())
self.0.batch(|root| {
let file_path = root.join(path);
let dir_path = file_path.parent().context("failed to get parent")?;
std::fs::create_dir_all(dir_path).context("failed to create directory")?;
std::fs::write(file_path, contents)?;
Ok(())
})?
}
pub fn remove(&self, path: &str) -> Result<()> {
let file_path = self.root.join(path);
if file_path.is_dir() {
match std::fs::remove_dir_all(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
self.0.batch(|root| {
let file_path = root.join(path);
if file_path.is_dir() {
match std::fs::remove_dir_all(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
} else {
match std::fs::remove_file(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
} else {
match std::fs::remove_file(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
})?
}
pub fn write_usize(&self, path: &str, contents: &usize) -> Result<()> {
@ -60,7 +64,7 @@ mod tests {
#[test]
fn test_write() {
let root = tempfile::tempdir().unwrap();
let writer = DirWriter::open(root.path().to_path_buf());
let writer = DirWriter::open(root.path()).unwrap();
writer.write("foo/bar", b"baz").unwrap();
assert_eq!(
std::fs::read_to_string(root.path().join("foo/bar")).unwrap(),
@ -71,7 +75,7 @@ mod tests {
#[test]
fn test_remove() {
let root = tempfile::tempdir().unwrap();
let writer = DirWriter::open(root.path().to_path_buf());
let writer = DirWriter::open(root.path()).unwrap();
writer.remove("foo/bar").unwrap();
assert!(!root.path().join("foo/bar").exists());
writer.write("foo/bar", b"baz").unwrap();