upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/test_utils/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils/src/lib.rs')
-rw-r--r--test_utils/src/lib.rs130
1 files changed, 129 insertions, 1 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index 1a4231a..0f870f6 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -1,4 +1,4 @@
1use std::ffi::OsStr; 1use std::{ffi::OsStr, path::PathBuf};
2 2
3use anyhow::{ensure, Context, Result}; 3use anyhow::{ensure, Context, Result};
4use dialoguer::theme::{ColorfulTheme, Theme}; 4use dialoguer::theme::{ColorfulTheme, Theme};
@@ -8,6 +8,8 @@ use once_cell::sync::Lazy;
8use rexpect::session::{Options, PtySession}; 8use rexpect::session::{Options, PtySession};
9use strip_ansi_escapes::strip_str; 9use strip_ansi_escapes::strip_str;
10 10
11pub mod git;
12
11pub static TEST_KEY_1_NSEC: &str = 13pub static TEST_KEY_1_NSEC: &str =
12 "nsec1ppsg5sm2aexq06juxmu9evtutr6jkwkhp98exxxvwamhru9lyx9s3rwseq"; 14 "nsec1ppsg5sm2aexq06juxmu9evtutr6jkwkhp98exxxvwamhru9lyx9s3rwseq";
13pub static TEST_KEY_1_SK_HEX: &str = 15pub static TEST_KEY_1_SK_HEX: &str =
@@ -75,6 +77,34 @@ impl CliTester {
75 i.prompt().context("initial password prompt")?; 77 i.prompt().context("initial password prompt")?;
76 Ok(i) 78 Ok(i)
77 } 79 }
80
81 pub fn expect_confirm(
82 &mut self,
83 prompt: &str,
84 default: Option<bool>,
85 ) -> Result<CliTesterConfirmPrompt> {
86 let mut i = CliTesterConfirmPrompt {
87 tester: self,
88 prompt: prompt.to_string(),
89 default,
90 };
91 i.prompt(false, default).context("initial confirm prompt")?;
92 Ok(i)
93 }
94
95 pub fn expect_confirm_eventually(
96 &mut self,
97 prompt: &str,
98 default: Option<bool>,
99 ) -> Result<CliTesterConfirmPrompt> {
100 let mut i = CliTesterConfirmPrompt {
101 tester: self,
102 prompt: prompt.to_string(),
103 default,
104 };
105 i.prompt(true, default).context("initial confirm prompt")?;
106 Ok(i)
107 }
78} 108}
79 109
80pub struct CliTesterInputPrompt<'a> { 110pub struct CliTesterInputPrompt<'a> {
@@ -199,6 +229,71 @@ impl CliTesterPasswordPrompt<'_> {
199 } 229 }
200} 230}
201 231
232pub struct CliTesterConfirmPrompt<'a> {
233 tester: &'a mut CliTester,
234 prompt: String,
235 default: Option<bool>,
236}
237
238impl CliTesterConfirmPrompt<'_> {
239 fn prompt(&mut self, eventually: bool, default: Option<bool>) -> Result<&mut Self> {
240 let mut s = String::new();
241 self.tester
242 .formatter
243 .format_confirm_prompt(&mut s, self.prompt.as_str(), default)
244 .expect("diagluer theme formatter should succeed");
245 ensure!(
246 s.contains(self.prompt.as_str()),
247 "dialoguer must be broken as formatted prompt success doesnt contain prompt"
248 );
249
250 if eventually {
251 self.tester
252 .expect_eventually(sanatize(s).as_str())
253 .context("expect input prompt eventually")?;
254 } else {
255 self.tester
256 .expect(sanatize(s).as_str())
257 .context("expect confirm prompt")?;
258 }
259
260 Ok(self)
261 }
262
263 pub fn succeeds_with(&mut self, input: Option<bool>) -> Result<&mut Self> {
264 self.tester.send_line(match input {
265 None => "",
266 Some(true) => "y",
267 Some(false) => "n",
268 })?;
269 self.tester
270 .expect("\r")
271 .context("expect new line after confirm input to be printed")?;
272
273 let mut s = String::new();
274 self.tester
275 .formatter
276 .format_confirm_prompt_selection(
277 &mut s,
278 self.prompt.as_str(),
279 match input {
280 None => self.default,
281 Some(_) => input,
282 },
283 )
284 .expect("diagluer theme formatter should succeed");
285 if !s.contains(self.prompt.as_str()) {
286 panic!("dialoguer must be broken as formatted prompt success doesnt contain prompt");
287 }
288 let formatted_success = format!("{}\r\n", sanatize(s));
289
290 self.tester
291 .expect(formatted_success.as_str())
292 .context("expect immediate prompt success")?;
293 Ok(self)
294 }
295}
296
202impl CliTester { 297impl CliTester {
203 pub fn new<I, S>(args: I) -> Self 298 pub fn new<I, S>(args: I) -> Self
204 where 299 where
@@ -210,6 +305,17 @@ impl CliTester {
210 formatter: ColorfulTheme::default(), 305 formatter: ColorfulTheme::default(),
211 } 306 }
212 } 307 }
308 pub fn new_from_dir<I, S>(dir: &PathBuf, args: I) -> Self
309 where
310 I: IntoIterator<Item = S>,
311 S: AsRef<OsStr>,
312 {
313 Self {
314 rexpect_session: rexpect_with_from_dir(dir, args, 2000)
315 .expect("rexpect to spawn new process"),
316 formatter: ColorfulTheme::default(),
317 }
318 }
213 pub fn new_with_timeout<I, S>(timeout_ms: u64, args: I) -> Self 319 pub fn new_with_timeout<I, S>(timeout_ms: u64, args: I) -> Self
214 where 320 where
215 I: IntoIterator<Item = S>, 321 I: IntoIterator<Item = S>,
@@ -338,6 +444,28 @@ where
338 ) 444 )
339} 445}
340 446
447pub fn rexpect_with_from_dir<I, S>(
448 dir: &PathBuf,
449 args: I,
450 timeout_ms: u64,
451) -> Result<PtySession, rexpect::error::Error>
452where
453 I: IntoIterator<Item = S>,
454 S: AsRef<std::ffi::OsStr>,
455{
456 let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("ngit"));
457 cmd.current_dir(dir);
458 cmd.args(args);
459 // using branch for PR https://github.com/rust-cli/rexpect/pull/103 to strip ansi escape codes
460 rexpect::session::spawn_with_options(
461 cmd,
462 Options {
463 timeout_ms: Some(timeout_ms),
464 strip_ansi_escape_codes: true,
465 },
466 )
467}
468
341/// backup and remove application config and data 469/// backup and remove application config and data
342pub fn before() -> Result<()> { 470pub fn before() -> Result<()> {
343 backup_existing_config() 471 backup_existing_config()