Skip to content

Commit

Permalink
feat(tests): include tests for bare executable matching
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev committed Aug 10, 2024
1 parent 85d740b commit 1e97559
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
17 changes: 14 additions & 3 deletions lib/sources/artifact/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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]
Expand All @@ -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));
}
}
2 changes: 2 additions & 0 deletions lib/sources/artifact/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
35 changes: 23 additions & 12 deletions lib/sources/artifact/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 1e97559

Please sign in to comment.