fix: polluting gitconfig in integrated test

This commit is contained in:
qwjyh 2025-02-04 03:16:38 +09:00
parent 90cebed15f
commit 45ec219433

View file

@ -1,12 +1,12 @@
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},
ops::Not,
}; };
use anyhow::{Context, Ok, Result}; use 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::trace;
use predicates::{boolean::PredicateBooleanExt, prelude::predicate}; use predicates::{boolean::PredicateBooleanExt, prelude::predicate};
@ -16,12 +16,12 @@ mod integrated_test {
/// # 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 home directory.
fn setup_gitconfig() -> Result<()> { fn setup_gitconfig(path: &std::path::Path) -> Result<()> {
let f = match File::create_new( let git_dir = path.join(".git");
home_dir() if !git_dir.exists() {
.context("Failed to find home directory")? fs::DirBuilder::new().create(git_dir.clone())?
.join(".gitconfig"), }
) { let f = match File::create_new(git_dir.join("config")) {
io::Result::Ok(f) => f, io::Result::Ok(f) => f,
io::Result::Err(_err) => return Ok(()), io::Result::Err(_err) => return Ok(()),
}; };
@ -41,7 +41,7 @@ mod integrated_test {
#[test] #[test]
fn single_device() -> Result<()> { fn single_device() -> Result<()> {
let config_dir = assert_fs::TempDir::new()?; let config_dir = assert_fs::TempDir::new()?;
setup_gitconfig()?; setup_gitconfig(&config_dir)?;
// init // init
let mut cmd = Command::cargo_bin("xdbm")?; let mut cmd = Command::cargo_bin("xdbm")?;
cmd.arg("-c") cmd.arg("-c")
@ -135,7 +135,7 @@ mod integrated_test {
fn two_devices_with_same_name() -> Result<()> { fn two_devices_with_same_name() -> Result<()> {
// 1st device // 1st device
let config_dir_1 = assert_fs::TempDir::new()?; let config_dir_1 = assert_fs::TempDir::new()?;
setup_gitconfig()?; setup_gitconfig(&config_dir_1)?;
let mut cmd1 = Command::cargo_bin("xdbm")?; let mut cmd1 = Command::cargo_bin("xdbm")?;
cmd1.arg("-c") cmd1.arg("-c")
.arg(config_dir_1.path()) .arg(config_dir_1.path())
@ -168,7 +168,7 @@ mod integrated_test {
// 2nd device // 2nd device
let config_dir_2 = assert_fs::TempDir::new()?; let config_dir_2 = assert_fs::TempDir::new()?;
setup_gitconfig()?; setup_gitconfig(&config_dir_2)?;
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())
@ -184,7 +184,7 @@ mod integrated_test {
fn directory_without_parent() -> Result<()> { fn directory_without_parent() -> Result<()> {
// 1st device // 1st device
let config_dir_1 = assert_fs::TempDir::new()?; let config_dir_1 = assert_fs::TempDir::new()?;
setup_gitconfig()?; setup_gitconfig(&config_dir_1)?;
let mut cmd1 = Command::cargo_bin("xdbm")?; let mut cmd1 = Command::cargo_bin("xdbm")?;
cmd1.arg("-c") cmd1.arg("-c")
.arg(config_dir_1.path()) .arg(config_dir_1.path())
@ -219,7 +219,7 @@ mod integrated_test {
// //
// devices: first // devices: first
let config_dir_1 = assert_fs::TempDir::new()?; let config_dir_1 = assert_fs::TempDir::new()?;
setup_gitconfig()?; setup_gitconfig(&config_dir_1)?;
let mut cmd1 = Command::cargo_bin("xdbm")?; let mut cmd1 = Command::cargo_bin("xdbm")?;
cmd1.arg("-c") cmd1.arg("-c")
.arg(config_dir_1.path()) .arg(config_dir_1.path())