2020-01-17 14:27:28 +01:00
|
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2020-03-05 12:19:39 +01:00
|
|
|
|
// This file is part of Open Ethereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
|
2020-03-05 12:19:39 +01:00
|
|
|
|
// Open Ethereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
2020-03-05 12:19:39 +01:00
|
|
|
|
// Open Ethereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-03-05 12:19:39 +01:00
|
|
|
|
// along with Open Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
|
2017-07-29 23:19:33 +02:00
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use std::io::Read;
|
2017-04-12 13:33:49 +02:00
|
|
|
|
use std::fs::{File, read_dir};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::ffi::OsString;
|
2020-02-19 13:07:33 +01:00
|
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
|
pub use ethereum_types::{H256, U256, Address};
|
2017-04-12 13:33:49 +02:00
|
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
|
/// Indicate when to run the hook passed to test functions.
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
|
|
|
|
pub enum HookType {
|
|
|
|
|
/// Hook to code to run on test start.
|
|
|
|
|
OnStart,
|
|
|
|
|
/// Hook to code to run on test end.
|
|
|
|
|
OnStop
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 10:02:04 +02:00
|
|
|
|
/// Run all tests under the given path (except for the test files named in the skip list) using the
|
|
|
|
|
/// provided runner function.
|
2018-06-25 11:21:45 +02:00
|
|
|
|
pub fn run_test_path<H: FnMut(&str, HookType)>(
|
2019-09-25 10:02:04 +02:00
|
|
|
|
path: &Path,
|
|
|
|
|
skip: &[&'static str],
|
|
|
|
|
runner: fn(path: &Path, json_data: &[u8], start_stop_hook: &mut H) -> Vec<String>,
|
2018-06-25 11:21:45 +02:00
|
|
|
|
start_stop_hook: &mut H
|
2018-09-25 12:24:40 +02:00
|
|
|
|
) {
|
2019-09-25 10:02:04 +02:00
|
|
|
|
if !skip.is_empty() {
|
|
|
|
|
// todo[dvdplm] it's really annoying to have to use flushln here. Should be `info!(target:
|
2020-03-01 23:40:59 +01:00
|
|
|
|
// "json-tests", …)`. Issue https://github.com/OpenEthereum/open-ethereum/issues/11084
|
2020-02-19 13:07:33 +01:00
|
|
|
|
flushed_writeln!("[run_test_path] Skipping tests in {}: {:?}", path.display(), skip);
|
2019-09-25 10:02:04 +02:00
|
|
|
|
}
|
2018-09-25 12:24:40 +02:00
|
|
|
|
let mut errors = Vec::new();
|
2019-09-25 10:02:04 +02:00
|
|
|
|
run_test_path_inner(path, skip, runner, start_stop_hook, &mut errors);
|
2018-09-25 12:24:40 +02:00
|
|
|
|
let empty: [String; 0] = [];
|
2019-09-25 10:02:04 +02:00
|
|
|
|
assert_eq!(errors, empty, "\nThere were {} tests in '{}' that failed.", errors.len(), path.display());
|
2018-09-25 12:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_test_path_inner<H: FnMut(&str, HookType)>(
|
2019-09-25 10:02:04 +02:00
|
|
|
|
p: &Path,
|
|
|
|
|
skip: &[&'static str],
|
|
|
|
|
runner: fn(path: &Path, json_data: &[u8], start_stop_hook: &mut H) -> Vec<String>,
|
2018-09-25 12:24:40 +02:00
|
|
|
|
start_stop_hook: &mut H,
|
|
|
|
|
errors: &mut Vec<String>
|
2018-06-25 11:21:45 +02:00
|
|
|
|
) {
|
2017-04-12 13:33:49 +02:00
|
|
|
|
let path = Path::new(p);
|
2019-09-25 10:02:04 +02:00
|
|
|
|
let extension = path.extension().and_then(|s| s.to_str());
|
|
|
|
|
let skip_list: HashSet<OsString> = skip.iter().map(|s| {
|
2017-04-12 13:33:49 +02:00
|
|
|
|
let mut os: OsString = s.into();
|
|
|
|
|
os.push(".json");
|
|
|
|
|
os
|
|
|
|
|
}).collect();
|
2019-09-25 10:02:04 +02:00
|
|
|
|
|
2017-04-12 13:33:49 +02:00
|
|
|
|
if path.is_dir() {
|
2019-09-25 10:02:04 +02:00
|
|
|
|
trace!(target: "json-tests", "running tests contained in '{}'", path.display());
|
|
|
|
|
let test_files = read_dir(path)
|
|
|
|
|
.expect("Directory exists on disk")
|
|
|
|
|
.filter_map(|dir_entry| {
|
|
|
|
|
let dir_entry = dir_entry.expect("Entry in directory listing exists");
|
|
|
|
|
if skip_list.contains(&dir_entry.file_name()) {
|
|
|
|
|
debug!(target: "json-tests", "'{:?}' is on the skip list.", dir_entry.file_name());
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(dir_entry.path())
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
for test_file in test_files {
|
|
|
|
|
run_test_path_inner(&test_file, skip, runner, start_stop_hook, errors);
|
2017-04-12 13:33:49 +02:00
|
|
|
|
}
|
2018-09-10 22:38:30 +02:00
|
|
|
|
} else if extension == Some("swp") || extension == None {
|
2019-09-25 10:02:04 +02:00
|
|
|
|
trace!(target: "json-tests", "ignoring '{}', extension {:?} – Junk?", path.display(), extension);
|
2018-09-10 22:38:30 +02:00
|
|
|
|
// Ignore junk
|
2017-04-12 13:33:49 +02:00
|
|
|
|
} else {
|
2019-09-25 10:02:04 +02:00
|
|
|
|
trace!(target: "json-tests", "running tests in '{}'", path.display());
|
2017-04-12 13:33:49 +02:00
|
|
|
|
let mut path = p.to_path_buf();
|
|
|
|
|
path.set_extension("json");
|
2018-09-25 12:24:40 +02:00
|
|
|
|
run_test_file_append(&path, runner, start_stop_hook, errors)
|
2017-04-12 13:33:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 12:24:40 +02:00
|
|
|
|
fn run_test_file_append<H: FnMut(&str, HookType)>(
|
|
|
|
|
path: &Path,
|
2019-09-25 10:02:04 +02:00
|
|
|
|
runner: fn(path: &Path, json_data: &[u8], start_stop_hook: &mut H) -> Vec<String>,
|
2018-09-25 12:24:40 +02:00
|
|
|
|
start_stop_hook: &mut H,
|
|
|
|
|
errors: &mut Vec<String>
|
|
|
|
|
) {
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
let mut file = match File::open(&path) {
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
Err(_) => panic!("Error opening test file at: {:?}", path),
|
|
|
|
|
};
|
|
|
|
|
file.read_to_end(&mut data).expect("Error reading test file");
|
2019-09-25 10:02:04 +02:00
|
|
|
|
errors.append(&mut runner(&path, &data, start_stop_hook));
|
2018-09-25 12:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
|
pub fn run_test_file<H: FnMut(&str, HookType)>(
|
|
|
|
|
path: &Path,
|
2019-09-25 10:02:04 +02:00
|
|
|
|
runner: fn(path: &Path, json_data: &[u8], start_stop_hook: &mut H) -> Vec<String>,
|
2018-06-25 11:21:45 +02:00
|
|
|
|
start_stop_hook: &mut H
|
|
|
|
|
) {
|
2017-04-12 13:33:49 +02:00
|
|
|
|
let mut data = Vec::new();
|
2018-09-10 22:38:30 +02:00
|
|
|
|
let mut file = match File::open(&path) {
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
Err(_) => panic!("Error opening test file at: {:?}", path),
|
|
|
|
|
};
|
2017-04-12 13:33:49 +02:00
|
|
|
|
file.read_to_end(&mut data).expect("Error reading test file");
|
2019-09-25 10:02:04 +02:00
|
|
|
|
let results = runner(&path, &data, start_stop_hook);
|
2017-09-25 19:45:33 +02:00
|
|
|
|
let empty: [String; 0] = [];
|
|
|
|
|
assert_eq!(results, empty);
|
2017-04-12 13:33:49 +02:00
|
|
|
|
}
|