Skip to content

Commit

Permalink
basic test for filesystem probing (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 28, 2022
1 parent a3714ef commit 43272f3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
51 changes: 24 additions & 27 deletions git-worktree/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

/// Common knowledge about the worktree that is needed across most interactions with the work tree
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
Expand All @@ -20,45 +22,40 @@ pub struct Context {
}

impl Context {
/// try to determine all values in this context by probing them in the current working directory, which should be on the file system
/// the git repository is located on.
/// try to determine all values in this context by probing them in the given `directory`, which
/// should be on the file system the git repository is located on.
///
/// All errors are ignored and interpreted on top of the default for the platform the binary is compiled for.
pub fn from_probing_cwd() -> Self {
pub fn probe(directory: impl AsRef<std::path::Path>) -> Self {
let root = directory.as_ref();
let ctx = Context::default();
Context {
symlink: Self::probe_symlink().unwrap_or(ctx.symlink),
symlink: Self::probe_symlink(root).unwrap_or(ctx.symlink),
..ctx
}
}

fn probe_symlink() -> Option<bool> {
let src_path = "__link_src_file";
std::fs::File::options()
.create_new(true)
.write(true)
.open(src_path)
.ok()?;
let link_path = "__file_link";
if symlink::symlink_file(src_path, link_path).is_err() {
std::fs::remove_file(src_path).ok();
return None;
fn probe_symlink(root: &Path) -> std::io::Result<bool> {
let src_path = root.join("__link_src_file");
std::fs::File::options().create_new(true).write(true).open(&src_path)?;
let link_path = root.join("__file_link");
if symlink::symlink_file(&src_path, &link_path).is_err() {
std::fs::remove_file(&src_path)?;
return Ok(false);
}
let cleanup_all = || {
std::fs::remove_file(src_path).ok();
symlink::remove_symlink_file(link_path)
.or_else(|_| std::fs::remove_file(link_path))
.ok();
let res = std::fs::remove_file(&src_path);
symlink::remove_symlink_file(&link_path)
.or_else(|_| std::fs::remove_file(&link_path))
.and(res)
};

let res = Some(
std::fs::symlink_metadata(link_path)
.map_err(|_| cleanup_all())
.ok()?
.is_symlink(),
);
cleanup_all();
res
let res = std::fs::symlink_metadata(&link_path)
.or_else(|err| cleanup_all().and(Err(err)))?
.is_symlink();

cleanup_all()?;
Ok(res)
}
}

Expand Down
16 changes: 16 additions & 0 deletions git-worktree/tests/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[test]
fn from_probing_cwd() {
let dir = tempfile::tempdir().unwrap();
let _ctx = git_worktree::fs::Context::probe(dir.path());
let entries: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.filter_map(Result::ok)
.map(|e| e.path().to_owned())
.collect();
assert_eq!(
entries.len(),
0,
"there should be no left-over files after probing, found {:?}",
entries
);
}
2 changes: 1 addition & 1 deletion git-worktree/tests/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod checkout {
#[test]
fn allow_symlinks() -> crate::Result {
let opts = opts_with_symlink(true);
if !git_worktree::fs::Context::from_probing_cwd().symlink {
if !git_worktree::fs::Context::probe(std::env::current_dir()?).symlink {
eprintln!("IGNORING symlink test on file system without symlink support");
// skip if symlinks aren't supported anyway.
return Ok(());
Expand Down
1 change: 1 addition & 0 deletions git-worktree/tests/worktree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};

mod fs;
mod index;

type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
Expand Down

0 comments on commit 43272f3

Please sign in to comment.