From 6b3aecbcbde669859533716225e9c3bbfd2023b2 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 8 Mar 2024 14:37:56 +0000 Subject: feat(send): select commits from a list when since_or_range isn't specified adds resilience as assuming master..HEAD can cause some issues eg when master is not up-to-date with origin/master --- test_utils/src/lib.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'test_utils/src/lib.rs') diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index b4f0360..2edbc60 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -1,6 +1,6 @@ use std::{ffi::OsStr, path::PathBuf}; -use anyhow::{ensure, Context, Result}; +use anyhow::{bail, ensure, Context, Result}; use dialoguer::theme::{ColorfulTheme, Theme}; use directories::ProjectDirs; use nostr::{self, prelude::FromSkStr, Kind, Tag}; @@ -259,6 +259,20 @@ impl CliTester { i.prompt(false).context("initial confirm prompt")?; Ok(i) } + + pub fn expect_multi_select( + &mut self, + prompt: &str, + choices: Vec, + ) -> Result { + let mut i = CliTesterMultiSelectPrompt { + tester: self, + prompt: prompt.to_string(), + choices, + }; + i.prompt(false).context("initial confirm prompt")?; + Ok(i) + } } pub struct CliTesterInputPrompt<'a> { @@ -448,6 +462,75 @@ impl CliTesterConfirmPrompt<'_> { } } +pub struct CliTesterMultiSelectPrompt<'a> { + tester: &'a mut CliTester, + prompt: String, + choices: Vec, +} + +impl CliTesterMultiSelectPrompt<'_> { + fn prompt(&mut self, eventually: bool) -> Result<&mut Self> { + if eventually { + self.tester + .expect_eventually(format!("{}:\r\n", self.prompt)) + .context("expect multi-select prompt eventually")?; + } else { + self.tester + .expect(format!("{}:\r\n", self.prompt)) + .context("expect multi-select prompt")?; + } + Ok(self) + } + + pub fn succeeds_with( + &mut self, + chosen_indexes: Vec, + report: bool, + default_indexes: Vec, + ) -> Result<&mut Self> { + if report { + bail!("TODO: add support for report") + } + + fn show_options( + tester: &mut CliTester, + choices: &[String], + active_index: usize, + selected_indexes: &[usize], + ) -> Result<()> { + for (index, item) in choices.iter().enumerate() { + tester.expect(format!( + "{}{}{}\r\n", + if active_index.eq(&index) { "> " } else { " " }, + if selected_indexes.iter().any(|i| i.eq(&index)) { + "[x] " + } else { + "[ ] " + }, + item, + ))?; + } + Ok(()) + } + + show_options(self.tester, &self.choices, 0, &default_indexes)?; + + if default_indexes.eq(&chosen_indexes) { + self.tester.send("\r\n")?; + } else { + bail!("TODO: add support changing options"); + } + + for _ in self.choices.iter() { + self.tester.expect("\r")?; + } + // one for removing prompt maybe? + self.tester.expect("\r")?; + + Ok(self) + } +} + pub struct CliTesterChoicePrompt<'a> { tester: &'a mut CliTester, prompt: String, -- cgit v1.2.3