Skip to content

Commit 2afd920

Browse files
Fix artifact selection bug (#53)
Artifact selection uses an outer loop over all assets, and an inner loop over possible valid names. This means that it will pick the first asset that matches any valid names, instead of the best match for the earliest name in the list. The fix should just be to switch the loops!
1 parent af0b1e5 commit 2afd920

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

src/tool_cache.rs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,28 @@ use crate::{
1818
error::{ForemanError, ForemanResult},
1919
fs,
2020
paths::ForemanPaths,
21-
tool_provider::ToolProvider,
21+
tool_provider::{Release, ToolProvider},
2222
};
2323

24+
fn choose_asset(release: &Release, platform_keywords: &[&str]) -> Option<usize> {
25+
log::trace!(
26+
"Checking for name with compatible os/arch pair from platform-derived list: {:?}",
27+
platform_keywords
28+
);
29+
let asset_index = platform_keywords.iter().find_map(|keyword| {
30+
release
31+
.assets
32+
.iter()
33+
.position(|asset| asset.name.contains(keyword))
34+
})?;
35+
36+
log::debug!(
37+
"Found matching artifact: {}",
38+
release.assets[asset_index].name
39+
);
40+
Some(asset_index)
41+
}
42+
2443
/// Contains the current state of all of the tools that Foreman manages.
2544
#[derive(Debug, PartialEq, Serialize, Deserialize)]
2645
pub struct ToolCache {
@@ -109,11 +128,7 @@ impl ToolCache {
109128
Version::parse(&release.tag_name[1..]).ok()
110129
})?;
111130

112-
let asset_index = release.assets.iter().position(|asset| {
113-
platform_keywords()
114-
.iter()
115-
.any(|keyword| asset.name.contains(keyword))
116-
})?;
131+
let asset_index = choose_asset(&release, platform_keywords())?;
117132

118133
Some((version, asset_index, release))
119134
})
@@ -223,8 +238,63 @@ fn tool_identifier_to_exe_name(tool: &ToolSpec, version: &Version) -> String {
223238
mod test {
224239
use tempfile::tempdir;
225240

241+
use crate::tool_provider::ReleaseAsset;
242+
226243
use super::*;
227244

245+
// Regression test for LUAFDN-1041, based on the release that surfaced it
246+
#[test]
247+
fn select_correct_asset() {
248+
let release = Release {
249+
prerelease: false,
250+
tag_name: "v0.5.2".to_string(),
251+
assets: vec![
252+
ReleaseAsset {
253+
name: "tool-linux.zip".to_string(),
254+
url: "https://example.com/some/repo/releases/assets/1".to_string(),
255+
},
256+
ReleaseAsset {
257+
name: "tool-macos-arm64.zip".to_string(),
258+
url: "https://example.com/some/repo/releases/assets/2".to_string(),
259+
},
260+
ReleaseAsset {
261+
name: "tool-macos-x86_64.zip".to_string(),
262+
url: "https://example.com/some/repo/releases/assets/3".to_string(),
263+
},
264+
ReleaseAsset {
265+
name: "tool-win64.zip".to_string(),
266+
url: "https://example.com/some/repo/releases/assets/4".to_string(),
267+
},
268+
],
269+
};
270+
assert_eq!(
271+
choose_asset(&release, &["win32", "win64", "windows"]),
272+
Some(3)
273+
);
274+
assert_eq!(
275+
choose_asset(
276+
&release,
277+
&["macos-x86_64", "darwin-x86_64", "macos", "darwin"]
278+
),
279+
Some(2)
280+
);
281+
assert_eq!(
282+
choose_asset(
283+
&release,
284+
&[
285+
"macos-arm64",
286+
"darwin-arm64",
287+
"macos-x86_64",
288+
"darwin-x86_64",
289+
"macos",
290+
"darwin",
291+
]
292+
),
293+
Some(1)
294+
);
295+
assert_eq!(choose_asset(&release, &["linux"]), Some(0));
296+
}
297+
228298
mod load {
229299
use super::*;
230300

0 commit comments

Comments
 (0)