Skip to content

Commit

Permalink
create_wallet -> serialize_wallet_to_file; using an enum with variant…
Browse files Browse the repository at this point in the history
… for data persistence
  • Loading branch information
zees-dev committed Oct 29, 2024
1 parent f8124e8 commit 9abc8ad
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,33 @@ mod tests {
const INPUT_YES: &[u8; 2] = b"y\n";
const INPUT_NO: &[u8; 2] = b"n\n";

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

/// Create a wallet file with optional wallet data; the data is written to the file if provided.
/// The data does not have to be a valid JSON.
fn create_wallet(wallet_path: &Path, data: Option<&str>) {
/// Simulates the serialization of a wallet to a file, optionally including dummy data.
/// Primarily used to test if checks for wallet file existence are functioning correctly.
fn serialize_wallet_to_file(wallet_path: &Path, state: WalletSerializedState) {
// Create the wallet file if it does not exist.
if !wallet_path.exists() {
fs::File::create(wallet_path).unwrap();
}
if let Some(data) = data {

// Write content to the wallet file based on the specified state.
if let WalletSerializedState::WithData(data) = state {
fs::write(wallet_path, data).unwrap();
}
}

fn remove_wallet(wallet_path: &Path) {
if wallet_path.exists() {
fs::remove_file(wallet_path).unwrap();
}
}

#[test]
fn handle_absolute_path_argument() {
let tmp_dir = tempfile::TempDir::new().unwrap();
Expand Down Expand Up @@ -287,13 +297,13 @@ mod tests {
// case: wallet path exist without --force and input[yes]
let tmp_dir = tempfile::TempDir::new().unwrap();
let wallet_path = tmp_dir.path().join("wallet.json");
create_wallet(&wallet_path, None);
serialize_wallet_to_file(&wallet_path, WalletSerializedState::Empty);
ensure_no_wallet_exists(&wallet_path, false, &INPUT_YES[..]).unwrap();

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

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

// Create non-empty file
create_wallet(&wallet_path, Some("some wallet content"));
serialize_wallet_to_file(
&wallet_path,
WalletSerializedState::WithData("some wallet content".to_string()),
);

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

// Test with user confirmation (yes)
create_wallet(&wallet_path, Some("some wallet content"));
serialize_wallet_to_file(
&wallet_path,
WalletSerializedState::WithData("some wallet content".to_string()),
);
ensure_no_wallet_exists(&wallet_path, false, &INPUT_YES[..]).unwrap();
assert!(
!wallet_path.exists(),
"File should be removed after user confirmation"
);

// Test with user rejection (no)
create_wallet(&wallet_path, Some("some wallet content"));
serialize_wallet_to_file(
&wallet_path,
WalletSerializedState::WithData("some wallet content".to_string()),
);
let result = ensure_no_wallet_exists(&wallet_path, false, &INPUT_NO[..]);
assert!(
result.is_err(),
Expand Down

0 comments on commit 9abc8ad

Please sign in to comment.