forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_builder.rs
170 lines (142 loc) Β· 4.12 KB
/
command_builder.rs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use super::*;
pub(crate) trait ToArgs {
fn to_args(&self) -> Vec<String>;
}
impl ToArgs for String {
fn to_args(&self) -> Vec<String> {
self.as_str().to_args()
}
}
impl ToArgs for &str {
fn to_args(&self) -> Vec<String> {
self.split_whitespace().map(str::to_string).collect()
}
}
impl<const N: usize> ToArgs for [&str; N] {
fn to_args(&self) -> Vec<String> {
self.iter().cloned().map(str::to_string).collect()
}
}
impl ToArgs for Vec<String> {
fn to_args(&self) -> Vec<String> {
self.clone()
}
}
pub(crate) struct CommandBuilder {
args: Vec<String>,
expected_exit_code: i32,
expected_stderr: Expected,
expected_stdout: Expected,
rpc_server_url: Option<String>,
stdin: Vec<u8>,
tempdir: TempDir,
}
impl CommandBuilder {
pub(crate) fn new(args: impl ToArgs) -> Self {
Self {
args: args.to_args(),
expected_exit_code: 0,
expected_stderr: Expected::String(String::new()),
expected_stdout: Expected::String(String::new()),
rpc_server_url: None,
stdin: Vec::new(),
tempdir: TempDir::new().unwrap(),
}
}
pub(crate) fn write(self, path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Self {
fs::write(self.tempdir.path().join(path), contents).unwrap();
self
}
pub(crate) fn rpc_server(self, rpc_server: &test_bitcoincore_rpc::Handle) -> Self {
Self {
rpc_server_url: Some(rpc_server.url()),
..self
}
}
pub(crate) fn stdin(self, stdin: Vec<u8>) -> Self {
Self { stdin, ..self }
}
pub(crate) fn stdout_regex(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::regex(expected_stdout.as_ref()),
..self
}
}
pub(crate) fn expected_stderr(self, expected_stderr: impl AsRef<str>) -> Self {
Self {
expected_stderr: Expected::String(expected_stderr.as_ref().to_owned()),
..self
}
}
pub(crate) fn stderr_regex(self, expected_stderr: impl AsRef<str>) -> Self {
Self {
expected_stderr: Expected::regex(expected_stderr.as_ref()),
..self
}
}
pub(crate) fn expected_exit_code(self, expected_exit_code: i32) -> Self {
Self {
expected_exit_code,
..self
}
}
pub(crate) fn temp_dir(self, tempdir: TempDir) -> Self {
Self { tempdir, ..self }
}
pub(crate) fn command(&self) -> Command {
let mut command = Command::new(executable_path("ord"));
if let Some(rpc_server_url) = &self.rpc_server_url {
let cookiefile = self.tempdir.path().join("cookie");
fs::write(&cookiefile, "username:password").unwrap();
command.args([
"--rpc-url",
rpc_server_url,
"--cookie-file",
cookiefile.to_str().unwrap(),
]);
}
command
.env("ORD_INTEGRATION_TEST", "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.current_dir(&self.tempdir)
.arg("--data-dir")
.arg(self.tempdir.path())
.args(&self.args);
command
}
fn run(self) -> (TempDir, String) {
let child = self.command().spawn().unwrap();
child
.stdin
.as_ref()
.unwrap()
.write_all(&self.stdin)
.unwrap();
let output = child.wait_with_output().unwrap();
let stdout = str::from_utf8(&output.stdout).unwrap();
let stderr = str::from_utf8(&output.stderr).unwrap();
if output.status.code() != Some(self.expected_exit_code) {
panic!(
"Test failed: {}\nstdout:\n{}\nstderr:\n{}",
output.status, stdout, stderr
);
}
self.expected_stderr.assert_match(stderr);
self.expected_stdout.assert_match(stdout);
(self.tempdir, stdout.into())
}
pub(crate) fn run_and_extract_file(self, path: impl AsRef<Path>) -> String {
let tempdir = self.run().0;
fs::read_to_string(tempdir.path().join(path)).unwrap()
}
pub(crate) fn run_and_extract_stdout(self) -> String {
self.run().1
}
pub(crate) fn run_and_deserialize_output<T: DeserializeOwned>(self) -> T {
let stdout = self.stdout_regex(".*").run_and_extract_stdout();
serde_json::from_str(&stdout)
.unwrap_or_else(|err| panic!("Failed to deserialize JSON: {err}\n{stdout}"))
}
}