Move to new path and io APIs in reftest.rs.

This commit is contained in:
Ms2ger 2015-03-21 13:29:49 +01:00
parent 2fde574b39
commit 061d0c94ec

View file

@ -7,25 +7,24 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#![feature(collections, core, env, io, hash, os, path, std_misc, test)] #![feature(collections, core, exit_status, fs_walk, io, old_io, path, path_ext, std_misc, test)]
#[macro_use] extern crate bitflags; #[macro_use] extern crate bitflags;
extern crate png; extern crate png;
extern crate test; extern crate test;
extern crate url; extern crate url;
use std::ascii::AsciiExt;
use std::env; use std::env;
use std::old_io as io; use std::ffi::OsStr;
use std::old_io::{File, Reader, Command, IoResult}; use std::fs::{PathExt, File, walk_dir};
use std::old_io::process::ExitStatus; use std::io::Read;
use std::old_io::fs::PathExtensions; use std::old_io::{Reader, IoResult};
use std::old_path::Path; use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thunk::Thunk; use std::thunk::Thunk;
use test::{AutoColor, DynTestName, DynTestFn, TestDesc, TestOpts, TestDescAndFn, ShouldPanic}; use test::{AutoColor, DynTestName, DynTestFn, TestDesc, TestOpts, TestDescAndFn, ShouldPanic};
use test::run_tests_console; use test::run_tests_console;
use url::Url; use url::Url;
bitflags!( bitflags!(
flags RenderMode: u32 { flags RenderMode: u32 {
const CPU_RENDERING = 0x00000001, const CPU_RENDERING = 0x00000001,
@ -68,11 +67,12 @@ fn main() {
let mut all_tests = vec!(); let mut all_tests = vec!();
println!("Scanning {} for manifests\n", base_path); println!("Scanning {} for manifests\n", base_path);
for file in io::fs::walk_dir(&Path::new(base_path)).unwrap() { for file in walk_dir(base_path).unwrap() {
let maybe_extension = file.extension_str(); let file = file.unwrap().path();
let maybe_extension = file.extension();
match maybe_extension { match maybe_extension {
Some(extension) => { Some(extension) => {
if extension.to_ascii_lowercase().as_slice() == "list" && file.is_file() { if extension == OsStr::from_str("list") && file.is_file() {
let mut tests = parse_lists(&file, servo_args, render_mode, all_tests.len()); let mut tests = parse_lists(&file, servo_args, render_mode, all_tests.len());
println!("\t{} [{} tests]", file.display(), tests.len()); println!("\t{} [{} tests]", file.display(), tests.len());
all_tests.append(&mut tests); all_tests.append(&mut tests);
@ -106,12 +106,11 @@ fn run(test_opts: TestOpts, all_tests: Vec<TestDescAndFn>,
// Verify that we're passing in valid servo arguments. Otherwise, servo // Verify that we're passing in valid servo arguments. Otherwise, servo
// will exit before we've run any tests, and it will appear to us as if // will exit before we've run any tests, and it will appear to us as if
// all the tests are failing. // all the tests are failing.
let mut command = match Command::new(servo_path()) let output = match Command::new(&servo_path()).args(&servo_args).output() {
.args(servo_args.as_slice()).spawn() {
Ok(p) => p, Ok(p) => p,
Err(e) => panic!("failed to execute process: {}", e), Err(e) => panic!("failed to execute process: {}", e),
}; };
let stderr = command.stderr.as_mut().unwrap().read_to_string().unwrap(); let stderr = String::from_utf8(output.stderr).unwrap();
if stderr.as_slice().contains("Unrecognized") { if stderr.as_slice().contains("Unrecognized") {
println!("Servo: {}", stderr.as_slice()); println!("Servo: {}", stderr.as_slice());
@ -130,7 +129,7 @@ enum ReftestKind {
struct Reftest { struct Reftest {
name: String, name: String,
kind: ReftestKind, kind: ReftestKind,
files: [Path; 2], files: [PathBuf; 2],
id: usize, id: usize,
servo_args: Vec<String>, servo_args: Vec<String>,
render_mode: RenderMode, render_mode: RenderMode,
@ -149,9 +148,12 @@ struct TestLine<'a> {
fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_offset: usize) -> Vec<TestDescAndFn> { fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_offset: usize) -> Vec<TestDescAndFn> {
let mut tests = Vec::new(); let mut tests = Vec::new();
let contents = File::open_mode(file, io::Open, io::Read) let contents = {
.and_then(|mut f| f.read_to_string()) let mut f = File::open(file).unwrap();
.ok().expect("Could not read file"); let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();
contents
};
for line in contents.as_slice().lines() { for line in contents.as_slice().lines() {
// ignore comments or empty lines // ignore comments or empty lines
@ -183,7 +185,7 @@ fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_o
part => panic!("reftest line: '{}' has invalid kind '{}'", line, part) part => panic!("reftest line: '{}' has invalid kind '{}'", line, part)
}; };
let base = Path::new(env::current_dir().unwrap().to_str().unwrap()).join(file.dir_path()); let base = env::current_dir().unwrap().join(file.parent().unwrap());
let file_left = base.join(test_line.file_left); let file_left = base.join(test_line.file_left);
let file_right = base.join(test_line.file_right); let file_right = base.join(test_line.file_right);
@ -244,16 +246,18 @@ fn make_test(reftest: Reftest) -> TestDescAndFn {
fn capture(reftest: &Reftest, side: usize) -> (u32, u32, Vec<u8>) { fn capture(reftest: &Reftest, side: usize) -> (u32, u32, Vec<u8>) {
let png_filename = format!("/tmp/servo-reftest-{:06}-{}.png", reftest.id, side); let png_filename = format!("/tmp/servo-reftest-{:06}-{}.png", reftest.id, side);
let mut command = Command::new(servo_path()); let mut command = Command::new(&servo_path());
command command
.stdout(Stdio::null())
.stderr(Stdio::null())
.args(&reftest.servo_args[..]) .args(&reftest.servo_args[..])
// Allows pixel perfect rendering of Ahem font for reftests. // Allows pixel perfect rendering of Ahem font for reftests.
.arg("-Z") .arg("-Z")
.arg("disable-text-aa") .arg("disable-text-aa")
.args(["-f", "-o"].as_slice()) .args(["-f", "-o"].as_slice())
.arg(png_filename.as_slice()) .arg(png_filename.as_slice())
.arg({ .arg(&{
let mut url = Url::from_file_path(&reftest.files[side]).unwrap(); let mut url = Url::from_file_path(&*reftest.files[side]).unwrap();
url.fragment = reftest.fragment_identifier.clone(); url.fragment = reftest.fragment_identifier.clone();
url.to_string() url.to_string()
}); });
@ -275,7 +279,7 @@ fn capture(reftest: &Reftest, side: usize) -> (u32, u32, Vec<u8>) {
Ok(status) => status, Ok(status) => status,
Err(e) => panic!("failed to execute process: {}", e), Err(e) => panic!("failed to execute process: {}", e),
}; };
assert_eq!(retval, ExitStatus(0)); assert!(retval.success());
let image = png::load_png(&png_filename).unwrap(); let image = png::load_png(&png_filename).unwrap();
let rgba8_bytes = match image.pixels { let rgba8_bytes = match image.pixels {
@ -285,9 +289,9 @@ fn capture(reftest: &Reftest, side: usize) -> (u32, u32, Vec<u8>) {
(image.width, image.height, rgba8_bytes) (image.width, image.height, rgba8_bytes)
} }
fn servo_path() -> Path { fn servo_path() -> PathBuf {
let current_exe = env::current_exe().ok().expect("Could not locate current executable"); let current_exe = env::current_exe().ok().expect("Could not locate current executable");
Path::new(current_exe.to_str().unwrap()).dir_path().join("servo") current_exe.parent().unwrap().join("servo")
} }
fn check_reftest(reftest: Reftest) { fn check_reftest(reftest: Reftest) {