mirror of
https://github.com/qwjyh/xdbm
synced 2025-04-21 04:05:49 +09:00
Compare commits
No commits in common. "df7b1dd819276df6203b41cd57f6ce37528fc2ff" and "c1ba761ddb3b3088600d0619c80c8ce5ba3eca89" have entirely different histories.
df7b1dd819
...
c1ba761ddb
3 changed files with 18 additions and 57 deletions
1
.github/workflows/rust.yml
vendored
1
.github/workflows/rust.yml
vendored
|
@ -8,7 +8,6 @@ on:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
XDBM_ENABLE_OVERWRITE_GITCONFIG: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-lint:
|
build-and-lint:
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
# 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
|
||||||
|
|
66
tests/cli.rs
66
tests/cli.rs
|
@ -4,58 +4,23 @@ mod integrated_test {
|
||||||
io::{self, BufWriter, Write},
|
io::{self, BufWriter, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use assert_cmd::{assert::OutputAssertExt, Command};
|
use assert_cmd::{assert::OutputAssertExt, Command};
|
||||||
use git2::Repository;
|
use git2::Repository;
|
||||||
use log::{debug, trace};
|
use log::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 git global config and environment
|
/// This function will return an error if it failed to get home directory.
|
||||||
/// variable [XDBM_ENABLE_OVERWRITE_GITCONFIG](`IS_GIT_CONFIG_WRITABLE`) is not set.
|
fn setup_gitconfig(path: &std::path::Path) -> Result<()> {
|
||||||
fn setup_gitconfig() -> Result<()> {
|
let git_dir = path.join(".git");
|
||||||
let config = git2::Config::open_default().expect("failed to get default");
|
if !git_dir.exists() {
|
||||||
if config.get_string("user.name").is_ok() && config.get_string("user.email").is_ok() {
|
fs::DirBuilder::new().create(git_dir.clone())?
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
|
|
||||||
match std::env::var_os(IS_GIT_CONFIG_WRITABLE) {
|
|
||||||
Some(_) => {
|
|
||||||
debug!(
|
|
||||||
"global git config not found & env var `{}` found",
|
|
||||||
IS_GIT_CONFIG_WRITABLE
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
None => {
|
let f = match File::create_new(git_dir.join("config")) {
|
||||||
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(()),
|
||||||
};
|
};
|
||||||
|
@ -69,13 +34,16 @@ mod integrated_test {
|
||||||
.as_bytes(),
|
.as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
eprintln!("{:?}", fs::read_dir(path)?.collect::<Vec<_>>());
|
||||||
|
eprintln!("{:?}", fs::read_dir(git_dir)?.collect::<Vec<_>>());
|
||||||
|
|
||||||
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()?;
|
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")
|
||||||
|
@ -171,7 +139,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())
|
||||||
|
@ -204,7 +172,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())
|
||||||
|
@ -220,7 +188,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())
|
||||||
|
@ -255,7 +223,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())
|
||||||
|
@ -289,7 +257,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()?;
|
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())
|
||||||
|
|
Loading…
Add table
Reference in a new issue