test_r::enable!();
#[allow(dead_code)]
#[path = "common/mod.rs"]
mod common;
use crate::common::js_subtest_parser::{
SubtestDiscovery, discover_subtests, discover_subtests_with_options,
};
use crate::common::strip_jsonc_comments;
use serde_json::Value;
use std::collections::BTreeSet;
use std::fs;
use test_r::test;
fn find_value_span(content: &str, key: &str) -> Option<(usize, usize)> {
let search = format!("\"{}\"", key);
let key_pos = content.find(&search)?;
let bytes = content.as_bytes();
let mut pos = key_pos + search.len();
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b':' {
return None;
}
pos += 1;
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b'{' {
return None;
}
let value_start = pos;
let mut depth = 0;
let mut in_string = false;
while pos < bytes.len() {
if in_string {
if bytes[pos] == b'\\' && pos + 1 < bytes.len() {
pos += 2;
continue;
}
if bytes[pos] == b'"' {
in_string = false;
}
} else {
match bytes[pos] {
b'"' => in_string = true,
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return Some((value_start, pos + 1));
}
}
b'/' if pos + 1 < bytes.len() && bytes[pos + 1] == b'/' => {
pos += 2;
while pos < bytes.len() && bytes[pos] != b'\n' {
pos += 1;
}
continue;
}
b'/' if pos + 1 < bytes.len() && bytes[pos + 1] == b'*' => {
pos += 2;
while pos + 1 < bytes.len() && !(bytes[pos] == b'*' && bytes[pos + 1] == b'/') {
pos += 1;
}
if pos + 1 < bytes.len() {
pos += 2;
}
continue;
}
_ => {}
}
}
pos += 1;
}
None
}
fn find_tests_object_close(content: &str) -> Option<usize> {
let tests_key = "\"tests\"";
let key_pos = content.find(tests_key)?;
let bytes = content.as_bytes();
let mut pos = key_pos + tests_key.len();
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b':' {
return None;
}
pos += 1;
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b'{' {
return None;
}
let mut depth = 0;
let mut in_string = false;
while pos < bytes.len() {
if in_string {
if bytes[pos] == b'\\' && pos + 1 < bytes.len() {
pos += 2;
continue;
}
if bytes[pos] == b'"' {
in_string = false;
}
} else {
match bytes[pos] {
b'"' => in_string = true,
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return Some(pos);
}
}
b'/' if pos + 1 < bytes.len() && bytes[pos + 1] == b'/' => {
pos += 2;
while pos < bytes.len() && bytes[pos] != b'\n' {
pos += 1;
}
continue;
}
b'/' if pos + 1 < bytes.len() && bytes[pos + 1] == b'*' => {
pos += 2;
while pos + 1 < bytes.len() && !(bytes[pos] == b'*' && bytes[pos + 1] == b'/') {
pos += 1;
}
if pos + 1 < bytes.len() {
pos += 2;
}
continue;
}
_ => {}
}
}
pos += 1;
}
None
}
fn escape_json(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t")
}
fn format_split_value(
skip: bool,
reason: &str,
subtests: &[(String, bool, String)], ) -> String {
let mut lines = Vec::new();
lines.push("{".to_string());
if skip {
lines.push(" \"skip\": true,".to_string());
if !reason.is_empty() {
lines.push(format!(" \"reason\": \"{}\",", escape_json(reason)));
}
}
lines.push(" \"split\": true,".to_string());
lines.push(" \"subtests\": {".to_string());
for (i, (name, sub_skip, sub_reason)) in subtests.iter().enumerate() {
let comma = if i + 1 < subtests.len() { "," } else { "" };
if *sub_skip {
lines.push(format!(
" \"{}\": {{ \"skip\": true, \"reason\": \"{}\" }}{}",
escape_json(name),
escape_json(sub_reason),
comma
));
} else {
lines.push(format!(" \"{}\": {{}}{}", escape_json(name), comma));
}
}
lines.push(" }".to_string());
lines.push(" }".to_string());
lines.join("\n")
}
fn build_subtests(
discovery: &SubtestDiscovery,
file_skip: bool,
file_reason: &str,
) -> Vec<(String, bool, String)> {
let names: Vec<String> = match discovery {
SubtestDiscovery::None => return Vec::new(),
SubtestDiscovery::Block(blocks) => blocks.iter().map(|b| b.name.clone()).collect(),
SubtestDiscovery::NodeTest(tests) => tests.iter().map(|t| t.name.clone()).collect(),
};
names
.into_iter()
.map(|name| {
if file_skip {
let reason = if file_reason.is_empty() {
"inherited".to_string()
} else {
format!("inherited: {}", file_reason)
};
(name, true, reason)
} else {
(name, false, String::new())
}
})
.collect()
}
fn build_merged_subtests(
discovery: &SubtestDiscovery,
existing_subtests: &serde_json::Map<String, Value>,
file_skip: bool,
file_reason: &str,
) -> Vec<(String, bool, String)> {
let names: Vec<String> = match discovery {
SubtestDiscovery::None => return Vec::new(),
SubtestDiscovery::Block(blocks) => blocks.iter().map(|b| b.name.clone()).collect(),
SubtestDiscovery::NodeTest(tests) => tests.iter().map(|t| t.name.clone()).collect(),
};
names
.into_iter()
.map(|name| {
if let Some(existing) = existing_subtests.get(&name) {
let skip = existing
.get("skip")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let reason = existing
.get("reason")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
(name, skip, reason)
} else if file_skip {
let reason = if file_reason.is_empty() {
"inherited".to_string()
} else {
format!("inherited: {}", file_reason)
};
(name, true, reason)
} else {
(name, false, String::new())
}
})
.collect()
}
fn format_unsplit_value(skip: bool, reason: &str) -> String {
if skip {
if reason.is_empty() {
"{ \"skip\": true }".to_string()
} else {
format!(
"{{ \"skip\": true, \"reason\": \"{}\" }}",
escape_json(reason)
)
}
} else {
"{}".to_string()
}
}
fn discovered_names(discovery: &SubtestDiscovery) -> BTreeSet<String> {
match discovery {
SubtestDiscovery::None => BTreeSet::new(),
SubtestDiscovery::Block(blocks) => blocks.iter().map(|b| b.name.clone()).collect(),
SubtestDiscovery::NodeTest(tests) => tests.iter().map(|t| t.name.clone()).collect(),
}
}
fn discover_suite_files() -> BTreeSet<String> {
let suites = ["parallel", "sequential", "es-module"];
let mut files = BTreeSet::new();
for suite in &suites {
let suite_dir = format!("tests/node_compat/suite/{suite}");
let suite_path = std::path::Path::new(&suite_dir);
if !suite_path.exists() {
eprintln!("Suite directory {suite_dir} not found, skipping");
continue;
}
if let Ok(entries) = fs::read_dir(&suite_dir) {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.ends_with(".js") {
files.insert(format!("{suite}/{name}"));
}
}
}
}
files
}
#[test]
fn migrate_config_split() {
let config_path = "tests/node_compat/config.jsonc";
let content = fs::read_to_string(config_path).expect("Failed to read config.jsonc");
let json_str = strip_jsonc_comments(&content);
let config: Value = serde_json::from_str(&json_str).expect("Failed to parse config");
let tests_obj = config
.get("tests")
.and_then(|v| v.as_object())
.expect("config.jsonc missing 'tests' object");
let existing_keys: BTreeSet<String> = tests_obj.keys().cloned().collect();
let all_suite_files = discover_suite_files();
let new_files: Vec<String> = all_suite_files
.difference(&existing_keys)
.cloned()
.collect();
let mut modified_content = content.clone();
let mut split_count = 0;
let mut update_count = 0;
let mut unsplit_count = 0;
let mut total_subtests = 0;
let mut new_count = 0;
let mut new_split_count = 0;
let empty_map = serde_json::Map::new();
let mut split_updates: Vec<(String, String)> = Vec::new();
for (test_path, opts) in tests_obj.iter() {
let is_split = opts.get("split").and_then(|v| v.as_bool()).unwrap_or(false);
let suite_file = format!("tests/node_compat/suite/{}", test_path);
let source = match fs::read_to_string(&suite_file) {
Ok(s) => s,
Err(_) => {
eprintln!("Skipping {}: file not found", test_path);
continue;
}
};
let nested_node_test = opts
.get("nestedNodeTest")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let isolate_block_subtests = opts
.get("isolateBlockSubtests")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let discovery = discover_subtests_with_options(
test_path,
&source,
nested_node_test,
isolate_block_subtests,
);
let file_skip = opts.get("skip").and_then(|v| v.as_bool()).unwrap_or(false);
let file_reason = opts.get("reason").and_then(|v| v.as_str()).unwrap_or("");
if is_split {
let existing_subtests = opts
.get("subtests")
.and_then(|v| v.as_object())
.unwrap_or(&empty_map);
let existing_names: BTreeSet<String> = existing_subtests.keys().cloned().collect();
let disc_names = discovered_names(&discovery);
if existing_names == disc_names {
continue; }
if disc_names.len() < 2 {
let new_value = format_unsplit_value(file_skip, file_reason);
split_updates.push((test_path.clone(), new_value));
unsplit_count += 1;
} else {
let subtests =
build_merged_subtests(&discovery, existing_subtests, file_skip, file_reason);
let new_value = format_split_value(file_skip, file_reason, &subtests);
split_updates.push((test_path.clone(), new_value));
update_count += 1;
total_subtests += subtests.len();
}
} else {
let subtests = build_subtests(&discovery, file_skip, file_reason);
if subtests.len() < 2 {
continue;
}
let new_value = format_split_value(file_skip, file_reason, &subtests);
split_updates.push((test_path.clone(), new_value));
split_count += 1;
total_subtests += subtests.len();
}
}
let mut positioned_updates: Vec<(usize, usize, String)> = Vec::new();
for (key, new_value) in &split_updates {
if let Some((start, end)) = find_value_span(&modified_content, key) {
positioned_updates.push((start, end, new_value.clone()));
} else {
eprintln!("WARNING: Could not find value span for key: {}", key);
}
}
positioned_updates.sort_by_key(|b| std::cmp::Reverse(b.0));
for (start, end, new_value) in positioned_updates {
modified_content.replace_range(start..end, &new_value);
}
if !new_files.is_empty() {
let close_pos = find_tests_object_close(&modified_content)
.expect("Could not find closing brace of tests object");
let prefix = &modified_content[..close_pos];
let suffix = &modified_content[close_pos..];
let trimmed_prefix = prefix.trim_end();
let needs_comma = !trimmed_prefix.ends_with(',') && !trimmed_prefix.ends_with('{');
let mut new_section = String::new();
if needs_comma {
new_section.push(',');
}
new_section.push_str("\n\n // === newly discovered tests (not yet evaluated) ===");
for test_path in &new_files {
let suite_file = format!("tests/node_compat/suite/{}", test_path);
let source = match fs::read_to_string(&suite_file) {
Ok(s) => s,
Err(_) => {
eprintln!("Skipping new file {}: could not read", test_path);
continue;
}
};
let discovery = discover_subtests(test_path, &source);
let subtests = build_subtests(&discovery, true, "newly discovered, not yet evaluated");
if subtests.len() >= 2 {
let value =
format_split_value(true, "newly discovered, not yet evaluated", &subtests);
new_section.push_str(&format!("\n \"{}\": {},", test_path, value));
total_subtests += subtests.len();
new_split_count += 1;
} else {
new_section.push_str(&format!(
"\n \"{}\": {{ \"skip\": true, \"reason\": \"newly discovered, not yet evaluated\" }},",
test_path
));
}
new_count += 1;
}
if new_section.ends_with(',') {
new_section.pop();
}
new_section.push_str("\n ");
modified_content = format!("{}{}{}", trimmed_prefix, new_section, suffix);
}
fs::write(config_path, &modified_content).expect("Failed to write config.jsonc");
println!("Migration complete:");
println!(" Existing files split: {}", split_count);
println!(" Existing splits updated: {}", update_count);
println!(" Existing splits unsplit: {}", unsplit_count);
println!(
" New files added: {} ({} with splits)",
new_count, new_split_count
);
println!(" Total subtests discovered: {}", total_subtests);
println!(" Config written to: {}", config_path);
}