Skip to content

Commit 831c429

Browse files
committed
restructure tests (#301)
1 parent 636fa8a commit 831c429

File tree

7 files changed

+117
-144
lines changed

7 files changed

+117
-144
lines changed

git-worktree/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ git-object = { version = "^0.17.0", path = "../git-object" }
2020
quick-error = "2.0.1"
2121

2222
[dev-dependencies]
23+
git-testtools = { path = "../tests/tools" }
2324
git-odb = { path = "../git-odb" }
25+
2426
walkdir = "2.3.2"
25-
git-testtools = { path = "../tests/tools" }
2627
tempfile = "3.2.0"

git-worktree/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod index {
5454
let root = path.as_ref();
5555
let mut buf = Vec::new();
5656
for (entry, entry_path) in index.entries_mut_with_paths() {
57+
// TODO: write test for that
5758
if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) {
5859
continue;
5960
}

git-worktree/tests/copy_index/mod.rs

-107
This file was deleted.

git-worktree/tests/fixtures/make_repo.sh

+7-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ set -eu -o pipefail
44
git init -q
55
git config commit.gpgsign false
66

7-
touch a
8-
echo "Test Vals" > a
9-
touch b
10-
touch c
11-
touch executable.sh
12-
chmod +x executable.sh
7+
touch empty
8+
echo "content" > executable
9+
chmod +x executable
1310

14-
mkdir d
15-
touch d/a
16-
echo "Subdir" > d/a
17-
ln -sf d/a sa
11+
mkdir dir
12+
echo "other content" > dir/content
13+
mkdir dir/sub-dir
14+
(cd dir/sub-dir && ln -sf ../content symlink)
1815

1916
git add -A
2017
git commit -m "Commit"

git-worktree/tests/index/mod.rs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
mod checkout {
2+
#[cfg(unix)]
3+
use std::os::unix::prelude::MetadataExt;
4+
use std::{
5+
fs,
6+
path::{Path, PathBuf},
7+
};
8+
use tempfile::TempDir;
9+
10+
use git_object::bstr::ByteSlice;
11+
use git_odb::FindExt;
12+
use git_worktree::index;
13+
14+
use crate::fixture_path;
15+
16+
#[test]
17+
fn allow_symlinks() -> crate::Result {
18+
let opts = Default::default();
19+
let (source_tree, destination) = setup_fixture_with_options(opts)?;
20+
21+
assert_equality(&source_tree, &destination, opts.symlinks)?;
22+
Ok(())
23+
}
24+
25+
#[test]
26+
fn symlinks_become_files_if_disabled() -> crate::Result {
27+
let opts = index::checkout::Options {
28+
symlinks: false,
29+
..Default::default()
30+
};
31+
let (source_tree, destination) = setup_fixture_with_options(opts)?;
32+
33+
assert_equality(&source_tree, &destination, opts.symlinks)?;
34+
35+
Ok(())
36+
}
37+
38+
fn assert_equality(source_tree: &Path, destination: &TempDir, allow_symlinks: bool) -> crate::Result {
39+
let source_files = dir_structure(source_tree);
40+
let worktree_files = dir_structure(&destination);
41+
42+
assert_eq!(
43+
stripped_prefix(source_tree, &source_files),
44+
stripped_prefix(&destination, &worktree_files),
45+
);
46+
47+
for (source_file, worktree_file) in source_files.iter().zip(worktree_files.iter()) {
48+
if !allow_symlinks && source_file.is_symlink() {
49+
assert!(!worktree_file.is_symlink());
50+
assert_eq!(fs::read(worktree_file)?.to_path()?, fs::read_link(source_file)?);
51+
} else {
52+
assert_eq!(fs::read(source_file)?, fs::read(worktree_file)?);
53+
#[cfg(unix)]
54+
assert_eq!(
55+
fs::symlink_metadata(source_file)?.mode() & 0o700,
56+
fs::symlink_metadata(worktree_file)?.mode() & 0o700,
57+
"permissions of source and checked out file are comparable"
58+
);
59+
}
60+
}
61+
Ok(())
62+
}
63+
64+
pub fn dir_structure<P: AsRef<std::path::Path>>(path: P) -> Vec<std::path::PathBuf> {
65+
let path = path.as_ref();
66+
let mut files: Vec<_> = walkdir::WalkDir::new(path)
67+
.follow_links(false)
68+
.into_iter()
69+
.filter_entry(|e| e.path() == path || !e.file_name().to_string_lossy().starts_with('.'))
70+
.flatten()
71+
.filter_map(|e| (!e.path().is_dir()).then(|| e.path().to_path_buf()))
72+
.collect();
73+
files.sort();
74+
files
75+
}
76+
77+
fn setup_fixture_with_options(opts: git_worktree::index::checkout::Options) -> crate::Result<(PathBuf, TempDir)> {
78+
let source_tree = fixture_path("make_repo");
79+
let git_dir = source_tree.join(".git");
80+
let mut index = git_index::File::at(git_dir.join("index"), Default::default())?;
81+
let odb = git_odb::at(git_dir.join("objects"))?;
82+
let destination = tempfile::tempdir()?;
83+
84+
index::checkout(
85+
&mut index,
86+
&destination,
87+
move |oid, buf| odb.find_blob(oid, buf).ok(),
88+
opts,
89+
)?;
90+
Ok((source_tree, destination))
91+
}
92+
93+
fn stripped_prefix(prefix: impl AsRef<Path>, source_files: &Vec<PathBuf>) -> Vec<&Path> {
94+
source_files.iter().flat_map(|p| p.strip_prefix(&prefix)).collect()
95+
}
96+
}

git-worktree/tests/mod.rs

-26
This file was deleted.

git-worktree/tests/worktree.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::path::{Path, PathBuf};
2+
3+
mod index;
4+
5+
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
6+
7+
pub fn fixture_path(name: &str) -> PathBuf {
8+
let dir =
9+
git_testtools::scripted_fixture_repo_read_only(Path::new(name).with_extension("sh")).expect("script works");
10+
dir
11+
}

0 commit comments

Comments
 (0)