Skip to content

Commit 418d016

Browse files
committed
fix tests with invalid utf8
1 parent b54711f commit 418d016

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

asyncgit/src/sync/hooks.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ mod tests {
130130
let subfolder = root.join("foo/");
131131
std::fs::create_dir_all(&subfolder).unwrap();
132132

133-
let res =
134-
hooks_post_commit(&subfolder.to_str().unwrap().into())
135-
.unwrap();
133+
let res = hooks_post_commit(&subfolder.into()).unwrap();
136134

137135
assert_eq!(
138136
res,
@@ -148,10 +146,11 @@ mod tests {
148146
fn test_pre_commit_workdir() {
149147
let (_td, repo) = repo_init().unwrap();
150148
let root = repo.path().parent().unwrap();
151-
let repo_path: &RepoPath =
152-
&root.as_os_str().to_str().unwrap().into();
149+
let repo_path: &RepoPath = &root.to_path_buf().into();
150+
let repository =
151+
crate::sync::repository::repo(repo_path).unwrap();
153152
let workdir =
154-
crate::sync::utils::repo_work_dir(repo_path).unwrap();
153+
crate::sync::utils::work_dir(&repository).unwrap();
155154

156155
let hook = b"#!/bin/sh
157156
echo \"$(pwd)\"
@@ -165,8 +164,9 @@ mod tests {
165164
let res = hooks_pre_commit(repo_path).unwrap();
166165
if let HookResult::NotOk(res) = res {
167166
assert_eq!(
168-
std::path::Path::new(res.trim_end()),
169-
std::path::Path::new(&workdir)
167+
res.trim_end().trim_end_matches('/'),
168+
// TODO: fix if output isn't utf8.
169+
workdir.to_string_lossy().trim_end_matches('/'),
170170
);
171171
} else {
172172
assert!(false);
@@ -194,11 +194,8 @@ mod tests {
194194
std::fs::create_dir_all(&subfolder).unwrap();
195195

196196
let mut msg = String::from("test");
197-
let res = hooks_commit_msg(
198-
&subfolder.to_str().unwrap().into(),
199-
&mut msg,
200-
)
201-
.unwrap();
197+
let res =
198+
hooks_commit_msg(&subfolder.into(), &mut msg).unwrap();
202199

203200
assert_eq!(
204201
res,

asyncgit/src/sync/repository.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ impl RepoPath {
4242
}
4343
}
4444

45+
impl From<PathBuf> for RepoPath {
46+
fn from(value: PathBuf) -> Self {
47+
Self::Path(value)
48+
}
49+
}
50+
4551
impl From<&str> for RepoPath {
4652
fn from(p: &str) -> Self {
4753
Self::Path(PathBuf::from(p))

git2-hooks/src/hookspath.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use git2::Repository;
33
use crate::{error::Result, HookResult, HooksError};
44

55
use std::{
6-
ffi::OsString,
6+
ffi::{OsStr, OsString},
77
path::{Path, PathBuf},
88
process::Command,
99
str::FromStr,
@@ -108,6 +108,16 @@ impl HookPaths {
108108
/// this function calls hook scripts based on conventions documented here
109109
/// see <https://git-scm.com/docs/githooks>
110110
pub fn run_hook(&self, args: &[&str]) -> Result<HookResult> {
111+
self.run_hook_os_str(args)
112+
}
113+
114+
/// this function calls hook scripts based on conventions documented here
115+
/// see <https://git-scm.com/docs/githooks>
116+
pub fn run_hook_os_str<I, S>(&self, args: I) -> Result<HookResult>
117+
where
118+
I: IntoIterator<Item = S> + Copy,
119+
S: AsRef<OsStr>,
120+
{
111121
let hook = self.hook.clone();
112122
log::trace!("run hook '{:?}' in '{:?}'", hook, self.pwd);
113123

git2-hooks/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ pub fn hooks_commit_msg(
132132
let temp_file = hook.git.join(HOOK_COMMIT_MSG_TEMP_FILE);
133133
File::create(&temp_file)?.write_all(msg.as_bytes())?;
134134

135-
let res = hook.run_hook(&[temp_file
136-
.as_os_str()
137-
.to_string_lossy()
138-
.as_ref()])?;
135+
let res = hook.run_hook_os_str([&temp_file])?;
139136

140137
// load possibly altered msg
141138
msg.clear();

0 commit comments

Comments
 (0)