Skip to content

Commit

Permalink
Simplify hostname conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
raindev committed Mar 26, 2024
1 parent c3cd2ba commit f06aa3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,10 +1456,10 @@ impl Config {
#[cfg(target_os = "linux")]
str_value!(linux, emerge_update_flags);

pub fn should_execute_remote(&self, hostname: Option<&str>, remote: &str) -> bool {
pub fn should_execute_remote(&self, hostname: Result<String>, remote: &str) -> bool {
let remote_host = remote.split_once('@').map_or(remote, |(_, host)| host);

if let Some(hostname) = hostname {
if let Ok(hostname) = hostname {
if remote_host == hostname {
return false;
}
Expand Down Expand Up @@ -1523,6 +1523,8 @@ impl Config {

#[cfg(test)]
mod test {

use color_eyre::eyre::eyre;
use crate::config::*;

Check warning on line 1528 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L1526-L1528

Added lines #L1526 - L1528 were not covered by tests

/// Test the default configuration in `config.example.toml` is valid.
Expand All @@ -1543,40 +1545,40 @@ mod test {

#[test]
fn test_should_execute_remote_different_hostname() {
assert!(config().should_execute_remote(Some("hostname"), "remote_hostname"))
assert!(config().should_execute_remote(Ok("hostname".to_string()), "remote_hostname"))
}

#[test]
fn test_should_execute_remote_different_hostname_with_user() {
assert!(config().should_execute_remote(Some("hostname"), "user@remote_hostname"))
assert!(config().should_execute_remote(Ok("hostname".to_string()), "user@remote_hostname"))
}

#[test]
fn test_should_execute_remote_unknown_hostname() {
assert!(config().should_execute_remote(None, "remote_hostname"))
assert!(config().should_execute_remote(Err(eyre!("failed to get hostname")), "remote_hostname"))
}

#[test]
fn test_should_not_execute_remote_same_hostname() {
assert!(!config().should_execute_remote(Some("hostname"), "hostname"))
assert!(!config().should_execute_remote(Ok("hostname".to_string()), "hostname"))
}

#[test]
fn test_should_not_execute_remote_same_hostname_with_user() {
assert!(!config().should_execute_remote(Some("hostname"), "user@hostname"))
assert!(!config().should_execute_remote(Ok("hostname".to_string()), "user@hostname"))
}

#[test]
fn test_should_execute_remote_matching_limit() {
let mut config = config();
config.opt = CommandLineArgs::parse_from(["topgrade", "--remote-host-limit", "remote_hostname"]);
assert!(config.should_execute_remote(Some("hostname"), "user@remote_hostname"))
assert!(config.should_execute_remote(Ok("hostname".to_string()), "user@remote_hostname"))
}

#[test]
fn test_should_not_execute_remote_not_matching_limit() {
let mut config = config();
config.opt = CommandLineArgs::parse_from(["topgrade", "--remote-host-limit", "other_hostname"]);
assert!(!config.should_execute_remote(Some("hostname"), "user@remote_hostname"))
assert!(!config.should_execute_remote(Ok("hostname".to_string()), "user@remote_hostname"))
}
}
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ fn run() -> Result<()> {
}

if let Some(topgrades) = config.remote_topgrades() {
let hostname_res = hostname();
let hostname: Option<&str> = hostname_res.as_ref().ok().map(|s| s.as_str());
for remote_topgrade in topgrades.iter().filter(|t| config.should_execute_remote(hostname, t)) {
for remote_topgrade in topgrades.iter().filter(|t| config.should_execute_remote(hostname(), t)) {

Check warning on line 185 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L185

Added line #L185 was not covered by tests
runner.execute(Step::Remotes, format!("Remote ({remote_topgrade})"), || {
ssh::ssh_step(&ctx, remote_topgrade)
})?;
Expand Down

0 comments on commit f06aa3c

Please sign in to comment.