Skip to content

Commit

Permalink
graphics/image: Test only the actual testfiles
Browse files Browse the repository at this point in the history
Previously, we simply tried to parse all the files
in the test dir, which includes the index.html file.

Now, we only test files that have a .bmp file extension.
  • Loading branch information
simonwuelker committed Feb 17, 2024
1 parent a1c66a7 commit 7f1a067
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions crates/graphics/image/tests/bmp-testsuite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs;
use std::{fs, path::Path};

use image::DynamicTexture;

Expand All @@ -12,44 +12,41 @@ mod images {
#[test]
fn valid_images() {
// Assert that all valid images can be parsed
for entry in fs::read_dir(images::VALID).expect("Cannot read dir") {
let entry = entry.expect("Cannot read entry");
let path = entry.path();
if path.is_file() {
println!("Testing {:?}", path.file_name().unwrap_or_default());
let bytes = fs::read(path).expect("Cannot read image file");
let result = DynamicTexture::from_bytes(&bytes);
assert!(result.is_ok());
}
for test_file in test_files(images::VALID) {
let result = DynamicTexture::from_bytes(&test_file);
assert!(result.is_ok());
}
}

#[test]
fn corrupt_images() {
// Assert that all corrupt images fail to parse without crashing
for entry in fs::read_dir(images::CORRUPT).expect("Cannot read dir") {
let entry = entry.expect("Cannot read entry");
let path = entry.path();
if path.is_file() {
println!("Testing {:?}", path.file_name().unwrap_or_default());
let bytes = fs::read(path).expect("Cannot read image file");
let result = DynamicTexture::from_bytes(&bytes);
assert!(result.is_err());
}
for test_file in test_files(images::CORRUPT) {
let result = DynamicTexture::from_bytes(&test_file);
assert!(result.is_err());
}
}

#[test]
fn questionable_images() {
// Assert that no questionable images cause crashes
// (Whether or not parsing succeeds is not specified)
for entry in fs::read_dir(images::QUESTIONABLE).expect("Cannot read dir") {
let entry = entry.expect("Cannot read entry");
let path = entry.path();
if path.is_file() {
println!("Testing {:?}", path.file_name().unwrap_or_default());
let bytes = fs::read(path).expect("Cannot read image file");
let _ = DynamicTexture::from_bytes(&bytes);
}
for test_file in test_files(images::QUESTIONABLE) {
let _ = DynamicTexture::from_bytes(&test_file);
}
}

fn test_files<P>(path: P) -> impl Iterator<Item = Vec<u8>>
where
P: AsRef<Path>,
{
fs::read_dir(path)
.expect("Cannot read dir")
.into_iter()
.map(|entry| entry.expect("Cannot read entry"))
.map(|entry| entry.path())
.filter(|path| path.is_file())
.filter(|path| path.extension().is_some_and(|ext| ext == "bmp"))
.inspect(|path| println!("Testing {:?}", path.file_name().unwrap_or_default()))
.map(|path| fs::read(path).expect("Cannot read image file"))
}

0 comments on commit 7f1a067

Please sign in to comment.