-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_cli_test.rs
More file actions
176 lines (153 loc) · 4.6 KB
/
sync_cli_test.rs
File metadata and controls
176 lines (153 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! CLI sync command tests
//!
//! TDD approach: Tests written first (RED), implementation follows (GREEN)
#![cfg(feature = "test-env")]
use clap::Parser;
use keyring_cli::cli::commands::sync::SyncCommand;
use serial_test::serial;
use tempfile::TempDir;
/// Helper to set up test environment and clean up afterwards
struct TestEnv {
_temp_dir: TempDir,
}
impl TestEnv {
fn setup(test_name: &str) -> Self {
// Clean up any existing environment variables first
std::env::remove_var("OK_CONFIG_DIR");
std::env::remove_var("OK_DATA_DIR");
std::env::remove_var("OK_MASTER_PASSWORD");
let temp_dir = TempDir::new().unwrap();
let config_dir = temp_dir.path().join(format!("config_{}", test_name));
let data_dir = temp_dir.path().join(format!("data_{}", test_name));
std::env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
std::env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
std::env::set_var("OK_MASTER_PASSWORD", "test-password");
std::fs::create_dir_all(&config_dir).unwrap();
std::fs::create_dir_all(&data_dir).unwrap();
Self {
_temp_dir: temp_dir,
}
}
}
impl Drop for TestEnv {
fn drop(&mut self) {
// Clean up environment variables
std::env::remove_var("OK_CONFIG_DIR");
std::env::remove_var("OK_DATA_DIR");
std::env::remove_var("OK_MASTER_PASSWORD");
}
}
#[serial]
#[test]
fn test_sync_command_parsing() {
let args = vec!["sync".to_string()];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert_eq!(command.direction, "both");
assert!(!command.dry_run);
assert!(!command.status);
assert!(!command.config);
}
#[serial]
#[test]
fn test_sync_command_with_direction() {
let args = vec![
"sync".to_string(),
"--direction".to_string(),
"up".to_string(),
];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert_eq!(command.direction, "up");
assert!(!command.dry_run);
}
#[serial]
#[test]
fn test_sync_command_with_dry_run() {
let args = vec!["sync".to_string(), "--dry-run".to_string()];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert_eq!(command.direction, "both");
assert!(command.dry_run);
}
#[serial]
#[test]
fn test_sync_status_command() {
let args = vec!["sync".to_string(), "--status".to_string()];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert!(command.status);
}
#[serial]
#[test]
fn test_sync_config_command() {
let args = vec![
"sync".to_string(),
"--config".to_string(),
"--provider".to_string(),
"dropbox".to_string(),
];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert!(command.config);
assert_eq!(command.provider, Some("dropbox".to_string()));
}
#[serial]
#[test]
fn test_sync_config_without_provider() {
let args = vec!["sync".to_string(), "--config".to_string()];
let command = SyncCommand::try_parse_from(&args).unwrap();
assert!(command.config);
assert_eq!(command.provider, None);
}
#[serial]
#[test]
fn test_sync_direction_validation() {
// Test valid directions
for direction in &["up", "down", "both"] {
let args = vec![
"sync".to_string(),
"--direction".to_string(),
direction.to_string(),
];
let command = SyncCommand::try_parse_from(&args);
assert!(command.is_ok(), "Direction '{}' should be valid", direction);
}
}
#[serial]
#[test]
fn test_sync_execute_sync() {
let _env = TestEnv::setup("execute_sync");
let command = SyncCommand {
status: false,
config: false,
provider: None,
direction: "both".to_string(),
dry_run: false,
};
let result = command.execute();
assert!(result.is_ok(), "Sync execution should succeed");
}
#[serial]
#[test]
fn test_sync_execute_status() {
let _env = TestEnv::setup("execute_status");
let command = SyncCommand {
status: true,
config: false,
provider: None,
direction: "both".to_string(),
dry_run: false,
};
let result = command.execute();
assert!(result.is_ok(), "Status execution should succeed");
}
#[serial]
#[test]
fn test_sync_execute_config() {
let _env = TestEnv::setup("execute_config");
let command = SyncCommand {
status: false,
config: true,
provider: Some("icloud".to_string()),
direction: "both".to_string(),
dry_run: false,
};
let result = command.execute();
assert!(result.is_ok(), "Config execution should succeed");
}