Skip to content

Commit

Permalink
Add some tests for the case where pick_asset returns an Err
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Nov 24, 2024
1 parent 7cabd20 commit 731acbb
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions ubi/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ impl<'a> AssetPicker<'a> {
let mut matches = self.os_matches(assets);
if matches.is_empty() {
return Err(anyhow!(
"could not find a release for this OS ({}) from {all_names}",
"could not find a release asset for this OS ({}) from {all_names}",
self.platform.target_os,
));
}

matches = self.arch_matches(matches);
if matches.is_empty() {
return Err(anyhow!(
"could not find a release for this OS ({}) and architecture ({}) from {all_names}",
"could not find a release asset for this OS ({}) and architecture ({}) from {all_names}",
self.platform.target_os,
self.platform.target_arch,
));
Expand All @@ -71,7 +71,7 @@ impl<'a> AssetPicker<'a> {
matches = self.libc_matches(matches);
if matches.is_empty() {
return Err(anyhow!(
"could not find a release for this OS ({}), architecture ({}), and libc ({}) from {all_names}",
"could not find a release asset for this OS ({}), architecture ({}), and libc ({}) from {all_names}",
self.platform.target_os,
self.platform.target_arch,
self.libc_name(),
Expand Down Expand Up @@ -548,4 +548,62 @@ mod test {

Ok(())
}

#[test_case(
"x86_64-unknown-linux-gnu",
&["project-macOS-arm64.tar.gz", "project-Windows-i686-gnu.tar.gz"],
None,
"could not find a release asset for this OS (linux) from" ;
"x86_64-unknown-linux-gnu - no assets for this OS"
)]
#[test_case(
"i686-unknown-linux-gnu",
&["project-Linux-x86_64-gnu.tar.gz", "project-Windows-i686-gnu.tar.gz"],
None,
"could not find a release asset for this OS (linux) and architecture (x86) from" ;
"i686-unknown-linux-gnu - no assets for this arch"
)]
#[test_case(
"x86_64-unknown-linux-musl",
&["project-Linux-x86_64-gnu.tar.gz", "project-Windows-i686-gnu.tar.gz"],
None,
"could not find a release asset for this OS (linux), architecture (x86_64), and libc (musl) from" ;
"x86_64-unknown-linux-musl - only one Linux asset and it is gnu"
)]
fn pick_asset_errors(
platform_name: &str,
asset_names: &[&str],
matching: Option<&str>,
expect_err: &str,
) -> Result<()> {
// It'd be nice to use `test_log` but that doesn't work with the test-case crate. See
// https://github.com/frondeus/test-case/pull/143.
//
// init_logger(log::LevelFilter::Debug)?;
let platform = Platform::find(platform_name).ok_or(anyhow!("invalid platform"))?;
let mut picker = AssetPicker {
matching,
platform,
is_musl: platform_name.contains("musl"),
};

let url = Url::parse("https://example.com")?;
let assets = asset_names
.iter()
.map(|name| Asset {
name: (*name).to_string(),
url: url.clone(),
})
.collect::<Vec<_>>();

let picked_asset = picker.pick_asset(assets);
assert!(picked_asset.is_err());
assert!(picked_asset
.err()
.map(|e| e.to_string())
.unwrap_or_default()
.starts_with(expect_err));

Ok(())
}
}

0 comments on commit 731acbb

Please sign in to comment.