Skip to content

Commit

Permalink
test262 hermetization
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelJastrzebski committed Sep 13, 2024
1 parent d4f55fc commit 6dc72ca
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 151 deletions.
8 changes: 4 additions & 4 deletions tests/tester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod exec;
mod results;

use exec::{RunTest, RunTestSuite};
use test262::{read, Ignored, SpecEdition, TestFlags};
use test262::{Harness, Ignored, SpecEdition, TestFlags};

use self::results::{compare_results, write_json};

Expand Down Expand Up @@ -233,10 +233,10 @@ fn run_test_suite(
if verbose != 0 {
println!("Loading the test suite...");
}
let harness = read::read_harness(test262_path).wrap_err("could not read harness")?;
let harness = Harness::read(test262_path).wrap_err("could not read harness")?;

if suite.to_string_lossy().ends_with(".js") {
let test = read::read_test(&test262_path.join(suite)).wrap_err_with(|| {
let test = test262::Test::read(&test262_path.join(suite)).wrap_err_with(|| {
let suite = suite.display();
format!("could not read the test {suite}")
})?;
Expand All @@ -254,7 +254,7 @@ fn run_test_suite(

println!();
} else {
let suite = read::read_suite(&test262_path.join(suite), config.ignored(), false)
let suite = test262::TestSuite::read(&test262_path.join(suite), config.ignored(), false)
.wrap_err_with(|| {
let suite = suite.display();
format!("could not read the suite {suite}")
Expand Down
2 changes: 1 addition & 1 deletion tools/test262/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn reset_commit(directory: &str, commit: &str, verbose: u8) -> Result<()> {
}

/// Clone repository
pub fn clone(
pub(super) fn clone(
directory: &str,
repor_url: &str, // "https://github.com/tc39/test262"
baranch: &str, // "origin/main"
Expand Down
10 changes: 5 additions & 5 deletions tools/test262/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod edition;
mod structs;
mod test_files;
mod test_flags;
mod git;
mod read;
mod error;

pub mod git;
pub mod read;
pub use error::Error262;
pub use edition::SpecEdition;
pub use structs::{ErrorType, Ignored, Outcome, Phase};
Expand Down Expand Up @@ -45,7 +45,7 @@ mod tests {
#[test]
#[ignore = "manual"]
fn should_read_harness() {
let harness = super::read::read_harness(Path::new(TEST262_DIRECTORY)).unwrap();
let harness = super::Harness::read(Path::new(TEST262_DIRECTORY)).unwrap();
assert!(harness.assert.path.is_file());
assert!(harness.sta.path.is_file());
assert!(harness.doneprint_handle.path.is_file());
Expand All @@ -58,12 +58,12 @@ mod tests {
.join("test")
.join("language")
.join("import");
let test_suite = super::read::read_suite(&path, &Ignored::default(), false).unwrap();
let test_suite = super::TestSuite::read(&path, &Ignored::default(), false).unwrap();
assert!(!test_suite.name.is_empty());
assert!(!test_suite.tests.is_empty());

let test_path = &test_suite.tests[0].path;
let test = super::read::read_test(test_path);
let test = super::Test::read(test_path);
assert!(test.is_ok());
}

Expand Down
294 changes: 153 additions & 141 deletions tools/test262/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,166 +10,178 @@ use std::{
path::{Path, PathBuf},
};

/// Reads the Test262 defined bindings.
pub fn read_harness(test262_path: &Path) -> Result<Harness, Error262> {
fn read_harness_file(path: PathBuf) -> Result<HarnessFile, Error262> {
let content = fs::read_to_string(&path).map_err(|_| Error262::HarnessFileReadError {
path: path.string(),
})?;

Ok(HarnessFile {
content: content.into_boxed_str(),
path: path.into_boxed_path(),
})
}
let mut includes = FxHashMap::default();
impl Harness {
/// Reads the Test262 defined bindings.
pub fn read(test262_path: &Path) -> Result<Harness, Error262> {
fn read_harness_file(path: PathBuf) -> Result<HarnessFile, Error262> {
let content =
fs::read_to_string(&path).map_err(|_| Error262::HarnessFileReadError {
path: path.string(),
})?;

Ok(HarnessFile {
content: content.into_boxed_str(),
path: path.into_boxed_path(),
})
}
let mut includes = FxHashMap::default();

let harness_dir = test262_path.join("harness");
for entry in fs::read_dir(&harness_dir).map_err(|_| Error262::InvalidHarnessDirecory {
path: harness_dir.string(),
})? {
let entry = entry.map_err(|_| Error262::InvalidHarnessDirecory {
let harness_dir = test262_path.join("harness");
for entry in fs::read_dir(&harness_dir).map_err(|_| Error262::InvalidHarnessDirecory {
path: harness_dir.string(),
})?;
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();

if file_name == "assert.js" || file_name == "sta.js" || file_name == "doneprintHandle.js" {
continue;
})? {
let entry = entry.map_err(|_| Error262::InvalidHarnessDirecory {
path: harness_dir.string(),
})?;
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();

if file_name == "assert.js"
|| file_name == "sta.js"
|| file_name == "doneprintHandle.js"
{
continue;
}

includes.insert(
file_name.into_owned().into_boxed_str(),
read_harness_file(entry.path())?,
);
}

includes.insert(
file_name.into_owned().into_boxed_str(),
read_harness_file(entry.path())?,
);
let assert = read_harness_file(test262_path.join("harness/assert.js"))?;
let sta = read_harness_file(test262_path.join("harness/sta.js"))?;
let doneprint_handle = read_harness_file(test262_path.join("harness/doneprintHandle.js"))?;

Ok(Harness {
assert,
sta,
doneprint_handle,
includes,
})
}
let assert = read_harness_file(test262_path.join("harness/assert.js"))?;
let sta = read_harness_file(test262_path.join("harness/sta.js"))?;
let doneprint_handle = read_harness_file(test262_path.join("harness/doneprintHandle.js"))?;

Ok(Harness {
assert,
sta,
doneprint_handle,
includes,
})
}

/// Reads a test suite in the given path.
pub fn read_suite(
path: &Path,
ignored: &crate::structs::Ignored,
mut ignore_suite: bool,
) -> Result<TestSuite, Error262> {
let name = path
.file_name()
.and_then(OsStr::to_str)
.ok_or(Error262::InvalidPathToTestSuite)?;

ignore_suite |= ignored.contains_test(name);

let mut suites = Vec::new();
let mut tests = Vec::new();

// TODO: iterate in parallel
for entry in path
.read_dir()
.map_err(|_| Error262::InvalidPathToTestSuite)?
{
let entry = entry.map_err(|_| Error262::InvalidPathToTestSuite)?;
let filetype = entry
.file_type()
.map_err(|_| Error262::FailedToGetFileType {
impl TestSuite {
/// Reads a test suite in the given path.
pub fn read(
path: &Path,
ignored: &crate::structs::Ignored,
mut ignore_suite: bool,
) -> Result<TestSuite, Error262> {
let name = path
.file_name()
.and_then(OsStr::to_str)
.ok_or(Error262::InvalidPathToTestSuite)?;

ignore_suite |= ignored.contains_test(name);

let mut suites = Vec::new();
let mut tests = Vec::new();

// TODO: iterate in parallel
for entry in path
.read_dir()
.map_err(|_| Error262::InvalidPathToTestSuite)?
{
let entry = entry.map_err(|_| Error262::InvalidPathToTestSuite)?;
let filetype = entry
.file_type()
.map_err(|_| Error262::FailedToGetFileType {
path: entry.path().string(),
})?;

if filetype.is_dir() {
suites.push(
TestSuite::read(entry.path().as_path(), ignored, ignore_suite).map_err(|e| {
Error262::SubSuiteReadError {
path: entry.path().string(),
suite: path.string(),
error: Box::new(e),
}
})?,
);
continue;
}

let path = entry.path();

if path.extension() != Some(OsStr::new("js")) {
// Ignore files that aren't executable.
continue;
}

if path
.file_stem()
.is_some_and(|stem| stem.as_encoded_bytes().ends_with(b"FIXTURE"))
{
// Ignore files that are fixtures.
continue;
}

let mut test = Test::read(&path).map_err(|e| Error262::SubTestReadError {
path: entry.path().string(),
suite: path.string(),
error: Box::new(e),
})?;

if filetype.is_dir() {
suites.push(
read_suite(entry.path().as_path(), ignored, ignore_suite).map_err(|e| {
Error262::SubSuiteReadError {
path: entry.path().string(),
suite: path.string(),
error: Box::new(e),
}
})?,
);
continue;
}

let path = entry.path();

if path.extension() != Some(OsStr::new("js")) {
// Ignore files that aren't executable.
continue;
if ignore_suite
|| ignored.contains_any_flag(test.flags)
|| ignored.contains_test(&test.name)
|| test
.features
.iter()
.any(|feat| ignored.contains_feature(feat))
{
test.set_ignored();
}
tests.push(test);
}

if path
.file_stem()
.is_some_and(|stem| stem.as_encoded_bytes().ends_with(b"FIXTURE"))
{
// Ignore files that are fixtures.
continue;
}

let mut test = read_test(&path).map_err(|e| Error262::SubTestReadError {
path: entry.path().string(),
suite: path.string(),
error: Box::new(e),
})?;

if ignore_suite
|| ignored.contains_any_flag(test.flags)
|| ignored.contains_test(&test.name)
|| test
.features
.iter()
.any(|feat| ignored.contains_feature(feat))
{
test.set_ignored();
}
tests.push(test);
Ok(TestSuite {
name: name.into(),
path: Box::from(path),
suites: suites.into_boxed_slice(),
tests: tests.into_boxed_slice(),
})
}

Ok(TestSuite {
name: name.into(),
path: Box::from(path),
suites: suites.into_boxed_slice(),
tests: tests.into_boxed_slice(),
})
}

/// Reads information about a given test case.
pub fn read_test(path: &Path) -> Result<Test, Error262> {
let name = path
.file_stem()
.and_then(OsStr::to_str)
.ok_or(Error262::InvalidPathToTest)?;
impl Test {
/// Reads information about a given test case.
pub fn read(path: &Path) -> Result<Test, Error262> {
let name = path
.file_stem()
.and_then(OsStr::to_str)
.ok_or(Error262::InvalidPathToTest)?;

let metadata = read_metadata(path)?;
let metadata = MetaData::read(path)?;

Test::new(name, path, metadata)
Test::new(name, path, metadata)
}
}

/// Reads the metadata from the input test code.
pub fn read_metadata(test: &Path) -> Result<MetaData, Error262> {
let code = fs::read_to_string(test).map_err(|_| Error262::MetadateReadError {
path: test.string(),
})?;

let (_, metadata) = code
.split_once("/*---")
.ok_or_else(|| Error262::MetadateParseError {
impl MetaData {
/// Reads the metadata from the input test code.
pub fn read(test: &Path) -> Result<MetaData, Error262> {
let code = fs::read_to_string(test).map_err(|_| Error262::MetadateReadError {
path: test.string(),
})?;
let (metadata, _) =
metadata
.split_once("---*/")
.ok_or_else(|| Error262::MetadateParseError {
path: test.string(),
})?;
let metadata = metadata.replace('\r', "\n");

serde_yaml::from_str(&metadata).map_err(|_| Error262::MetadateParseError {
path: test.string(),
})
let (_, metadata) =
code.split_once("/*---")
.ok_or_else(|| Error262::MetadateParseError {
path: test.string(),
})?;
let (metadata, _) =
metadata
.split_once("---*/")
.ok_or_else(|| Error262::MetadateParseError {
path: test.string(),
})?;
let metadata = metadata.replace('\r', "\n");

serde_yaml::from_str(&metadata).map_err(|_| Error262::MetadateParseError {
path: test.string(),
})
}
}

0 comments on commit 6dc72ca

Please sign in to comment.