Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a015dc5

Browse files
author
Naseschwarz
committedMar 19, 2025
Pub git2_hooks::create_hook only for feature "test-utils"
git2_hooks::create_hook is a function used in tests, which panics on common errors. It can't really be moved into git2-testing, as it depends on git2_hooks::hookspath::HookPaths. In order to avoid accidental use, move the function into git2_hooks::test_utils[_priv] and only publish it as git2_hooks::test_utils if feature = "test-utils" is enabled.
1 parent bcee68a commit a015dc5

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed
 

Diff for: ‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎asyncgit/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dirs = "5.0"
1818
easy-cast = "0.5"
1919
fuzzy-matcher = "0.3"
2020
git2 = "0.20"
21-
git2-hooks = { path = "../git2-hooks", version = ">=0.4" }
21+
git2-hooks = { path = "../git2-hooks", version = ">=0.4", features = ["test-utils"] }
2222
gix = { version = "0.70.0", default-features = false, features = [
2323
"max-performance",
2424
"revision",
@@ -39,6 +39,7 @@ url = "2.5"
3939

4040
[dev-dependencies]
4141
env_logger = "0.11"
42+
git2-testing = { path = "../git2-testing" }
4243
invalidstring = { path = "../invalidstring", version = "0.1" }
4344
pretty_assertions = "1.4"
4445
serial_test = "3.2"

Diff for: ‎git2-hooks/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ license = "MIT"
1212
categories = ["development-tools"]
1313
keywords = ["git"]
1414

15+
[features]
16+
default = []
17+
18+
# test-utils publishes fns that are useful for testing. These may panic on common errors.
19+
test-utils = ["git2-testing"]
20+
1521
[dependencies]
1622
git2 = ">=0.17"
23+
git2-testing = { path = "../git2-testing", optional = true }
1724
log = "0.4"
1825
shellexpand = "3.1"
1926
thiserror = "2.0"

Diff for: ‎git2-hooks/src/lib.rs

+35-19
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod hookspath;
3030
use std::{
3131
fs::File,
3232
io::{Read, Write},
33-
path::{Path, PathBuf},
33+
path::PathBuf,
3434
};
3535

3636
pub use error::HooksError;
@@ -80,23 +80,6 @@ impl HookResult {
8080
}
8181
}
8282

83-
/// helper method to create git hooks programmatically (heavy used in unittests)
84-
///
85-
/// # Panics
86-
/// Panics if hook could not be created
87-
pub fn create_hook(
88-
r: &Repository,
89-
hook: &str,
90-
hook_script: &[u8],
91-
) -> PathBuf {
92-
let hook = HookPaths::new(r, None, hook).unwrap();
93-
94-
let path = hook.hook.clone();
95-
96-
create_hook_in_path(&hook.hook, hook_script);
97-
98-
path
99-
}
10083
/// Git hook: `commit_msg`
10184
///
10285
/// This hook is documented here <https://git-scm.com/docs/githooks#_commit_msg>.
@@ -216,12 +199,45 @@ pub fn hooks_prepare_commit_msg(
216199
Ok(res)
217200
}
218201

202+
#[cfg(any(feature = "test-utils", test))]
203+
mod test_utils_priv {
204+
use crate::hookspath::HookPaths;
205+
use git2::Repository;
206+
use std::path::PathBuf;
207+
208+
/// helper method to create git hooks programmatically (heavy used in unittests)
209+
///
210+
/// # Panics
211+
/// Panics if hook could not be created
212+
pub fn create_hook(
213+
r: &Repository,
214+
hook: &str,
215+
hook_script: &[u8],
216+
) -> PathBuf {
217+
let hook = HookPaths::new(r, None, hook).unwrap();
218+
219+
let path = hook.hook.clone();
220+
221+
git2_testing::create_hook_in_path(&hook.hook, hook_script);
222+
223+
path
224+
}
225+
}
226+
227+
#[cfg(feature = "test-utils")]
228+
pub mod test_utils {
229+
pub use crate::test_utils_priv::create_hook;
230+
}
231+
219232
#[cfg(test)]
220233
mod tests {
221234
use super::*;
222-
use git2_testing::{repo_init, repo_init_bare};
235+
use git2_testing::{
236+
create_hook_in_path, repo_init, repo_init_bare,
237+
};
223238
use pretty_assertions::assert_eq;
224239
use tempfile::TempDir;
240+
use super::test_utils_priv::*;
225241

226242
#[test]
227243
fn test_smoke() {

0 commit comments

Comments
 (0)
Please sign in to comment.