1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
use std::{
fs::{create_dir_all, OpenOptions},
io::Write,
path::PathBuf,
};
pub fn get_mf_fixture_dir_for(path_name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../vendor/microformats-test/tests")
.join(path_name)
}
fn generate_parameterized_tests() {
if PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../vendor")
.exists()
{
// Instead of reading the suites directly from disk, we load it like this to make it easier to
// toggle which ones we'd like to enable.
// TODO: Enable v1 when support for it is underway.
// TODO: Enable mixed when both prior versions are passing.
let suites = [
"microformats-v2/h-adr",
"microformats-v2/h-card",
"microformats-v2/h-entry",
"microformats-v2/h-event",
"microformats-v2/h-feed",
"microformats-v2/h-geo",
"microformats-v2/h-product",
"microformats-v2/h-recipe",
"microformats-v2/h-resume",
"microformats-v2/h-review",
"microformats-v2/h-review-aggregate",
// "microformats-v2/mixed",
"microformats-v2/rel",
];
let ignored_tests: Vec<_> =
include_str!("src/parse/test/suite/ignored_generated_tests.txt")
.split('\n')
.collect();
let test_files = suites
.iter()
.map(|suite| {
let fixtures = collect_fixtures_for(suite);
let (suite_dir, suite_name) = suite.split_once('/').unwrap();
let safe_suite_name = suite_name.replace('-', "_");
let suite_path = PathBuf::from(format!(
"{}/src/parse/test/suite/generated/{}",
env!("CARGO_MANIFEST_DIR"),
suite_dir.replace('-', "_")
));
if !suite_path.exists() {
create_dir_all(suite_path.clone()).unwrap_or_else(|_| {
panic!(
"failed to create the recursive directory {:?} for the test suite {:?}",
suite_path, suite_name
)
});
}
let test_file_location = suite_path.join(format!("{}.rs", safe_suite_name));
let mut f = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&test_file_location)
.unwrap_or_else(|_| {
panic!(
"Could not create the file for the test {} at {:?}.",
safe_suite_name, test_file_location
)
});
let test_header = format!(
"// Tests for {suite_name}
#![allow(non_snake_case, unused_imports)]
use crate::parse::test::suite::check_fixture_for_parser;
",
suite_name = suite
)
.to_string();
let test_contents = fixtures
.into_iter()
.filter(|test_case| !ignored_tests.contains(&test_case.as_str()))
.map(|test_name| {
let safe_test_name = test_name
.replace((suite.to_string() + "/").as_str(), "")
.replace('/', "__")
.replace('-', "_");
format!(
r#"
#[test]
fn {name}() {{
check_fixture_for_parser("{fixture_name}");
}}"#,
name = safe_test_name,
fixture_name = test_name
)
})
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
write!(f, "{}{}", test_header, test_contents).expect("Failed to write test file.");
test_file_location
})
.collect::<Vec<_>>();
let generated_test_module_file = PathBuf::from(format!(
"{}/src/parse/test/suite/generated/mod.rs",
env!("CARGO_MANIFEST_DIR")
));
let version_mods = test_files
.into_iter()
.filter_map(|test_file_path| {
let file_name = test_file_path
.file_name()
.and_then(|os| os.to_str())
.map(|t| t.to_string());
let dir_name = test_file_path
.parent()
.and_then(|p| p.file_name())
.and_then(|os| os.to_str())
.map(|s| s.to_string());
file_name.and_then(|fin| dir_name.map(|dn| (dn, fin)))
})
.map(|(dir_name, file_name)| {
(dir_name, file_name.replace('-', "_").replace(".rs", ";"))
})
.map(|(version_name, suite_name)| {
let version_mod_file = format!(
"{}/src/parse/test/suite/generated/{}/mod.rs",
env!("CARGO_MANIFEST_DIR"),
version_name
);
if !PathBuf::from(version_mod_file.clone()).exists() {
std::fs::write(&version_mod_file, "").expect("cannot create mod file");
}
if !String::from_utf8_lossy(&std::fs::read(version_mod_file.clone()).unwrap())
.contains(&suite_name)
{
let mut f = OpenOptions::new()
.create(true)
.append(true)
.open(version_mod_file.clone())
.unwrap();
writeln!(f, "mod {}", suite_name)
.expect("Failed to add suite into version folder");
}
version_name
})
.fold(vec![], |mut acc, value| {
let mod_line = format!("mod {};", value);
if !acc.contains(&mod_line) {
acc.push(mod_line);
}
acc
})
.join("\n");
std::fs::write(generated_test_module_file.clone(), version_mods).unwrap_or_else(|_| {
panic!(
"failed to write out the module at {:?}",
generated_test_module_file
)
});
}
}
fn main() {
generate_parameterized_tests();
}
fn collect_fixtures_for(suite: &str) -> Vec<String> {
let mut fixtures = Vec::new();
get_mf_fixture_dir_for(suite)
.read_dir()
.unwrap_or_else(|_| panic!("Couldn't find fixtures for testing '{}' support.", suite))
.filter_map(|file_path| file_path.ok())
.filter_map(|file_path| {
file_path
.path()
.extension()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
.and_then(|f| {
if f == *"json" {
Some(file_path.path().to_owned())
} else {
None
}
})
})
.map(|whole_path| {
let file_name = whole_path
.clone()
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
.unwrap_or_default();
let test_name = format!("{}/{}", suite, file_name);
fixtures.push(test_name)
});
return fixtures;
}
|