added extra test cases and improved error handling in git.rs

This commit is contained in:
emma31-dev 2026-04-05 23:09:33 +01:00
parent 4c01ae0a38
commit ed1791c71a

View file

@ -16,7 +16,23 @@ pub fn meta(uri: &str) -> (String, String, String) {
let actual_uri = if uri.contains("://") || uri.contains('@') {
uri.to_string()
} else {
format!("https://github.com/{uri}")
if let Some((domain, route)) = uri.split_once('/') {
if domain.contains(".") {
format!("https://{domain}/{route}")
} else {
// Users can pass name starting wirh a slash
let first_char = uri.chars().next();
if first_char == Some('/') {
format!("https://github.com{uri}")
}
else {
format!("https://github.com/{uri}")
}
}
} else {
panic!("Invalid link")
}
};
let uri_to_split = actual_uri.replace(':', "/");
@ -54,4 +70,36 @@ mod tests {
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
#[test]
fn test_meta_github_repo_name() {
let (actual_uri, user, repo) = meta("user/repo");
assert_eq!(actual_uri, "https://github.com/user/repo".to_string());
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
#[test]
fn test_meta_github_repo_name_with_slash() {
let (actual_uri, user, repo) = meta("/user/repo");
assert_eq!(actual_uri, "https://github.com/user/repo".to_string());
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
#[test]
fn test_meta_github_clean_link() {
let (actual_uri, user, repo) = meta("github.com/user/repo");
assert_eq!(actual_uri, "https://github.com/user/repo".to_string());
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
#[test]
fn test_meta_random_git_provider_http() {
let (actual_uri, user, repo) = meta("https://sr.ht/user/repo");
assert_eq!(actual_uri, "https://sr.ht/user/repo".to_string());
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
}