diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2021-08-31 19:29:14 +0000 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2021-08-31 19:29:14 +0000 |
| commit | 8d7df73c2a02325c0754db28a1ca3562a8962e03 (patch) | |
| tree | 6a38c3b665a7f2b57a4f2695af0f3fc1529d1db4 /tests/util/mod.rs | |
| parent | f608cce748d80d46f49ba25999e4334fd959b070 (diff) | |
Initial typed data signing support
Diffstat (limited to 'tests/util/mod.rs')
| -rw-r--r-- | tests/util/mod.rs | 77 |
1 files changed, 60 insertions, 17 deletions
diff --git a/tests/util/mod.rs b/tests/util/mod.rs index 3cc4c66..cd29b28 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -1,29 +1,72 @@ +#![allow(dead_code)] + #[path = "../../src/ganache.rs"] mod ganache; -use std::{str, process::Command}; pub use ganache::*; +use std::{ + io::Write as _, + process::{Command, Stdio}, + str, thread, +}; -/// Utility function for running the `hdwallet` command for integration tests. -pub fn exec(subcommand: &str, args: &[&str]) -> String { - try_exec(subcommand, args).unwrap() +/// Utility type for building an `hdwallet` command for integration tests. +pub struct Hdwallet { + command: Command, + stdin: Option<Vec<u8>>, } -/// Try and execute a command, returning an error if there was one. -pub fn try_exec(subcommand: &str, args: &[&str]) -> Result<String, String> { - 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)) +impl Hdwallet { + /// Create a new `hdwallet` command builder. + pub fn new(subcommand: &str, args: &[&str]) -> Self { + let mut command = Command::new("cargo"); + command + .env("MNEMONIC", ganache::DETERMINISTIC_MNEMONIC) + .args(["run", "--", subcommand]) + .args(args); + Self { + command, + stdin: None, + } + } + + /// Sets the standard input to be used for the command. + pub fn stdin(mut self, data: impl Into<Vec<u8>>) -> Self { + self.stdin = Some(data.into()); + self + } + + /// Executes the command and returns the standard output on success and + /// standard error on failure. + pub fn execute(mut self) -> Result<String, String> { + self.command.stdout(Stdio::piped()).stderr(Stdio::piped()); + if self.stdin.is_some() { + self.command.stdin(Stdio::piped()); + } + + let mut process = self.command.spawn().map_err(|err| err.to_string())?; + + if let Some(data) = self.stdin { + let mut stdin = process.stdin.take().unwrap(); + thread::spawn(move || { + stdin.write_all(&data).unwrap(); + }); + } + + let output = process.wait_with_output().map_err(|err| err.to_string())?; + if output.status.success() { + Ok(string_from_utf8(output.stdout)) + } else { + Err(string_from_utf8(output.stderr)) + } + } + + /// Builds and executes an `hdwallet` command. + pub fn run(subcommand: &str, args: &[&str]) -> String { + Hdwallet::new(subcommand, args).execute().unwrap() } } fn string_from_utf8(bytes: Vec<u8>) -> String { str::from_utf8(&bytes).unwrap().trim().to_string() -}
\ No newline at end of file +} |
