Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Add tests for fn used in config fn
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi3700 committed Feb 5, 2024
1 parent 689b061 commit fb6c772
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
68 changes: 66 additions & 2 deletions pulsar/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::str::FromStr;

use rand::rngs::SmallRng;
Expand All @@ -7,8 +8,9 @@ use subspace_sdk::ByteSize;
use crate::config::ChainConfig;
use crate::summary::*;
use crate::utils::{
apply_extra_options, custom_log_dir, directory_parser, farm_directory_getter,
node_directory_getter, node_name_parser, reward_address_parser, size_parser, yes_or_no_parser,
apply_extra_options, create_or_move_data, custom_log_dir, directory_parser,
farm_directory_getter, node_directory_getter, node_name_parser, reward_address_parser,
size_parser, yes_or_no_parser,
};

async fn update_summary_file_randomly(summary_file: SummaryFile) {
Expand Down Expand Up @@ -175,3 +177,65 @@ fn custom_log_dir_test() {
#[cfg(target_os = "windows")]
assert!(log_path.ends_with("AppData/Local/pulsar/logs"));
}

#[cfg(test)]
mod create_or_move_data_tests {

use std::fs::{self};
use std::path::Path;

use super::*;

/// Set up a temporary directory for testing.
/// Returns a `PathBuf` representing the path to the temporary directory.
fn setup_temp_directory() -> PathBuf {
let temp_dir = Path::new("/tmp").join(format!("test_pulsar"));

Check failure on line 192 in pulsar/src/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> pulsar/src/tests.rs:192:47 | 192 | let temp_dir = Path::new("/tmp").join(format!("test_pulsar")); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"test_pulsar".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `-D clippy::useless-format` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_format)]`
fs::create_dir_all(&temp_dir).expect("Failed to create temp directory for test");
temp_dir
}

/// Tear down the temporary directory used in testing.
fn teardown_temp_directory(temp_dir: PathBuf) {
fs::remove_dir_all(temp_dir).expect("Failed to remove temp directory after test");
}

/// Ensuring the function correctly handles the case where the old and new
/// directories are the same.
#[test]
fn test_fails_when_old_new_dirs_same() {
let temp_dir = setup_temp_directory();
let old_dir = temp_dir.join("node");
let new_dir = temp_dir.join("node");

assert!(create_or_move_data(PathBuf::from(old_dir), PathBuf::from(new_dir)).is_err());

Check failure on line 210 in pulsar/src/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::path::PathBuf`

error: useless conversion to the same type: `std::path::PathBuf` --> pulsar/src/tests.rs:210:61 | 210 | assert!(create_or_move_data(PathBuf::from(old_dir), PathBuf::from(new_dir)).is_err()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `PathBuf::from()`: `new_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Check failure on line 210 in pulsar/src/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::path::PathBuf`

error: useless conversion to the same type: `std::path::PathBuf` --> pulsar/src/tests.rs:210:37 | 210 | assert!(create_or_move_data(PathBuf::from(old_dir), PathBuf::from(new_dir)).is_err()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `PathBuf::from()`: `old_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `-D clippy::useless-conversion` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_conversion)]`

teardown_temp_directory(temp_dir);
}

/// Verifying the function fails when the new directory path does not start
/// with "/".
#[test]
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn test_fails_when_new_dir_no_slash() {
let temp_dir = setup_temp_directory();
let old_dir = temp_dir.join("old_node");
let new_dir = "pulsar/node";

assert!(create_or_move_data(PathBuf::from(old_dir), PathBuf::from(new_dir)).is_err());

Check failure on line 224 in pulsar/src/tests.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::path::PathBuf`

error: useless conversion to the same type: `std::path::PathBuf` --> pulsar/src/tests.rs:224:37 | 224 | assert!(create_or_move_data(PathBuf::from(old_dir), PathBuf::from(new_dir)).is_err()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `PathBuf::from()`: `old_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

teardown_temp_directory(temp_dir);
}

/// test if old dir doesn't exist
#[test]
fn test_if_old_dir_doesnt_exist() {
// TODO: Add test logic
}

/// test if empty old dir is removed after the data is moved from there to
/// new dir.
#[test]
fn test_remove_dir_if_old_dir_empty() {
// TODO: Add test logic
}
}
1 change: 1 addition & 0 deletions pulsar/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ pub(crate) fn create_or_move_data(old_dir: PathBuf, new_dir: PathBuf) -> Result<
bail!("This directory is already set");
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
if !new_dir.starts_with("/") {
bail!("New directory path must start with /");
}
Expand Down

0 comments on commit fb6c772

Please sign in to comment.