fixup! [fix] test: setup gitconfig at repo init

This commit is contained in:
qwjyh 2024-04-26 01:35:42 +09:00
parent 607013c649
commit 4fcb7fc354

View file

@ -1,22 +1,30 @@
mod integrated_test { mod integrated_test {
use std::{ use std::{
fs::{DirBuilder, File}, fs::{DirBuilder, File},
io::{BufWriter, Write}, io::{self, BufWriter, Write},
path::Path,
thread,
time::Duration,
}; };
use anyhow::{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::prelude::predicate; use predicates::prelude::predicate;
fn setup_gitconfig(dir_path: &Path) -> Result<()> { /// Setup global gitconfig if it doesn't exist.
DirBuilder::new().create(dir_path.join(".git"))?; ///
{ /// # Errors
let f = File::create(dir_path.join(".git/config"))?; ///
/// This function will return an error if it failed to get home directory.
fn setup_gitconfig() -> Result<()> {
let f = match File::create_new(
home_dir()
.context("Failed to find home directory")?
.join(".gitconfig"),
) {
io::Result::Ok(f) => f,
io::Result::Err(_err) => return Ok(()),
};
let mut buf = BufWriter::new(f); let mut buf = BufWriter::new(f);
buf.write_all( buf.write_all(
r#" r#"
@ -26,14 +34,14 @@ mod integrated_test {
"# "#
.as_bytes(), .as_bytes(),
)?; )?;
}
Ok(()) Ok(())
} }
#[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(&config_dir)?; setup_gitconfig()?;
// init // init
let mut cmd = Command::cargo_bin("xdbm")?; let mut cmd = Command::cargo_bin("xdbm")?;
cmd.arg("-c") cmd.arg("-c")
@ -120,12 +128,6 @@ mod integrated_test {
.assert() .assert()
.success(); .success();
let out = std::process::Command::new("git")
.arg("config")
.arg("--get")
.arg("user.email")
.output()?;
panic!("{}", String::from_utf8_lossy(&out.stdout));
Ok(()) Ok(())
} }
@ -133,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(&config_dir_1)?; setup_gitconfig()?;
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())
@ -158,7 +160,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(&config_dir_2)?; 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())
@ -174,7 +176,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(&config_dir_1)?; setup_gitconfig()?;
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())
@ -207,7 +209,7 @@ mod integrated_test {
fn two_devices() -> Result<()> { fn two_devices() -> Result<()> {
// 1st device // 1st device
let config_dir_1 = assert_fs::TempDir::new()?; let config_dir_1 = assert_fs::TempDir::new()?;
setup_gitconfig(&config_dir_1)?; setup_gitconfig()?;
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())