summaryrefslogtreecommitdiff
path: root/tests/util/server/src/factory.rs
diff options
context:
space:
mode:
authorAsher Gomez <ashersaupingomez@gmail.com>2024-02-20 00:34:24 +1100
committerGitHub <noreply@github.com>2024-02-19 06:34:24 -0700
commit2b279ad630651e973d5a31586f58809f005bc925 (patch)
tree3e3cbeb4126643c75381dd5422e8603a7488bb8a /tests/util/server/src/factory.rs
parenteb542bc185c6c4ce1847417a2dfdf04862cd86db (diff)
chore: move `test_util` to `tests/util/server` (#22444)
As discussed with @mmastrac. --------- Signed-off-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: Matt Mastracci <matthew@mastracci.com>
Diffstat (limited to 'tests/util/server/src/factory.rs')
-rw-r--r--tests/util/server/src/factory.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/util/server/src/factory.rs b/tests/util/server/src/factory.rs
new file mode 100644
index 000000000..5b796fbc1
--- /dev/null
+++ b/tests/util/server/src/factory.rs
@@ -0,0 +1,98 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+use glob::glob;
+use std::collections::HashSet;
+use std::path::PathBuf;
+
+/// Generate a unit test factory verified and backed by a glob.
+#[macro_export]
+macro_rules! unit_test_factory {
+ ($test_fn:ident, $base:literal, $glob:literal, [ $( $test:ident $(= $($path:ident)/+)? ),+ $(,)? ]) => {
+ #[test]
+ fn check_test_glob() {
+ $crate::factory::check_test_glob($base, $glob, [ $( ( stringify!($test), stringify!( $( $($path)/+ )? ) ) ),+ ].as_slice());
+ }
+
+ $(
+ #[allow(non_snake_case)]
+ #[test]
+ fn $test() {
+ $test_fn($crate::factory::get_path(stringify!($test), stringify!( $( $($path)/+ )?)))
+ }
+ )+
+ };
+ (__test__ $($prefix:ident)* $test:ident) => {
+ #[allow(non_snake_case)]
+ #[test]
+ fn $test() {
+ $test_fn(stringify!($($prefix)/+ $test))
+ }
+ };
+}
+
+pub fn get_path(test: &'static str, path: &'static str) -> String {
+ if path.is_empty() {
+ test.to_owned()
+ } else {
+ path.replace(' ', "")
+ }
+}
+
+/// Validate that the glob matches the list of tests specified.
+pub fn check_test_glob(
+ base: &'static str,
+ glob_pattern: &'static str,
+ files: &[(&'static str, &'static str)],
+) {
+ let base_dir = PathBuf::from(base)
+ .canonicalize()
+ .unwrap()
+ .to_string_lossy()
+ // Strip Windows slashes
+ .replace('\\', "/");
+ let mut found = HashSet::new();
+ let mut list = vec![];
+ for file in glob(&format!("{}/{}", base, glob_pattern))
+ .expect("Failed to read test path")
+ {
+ let mut file = file
+ .expect("Invalid file from glob")
+ .canonicalize()
+ .unwrap();
+ file.set_extension("");
+ let name = file.file_name().unwrap().to_string_lossy();
+ // Strip windows slashes
+ let file = file.to_string_lossy().replace('\\', "/");
+ let file = file
+ .strip_prefix(&base_dir)
+ .expect("File {file} did not start with {base_dir} prefix");
+ let file = file.strip_prefix('/').unwrap().to_owned();
+ if file.contains('/') {
+ list.push(format!("{}={}", name, file))
+ } else {
+ list.push(file.clone());
+ }
+ found.insert(file);
+ }
+
+ let mut error = false;
+ for (test, path) in files {
+ // Remove spaces from the macro
+ let path = if path.is_empty() {
+ (*test).to_owned()
+ } else {
+ path.replace(' ', "")
+ };
+ if found.contains(&path) {
+ found.remove(&path);
+ } else {
+ error = true;
+ }
+ }
+
+ if error || !found.is_empty() {
+ panic!(
+ "Glob did not match provided list of files. Expected: \n[\n {}\n]",
+ list.join(",\n ")
+ );
+ }
+}