Skip to content

Commit 8cf1f8d

Browse files
committed
Revert and add test
1 parent dba44f3 commit 8cf1f8d

File tree

5 files changed

+88
-178
lines changed

5 files changed

+88
-178
lines changed

clap-v3-utils/src/input_parsers/signer.rs

Lines changed: 26 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl SignerSource {
9494
matches: &ArgMatches,
9595
name: &str,
9696
) -> Result<Option<Keypair>, Box<dyn error::Error>> {
97-
let source = matches.try_get_one::<Self>(name).ok().flatten();
97+
let source = matches.try_get_one::<Self>(name)?;
9898
if let Some(source) = source {
9999
keypair_from_source(matches, source, name, true).map(Some)
100100
} else {
@@ -106,7 +106,7 @@ impl SignerSource {
106106
matches: &ArgMatches,
107107
name: &str,
108108
) -> Result<Option<Vec<Keypair>>, Box<dyn error::Error>> {
109-
let sources = matches.try_get_many::<Self>(name).ok().flatten();
109+
let sources = matches.try_get_many::<Self>(name)?;
110110
if let Some(sources) = sources {
111111
let keypairs = sources
112112
.filter_map(|source| keypair_from_source(matches, source, name, true).ok())
@@ -123,7 +123,7 @@ impl SignerSource {
123123
name: &str,
124124
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
125125
) -> Result<Option<(Box<dyn Signer>, Pubkey)>, Box<dyn error::Error>> {
126-
let source = matches.try_get_one::<Self>(name).ok().flatten();
126+
let source = matches.try_get_one::<Self>(name)?;
127127
if let Some(source) = source {
128128
let signer = signer_from_source(matches, source, name, wallet_manager)?;
129129
let signer_pubkey = signer.pubkey();
@@ -139,7 +139,7 @@ impl SignerSource {
139139
name: &str,
140140
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
141141
) -> Result<Option<Vec<(Box<dyn Signer>, Pubkey)>>, Box<dyn error::Error>> {
142-
let sources = matches.try_get_many::<Self>(name).ok().flatten();
142+
let sources = matches.try_get_many::<Self>(name)?;
143143
if let Some(sources) = sources {
144144
let signers = sources
145145
.filter_map(|source| {
@@ -159,7 +159,7 @@ impl SignerSource {
159159
name: &str,
160160
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
161161
) -> Result<Option<Pubkey>, Box<dyn error::Error>> {
162-
let source = matches.try_get_one::<Self>(name).ok().flatten();
162+
let source = matches.try_get_one::<Self>(name)?;
163163
if let Some(source) = source {
164164
pubkey_from_source(matches, source, name, wallet_manager).map(Some)
165165
} else {
@@ -172,7 +172,7 @@ impl SignerSource {
172172
name: &str,
173173
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
174174
) -> Result<Option<Vec<Pubkey>>, Box<dyn std::error::Error>> {
175-
let sources = matches.try_get_many::<Self>(name).ok().flatten();
175+
let sources = matches.try_get_many::<Self>(name)?;
176176
if let Some(sources) = sources {
177177
let pubkeys = sources
178178
.filter_map(|source| pubkey_from_source(matches, source, name, wallet_manager).ok())
@@ -188,7 +188,7 @@ impl SignerSource {
188188
name: &str,
189189
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
190190
) -> Result<Option<String>, Box<dyn std::error::Error>> {
191-
let source = matches.try_get_one::<Self>(name).ok().flatten();
191+
let source = matches.try_get_one::<Self>(name)?;
192192
if let Some(source) = source {
193193
resolve_signer_from_source(matches, source, name, wallet_manager)
194194
} else {
@@ -343,7 +343,7 @@ pub fn try_keypair_of(
343343
matches: &ArgMatches,
344344
name: &str,
345345
) -> Result<Option<Keypair>, Box<dyn error::Error>> {
346-
if let Some(value) = matches.try_get_one::<String>(name).ok().flatten() {
346+
if let Some(value) = matches.try_get_one::<String>(name)? {
347347
extract_keypair(matches, name, value)
348348
} else {
349349
Ok(None)
@@ -354,15 +354,11 @@ pub fn try_keypairs_of(
354354
matches: &ArgMatches,
355355
name: &str,
356356
) -> Result<Option<Vec<Keypair>>, Box<dyn error::Error>> {
357-
Ok(matches
358-
.try_get_many::<String>(name)
359-
.ok()
360-
.flatten()
361-
.map(|values| {
362-
values
363-
.filter_map(|value| extract_keypair(matches, name, value).ok().flatten())
364-
.collect()
365-
}))
357+
Ok(matches.try_get_many::<String>(name)?.map(|values| {
358+
values
359+
.filter_map(|value| extract_keypair(matches, name, value).ok().flatten())
360+
.collect()
361+
}))
366362
}
367363

368364
fn extract_keypair(
@@ -371,9 +367,7 @@ fn extract_keypair(
371367
path: &str,
372368
) -> Result<Option<Keypair>, Box<dyn error::Error>> {
373369
if path == ASK_KEYWORD {
374-
let skip_validation = matches
375-
.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)
376-
.unwrap_or(false);
370+
let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?;
377371
keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some)
378372
} else {
379373
read_keypair_file(path).map(Some)
@@ -386,7 +380,7 @@ pub fn try_pubkey_of(
386380
matches: &ArgMatches,
387381
name: &str,
388382
) -> Result<Option<Pubkey>, Box<dyn error::Error>> {
389-
if let Some(pubkey) = matches.try_get_one::<Pubkey>(name).ok().flatten() {
383+
if let Some(pubkey) = matches.try_get_one::<Pubkey>(name)? {
390384
Ok(Some(*pubkey))
391385
} else {
392386
Ok(try_keypair_of(matches, name)?.map(|keypair| keypair.pubkey()))
@@ -397,7 +391,7 @@ pub fn try_pubkeys_of(
397391
matches: &ArgMatches,
398392
name: &str,
399393
) -> Result<Option<Vec<Pubkey>>, Box<dyn error::Error>> {
400-
if let Some(pubkey_strings) = matches.try_get_many::<String>(name).ok().flatten() {
394+
if let Some(pubkey_strings) = matches.try_get_many::<String>(name)? {
401395
let mut pubkeys = Vec::with_capacity(pubkey_strings.len());
402396
for pubkey_string in pubkey_strings {
403397
pubkeys.push(pubkey_string.parse::<Pubkey>()?);
@@ -430,7 +424,7 @@ pub fn try_pubkeys_sigs_of(
430424
matches: &ArgMatches,
431425
name: &str,
432426
) -> Result<Option<Vec<(Pubkey, Signature)>>, Box<dyn error::Error>> {
433-
if let Some(pubkey_signer_strings) = matches.try_get_many::<String>(name).ok().flatten() {
427+
if let Some(pubkey_signer_strings) = matches.try_get_many::<String>(name)? {
434428
let mut pubkey_sig_pairs = Vec::with_capacity(pubkey_signer_strings.len());
435429
for pubkey_signer_string in pubkey_signer_strings {
436430
let (pubkey_string, sig_string) = pubkey_signer_string
@@ -453,7 +447,7 @@ pub fn signer_of(
453447
name: &str,
454448
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
455449
) -> Result<(Option<Box<dyn Signer>>, Option<Pubkey>), Box<dyn std::error::Error>> {
456-
if let Some(location) = matches.try_get_one::<String>(name).ok().flatten() {
450+
if let Some(location) = matches.try_get_one::<String>(name)? {
457451
let signer = signer_from_path(matches, location, name, wallet_manager)?;
458452
let signer_pubkey = signer.pubkey();
459453
Ok((Some(signer), Some(signer_pubkey)))
@@ -467,7 +461,7 @@ pub fn pubkey_of_signer(
467461
name: &str,
468462
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
469463
) -> Result<Option<Pubkey>, Box<dyn std::error::Error>> {
470-
if let Some(location) = matches.try_get_one::<String>(name).ok().flatten() {
464+
if let Some(location) = matches.try_get_one::<String>(name)? {
471465
Ok(Some(pubkey_from_path(
472466
matches,
473467
location,
@@ -484,7 +478,7 @@ pub fn pubkeys_of_multiple_signers(
484478
name: &str,
485479
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
486480
) -> Result<Option<Vec<Pubkey>>, Box<dyn std::error::Error>> {
487-
if let Some(pubkey_matches) = matches.try_get_many::<String>(name).ok().flatten() {
481+
if let Some(pubkey_matches) = matches.try_get_many::<String>(name)? {
488482
let mut pubkeys: Vec<Pubkey> = vec![];
489483
for signer in pubkey_matches {
490484
pubkeys.push(pubkey_from_path(matches, signer, name, wallet_manager)?);
@@ -500,12 +494,12 @@ pub fn resolve_signer(
500494
name: &str,
501495
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
502496
) -> Result<Option<String>, Box<dyn std::error::Error>> {
503-
let path = matches
504-
.try_get_one::<String>(name)
505-
.ok()
506-
.flatten()
507-
.ok_or_else(|| format!("Missing required path argument: {}", name))?;
508-
resolve_signer_from_path(matches, path, name, wallet_manager)
497+
resolve_signer_from_path(
498+
matches,
499+
matches.try_get_one::<String>(name)?.unwrap(),
500+
name,
501+
wallet_manager,
502+
)
509503
}
510504

511505
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -703,20 +697,6 @@ mod tests {
703697
fs::remove_file(&outfile).unwrap();
704698
}
705699

706-
#[test]
707-
fn test_try_keypair_of() {
708-
let matches = ArgMatches::default();
709-
let result = try_keypair_of(&matches, "test").unwrap();
710-
assert!(result.is_none());
711-
}
712-
713-
#[test]
714-
fn test_try_keypairs_of() {
715-
let matches = ArgMatches::default();
716-
let result = try_keypairs_of(&matches, "test").unwrap();
717-
assert!(result.is_none());
718-
}
719-
720700
#[test]
721701
fn test_pubkey_of() {
722702
let keypair = Keypair::new();
@@ -757,20 +737,6 @@ mod tests {
757737
fs::remove_file(&outfile).unwrap();
758738
}
759739

760-
#[test]
761-
fn test_try_pubkeys_of() {
762-
let matches = ArgMatches::default();
763-
let result = try_pubkeys_of(&matches, "test").unwrap();
764-
assert!(result.is_none());
765-
}
766-
767-
#[test]
768-
fn test_pubkeys_of_multiple_signers() {
769-
let matches = ArgMatches::default();
770-
let result = pubkeys_of_multiple_signers(&matches, "test", &mut None).unwrap();
771-
assert!(result.is_none());
772-
}
773-
774740
#[test]
775741
fn test_pubkeys_sigs_of() {
776742
let key1 = solana_pubkey::new_rand();
@@ -787,13 +753,6 @@ mod tests {
787753
);
788754
}
789755

790-
#[test]
791-
fn test_try_pubkeys_sigs_of() {
792-
let matches = ArgMatches::default();
793-
let result = try_pubkeys_sigs_of(&matches, "test").unwrap();
794-
assert!(result.is_none());
795-
}
796-
797756
#[test]
798757
fn test_parse_pubkey_signature() {
799758
let command = Command::new("test").arg(
@@ -1089,74 +1048,4 @@ mod tests {
10891048
.unwrap_err();
10901049
assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation);
10911050
}
1092-
1093-
#[test]
1094-
fn test_signer_of() {
1095-
let matches = ArgMatches::default();
1096-
let result = signer_of(&matches, "test", &mut None).unwrap();
1097-
assert_eq!(result, (None, None));
1098-
}
1099-
1100-
#[test]
1101-
fn test_try_get_signer() {
1102-
let matches = ArgMatches::default();
1103-
let result = SignerSource::try_get_signer(&matches, "test", &mut None).unwrap();
1104-
assert!(result.is_none());
1105-
}
1106-
1107-
#[test]
1108-
fn test_try_get_signers() {
1109-
let matches = ArgMatches::default();
1110-
let result = SignerSource::try_get_signers(&matches, "test", &mut None).unwrap();
1111-
assert!(result.is_none());
1112-
}
1113-
1114-
#[test]
1115-
fn test_try_get_keypair() {
1116-
let matches = ArgMatches::default();
1117-
let result = SignerSource::try_get_keypair(&matches, "test").unwrap();
1118-
assert!(result.is_none());
1119-
}
1120-
1121-
#[test]
1122-
fn test_try_get_keypairs() {
1123-
let matches = ArgMatches::default();
1124-
let result = SignerSource::try_get_keypairs(&matches, "test").unwrap();
1125-
assert!(result.is_none());
1126-
}
1127-
1128-
#[test]
1129-
fn test_pubkey_of_signer() {
1130-
let matches = ArgMatches::default();
1131-
let result = pubkey_of_signer(&matches, "test", &mut None).unwrap();
1132-
assert!(result.is_none());
1133-
}
1134-
1135-
#[test]
1136-
fn test_try_pubkey_of() {
1137-
let matches = ArgMatches::default();
1138-
let result = try_pubkey_of(&matches, "test").unwrap();
1139-
assert!(result.is_none());
1140-
}
1141-
1142-
#[test]
1143-
fn test_try_get_pubkey() {
1144-
let matches = ArgMatches::default();
1145-
let result = SignerSource::try_get_pubkey(&matches, "test", &mut None).unwrap();
1146-
assert!(result.is_none());
1147-
}
1148-
1149-
#[test]
1150-
fn test_try_get_pubkeys() {
1151-
let matches = ArgMatches::default();
1152-
let result = SignerSource::try_get_pubkeys(&matches, "test", &mut None).unwrap();
1153-
assert!(result.is_none());
1154-
}
1155-
1156-
#[test]
1157-
fn test_try_resolve() {
1158-
let matches = ArgMatches::default();
1159-
let result = SignerSource::try_resolve(&matches, "test", &mut None).unwrap();
1160-
assert!(result.is_none());
1161-
}
11621051
}

clap-v3-utils/src/keygen/derivation_path.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn derivation_path_arg<'a>() -> Arg<'a> {
2222
pub fn acquire_derivation_path(
2323
matches: &ArgMatches,
2424
) -> Result<Option<DerivationPath>, Box<dyn error::Error>> {
25-
if matches.try_contains_id("derivation_path").unwrap_or(false) {
25+
if matches.try_contains_id("derivation_path")? {
2626
Ok(Some(DerivationPath::from_absolute_path_str(
2727
matches
2828
.try_get_one::<String>("derivation_path")?
@@ -33,15 +33,3 @@ pub fn acquire_derivation_path(
3333
Ok(None)
3434
}
3535
}
36-
37-
#[cfg(test)]
38-
mod tests {
39-
use super::*;
40-
41-
#[test]
42-
fn test_acquire_derivation_path() {
43-
let matches = ArgMatches::default();
44-
let result = acquire_derivation_path(&matches).unwrap();
45-
assert!(result.is_none());
46-
}
47-
}

clap-v3-utils/src/keygen/mnemonic.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ pub fn no_passphrase_and_message() -> (String, String) {
121121
pub fn acquire_passphrase_and_message(
122122
matches: &ArgMatches,
123123
) -> Result<(String, String), Box<dyn error::Error>> {
124-
if matches
125-
.try_contains_id(NO_PASSPHRASE_ARG.name)
126-
.unwrap_or(false)
127-
{
124+
if matches.try_contains_id(NO_PASSPHRASE_ARG.name)? {
128125
Ok(no_passphrase_and_message())
129126
} else {
130127
match prompt_passphrase(

clap-v3-utils/src/keygen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn check_for_overwrite(
3838
outfile: &str,
3939
matches: &ArgMatches,
4040
) -> Result<(), Box<dyn error::Error>> {
41-
let force = matches.try_contains_id("force").unwrap_or(false);
41+
let force = matches.try_contains_id("force")?;
4242
if !force && Path::new(outfile).exists() {
4343
let err_msg = format!("Refusing to overwrite {outfile} without --force flag");
4444
return Err(err_msg.into());

0 commit comments

Comments
 (0)