fix: polluting gitconfig in integrated test (#20)

* fix: polluting gitconfig in integrated test

* debug(CI): add eprintln

* debug(CI): git config

* debug(CI): git init test

* fix(CI): command construction

* debug(CI): change workdir & add same config setup

* debug(CI): use libgit2

* debug(CI): return err at the end of test using libgit2

* debug(CI): change current_dir in git config

* fix(git): git config now gets local config

CI was failing since xdbm init didn't get local git config.

* debug: add trace log

* debug: increase log level in CI

* fix: getting gitconfig now respects local config

* debug(CI): clean up debugging codes

* fix(CI): add setup_gitconfig to two_devices test

* debug(CI): inspect git config

* fix(CI): add env var to allow setting git global config

* refactor(CI): remove unnecessary arg for setup_gitconfig

* update changelog
This commit is contained in:
qwjyh 2025-02-09 08:39:57 +09:00 committed by GitHub
parent 90cebed15f
commit 47b3a5e69d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 56 additions and 12 deletions

View file

@ -8,6 +8,7 @@ on:
env: env:
CARGO_TERM_COLOR: always CARGO_TERM_COLOR: always
XDBM_ENABLE_OVERWRITE_GITCONFIG: true
jobs: jobs:
build-and-lint: build-and-lint:

View file

@ -1,5 +1,11 @@
# Changelog # Changelog
## [Unreleased]
### Fixed
- Git local config is now looked up. (#20)
- Git global config will not be polluted in test by default. (#20)
## [0.3.0] - 2024-12-02 ## [0.3.0] - 2024-12-02
### Added ### Added

View file

@ -156,7 +156,7 @@ fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result<Oid,
index.write()?; index.write()?;
let oid = index.write_tree()?; let oid = index.write_tree()?;
let tree = repo.find_tree(oid)?; let tree = repo.find_tree(oid)?;
let config = git2::Config::open_default()?; let config = repo.config()?;
let signature = git2::Signature::now( let signature = git2::Signature::now(
config.get_entry("user.name")?.value().unwrap(), config.get_entry("user.name")?.value().unwrap(),
config.get_entry("user.email")?.value().unwrap(), config.get_entry("user.email")?.value().unwrap(),

View file

@ -1,27 +1,61 @@
mod integrated_test { mod integrated_test {
use std::{ use std::{
fs::{DirBuilder, File}, fs::{self, DirBuilder, File},
io::{self, BufWriter, Write}, io::{self, BufWriter, Write},
}; };
use anyhow::{Context, Ok, Result}; use anyhow::{anyhow, Context, Ok, Result};
use assert_cmd::{assert::OutputAssertExt, Command}; use assert_cmd::{assert::OutputAssertExt, Command};
use dirs::home_dir;
use git2::Repository; use git2::Repository;
use log::trace; use log::{debug, trace};
use predicates::{boolean::PredicateBooleanExt, prelude::predicate}; use predicates::{boolean::PredicateBooleanExt, prelude::predicate};
const IS_GIT_CONFIG_WRITABLE: &str = "XDBM_ENABLE_OVERWRITE_GITCONFIG";
/// Setup global gitconfig if it doesn't exist. /// Setup global gitconfig if it doesn't exist.
/// ///
/// # Errors /// # Errors
/// ///
/// This function will return an error if it failed to get home directory. /// This function will return an error if it failed to get git global config and environment
/// variable [XDBM_ENABLE_OVERWRITE_GITCONFIG](`IS_GIT_CONFIG_WRITABLE`) is not set.
fn setup_gitconfig() -> Result<()> { fn setup_gitconfig() -> Result<()> {
let f = match File::create_new( let config = git2::Config::open_default().expect("failed to get default");
home_dir() if config.get_string("user.name").is_ok() && config.get_string("user.email").is_ok() {
.context("Failed to find home directory")? return Ok(());
.join(".gitconfig"), };
) {
match std::env::var_os(IS_GIT_CONFIG_WRITABLE) {
Some(_) => {
debug!(
"global git config not found & env var `{}` found",
IS_GIT_CONFIG_WRITABLE
);
}
None => {
eprintln!("Failed to get git global config");
eprintln!(
"Set env var `{}` to set automatically (mainly for CI)",
IS_GIT_CONFIG_WRITABLE
);
return Err(anyhow!("failed to get git global config"));
}
};
let config_file = git2::Config::find_global().map_or_else(
|e| {
trace!("global git config file not found: {e:?}");
Ok(dirs::home_dir()
.context("Failed to get home dir")?
.join(".gitconfig"))
},
Ok,
)?;
let f = match File::options()
.create(true)
.truncate(true)
.write(true)
.open(config_file)
{
io::Result::Ok(f) => f, io::Result::Ok(f) => f,
io::Result::Err(_err) => return Ok(()), io::Result::Err(_err) => return Ok(()),
}; };
@ -47,8 +81,10 @@ mod integrated_test {
cmd.arg("-c") cmd.arg("-c")
.arg(config_dir.path()) .arg(config_dir.path())
.arg("init") .arg("init")
.arg("testdev"); .arg("testdev")
.arg("-vvvv");
cmd.assert().success().stdout(predicate::str::contains("")); cmd.assert().success().stdout(predicate::str::contains(""));
eprintln!("{:?}", fs::read_dir(config_dir.path())?.collect::<Vec<_>>());
assert_eq!( assert_eq!(
std::fs::read_to_string(config_dir.path().join("devname"))?, std::fs::read_to_string(config_dir.path().join("devname"))?,
"testdev\n" "testdev\n"
@ -253,6 +289,7 @@ mod integrated_test {
// //
// devices: first, second // devices: first, second
let config_dir_2 = assert_fs::TempDir::new()?; let config_dir_2 = assert_fs::TempDir::new()?;
setup_gitconfig()?;
let mut cmd2 = Command::cargo_bin("xdbm")?; let mut cmd2 = Command::cargo_bin("xdbm")?;
cmd2.arg("-c") cmd2.arg("-c")
.arg(config_dir_2.path()) .arg(config_dir_2.path())