🔧 fix: address clippy warnings and improve code formatting

This commit is contained in:
Jeremiah Russell
2025-10-21 12:24:47 +01:00
committed by Jeremiah Russell
parent 5f40af6c88
commit 8f79081b4f
3 changed files with 86 additions and 48 deletions

View File

@@ -684,9 +684,8 @@ impl InitCli {
Operation::EnsureTokenDir { path, .. } => {
log::info!("Ensuring token directory: {}", path.display());
fs::create_dir_all(path).map_err(|e| {
Error::FileIo(format!("Failed to create token directory: {e}"))
})?;
fs::create_dir_all(path)
.map_err(|e| Error::FileIo(format!("Failed to create token directory: {e}")))?;
#[cfg(unix)]
if let Some(mode) = operation.get_mode() {

View File

@@ -3,9 +3,9 @@
#[cfg(test)]
mod unit_tests {
use super::super::*;
use tempfile::TempDir;
use std::fs;
use std::path::Path;
use tempfile::TempDir;
/// Test helper to create a mock credential file
fn create_mock_credential_file(dir: &Path) -> std::io::Result<()> {
@@ -117,7 +117,12 @@ mod unit_tests {
let result = init_cli.validate_credential_file(&credential_path);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Invalid credential file format"));
assert!(
result
.unwrap_err()
.to_string()
.contains("Invalid credential file format")
);
}
#[test]
@@ -185,17 +190,23 @@ mod unit_tests {
interactive: false,
};
let operations = init_cli.plan_operations(&config_path, Some(&cred_path)).unwrap();
let operations = init_cli
.plan_operations(&config_path, Some(&cred_path))
.unwrap();
// Should have: CreateDir, CopyFile (credential), WriteFile (config), WriteFile (rules), EnsureTokenDir, RunOAuth2
assert_eq!(operations.len(), 6);
// Check that CopyFile operation exists
let copy_op = operations.iter().find(|op| matches!(op, Operation::CopyFile { .. }));
let copy_op = operations
.iter()
.find(|op| matches!(op, Operation::CopyFile { .. }));
assert!(copy_op.is_some());
// Check that RunOAuth2 operation exists
let oauth_op = operations.iter().find(|op| matches!(op, Operation::RunOAuth2 { .. }));
let oauth_op = operations
.iter()
.find(|op| matches!(op, Operation::RunOAuth2 { .. }));
assert!(oauth_op.is_some());
}
@@ -243,7 +254,12 @@ mod unit_tests {
// Should succeed and plan backup operations
let config_op = operations.iter().find(|op| {
if let Operation::WriteFile { path, backup_if_exists, .. } = op {
if let Operation::WriteFile {
path,
backup_if_exists,
..
} = op
{
path.file_name().unwrap() == "cull-gmail.toml" && *backup_if_exists
} else {
false
@@ -334,7 +350,10 @@ mod unit_tests {
mode: Some(0o600),
backup_if_exists: false,
};
assert_eq!(format!("{copy_file_op}"), "Copy file: /tmp/test → /tmp/test/dest");
assert_eq!(
format!("{copy_file_op}"),
"Copy file: /tmp/test → /tmp/test/dest"
);
let write_file_op = Operation::WriteFile {
path: temp_path.clone(),

View File

@@ -12,7 +12,9 @@ fn test_init_help() {
cmd.assert()
.success()
.stdout(predicate::str::contains("Initialize cull-gmail configuration"))
.stdout(predicate::str::contains(
"Initialize cull-gmail configuration",
))
.stdout(predicate::str::contains("--config-dir"))
.stdout(predicate::str::contains("--credential-file"))
.stdout(predicate::str::contains("--dry-run"))
@@ -30,7 +32,7 @@ fn test_init_dry_run_new_setup() {
"init",
"--config-dir",
&format!("c:{}", config_dir.to_string_lossy()),
"--dry-run"
"--dry-run",
]);
cmd.assert()
@@ -44,7 +46,9 @@ fn test_init_dry_run_new_setup() {
.stdout(predicate::str::contains("Ensure token directory:"))
.stdout(predicate::str::contains("gmail1"))
.stdout(predicate::str::contains("OAuth2 authentication skipped"))
.stdout(predicate::str::contains("To apply these changes, run without --dry-run"));
.stdout(predicate::str::contains(
"To apply these changes, run without --dry-run",
));
// Verify no files were actually created
assert!(!config_dir.exists());
@@ -59,7 +63,9 @@ fn test_init_dry_run_with_credential_file() {
let credential_file = temp_dir.child("credential.json");
// Create a mock credential file
credential_file.write_str(r#"{
credential_file
.write_str(
r#"{
"installed": {
"client_id": "test-client-id.googleusercontent.com",
"client_secret": "test-client-secret",
@@ -67,7 +73,9 @@ fn test_init_dry_run_with_credential_file() {
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": ["http://localhost"]
}
}"#).unwrap();
}"#,
)
.unwrap();
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
cmd.args([
@@ -76,7 +84,7 @@ fn test_init_dry_run_with_credential_file() {
&format!("c:{}", config_dir.to_string_lossy()),
"--credential-file",
credential_file.path().to_str().unwrap(),
"--dry-run"
"--dry-run",
]);
cmd.assert()
@@ -107,7 +115,9 @@ fn test_init_actual_execution() {
cmd.assert()
.success()
.stdout(predicate::str::contains("Initialization completed successfully!"))
.stdout(predicate::str::contains(
"Initialization completed successfully!",
))
.stdout(predicate::str::contains("Configuration directory:"))
.stdout(predicate::str::contains("Files created:"))
.stdout(predicate::str::contains("cull-gmail.toml"))
@@ -146,7 +156,7 @@ fn test_init_force_overwrite() {
"--config-dir",
&format!("c:{}", config_dir.to_string_lossy()),
"--force",
"--dry-run"
"--dry-run",
]);
cmd.assert()
@@ -189,7 +199,9 @@ fn test_init_with_credential_file_copy() {
let credential_file = temp_dir.child("source_credential.json");
// Create a mock credential file
credential_file.write_str(r#"{
credential_file
.write_str(
r#"{
"installed": {
"client_id": "test-client-id.googleusercontent.com",
"client_secret": "test-client-secret",
@@ -197,7 +209,9 @@ fn test_init_with_credential_file_copy() {
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": ["http://localhost"]
}
}"#).unwrap();
}"#,
)
.unwrap();
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
cmd.args([
@@ -220,7 +234,8 @@ fn test_init_with_credential_file_copy() {
assert!(config_dir.join("gmail1").exists());
// Verify credential file was copied
let copied_credential_content = std::fs::read_to_string(config_dir.join("credential.json")).unwrap();
let copied_credential_content =
std::fs::read_to_string(config_dir.join("credential.json")).unwrap();
assert!(copied_credential_content.contains("test-client-id.googleusercontent.com"));
#[cfg(unix)]
@@ -304,7 +319,9 @@ fn test_init_oauth_integration() {
cmd.assert()
.success()
.stdout(predicate::str::contains("OAuth2 authentication successful!"))
.stdout(predicate::str::contains(
"OAuth2 authentication successful!",
))
.stdout(predicate::str::contains("gmail1/ (OAuth2 token cache)"));
// Verify token files were created
@@ -313,7 +330,10 @@ fn test_init_oauth_integration() {
// Check if there are token-related files in the gmail1 directory
let gmail_dir_contents = std::fs::read_dir(config_dir.join("gmail1")).unwrap();
let has_token_files = gmail_dir_contents.count() > 0;
assert!(has_token_files, "Expected token files to be created in gmail1 directory");
assert!(
has_token_files,
"Expected token files to be created in gmail1 directory"
);
temp_dir.close().unwrap();
}