🔧 fix: address clippy warnings and improve code formatting
This commit is contained in:
committed by
Jeremiah Russell
parent
5f40af6c88
commit
8f79081b4f
@@ -684,9 +684,8 @@ impl InitCli {
|
|||||||
|
|
||||||
Operation::EnsureTokenDir { path, .. } => {
|
Operation::EnsureTokenDir { path, .. } => {
|
||||||
log::info!("Ensuring token directory: {}", path.display());
|
log::info!("Ensuring token directory: {}", path.display());
|
||||||
fs::create_dir_all(path).map_err(|e| {
|
fs::create_dir_all(path)
|
||||||
Error::FileIo(format!("Failed to create token directory: {e}"))
|
.map_err(|e| Error::FileIo(format!("Failed to create token directory: {e}")))?;
|
||||||
})?;
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
if let Some(mode) = operation.get_mode() {
|
if let Some(mode) = operation.get_mode() {
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod unit_tests {
|
mod unit_tests {
|
||||||
use super::super::*;
|
use super::super::*;
|
||||||
use tempfile::TempDir;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
/// Test helper to create a mock credential file
|
/// Test helper to create a mock credential file
|
||||||
fn create_mock_credential_file(dir: &Path) -> std::io::Result<()> {
|
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);
|
let result = init_cli.validate_credential_file(&credential_path);
|
||||||
assert!(result.is_err());
|
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]
|
#[test]
|
||||||
@@ -185,17 +190,23 @@ mod unit_tests {
|
|||||||
interactive: false,
|
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
|
// Should have: CreateDir, CopyFile (credential), WriteFile (config), WriteFile (rules), EnsureTokenDir, RunOAuth2
|
||||||
assert_eq!(operations.len(), 6);
|
assert_eq!(operations.len(), 6);
|
||||||
|
|
||||||
// Check that CopyFile operation exists
|
// 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());
|
assert!(copy_op.is_some());
|
||||||
|
|
||||||
// Check that RunOAuth2 operation exists
|
// 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());
|
assert!(oauth_op.is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +254,12 @@ mod unit_tests {
|
|||||||
|
|
||||||
// Should succeed and plan backup operations
|
// Should succeed and plan backup operations
|
||||||
let config_op = operations.iter().find(|op| {
|
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
|
path.file_name().unwrap() == "cull-gmail.toml" && *backup_if_exists
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
@@ -334,7 +350,10 @@ mod unit_tests {
|
|||||||
mode: Some(0o600),
|
mode: Some(0o600),
|
||||||
backup_if_exists: false,
|
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 {
|
let write_file_op = Operation::WriteFile {
|
||||||
path: temp_path.clone(),
|
path: temp_path.clone(),
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ fn test_init_help() {
|
|||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.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("--config-dir"))
|
||||||
.stdout(predicate::str::contains("--credential-file"))
|
.stdout(predicate::str::contains("--credential-file"))
|
||||||
.stdout(predicate::str::contains("--dry-run"))
|
.stdout(predicate::str::contains("--dry-run"))
|
||||||
@@ -30,7 +32,7 @@ fn test_init_dry_run_new_setup() {
|
|||||||
"init",
|
"init",
|
||||||
"--config-dir",
|
"--config-dir",
|
||||||
&format!("c:{}", config_dir.to_string_lossy()),
|
&format!("c:{}", config_dir.to_string_lossy()),
|
||||||
"--dry-run"
|
"--dry-run",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
@@ -44,7 +46,9 @@ fn test_init_dry_run_new_setup() {
|
|||||||
.stdout(predicate::str::contains("Ensure token directory:"))
|
.stdout(predicate::str::contains("Ensure token directory:"))
|
||||||
.stdout(predicate::str::contains("gmail1"))
|
.stdout(predicate::str::contains("gmail1"))
|
||||||
.stdout(predicate::str::contains("OAuth2 authentication skipped"))
|
.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
|
// Verify no files were actually created
|
||||||
assert!(!config_dir.exists());
|
assert!(!config_dir.exists());
|
||||||
@@ -59,7 +63,9 @@ fn test_init_dry_run_with_credential_file() {
|
|||||||
let credential_file = temp_dir.child("credential.json");
|
let credential_file = temp_dir.child("credential.json");
|
||||||
|
|
||||||
// Create a mock credential file
|
// Create a mock credential file
|
||||||
credential_file.write_str(r#"{
|
credential_file
|
||||||
|
.write_str(
|
||||||
|
r#"{
|
||||||
"installed": {
|
"installed": {
|
||||||
"client_id": "test-client-id.googleusercontent.com",
|
"client_id": "test-client-id.googleusercontent.com",
|
||||||
"client_secret": "test-client-secret",
|
"client_secret": "test-client-secret",
|
||||||
@@ -67,7 +73,9 @@ fn test_init_dry_run_with_credential_file() {
|
|||||||
"token_uri": "https://oauth2.googleapis.com/token",
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
"redirect_uris": ["http://localhost"]
|
"redirect_uris": ["http://localhost"]
|
||||||
}
|
}
|
||||||
}"#).unwrap();
|
}"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
|
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
|
||||||
cmd.args([
|
cmd.args([
|
||||||
@@ -76,7 +84,7 @@ fn test_init_dry_run_with_credential_file() {
|
|||||||
&format!("c:{}", config_dir.to_string_lossy()),
|
&format!("c:{}", config_dir.to_string_lossy()),
|
||||||
"--credential-file",
|
"--credential-file",
|
||||||
credential_file.path().to_str().unwrap(),
|
credential_file.path().to_str().unwrap(),
|
||||||
"--dry-run"
|
"--dry-run",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
@@ -107,7 +115,9 @@ fn test_init_actual_execution() {
|
|||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.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("Configuration directory:"))
|
||||||
.stdout(predicate::str::contains("Files created:"))
|
.stdout(predicate::str::contains("Files created:"))
|
||||||
.stdout(predicate::str::contains("cull-gmail.toml"))
|
.stdout(predicate::str::contains("cull-gmail.toml"))
|
||||||
@@ -146,7 +156,7 @@ fn test_init_force_overwrite() {
|
|||||||
"--config-dir",
|
"--config-dir",
|
||||||
&format!("c:{}", config_dir.to_string_lossy()),
|
&format!("c:{}", config_dir.to_string_lossy()),
|
||||||
"--force",
|
"--force",
|
||||||
"--dry-run"
|
"--dry-run",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
@@ -189,7 +199,9 @@ fn test_init_with_credential_file_copy() {
|
|||||||
let credential_file = temp_dir.child("source_credential.json");
|
let credential_file = temp_dir.child("source_credential.json");
|
||||||
|
|
||||||
// Create a mock credential file
|
// Create a mock credential file
|
||||||
credential_file.write_str(r#"{
|
credential_file
|
||||||
|
.write_str(
|
||||||
|
r#"{
|
||||||
"installed": {
|
"installed": {
|
||||||
"client_id": "test-client-id.googleusercontent.com",
|
"client_id": "test-client-id.googleusercontent.com",
|
||||||
"client_secret": "test-client-secret",
|
"client_secret": "test-client-secret",
|
||||||
@@ -197,7 +209,9 @@ fn test_init_with_credential_file_copy() {
|
|||||||
"token_uri": "https://oauth2.googleapis.com/token",
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
"redirect_uris": ["http://localhost"]
|
"redirect_uris": ["http://localhost"]
|
||||||
}
|
}
|
||||||
}"#).unwrap();
|
}"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
|
let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
|
||||||
cmd.args([
|
cmd.args([
|
||||||
@@ -220,7 +234,8 @@ fn test_init_with_credential_file_copy() {
|
|||||||
assert!(config_dir.join("gmail1").exists());
|
assert!(config_dir.join("gmail1").exists());
|
||||||
|
|
||||||
// Verify credential file was copied
|
// 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"));
|
assert!(copied_credential_content.contains("test-client-id.googleusercontent.com"));
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@@ -304,7 +319,9 @@ fn test_init_oauth_integration() {
|
|||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::str::contains("OAuth2 authentication successful!"))
|
.stdout(predicate::str::contains(
|
||||||
|
"OAuth2 authentication successful!",
|
||||||
|
))
|
||||||
.stdout(predicate::str::contains("gmail1/ (OAuth2 token cache)"));
|
.stdout(predicate::str::contains("gmail1/ (OAuth2 token cache)"));
|
||||||
|
|
||||||
// Verify token files were created
|
// 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
|
// 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 gmail_dir_contents = std::fs::read_dir(config_dir.join("gmail1")).unwrap();
|
||||||
let has_token_files = gmail_dir_contents.count() > 0;
|
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();
|
temp_dir.close().unwrap();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user