summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2023-11-06 12:57:15 +0800
committercyqsimon <28627918+cyqsimon@users.noreply.github.com>2023-12-11 10:21:06 +0800
commit586c804b1e7c665b909e65840c185f86360a9701 (patch)
tree4de9fcc498bfde4041aeb62b0d5964bdc8cfd9d7
parente30161ac3cb2a67b300572e2dc1a18141631d3cb (diff)
Add test: `builtin_mappings_are_lazily_evaluated`
-rw-r--r--Cargo.lock19
-rw-r--r--Cargo.toml1
-rw-r--r--src/syntax_mapping.rs28
3 files changed, 48 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 93aabba5..5fa1bae8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -155,6 +155,7 @@ dependencies = [
"predicates",
"regex",
"run_script",
+ "rusty-fork",
"semver",
"serde",
"serde_with",
@@ -1040,6 +1041,12 @@ dependencies = [
]
[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
+[[package]]
name = "quick-xml"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1172,6 +1179,18 @@ dependencies = [
]
[[package]]
+name = "rusty-fork"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f"
+dependencies = [
+ "fnv",
+ "quick-error",
+ "tempfile",
+ "wait-timeout",
+]
+
+[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index d9a149a6..ecb17365 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -67,6 +67,7 @@ bytesize = { version = "1.3.0" }
encoding_rs = "0.8.33"
os_str_bytes = { version = "~6.6", optional = true }
run_script = { version = "^0.10.1", optional = true}
+rusty-fork = "0.3.0"
[dependencies.git2]
version = "0.18"
diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs
index e523f85e..6fd85463 100644
--- a/src/syntax_mapping.rs
+++ b/src/syntax_mapping.rs
@@ -128,6 +128,9 @@ impl<'a> SyntaxMapping<'a> {
#[cfg(test)]
mod tests {
+ use once_cell::sync::Lazy;
+ use rusty_fork::rusty_fork_test;
+
use super::*;
#[test]
fn builtin_mappings_work() {
@@ -148,6 +151,31 @@ mod tests {
let _mappings = map.builtin_mappings().collect::<Vec<_>>();
}
+ // lazy initialisation test needs to be run on a separate instance because
+ // it will race with other tests
+ // see: https://github.com/rust-lang/rust/issues/47506
+ rusty_fork_test! {
+ #[test]
+ fn builtin_mappings_are_lazily_evaluated() {
+ let map = SyntaxMapping::new();
+
+ assert!(BUILTIN_MAPPINGS
+ .iter()
+ .all(|(matcher, _)| Lazy::get(matcher).is_none()));
+
+ // calling `builtin_mappings` should not trigger evaluation
+ let mappings_iter = map.builtin_mappings();
+ assert!(BUILTIN_MAPPINGS
+ .iter()
+ .all(|(matcher, _)| Lazy::get(matcher).is_none()));
+
+ let _mappings: Vec<_> = mappings_iter.collect();
+ assert!(BUILTIN_MAPPINGS
+ .iter()
+ .all(|(matcher, _)| Lazy::get(matcher).is_some()));
+ }
+ }
+
#[test]
fn builtin_mappings_matcher_only_compile_once() {
let map = SyntaxMapping::new();