use codex_usage::utils::git::get_git_remote_url;
use std::fs;
use std::io::Write;
use std::process::Command;
use tempfile::TempDir;
#[test]
fn test_get_git_remote_url_with_git_repo() {
let current_dir = std::env::current_dir().unwrap();
let result = get_git_remote_url(¤t_dir);
assert!(result.is_empty() || !result.is_empty());
}
#[test]
fn test_get_git_remote_url_non_git_directory() {
let temp_dir = TempDir::new().unwrap();
let result = get_git_remote_url(temp_dir.path());
assert!(
result.is_empty(),
"Non-git directory should return empty string"
);
}
#[test]
fn test_get_git_remote_url_with_mock_git_repo() {
let temp_dir = TempDir::new().unwrap();
let git_dir = temp_dir.path().join(".git");
fs::create_dir(&git_dir).unwrap();
let config_path = git_dir.join("config");
let mut config_file = fs::File::create(&config_path).unwrap();
writeln!(
config_file,
"[remote \"origin\"]\n\turl = https://github.com/test/repo.git"
)
.unwrap();
let result = get_git_remote_url(temp_dir.path());
assert!(result.is_empty() || result.contains("github.com/test/repo"));
}
#[test]
fn test_get_git_remote_url_with_initialized_repo() {
if Command::new("git").arg("--version").output().is_err() {
return; }
let temp_dir = TempDir::new().unwrap();
let init_result = Command::new("git")
.arg("init")
.current_dir(temp_dir.path())
.output();
if init_result.is_err() {
return; }
let _ = Command::new("git")
.args([
"remote",
"add",
"origin",
"https://github.com/test/repo.git",
])
.current_dir(temp_dir.path())
.output();
let result = get_git_remote_url(temp_dir.path());
if !result.is_empty() {
assert!(
result.contains("github.com/test/repo"),
"Should contain the remote URL"
);
}
}