From 586c804b1e7c665b909e65840c185f86360a9701 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Mon, 6 Nov 2023 12:57:15 +0800 Subject: [PATCH] Add test: `builtin_mappings_are_lazily_evaluated` --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 1 + src/syntax_mapping.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) 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", @@ -1039,6 +1040,12 @@ dependencies = [ "unicode-ident", ] +[[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" @@ -1171,6 +1178,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[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" 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::>(); } + // 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();