Skip to content

Commit

Permalink
restructure tests (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 7, 2022
1 parent 636fa8a commit 831c429
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 144 deletions.
3 changes: 2 additions & 1 deletion git-worktree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ git-object = { version = "^0.17.0", path = "../git-object" }
quick-error = "2.0.1"

[dev-dependencies]
git-testtools = { path = "../tests/tools" }
git-odb = { path = "../git-odb" }

walkdir = "2.3.2"
git-testtools = { path = "../tests/tools" }
tempfile = "3.2.0"
1 change: 1 addition & 0 deletions git-worktree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod index {
let root = path.as_ref();
let mut buf = Vec::new();
for (entry, entry_path) in index.entries_mut_with_paths() {
// TODO: write test for that
if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) {
continue;
}
Expand Down
107 changes: 0 additions & 107 deletions git-worktree/tests/copy_index/mod.rs

This file was deleted.

17 changes: 7 additions & 10 deletions git-worktree/tests/fixtures/make_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ set -eu -o pipefail
git init -q
git config commit.gpgsign false

touch a
echo "Test Vals" > a
touch b
touch c
touch executable.sh
chmod +x executable.sh
touch empty
echo "content" > executable
chmod +x executable

mkdir d
touch d/a
echo "Subdir" > d/a
ln -sf d/a sa
mkdir dir
echo "other content" > dir/content
mkdir dir/sub-dir
(cd dir/sub-dir && ln -sf ../content symlink)

git add -A
git commit -m "Commit"
96 changes: 96 additions & 0 deletions git-worktree/tests/index/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
mod checkout {
#[cfg(unix)]
use std::os::unix::prelude::MetadataExt;
use std::{
fs,
path::{Path, PathBuf},
};
use tempfile::TempDir;

use git_object::bstr::ByteSlice;
use git_odb::FindExt;
use git_worktree::index;

use crate::fixture_path;

#[test]
fn allow_symlinks() -> crate::Result {
let opts = Default::default();
let (source_tree, destination) = setup_fixture_with_options(opts)?;

assert_equality(&source_tree, &destination, opts.symlinks)?;
Ok(())
}

#[test]
fn symlinks_become_files_if_disabled() -> crate::Result {
let opts = index::checkout::Options {
symlinks: false,
..Default::default()
};
let (source_tree, destination) = setup_fixture_with_options(opts)?;

assert_equality(&source_tree, &destination, opts.symlinks)?;

Ok(())
}

fn assert_equality(source_tree: &Path, destination: &TempDir, allow_symlinks: bool) -> crate::Result {
let source_files = dir_structure(source_tree);
let worktree_files = dir_structure(&destination);

assert_eq!(
stripped_prefix(source_tree, &source_files),
stripped_prefix(&destination, &worktree_files),
);

for (source_file, worktree_file) in source_files.iter().zip(worktree_files.iter()) {
if !allow_symlinks && source_file.is_symlink() {
assert!(!worktree_file.is_symlink());
assert_eq!(fs::read(worktree_file)?.to_path()?, fs::read_link(source_file)?);
} else {
assert_eq!(fs::read(source_file)?, fs::read(worktree_file)?);
#[cfg(unix)]
assert_eq!(
fs::symlink_metadata(source_file)?.mode() & 0o700,
fs::symlink_metadata(worktree_file)?.mode() & 0o700,
"permissions of source and checked out file are comparable"
);
}
}
Ok(())
}

pub fn dir_structure<P: AsRef<std::path::Path>>(path: P) -> Vec<std::path::PathBuf> {
let path = path.as_ref();
let mut files: Vec<_> = walkdir::WalkDir::new(path)
.follow_links(false)
.into_iter()
.filter_entry(|e| e.path() == path || !e.file_name().to_string_lossy().starts_with('.'))
.flatten()
.filter_map(|e| (!e.path().is_dir()).then(|| e.path().to_path_buf()))
.collect();
files.sort();
files
}

fn setup_fixture_with_options(opts: git_worktree::index::checkout::Options) -> crate::Result<(PathBuf, TempDir)> {
let source_tree = fixture_path("make_repo");
let git_dir = source_tree.join(".git");
let mut index = git_index::File::at(git_dir.join("index"), Default::default())?;
let odb = git_odb::at(git_dir.join("objects"))?;
let destination = tempfile::tempdir()?;

index::checkout(
&mut index,
&destination,
move |oid, buf| odb.find_blob(oid, buf).ok(),
opts,
)?;
Ok((source_tree, destination))
}

fn stripped_prefix(prefix: impl AsRef<Path>, source_files: &Vec<PathBuf>) -> Vec<&Path> {
source_files.iter().flat_map(|p| p.strip_prefix(&prefix)).collect()
}
}
26 changes: 0 additions & 26 deletions git-worktree/tests/mod.rs

This file was deleted.

11 changes: 11 additions & 0 deletions git-worktree/tests/worktree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::path::{Path, PathBuf};

mod index;

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

pub fn fixture_path(name: &str) -> PathBuf {
let dir =
git_testtools::scripted_fixture_repo_read_only(Path::new(name).with_extension("sh")).expect("script works");
dir
}

0 comments on commit 831c429

Please sign in to comment.