Commit

Author:

Hash:

Timestamp:

+34 -10 +/-2 browse

Kevin Schoon [me@kevinschoon.com]

366fe6a1e08cd1fdf128df1677e7c975fe300881

Sun, 04 Feb 2024 12:53:48 +0000 (1.4 years ago)

use a standard git global config for test helper
use a standard git global config for test helper

This adds a basic global git config to avoid conflicting with the testing
user's gitconfig e.g. attempting to GPG sign test commits.
1diff --git a/crates/git/src/testing.rs b/crates/git/src/testing.rs
2index 587fa35..32da0d0 100644
3--- a/crates/git/src/testing.rs
4+++ b/crates/git/src/testing.rs
5 @@ -5,6 +5,15 @@ use std::process::Command;
6
7 use rand::{distributions::Alphanumeric, Rng};
8
9+ const DEFAULT_GIT_CONFIG: &str = r#"
10+ [user]
11+ name = "Test User"
12+ email = "test@example.org"
13+
14+ [init]
15+ defaultBranch = main
16+ "#;
17+
18 /// return escaped shell variables with timestamps for use in testing
19 pub fn timestamp_envs(timestamp: &str) -> String {
20 format!(
21 @@ -20,6 +29,7 @@ pub struct Builder {
22 bare: bool,
23 commands: Vec<String>,
24 repo_path: Option<PathBuf>,
25+ base_path: Option<PathBuf>,
26 }
27
28 impl Builder {
29 @@ -33,6 +43,11 @@ impl Builder {
30 self
31 }
32
33+ pub fn with_basepath(mut self, base_path: PathBuf) -> Self {
34+ self.base_path = Some(base_path.clone());
35+ self
36+ }
37+
38 pub fn cleanup(self) -> Result<(), Error> {
39 match self.repo_path {
40 Some(path) => fs::remove_dir_all(path),
41 @@ -46,27 +61,36 @@ impl Builder {
42 .take(12)
43 .map(char::from)
44 .collect();
45- let repo_name = format!("test-repo-{}", rand_id);
46- let repo_path = PathBuf::from(".")
47- .canonicalize()?
48- .join(format!("test/{}", repo_name));
49- fs::create_dir_all(&repo_path)?;
50+ let unique_name = format!("test-repo-{}", rand_id);
51+ let base_path = if let Some(base_path) = self.base_path.as_ref() {
52+ base_path.join(&unique_name)
53+ } else {
54+ let base_path = PathBuf::from(".").canonicalize()?;
55+ base_path.join("test").join(&unique_name)
56+ };
57+ let source_path = base_path.join("src");
58+ fs::create_dir_all(&source_path)?;
59+ let git_config_path = base_path.join("gitconfig");
60+ fs::write(&git_config_path, DEFAULT_GIT_CONFIG)?;
61+ let git_config_path = git_config_path.to_str().unwrap();
62 let args = if self.bare {
63 ["init", "--bare"].to_vec()
64 } else {
65 ["init"].to_vec()
66 };
67- self.repo_path = Some(repo_path.clone());
68+ self.repo_path = Some(source_path.clone());
69 Command::new("git")
70 .args(args)
71- .current_dir(&repo_path)
72+ .env("GIT_CONFIG_GLOBAL", git_config_path)
73+ .current_dir(&source_path)
74 .output()?;
75 for command in self.commands.iter() {
76 Command::new("/bin/sh")
77 .args(vec!["-c", command.as_str()])
78- .current_dir(repo_path.clone())
79+ .env("GIT_CONFIG_GLOBAL", git_config_path)
80+ .current_dir(&source_path)
81 .output()?;
82 }
83- Ok((repo_name, repo_path))
84+ Ok((unique_name, source_path))
85 }
86 }
87 diff --git a/scripts/test.sh b/scripts/test.sh
88index 972db85..deef95d 100755
89--- a/scripts/test.sh
90+++ b/scripts/test.sh
91 @@ -3,7 +3,7 @@ set -e
92
93 COMPONENT="$1"
94
95- IGNORE_FLAGS="-i ayllu-build/logs -i test"
96+ IGNORE_FLAGS="-i ayllu-build/logs -i test -i crates/git/test"
97 EXEC_FLAGS="test"
98
99 if [ -n "$COMPONENT" ] ; then