Skip to content

Commit 9abc8ad

Browse files
committed
create_wallet -> serialize_wallet_to_file; using an enum with variant for data persistence
1 parent f8124e8 commit 9abc8ad

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/utils.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,33 @@ mod tests {
188188
const INPUT_YES: &[u8; 2] = b"y\n";
189189
const INPUT_NO: &[u8; 2] = b"n\n";
190190

191-
fn remove_wallet(wallet_path: &Path) {
192-
if wallet_path.exists() {
193-
fs::remove_file(wallet_path).unwrap();
194-
}
191+
/// Represents the possible serialized states of a wallet.
192+
/// Used primarily for simulating wallet creation and serialization processes.
193+
enum WalletSerializedState {
194+
Empty,
195+
WithData(String),
195196
}
196197

197-
/// Create a wallet file with optional wallet data; the data is written to the file if provided.
198-
/// The data does not have to be a valid JSON.
199-
fn create_wallet(wallet_path: &Path, data: Option<&str>) {
198+
/// Simulates the serialization of a wallet to a file, optionally including dummy data.
199+
/// Primarily used to test if checks for wallet file existence are functioning correctly.
200+
fn serialize_wallet_to_file(wallet_path: &Path, state: WalletSerializedState) {
201+
// Create the wallet file if it does not exist.
200202
if !wallet_path.exists() {
201203
fs::File::create(wallet_path).unwrap();
202204
}
203-
if let Some(data) = data {
205+
206+
// Write content to the wallet file based on the specified state.
207+
if let WalletSerializedState::WithData(data) = state {
204208
fs::write(wallet_path, data).unwrap();
205209
}
206210
}
207211

212+
fn remove_wallet(wallet_path: &Path) {
213+
if wallet_path.exists() {
214+
fs::remove_file(wallet_path).unwrap();
215+
}
216+
}
217+
208218
#[test]
209219
fn handle_absolute_path_argument() {
210220
let tmp_dir = tempfile::TempDir::new().unwrap();
@@ -287,13 +297,13 @@ mod tests {
287297
// case: wallet path exist without --force and input[yes]
288298
let tmp_dir = tempfile::TempDir::new().unwrap();
289299
let wallet_path = tmp_dir.path().join("wallet.json");
290-
create_wallet(&wallet_path, None);
300+
serialize_wallet_to_file(&wallet_path, WalletSerializedState::Empty);
291301
ensure_no_wallet_exists(&wallet_path, false, &INPUT_YES[..]).unwrap();
292302

293303
// case: wallet path exist with --force
294304
let tmp_dir = tempfile::TempDir::new().unwrap();
295305
let wallet_path = tmp_dir.path().join("empty_wallet.json");
296-
create_wallet(&wallet_path, None);
306+
serialize_wallet_to_file(&wallet_path, WalletSerializedState::Empty);
297307

298308
// Empty file should not trigger the replacement prompt
299309
ensure_no_wallet_exists(&wallet_path, false, &INPUT_YES[..]).unwrap();
@@ -306,7 +316,10 @@ mod tests {
306316
let wallet_path = tmp_dir.path().join("nonempty_wallet.json");
307317

308318
// Create non-empty file
309-
create_wallet(&wallet_path, Some("some wallet content"));
319+
serialize_wallet_to_file(
320+
&wallet_path,
321+
WalletSerializedState::WithData("some wallet content".to_string()),
322+
);
310323

311324
// Test with --force flag
312325
ensure_no_wallet_exists(&wallet_path, true, &INPUT_NO[..]).unwrap();
@@ -316,15 +329,21 @@ mod tests {
316329
);
317330

318331
// Test with user confirmation (yes)
319-
create_wallet(&wallet_path, Some("some wallet content"));
332+
serialize_wallet_to_file(
333+
&wallet_path,
334+
WalletSerializedState::WithData("some wallet content".to_string()),
335+
);
320336
ensure_no_wallet_exists(&wallet_path, false, &INPUT_YES[..]).unwrap();
321337
assert!(
322338
!wallet_path.exists(),
323339
"File should be removed after user confirmation"
324340
);
325341

326342
// Test with user rejection (no)
327-
create_wallet(&wallet_path, Some("some wallet content"));
343+
serialize_wallet_to_file(
344+
&wallet_path,
345+
WalletSerializedState::WithData("some wallet content".to_string()),
346+
);
328347
let result = ensure_no_wallet_exists(&wallet_path, false, &INPUT_NO[..]);
329348
assert!(
330349
result.is_err(),

0 commit comments

Comments
 (0)