1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#![allow(dead_code)]
#[path = "../../src/ganache.rs"]
mod ganache;
pub use ganache::*;
use std::{
io::Write as _,
process::{Command, Stdio},
str, thread,
};
/// Utility type for building an `hdwallet` command for integration tests.
pub struct Hdwallet {
command: Command,
stdin: Option<Vec<u8>>,
}
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().unwrap();
let input = self.stdin.map(|data| {
let mut stdin = process.stdin.take().unwrap();
thread::spawn(move || {
stdin.write_all(&data).unwrap();
})
});
let output = process.wait_with_output().unwrap();
if let Some(input) = input {
input.join().unwrap();
}
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()
}
|