Skip to content

Commit

Permalink
sort artifacts by known format
Browse files Browse the repository at this point in the history
  • Loading branch information
meruiden committed Aug 28, 2024
1 parent 3f54987 commit 2d774d3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sources/artifact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod sorting;
mod util;

use self::sorting::sort_preferred_artifact;
use self::sorting::sort_prefered_formats;
use self::util::split_filename_and_extensions;

pub use self::format::ArtifactFormat;
Expand Down Expand Up @@ -180,6 +181,7 @@ impl Artifact {
current_desc
.sort_by_preferred_compat(desc_a, desc_b)
.then_with(|| sort_preferred_artifact(artifact_a, artifact_b))
.then_with(|| sort_prefered_formats(artifact_a, artifact_b))
});

compatible_artifacts
Expand Down
56 changes: 56 additions & 0 deletions lib/sources/artifact/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,21 @@ fn word_is_not_arch_or_os_or_version_or_numeric(word: impl AsRef<str>) -> bool {
&& !word.chars().all(char::is_numeric)
}

pub(super) fn sort_prefered_formats(artifact_a: &Artifact, artifact_b: &Artifact) -> Ordering {
let a_is_known = artifact_a.format.is_some();
let b_is_known = artifact_b.format.is_some();

match (a_is_known, b_is_known) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => Ordering::Equal,
}
}

#[cfg(test)]
mod tests {
use crate::sources::{ArtifactFormat, ArtifactProvider};

use super::*;

fn new_id(author: &str, name: &str) -> ToolId {
Expand Down Expand Up @@ -163,4 +176,47 @@ mod tests {
test_no_mentions("sentry-cli-linux-i686-2.32.1", "sentry-cli");
test_no_mentions("selene-light-0.27.1-linux.zip", "selene-light");
}

#[test]
fn test_prefers_known_format() {
let artifact_names = vec![
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.gz",
];

let mut artifacts = artifact_names
.into_iter()
.map(|name| Artifact {
provider: ArtifactProvider::GitHub,
format: ArtifactFormat::from_path_or_url(name),
id: Some("id".to_string()),
url: Some("https://github.com".parse().unwrap()),
name: Some(name.to_string()),
tool_spec: new_id("author", name).into_spec(Version::parse("1.0.0").unwrap()),
})
.collect::<Vec<_>>();

artifacts.sort_by(sort_prefered_formats);

let artifact_names_sorted = artifacts
.iter()
.map(|artifact| artifact.name.as_deref().unwrap())
.collect::<Vec<_>>();

assert_eq!(
artifact_names_sorted,
vec![
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.gz",
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
]
);
}
}

0 comments on commit 2d774d3

Please sign in to comment.