oxipng/tests/flags.rs
Joshua Holmer aa80965196 Ensure each test has its own file
Since we are using real files and not mocks, each tests needs
its own file or else Travis will try to write multiple files
concurrently and fail.
2016-03-03 14:55:37 -05:00

86 lines
2.2 KiB
Rust

extern crate image;
extern crate oxipng;
use image::GenericImage;
use image::Pixel;
use oxipng::png;
use std::collections::HashSet;
use std::fs::remove_file;
use std::path::Path;
use std::path::PathBuf;
fn get_opts(input: &Path) -> oxipng::Options {
let mut filter = HashSet::new();
filter.insert(0);
let mut compression = HashSet::new();
compression.insert(9);
let mut memory = HashSet::new();
memory.insert(9);
let mut strategies = HashSet::new();
for i in 0..4 {
strategies.insert(i);
}
oxipng::Options {
backup: false,
out_file: input.with_extension("out.png").to_owned(),
out_dir: None,
stdout: false,
pretend: false,
recursive: false,
fix_errors: false,
force: true,
clobber: true,
create: true,
preserve_attrs: false,
verbosity: Some(0),
filter: filter,
interlace: None,
compression: compression,
memory: memory,
strategies: strategies,
window: 15,
bit_depth_reduction: true,
color_type_reduction: true,
palette_reduction: true,
idat_recoding: true,
strip: false,
}
}
#[test]
fn strip_headers() {
let input = PathBuf::from("tests/files/strip_headers.png");
let mut opts = get_opts(&input);
opts.strip = true;
let output = opts.out_file.clone();
let png = png::PngData::new(&input).unwrap();
assert!(png.aux_headers.contains_key("tEXt"));
match oxipng::optimize(&input, &opts) {
Ok(_) => (),
Err(x) => panic!(x.to_owned()),
};
assert!(output.exists());
let png = match png::PngData::new(&output) {
Ok(x) => x,
Err(x) => {
remove_file(output).ok();
panic!(x.to_owned())
}
};
assert!(!png.aux_headers.contains_key("tEXt"));
let old_png = image::open(&input).unwrap();
let new_png = image::open(&output).unwrap();
// Conversion should be lossless
assert!(old_png.pixels().map(|x| x.2.channels().to_owned()).collect::<Vec<Vec<u8>>>() ==
new_png.pixels().map(|x| x.2.channels().to_owned()).collect::<Vec<Vec<u8>>>());
remove_file(output).ok();
}