From 1e97559d1f93a891afa9e1942ee6314a0ae00e84 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 10 Aug 2024 11:05:19 +0530 Subject: [PATCH] feat(tests): include tests for bare executable matching --- lib/sources/artifact/format.rs | 17 +++++++++++++--- lib/sources/artifact/sorting.rs | 2 ++ lib/sources/artifact/util.rs | 35 ++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/sources/artifact/format.rs b/lib/sources/artifact/format.rs index 4b8b24a..9830b37 100644 --- a/lib/sources/artifact/format.rs +++ b/lib/sources/artifact/format.rs @@ -38,7 +38,7 @@ impl ArtifactFormat { Some(Self::TarGz) } [.., ext] if ext.eq_ignore_ascii_case("exe") => Some(Self::Pe), - [] => Some(Self::Elf), + [""] => Some(Self::Elf), _ => None, } } @@ -102,8 +102,8 @@ mod tests { #[test] fn format_from_extensions_invalid() { - assert_eq!(format_from_str("file-name"), None); - assert_eq!(format_from_str("some/file.exe"), None); + assert_eq!(format_from_str("file-name.txt"), None); + assert_eq!(format_from_str("some/file.mp4"), None); assert_eq!(format_from_str("really.long.file.name"), None); } @@ -125,6 +125,14 @@ mod tests { format_from_str("sentry-cli-linux-i686-2.32.1.tgz"), Some(ArtifactFormat::TarGz) ); + assert_eq!( + format_from_str("sentry-cli-Linux-x86_64"), + Some(ArtifactFormat::Elf) + ); + assert_eq!( + format_from_str("sentry-cli-Windows-x86_64.exe"), + Some(ArtifactFormat::Pe) + ); } #[test] @@ -138,5 +146,8 @@ mod tests { assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz)); assert_eq!(format_from_str("file.TAR.GZ"), Some(ArtifactFormat::TarGz)); assert_eq!(format_from_str("file.Tar.Gz"), Some(ArtifactFormat::TarGz)); + assert_eq!(format_from_str("file.exe"), Some(ArtifactFormat::Pe)); + assert_eq!(format_from_str("file.EXE"), Some(ArtifactFormat::Pe)); + assert_eq!(format_from_str("file"), Some(ArtifactFormat::Elf)); } } diff --git a/lib/sources/artifact/sorting.rs b/lib/sources/artifact/sorting.rs index e9208c0..376a8da 100644 --- a/lib/sources/artifact/sorting.rs +++ b/lib/sources/artifact/sorting.rs @@ -161,6 +161,8 @@ mod tests { test_some_mentions("selene-light-0.27.1-linux.zip", "selene"); // Valid - but multiple words test_no_mentions("sentry-cli-linux-i686-2.32.1", "sentry-cli"); + test_no_mentions("sentry-cli-Windows-i686.exe", "sentry-cli"); + test_no_mentions("sentry-cli-Linux-i686", "sentry-cli"); test_no_mentions("selene-light-0.27.1-linux.zip", "selene-light"); } } diff --git a/lib/sources/artifact/util.rs b/lib/sources/artifact/util.rs index 966bbfc..e7445a8 100644 --- a/lib/sources/artifact/util.rs +++ b/lib/sources/artifact/util.rs @@ -9,25 +9,36 @@ pub(super) fn split_filename_and_extensions(name: &str) -> (&str, Vec<&str>) { // Reverse-pop extensions off the path until we reach the // base name - we will then need to reverse afterwards, too - while let Some(ext) = path.extension() { - let ext = ext.to_str().expect("input was str"); - let stem = path.file_stem().expect("had an extension"); + loop { + if let Some(ext) = path.extension() { + let ext = ext.to_str().expect("input was str"); + println!("Ext: {ext}"); + let stem = path.file_stem().expect("had an extension"); - if !ALLOWED_EXTENSION_NAMES - .iter() - .any(|e| e.eq_ignore_ascii_case(ext)) - { - break; - } + if !ALLOWED_EXTENSION_NAMES + .iter() + .any(|e| e.eq_ignore_ascii_case(ext)) + { + break; + } - exts.push(ext); - path = Path::new(stem); + exts.push(ext); + path = Path::new(stem); - if exts.len() >= ALLOWED_EXTENSION_COUNT { + if exts.len() >= ALLOWED_EXTENSION_COUNT { + break; + } + } else { + // Push an empty string if there are no more extensions in the path + exts.push(""); break; } } + if exts.len() > 1 && exts.contains(&"") { + exts.pop(); + } + exts.reverse(); let path = path.to_str().expect("input was str");