Skip to content

Commit a4b880c

Browse files
committed
Refactored code and tests
1 parent 25a9dc1 commit a4b880c

File tree

3 files changed

+32
-47
lines changed

3 files changed

+32
-47
lines changed

git-worktree/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ quick_error! {
3333
display("IO error while writing blob or reading file metadata or changing filetype: {}", err)
3434
}
3535
NotFound(oid: git_hash::ObjectId, path: PathBuf) {
36-
display("unable to read sha1 file of {} ({})", path.display(), oid.to_hex())
36+
display("unable find object of {} ({})", path.display(), oid.to_hex())
3737
}
3838
}
3939
}
@@ -68,10 +68,10 @@ where
6868
let met = file.metadata()?;
6969
let ctime = met
7070
.created()
71-
.map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
71+
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH));
7272
let mtime = met
7373
.modified()
74-
.map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
74+
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH));
7575
entry_time.push((ctime?, mtime?, i));
7676
}
7777
git_index::entry::Mode::SYMLINK => {
@@ -94,10 +94,10 @@ where
9494
let met = std::fs::symlink_metadata(&dest)?;
9595
let ctime = met
9696
.created()
97-
.map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
97+
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH));
9898
let mtime = met
9999
.modified()
100-
.map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
100+
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH));
101101
entry_time.push((ctime?, mtime?, i));
102102
}
103103
git_index::entry::Mode::DIR => todo!(),

git-worktree/tests/copy_index/mod.rs

+26-42
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,24 @@ fn test_copy_index() -> crate::Result<()> {
2626
let repo_files = dir_structure(&path);
2727
let copy_files = dir_structure(output);
2828

29-
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
30-
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
3129
assert_eq!(
32-
srepo_files,
33-
scopy_files,
34-
"Testing if {} and {} have the same structure",
35-
path.display(),
36-
output.display()
30+
repo_files
31+
.iter()
32+
.flat_map(|p| p.strip_prefix(&path))
33+
.collect::<Vec<_>>(),
34+
copy_files
35+
.iter()
36+
.flat_map(|p| p.strip_prefix(output))
37+
.collect::<Vec<_>>()
3738
);
3839

3940
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
40-
assert_eq!(
41-
fs::read(file1)?,
42-
fs::read(file2)?,
43-
"Testing if the contents of {} and {} are the same",
44-
file1.display(),
45-
file2.display(),
46-
);
41+
assert_eq!(fs::read(file1)?, fs::read(file2)?);
4742
#[cfg(unix)]
4843
assert_eq!(
49-
fs::symlink_metadata(file1)?.mode() & 0b111 << 6,
50-
fs::symlink_metadata(file2)?.mode() & 0b111 << 6,
51-
"Testing if the permissions of {} and {} are the same",
44+
fs::symlink_metadata(file1)?.mode() & 0o700,
45+
fs::symlink_metadata(file2)?.mode() & 0o700,
46+
"Testing if the permissions (normal/executable) of {} and {} are the same",
5247
file1.display(),
5348
file2.display(),
5449
);
@@ -76,39 +71,28 @@ fn test_copy_index_without_symlinks() -> crate::Result<()> {
7671
let repo_files = dir_structure(&path);
7772
let copy_files = dir_structure(output);
7873

79-
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
80-
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
8174
assert_eq!(
82-
srepo_files,
83-
scopy_files,
84-
"Testing if {} and {} have the same structure",
85-
path.display(),
86-
output.display()
75+
repo_files
76+
.iter()
77+
.flat_map(|p| p.strip_prefix(&path))
78+
.collect::<Vec<_>>(),
79+
copy_files
80+
.iter()
81+
.flat_map(|p| p.strip_prefix(output))
82+
.collect::<Vec<_>>()
8783
);
8884

8985
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
9086
if file1.is_symlink() {
91-
assert!(!file2.is_symlink(), "Testing if the new file is not a symlink");
92-
assert_eq!(
93-
fs::read(file2)?.to_path()?,
94-
fs::read_link(file1)?,
95-
"Testing if the {} contains the path the symlink {} is pointing to",
96-
file2.display(),
97-
file1.display(),
98-
);
87+
assert!(!file2.is_symlink());
88+
assert_eq!(fs::read(file2)?.to_path()?, fs::read_link(file1)?);
9989
} else {
100-
assert_eq!(
101-
fs::read(file1)?,
102-
fs::read(file2)?,
103-
"Testing if the contents of {} and {} are the same",
104-
file1.display(),
105-
file2.display(),
106-
);
90+
assert_eq!(fs::read(file1)?, fs::read(file2)?);
10791
#[cfg(unix)]
10892
assert_eq!(
109-
fs::symlink_metadata(file1)?.mode() & 0b111 << 6,
110-
fs::symlink_metadata(file2)?.mode() & 0b111 << 6,
111-
"Testing if the permissions of {} and {} are the same",
93+
fs::symlink_metadata(file1)?.mode() & 0o700,
94+
fs::symlink_metadata(file2)?.mode() & 0o700,
95+
"Testing if the permissions (normal/executable) of {} and {} are the same",
11296
file1.display(),
11397
file2.display(),
11498
);

git-worktree/tests/fixtures/make_repo.sh

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -eu -o pipefail
33

44
git init -q
5+
git config commit.gpgsign false
56

67
touch a
78
echo "Test Vals" > a

0 commit comments

Comments
 (0)