mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Automatically provide a resource reader for tests
This commit is contained in:
parent
bf667677f7
commit
e02a23b2f6
20 changed files with 72 additions and 109 deletions
|
@ -2,14 +2,18 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Once, ONCE_INIT, RwLock};
|
||||
use std::sync::RwLock;
|
||||
|
||||
lazy_static! {
|
||||
static ref RES: RwLock<Option<Box<ResourceReaderMethods + Sync + Send>>> = RwLock::new(None);
|
||||
static ref RES: RwLock<Option<Box<ResourceReaderMethods + Sync + Send>>> = RwLock::new({
|
||||
#[cfg(not(feature = "tests"))] {
|
||||
None
|
||||
}
|
||||
#[cfg(feature = "tests")] {
|
||||
Some(resources_for_tests())
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn set(reader: Box<ResourceReaderMethods + Sync + Send>) {
|
||||
|
@ -53,45 +57,45 @@ pub trait ResourceReaderMethods {
|
|||
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf>;
|
||||
}
|
||||
|
||||
static INIT: Once = ONCE_INIT;
|
||||
|
||||
pub fn register_resources_for_tests() {
|
||||
INIT.call_once(|| {
|
||||
struct ResourceReader;
|
||||
impl ResourceReaderMethods for ResourceReader {
|
||||
fn sandbox_access_files(&self) -> Vec<PathBuf> { vec![] }
|
||||
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { vec![] }
|
||||
fn read(&self, file: Resource) -> Vec<u8> {
|
||||
let file = match file {
|
||||
Resource::Preferences => "prefs.json",
|
||||
Resource::BluetoothBlocklist => "gatt_blocklist.txt",
|
||||
Resource::DomainList => "public_domains.txt",
|
||||
Resource::HstsPreloadList => "hsts_preload.json",
|
||||
Resource::SSLCertificates => "certs",
|
||||
Resource::BadCertHTML => "badcert.html",
|
||||
Resource::NetErrorHTML => "neterror.html",
|
||||
Resource::UserAgentCSS => "user-agent.css",
|
||||
Resource::ServoCSS => "servo.css",
|
||||
Resource::PresentationalHintsCSS => "presentational-hints.css",
|
||||
Resource::QuirksModeCSS => "quirks-mode.css",
|
||||
Resource::RippyPNG => "rippy.png",
|
||||
};
|
||||
let mut path = env::current_exe().unwrap();
|
||||
path = path.canonicalize().unwrap();
|
||||
while path.pop() {
|
||||
path.push("resources");
|
||||
if path.is_dir() {
|
||||
break;
|
||||
}
|
||||
path.pop();
|
||||
#[cfg(feature = "tests")]
|
||||
fn resources_for_tests() -> Box<ResourceReaderMethods + Sync + Send> {
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
struct ResourceReader;
|
||||
impl ResourceReaderMethods for ResourceReader {
|
||||
fn sandbox_access_files(&self) -> Vec<PathBuf> { vec![] }
|
||||
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { vec![] }
|
||||
fn read(&self, file: Resource) -> Vec<u8> {
|
||||
let file = match file {
|
||||
Resource::Preferences => "prefs.json",
|
||||
Resource::BluetoothBlocklist => "gatt_blocklist.txt",
|
||||
Resource::DomainList => "public_domains.txt",
|
||||
Resource::HstsPreloadList => "hsts_preload.json",
|
||||
Resource::SSLCertificates => "certs",
|
||||
Resource::BadCertHTML => "badcert.html",
|
||||
Resource::NetErrorHTML => "neterror.html",
|
||||
Resource::UserAgentCSS => "user-agent.css",
|
||||
Resource::ServoCSS => "servo.css",
|
||||
Resource::PresentationalHintsCSS => "presentational-hints.css",
|
||||
Resource::QuirksModeCSS => "quirks-mode.css",
|
||||
Resource::RippyPNG => "rippy.png",
|
||||
};
|
||||
let mut path = env::current_exe().unwrap();
|
||||
path = path.canonicalize().unwrap();
|
||||
while path.pop() {
|
||||
path.push("resources");
|
||||
if path.is_dir() {
|
||||
break;
|
||||
}
|
||||
path.push(file);
|
||||
let mut buffer = vec![];
|
||||
File::open(path).expect(&format!("Can't find file: {}", file))
|
||||
.read_to_end(&mut buffer).expect("Can't read file");
|
||||
buffer
|
||||
path.pop();
|
||||
}
|
||||
path.push(file);
|
||||
let mut buffer = vec![];
|
||||
File::open(path).expect(&format!("Can't find file: {}", file))
|
||||
.read_to_end(&mut buffer).expect("Can't read file");
|
||||
buffer
|
||||
}
|
||||
set(Box::new(ResourceReader));
|
||||
});
|
||||
}
|
||||
Box::new(ResourceReader)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue