Skip to content

Commit

Permalink
test: add tests for Passphrase
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Oct 16, 2024
1 parent e0e378d commit 8ac72a6
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions maa-cli/src/config/cli/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,9 @@ impl Passphrase {
.args(&cmd[1..])
.output()?;
if output.status.success() {
String::from_utf8(output.stdout)
.map(Cow::Owned)
.map(Some)
.map_err(std::io::Error::other)
let passphrase = std::str::from_utf8(&output.stdout)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
Ok(Some(Cow::Owned(passphrase.trim().to_owned())))
} else {
Err(std::io::Error::other(
String::from_utf8(output.stderr).unwrap_or_default(),

Check warning on line 299 in maa-cli/src/config/cli/resource.rs

View check run for this annotation

Codecov / codecov/patch

maa-cli/src/config/cli/resource.rs#L298-L299

Added lines #L298 - L299 were not covered by tests
Expand Down Expand Up @@ -621,17 +620,46 @@ pub mod tests {

assert_eq!(
Remote {
certificate: Some(Certificate::SshKey {
path: PathBuf::from("~/.ssh/id_ed25519"),
passphrase: Passphrase::None,
}),
certificate: Some(Certificate::SshAgent),
..Default::default()
}
.certificate(),
Some(&Certificate::SshKey {
path: PathBuf::from("~/.ssh/id_ed25519"),
passphrase: Passphrase::None,
})
Some(&Certificate::SshAgent)
);
}

#[test]
fn passphrase() {
assert!(!Passphrase::Plain(String::from("password")).compatible_with_git());
assert!(Passphrase::Prompt.compatible_with_git());

assert_eq!(Passphrase::None.get().unwrap(), None);

assert_eq!(
Passphrase::Plain(String::from("password")).get().unwrap(),
Some(Cow::Borrowed("password"))
);

assert!(Passphrase::Env(String::from("MMA_TEST_SSH_PASSPHRASE"))
.get()
.is_err());

std::env::set_var("MMA_TEST_SSH_PASSPHRASE", "password");
assert_eq!(
Passphrase::Env(String::from("MMA_TEST_SSH_PASSPHRASE"))
.get()
.unwrap()
.unwrap(),
"password"
);
std::env::remove_var("MMA_TEST_SSH_PASSPHRASE");

assert_eq!(
Passphrase::Command(vec![String::from("echo"), String::from("password")])
.get()
.unwrap()
.unwrap(),
"password"
);
}
}

0 comments on commit 8ac72a6

Please sign in to comment.