From f608cce748d80d46f49ba25999e4334fd959b070 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Sat, 21 Aug 2021 07:35:13 +0000 Subject: Refactor into lib+bin and start adding e2e tests --- tests/new.rs | 20 ++++++++++++++++++++ tests/util/mod.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/new.rs create mode 100644 tests/util/mod.rs (limited to 'tests') diff --git a/tests/new.rs b/tests/new.rs new file mode 100644 index 0000000..4ddbf0c --- /dev/null +++ b/tests/new.rs @@ -0,0 +1,20 @@ +mod util; + +use hdwallet::mnemonic::Mnemonic; + +#[test] +fn generates_random_mnemonic() { + let mnemonic = util::exec("new", &[]); + assert!(Mnemonic::from_phrase(mnemonic).is_ok()) +} + +#[test] +fn errors_on_invalid_language() { + assert!(util::try_exec("new", &["--language", "klingon"]).is_err()); +} + +#[test] +fn errors_on_invalid_length() { + assert!(util::try_exec("new", &["--length", "1"]).is_err()); + assert!(util::try_exec("new", &["--length", "42"]).is_err()); +} \ No newline at end of file diff --git a/tests/util/mod.rs b/tests/util/mod.rs new file mode 100644 index 0000000..3cc4c66 --- /dev/null +++ b/tests/util/mod.rs @@ -0,0 +1,29 @@ +#[path = "../../src/ganache.rs"] +mod ganache; + +use std::{str, process::Command}; +pub use ganache::*; + +/// Utility function for running the `hdwallet` command for integration tests. +pub fn exec(subcommand: &str, args: &[&str]) -> String { + try_exec(subcommand, args).unwrap() +} + +/// Try and execute a command, returning an error if there was one. +pub fn try_exec(subcommand: &str, args: &[&str]) -> Result { + let output = Command::new("cargo") + .env("MNEMONIC", ganache::DETERMINISTIC_MNEMONIC) + .args(["run", "--", subcommand]) + .args(args) + .output() + .unwrap(); + if output.status.success() { + Ok(string_from_utf8(output.stdout)) + } else { + Err(string_from_utf8(output.stderr)) + } +} + +fn string_from_utf8(bytes: Vec) -> String { + str::from_utf8(&bytes).unwrap().trim().to_string() +} \ No newline at end of file -- cgit v1.2.3