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

Commit

Permalink
feat: add proper error handling to setup command (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberb authored Feb 15, 2024
1 parent 7030341 commit 3335846
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
12 changes: 11 additions & 1 deletion crates/topos/src/components/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ pub(crate) async fn handle_command(
"Starting installation of Polygon Edge binary to target path: {}",
&cmd.path.display()
);
println!(
"Starting installation of Polygon Edge binary to target path: {}",
&cmd.path.display()
);
if let Err(e) =
install_polygon_edge(cmd.repository, cmd.release, cmd.path.as_path()).await
{
error!("Error installing Polygon Edge: {e}");
eprintln!("Error installing Polygon Edge: {e}");
std::process::exit(1);
} else {
info!("Polygon Edge installation successful");
println!("Polygon Edge installation successful");
std::process::exit(0);
}
}
Expand All @@ -46,6 +52,10 @@ pub(crate) async fn handle_command(

Ok(())
}
None => Ok(()),
None => {
error!("No subcommand provided. You can use `--help` to see available subcommands.");
eprintln!("No subcommand provided. You can use `--help` to see available subcommands.");
std::process::exit(1);
}
}
}
2 changes: 1 addition & 1 deletion crates/topos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Error {
Http(reqwest::Error),
#[error("Json parsing error: {0}")]
InvalidJson(serde_json::Error),
#[error("There are no available release")]
#[error("There is no valid Polygon Edge release available")]
NoValidRelease,
#[error("Invalid release metadata")]
InvalidReleaseMetadata,
Expand Down
63 changes: 63 additions & 0 deletions crates/topos/tests/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
mod utils;

use std::process::Command;

use assert_cmd::prelude::*;
use tempfile::tempdir;

#[test]
fn setup_subnet_install_edge() -> Result<(), Box<dyn std::error::Error>> {
let tmp_home_dir = tempdir()?;

let mut cmd = Command::cargo_bin("topos")?;
cmd.arg("setup")
.arg("subnet")
.arg("--path")
.arg(tmp_home_dir.path());

let output = cmd.assert().success();

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

assert!(result.contains("Polygon Edge installation successful"));

Ok(())
}

#[test]
fn setup_with_no_arguments() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("topos")?;
cmd.arg("setup");

let output = cmd.assert().failure();

let result: &str = std::str::from_utf8(&output.get_output().stderr)?;

assert!(result
.contains("No subcommand provided. You can use `--help` to see available subcommands."));

Ok(())
}

#[test]
fn setup_subnet_fail_to_install_release() -> Result<(), Box<dyn std::error::Error>> {
let tmp_home_dir = tempdir()?;

let mut cmd = Command::cargo_bin("topos")?;
cmd.arg("setup")
.arg("subnet")
.arg("--path")
.arg(tmp_home_dir.path())
.arg("--release")
.arg("invalid");

let output = cmd.assert().failure();

let result: &str = std::str::from_utf8(&output.get_output().stderr)?;

assert!(result.contains(
"Error installing Polygon Edge: There is no valid Polygon Edge release available"
));

Ok(())
}

0 comments on commit 3335846

Please sign in to comment.