test: improve Rust test coverage for settings and github modules
Refactor settings.rs to expose testable internal functions (get_settings_at, save_settings_at) and add integration tests for file I/O roundtrips, whitespace trimming, directory creation, and malformed JSON handling. Coverage for settings.rs improved from 46% to 91%. Overall Rust coverage now 86.2% (above 85% threshold). Also add coverage/ to .gitignore. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -27,6 +27,9 @@ dist-ssr
|
||||
/test-results/
|
||||
/playwright-report/
|
||||
|
||||
# Coverage reports
|
||||
/coverage/
|
||||
|
||||
# Laputa vault cache
|
||||
.laputa-cache.json
|
||||
|
||||
|
||||
@@ -199,6 +199,7 @@ fn configure_remote_auth(local_path: &str, original_url: &str, token: &str) -> R
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::process::Command as StdCommand;
|
||||
|
||||
#[test]
|
||||
fn test_inject_token_basic_github_url() {
|
||||
@@ -224,6 +225,20 @@ mod tests {
|
||||
assert!(result.unwrap_err().contains("Unsupported URL format"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inject_token_http_url_rejected() {
|
||||
let url = "http://github.com/user/repo.git";
|
||||
let result = inject_token_into_url(url, "token");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inject_token_github_without_dot_git() {
|
||||
let url = "https://github.com/user/repo";
|
||||
let result = inject_token_into_url(url, "tok").unwrap();
|
||||
assert_eq!(result, "https://oauth2:tok@github.com/user/repo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone_repo_nonempty_dest() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
@@ -234,4 +249,84 @@ mod tests {
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("not empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone_repo_ssh_url_rejected() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let dest = dir.path().join("new-clone");
|
||||
|
||||
let result = clone_repo("git@github.com:user/repo.git", "token", dest.to_str().unwrap());
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Unsupported URL format"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone_repo_empty_dest_allowed() {
|
||||
// An empty existing directory should not be rejected
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let dest = dir.path().join("empty-dir");
|
||||
std::fs::create_dir(&dest).unwrap();
|
||||
|
||||
// This will fail at the git clone step (invalid URL) but should pass the directory check
|
||||
let result = clone_repo("https://github.com/nonexistent/repo.git", "token", dest.to_str().unwrap());
|
||||
assert!(result.is_err());
|
||||
// Should fail at git clone, not at directory check
|
||||
assert!(result.unwrap_err().contains("git clone failed"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_configure_remote_auth_on_git_repo() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let path = dir.path();
|
||||
|
||||
// Initialize a git repo
|
||||
StdCommand::new("git").args(["init"]).current_dir(path).output().unwrap();
|
||||
StdCommand::new("git").args(["remote", "add", "origin", "https://github.com/user/repo.git"])
|
||||
.current_dir(path).output().unwrap();
|
||||
|
||||
let result = configure_remote_auth(
|
||||
path.to_str().unwrap(),
|
||||
"https://github.com/user/repo.git",
|
||||
"gho_test123",
|
||||
);
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify the remote URL was updated
|
||||
let output = StdCommand::new("git")
|
||||
.args(["remote", "get-url", "origin"])
|
||||
.current_dir(path)
|
||||
.output()
|
||||
.unwrap();
|
||||
let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
assert_eq!(url, "https://oauth2:gho_test123@github.com/user/repo.git");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_github_repo_serialization() {
|
||||
let repo = GithubRepo {
|
||||
name: "test-repo".to_string(),
|
||||
full_name: "user/test-repo".to_string(),
|
||||
description: Some("A test repo".to_string()),
|
||||
private: true,
|
||||
clone_url: "https://github.com/user/test-repo.git".to_string(),
|
||||
html_url: "https://github.com/user/test-repo".to_string(),
|
||||
updated_at: Some("2026-02-20T10:00:00Z".to_string()),
|
||||
};
|
||||
let json = serde_json::to_string(&repo).unwrap();
|
||||
let parsed: GithubRepo = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(parsed.name, "test-repo");
|
||||
assert_eq!(parsed.full_name, "user/test-repo");
|
||||
assert!(parsed.private);
|
||||
assert_eq!(parsed.description, Some("A test repo".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_github_repo_deserialization_null_fields() {
|
||||
let json = r#"{"name":"r","full_name":"u/r","description":null,"private":false,"clone_url":"https://x","html_url":"https://y","updated_at":null}"#;
|
||||
let repo: GithubRepo = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(repo.name, "r");
|
||||
assert!(repo.description.is_none());
|
||||
assert!(repo.updated_at.is_none());
|
||||
assert!(!repo.private);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,17 @@ fn settings_path() -> Result<PathBuf, String> {
|
||||
.ok_or_else(|| "Could not determine config directory".to_string())
|
||||
}
|
||||
|
||||
pub fn get_settings() -> Result<Settings, String> {
|
||||
let path = settings_path()?;
|
||||
fn get_settings_at(path: &PathBuf) -> Result<Settings, String> {
|
||||
if !path.exists() {
|
||||
return Ok(Settings::default());
|
||||
}
|
||||
let content = fs::read_to_string(&path)
|
||||
let content = fs::read_to_string(path)
|
||||
.map_err(|e| format!("Failed to read settings: {}", e))?;
|
||||
serde_json::from_str(&content)
|
||||
.map_err(|e| format!("Failed to parse settings: {}", e))
|
||||
}
|
||||
|
||||
pub fn save_settings(settings: Settings) -> Result<(), String> {
|
||||
let path = settings_path()?;
|
||||
fn save_settings_at(path: &PathBuf, settings: Settings) -> Result<(), String> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("Failed to create config directory: {}", e))?;
|
||||
@@ -44,34 +42,41 @@ pub fn save_settings(settings: Settings) -> Result<(), String> {
|
||||
|
||||
let json = serde_json::to_string_pretty(&cleaned)
|
||||
.map_err(|e| format!("Failed to serialize settings: {}", e))?;
|
||||
fs::write(&path, json)
|
||||
fs::write(path, json)
|
||||
.map_err(|e| format!("Failed to write settings: {}", e))
|
||||
}
|
||||
|
||||
pub fn get_settings() -> Result<Settings, String> {
|
||||
get_settings_at(&settings_path()?)
|
||||
}
|
||||
|
||||
pub fn save_settings(settings: Settings) -> Result<(), String> {
|
||||
save_settings_at(&settings_path()?, settings)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::env;
|
||||
|
||||
fn with_temp_config<F: FnOnce()>(f: F) {
|
||||
let tmp = env::temp_dir().join(format!("laputa-test-{}", std::process::id()));
|
||||
fs::create_dir_all(&tmp).unwrap();
|
||||
env::set_var("XDG_CONFIG_HOME", &tmp);
|
||||
f();
|
||||
let _ = fs::remove_dir_all(&tmp);
|
||||
/// Helper: save settings to a temp file and reload them.
|
||||
fn save_and_reload(settings: Settings) -> Settings {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let path = dir.path().join("settings.json");
|
||||
save_settings_at(&path, settings).unwrap();
|
||||
get_settings_at(&path).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_settings_when_no_file() {
|
||||
let settings = Settings::default();
|
||||
assert!(settings.anthropic_key.is_none());
|
||||
assert!(settings.openai_key.is_none());
|
||||
assert!(settings.google_key.is_none());
|
||||
assert!(settings.github_token.is_none());
|
||||
fn test_default_settings_all_none() {
|
||||
let s = Settings::default();
|
||||
assert_eq!(
|
||||
format!("{:?}", s),
|
||||
format!("{:?}", Settings { anthropic_key: None, openai_key: None, google_key: None, github_token: None })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings_serialize_deserialize() {
|
||||
fn test_settings_json_roundtrip() {
|
||||
let settings = Settings {
|
||||
anthropic_key: Some("sk-ant-test123".to_string()),
|
||||
openai_key: None,
|
||||
@@ -80,30 +85,80 @@ mod tests {
|
||||
};
|
||||
let json = serde_json::to_string(&settings).unwrap();
|
||||
let parsed: Settings = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(parsed.anthropic_key, Some("sk-ant-test123".to_string()));
|
||||
assert!(parsed.openai_key.is_none());
|
||||
assert_eq!(parsed.google_key, Some("AIza-test".to_string()));
|
||||
assert_eq!(parsed.github_token, Some("gho_xyz789".to_string()));
|
||||
assert_eq!(parsed.anthropic_key, settings.anthropic_key);
|
||||
assert_eq!(parsed.google_key, settings.google_key);
|
||||
assert_eq!(parsed.github_token, settings.github_token);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_trims_whitespace_and_filters_empty() {
|
||||
// Test the cleaning logic directly
|
||||
let settings = Settings {
|
||||
fn test_get_settings_returns_default_for_missing_file() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let path = dir.path().join("nonexistent.json");
|
||||
let result = get_settings_at(&path).unwrap();
|
||||
assert!(result.anthropic_key.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_and_load_preserves_values() {
|
||||
let loaded = save_and_reload(Settings {
|
||||
anthropic_key: Some("sk-ant-key".to_string()),
|
||||
openai_key: Some("sk-openai".to_string()),
|
||||
google_key: None,
|
||||
github_token: Some("gho_token123".to_string()),
|
||||
});
|
||||
assert_eq!(loaded.anthropic_key.as_deref(), Some("sk-ant-key"));
|
||||
assert_eq!(loaded.openai_key.as_deref(), Some("sk-openai"));
|
||||
assert_eq!(loaded.github_token.as_deref(), Some("gho_token123"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_trims_whitespace() {
|
||||
let loaded = save_and_reload(Settings {
|
||||
anthropic_key: Some(" sk-ant-test ".to_string()),
|
||||
openai_key: Some(" ".to_string()),
|
||||
google_key: Some("".to_string()),
|
||||
openai_key: None,
|
||||
google_key: None,
|
||||
github_token: Some(" gho_abc ".to_string()),
|
||||
};
|
||||
let cleaned = Settings {
|
||||
anthropic_key: settings.anthropic_key.map(|k| k.trim().to_string()).filter(|k| !k.is_empty()),
|
||||
openai_key: settings.openai_key.map(|k| k.trim().to_string()).filter(|k| !k.is_empty()),
|
||||
google_key: settings.google_key.map(|k| k.trim().to_string()).filter(|k| !k.is_empty()),
|
||||
github_token: settings.github_token.map(|k| k.trim().to_string()).filter(|k| !k.is_empty()),
|
||||
};
|
||||
assert_eq!(cleaned.anthropic_key, Some("sk-ant-test".to_string()));
|
||||
assert!(cleaned.openai_key.is_none());
|
||||
assert!(cleaned.google_key.is_none());
|
||||
assert_eq!(cleaned.github_token, Some("gho_abc".to_string()));
|
||||
});
|
||||
assert_eq!(loaded.anthropic_key.as_deref(), Some("sk-ant-test"));
|
||||
assert_eq!(loaded.github_token.as_deref(), Some("gho_abc"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_filters_empty_and_whitespace_only() {
|
||||
let loaded = save_and_reload(Settings {
|
||||
anthropic_key: Some("".to_string()),
|
||||
openai_key: Some(" ".to_string()),
|
||||
google_key: None,
|
||||
github_token: None,
|
||||
});
|
||||
assert!(loaded.anthropic_key.is_none());
|
||||
assert!(loaded.openai_key.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_creates_parent_directories() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let path = dir.path().join("nested").join("dir").join("settings.json");
|
||||
|
||||
save_settings_at(&path, Settings { anthropic_key: Some("key".to_string()), ..Default::default() }).unwrap();
|
||||
assert!(path.exists());
|
||||
assert_eq!(get_settings_at(&path).unwrap().anthropic_key.as_deref(), Some("key"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_settings_malformed_json() {
|
||||
let dir = tempfile::TempDir::new().unwrap();
|
||||
let path = dir.path().join("bad.json");
|
||||
fs::write(&path, "not valid json{{{").unwrap();
|
||||
|
||||
let err = get_settings_at(&path).unwrap_err();
|
||||
assert!(err.contains("Failed to parse settings"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings_path_returns_ok() {
|
||||
let result = settings_path();
|
||||
assert!(result.is_ok());
|
||||
assert!(result.unwrap().to_str().unwrap().contains("com.laputa.app"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user