Skip to content

Commit

Permalink
fix: node init CLI command bugfix (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberb authored Jul 10, 2023
1 parent 4e911bb commit 02da2e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
20 changes: 12 additions & 8 deletions crates/topos/src/components/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub(crate) async fn handle_command(
) -> Result<(), Box<dyn std::error::Error>> {
match subcommands {
Some(NodeCommands::Init(cmd)) => {
let name = cmd.name.expect("No name or default was given");
let cmd = *cmd;
let name = cmd.name.as_ref().expect("No name or default was given");

// Construct path to node config
// will be $TOPOS_HOME/node/default/ with no given name
Expand All @@ -48,16 +49,16 @@ pub(crate) async fn handle_command(
create_dir_all(&node_path).expect("failed to create node folder");

// Check if the config file exists
let config_path = home.join("config.toml");
let config_path = node_path.join("config.toml");

if Path::new(&config_path).exists() {
println!("Config file: {} already exists", config_path.display());
std::process::exit(1);
}

let base_config = load_config::<BaseConfig>(&node_path);
let tce_config = load_config::<TceConfig>(&node_path);
let sequencer_config = load_config::<SequencerConfig>(&node_path);
let base_config = load_config::<BaseConfig>(&node_path, Some(cmd));
let tce_config = load_config::<TceConfig>(&node_path, None);
let sequencer_config = load_config::<SequencerConfig>(&node_path, None);

// Creating the TOML output
let mut config_toml = toml::Table::new();
Expand Down Expand Up @@ -85,16 +86,19 @@ pub(crate) async fn handle_command(
Ok(())
}
Some(NodeCommands::Up(cmd)) => {
let name = cmd.name.expect("No name or default was given for node");
let node_path = home.join("node").join(&name);
let name = cmd
.name
.as_ref()
.expect("No name or default was given for node");
let node_path = home.join("node").join(name);
let config_path = node_path.join("config.toml");

if !Path::new(&config_path).exists() {
println!("Please run 'topos init --name {name}' to create a config file first.");
std::process::exit(1);
}

let config = load_config::<NodeConfig>(&node_path);
let config = load_config::<NodeConfig>(&node_path, Some(*cmd));

println!(
"Reading the configuration from {}/{}/config.toml",
Expand Down
4 changes: 2 additions & 2 deletions crates/topos/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub(crate) trait Config: Serialize {
}

#[allow(dead_code)]
pub(crate) fn load_config<T: Config>(node_path: &Path) -> T::Output {
match T::load(node_path, None) {
pub(crate) fn load_config<T: Config>(node_path: &Path, command: Option<T::Command>) -> T::Output {
match T::load(node_path, command) {
Ok(config) => config,
Err(figment::Error {
kind: Kind::MissingField(name),
Expand Down
44 changes: 40 additions & 4 deletions crates/topos/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use assert_cmd::prelude::*;

#[test]
fn test_handle_command_init() -> Result<(), Box<dyn std::error::Error>> {
let temporary_test_folder = "/tmp/topos";
let temporary_test_folder = "/tmp/topos/handle_command_init";

let mut cmd = Command::cargo_bin("topos")?;
cmd.arg("node")
Expand All @@ -18,19 +18,55 @@ fn test_handle_command_init() -> Result<(), Box<dyn std::error::Error>> {

assert!(result.contains("Created node config file"));

let mut home = PathBuf::from("/tmp");
home.push("topos");
let home = PathBuf::from(temporary_test_folder);

// Verification: check that the config file was created
let config_path = home.join("node").join("default").join("config.toml");
assert!(config_path.exists());

// Further verification might include checking the contents of the config file
let config_contents = std::fs::read_to_string(&config_path).unwrap();

assert!(config_contents.contains("[base]"));
assert!(config_contents.contains("name = \"default\""));
assert!(config_contents.contains("[tce]"));
assert!(config_contents.contains("[sequencer]"));

std::fs::remove_dir_all(temporary_test_folder)?;
Ok(())
}

#[test]
fn test_handle_command_init_with_custom_name() -> Result<(), Box<dyn std::error::Error>> {
let temporary_test_folder = "/tmp/topos/test_handle_command_init_with_custom_name";
let node_name = "TEST_NODE";

let mut cmd = Command::cargo_bin("topos")?;
cmd.arg("node")
.arg("init")
.arg("--home")
.arg(temporary_test_folder)
.arg("--name")
.arg(node_name);

let output = cmd.assert().success();
let result: &str = std::str::from_utf8(&output.get_output().stdout)?;

assert!(result.contains("Created node config file"));

let home = PathBuf::from(temporary_test_folder);

// Verification: check that the config file was created
let config_path = home.join("node").join(node_name).join("config.toml");
assert!(config_path.exists());

// Further verification might include checking the contents of the config file
let config_contents = std::fs::read_to_string(&config_path).unwrap();
assert!(config_contents.contains("[base]"));
assert!(config_contents.contains(node_name));
assert!(config_contents.contains("[tce]"));
assert!(config_contents.contains("[sequencer]"));

std::fs::remove_dir_all("/tmp/topos")?;
std::fs::remove_dir_all(temporary_test_folder)?;
Ok(())
}

0 comments on commit 02da2e8

Please sign in to comment.