@@ -188,23 +188,33 @@ mod tests {
188
188
const INPUT_YES : & [ u8 ; 2 ] = b"y\n " ;
189
189
const INPUT_NO : & [ u8 ; 2 ] = b"n\n " ;
190
190
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 ) ,
195
196
}
196
197
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.
200
202
if !wallet_path. exists ( ) {
201
203
fs:: File :: create ( wallet_path) . unwrap ( ) ;
202
204
}
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 {
204
208
fs:: write ( wallet_path, data) . unwrap ( ) ;
205
209
}
206
210
}
207
211
212
+ fn remove_wallet ( wallet_path : & Path ) {
213
+ if wallet_path. exists ( ) {
214
+ fs:: remove_file ( wallet_path) . unwrap ( ) ;
215
+ }
216
+ }
217
+
208
218
#[ test]
209
219
fn handle_absolute_path_argument ( ) {
210
220
let tmp_dir = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
@@ -287,13 +297,13 @@ mod tests {
287
297
// case: wallet path exist without --force and input[yes]
288
298
let tmp_dir = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
289
299
let wallet_path = tmp_dir. path ( ) . join ( "wallet.json" ) ;
290
- create_wallet ( & wallet_path, None ) ;
300
+ serialize_wallet_to_file ( & wallet_path, WalletSerializedState :: Empty ) ;
291
301
ensure_no_wallet_exists ( & wallet_path, false , & INPUT_YES [ ..] ) . unwrap ( ) ;
292
302
293
303
// case: wallet path exist with --force
294
304
let tmp_dir = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
295
305
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 ) ;
297
307
298
308
// Empty file should not trigger the replacement prompt
299
309
ensure_no_wallet_exists ( & wallet_path, false , & INPUT_YES [ ..] ) . unwrap ( ) ;
@@ -306,7 +316,10 @@ mod tests {
306
316
let wallet_path = tmp_dir. path ( ) . join ( "nonempty_wallet.json" ) ;
307
317
308
318
// 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
+ ) ;
310
323
311
324
// Test with --force flag
312
325
ensure_no_wallet_exists ( & wallet_path, true , & INPUT_NO [ ..] ) . unwrap ( ) ;
@@ -316,15 +329,21 @@ mod tests {
316
329
) ;
317
330
318
331
// 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
+ ) ;
320
336
ensure_no_wallet_exists ( & wallet_path, false , & INPUT_YES [ ..] ) . unwrap ( ) ;
321
337
assert ! (
322
338
!wallet_path. exists( ) ,
323
339
"File should be removed after user confirmation"
324
340
) ;
325
341
326
342
// 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
+ ) ;
328
347
let result = ensure_no_wallet_exists ( & wallet_path, false , & INPUT_NO [ ..] ) ;
329
348
assert ! (
330
349
result. is_err( ) ,
0 commit comments