From 34d6f7f3bb753ad8636afcf58ab5a348dc405de2 Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 17 Jul 2024 11:45:07 -0400 Subject: [PATCH] Support no_std integration tests --- Cargo.toml | 6 +----- test | 8 ++------ tests/lib.rs | 12 ++++++++++++ tests/suite_bytes.rs | 8 +++++++- tests/suite_bytes_set.rs | 8 +++++++- tests/suite_string.rs | 8 +++++++- tests/suite_string_set.rs | 8 +++++++- 7 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4fe3be20a..be75e74a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,11 +34,7 @@ default = ["std", "perf", "unicode", "regex-syntax/default"] # ECOSYSTEM FEATURES -# The 'std' feature permits the regex crate to use the standard library. This -# is intended to support future use cases where the regex crate may be able -# to compile without std, and instead just rely on 'core' and 'alloc' (for -# example). Currently, this isn't supported, and removing the 'std' feature -# will prevent regex from compiling. +# The 'std' feature permits the regex crate to use the standard library. std = [ "aho-corasick?/std", "memchr?/std", diff --git a/test b/test index 48224c6d1..aa64aeda3 100755 --- a/test +++ b/test @@ -13,15 +13,11 @@ cd "$(dirname "$0")" echo "===== DEFAULT FEATURES =====" cargo test -# no-std mode is annoyingly difficult to test. Currently, the integration tests -# don't run. So for now, we just test that library tests run. (There aren't -# many because `regex` is just a wrapper crate.) -cargo test --no-default-features --lib - echo "===== DOC TESTS =====" cargo test --doc features=( + "" "std" "std unicode" "std unicode-perl" @@ -36,7 +32,7 @@ features=( ) for f in "${features[@]}"; do echo "===== FEATURE: $f =====" - cargo test --test integration --no-default-features --features "$f" + cargo test --test integration --no-default-features --features="$f" done # And test the probably-forever-nightly-only 'pattern' feature... diff --git a/tests/lib.rs b/tests/lib.rs index b3f69423d..3ea4ff471 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -16,6 +16,18 @@ const BLACKLIST: &[&str] = &[ // Nothing to blacklist yet! ]; +#[cfg(not(feature = "std"))] +pub(crate) fn nostd_compat( + re: Result, +) -> T { + assert!( + re.is_ok(), + "regex compilation failed: {}", + re.as_ref().unwrap_err(), + ); + re.unwrap() +} + fn suite() -> anyhow::Result { let _ = env_logger::try_init(); diff --git a/tests/suite_bytes.rs b/tests/suite_bytes.rs index 106d99808..ae3d4e919 100644 --- a/tests/suite_bytes.rs +++ b/tests/suite_bytes.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "std"))] +use crate::nostd_compat; use { anyhow::Result, regex::bytes::{Regex, RegexBuilder}, @@ -90,7 +92,11 @@ fn compiler( .case_insensitive(test.case_insensitive()) .unicode(test.unicode()) .line_terminator(test.line_terminator()) - .build()?; + .build(); + #[cfg(not(feature = "std"))] + let re = nostd_compat(re); + #[cfg(feature = "std")] + let re = re?; // Propagate any errors. Ok(CompiledRegex::compiled(move |test| run_test(&re, test))) } diff --git a/tests/suite_bytes_set.rs b/tests/suite_bytes_set.rs index 899d24c17..ba6ce18fe 100644 --- a/tests/suite_bytes_set.rs +++ b/tests/suite_bytes_set.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "std"))] +use crate::nostd_compat; use { anyhow::Result, regex::bytes::{RegexSet, RegexSetBuilder}, @@ -66,6 +68,10 @@ fn compiler( .case_insensitive(test.case_insensitive()) .unicode(test.unicode()) .line_terminator(test.line_terminator()) - .build()?; + .build(); + #[cfg(not(feature = "std"))] + let re = nostd_compat(re); + #[cfg(feature = "std")] + let re = re?; // Propagate any errors. Ok(CompiledRegex::compiled(move |test| run_test(&re, test))) } diff --git a/tests/suite_string.rs b/tests/suite_string.rs index 1e5bf0bb3..c9ab267b6 100644 --- a/tests/suite_string.rs +++ b/tests/suite_string.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "std"))] +use crate::nostd_compat; use { anyhow::Result, regex::{Regex, RegexBuilder}, @@ -98,7 +100,11 @@ fn compiler( .case_insensitive(test.case_insensitive()) .unicode(test.unicode()) .line_terminator(test.line_terminator()) - .build()?; + .build(); + #[cfg(not(feature = "std"))] + let re = nostd_compat(re); + #[cfg(feature = "std")] + let re = re?; // Propagate any errors. Ok(CompiledRegex::compiled(move |test| run_test(&re, test))) } diff --git a/tests/suite_string_set.rs b/tests/suite_string_set.rs index dffdc7081..28d253826 100644 --- a/tests/suite_string_set.rs +++ b/tests/suite_string_set.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "std"))] +use crate::nostd_compat; use { anyhow::Result, regex::{RegexSet, RegexSetBuilder}, @@ -74,6 +76,10 @@ fn compiler( .case_insensitive(test.case_insensitive()) .unicode(test.unicode()) .line_terminator(test.line_terminator()) - .build()?; + .build(); + #[cfg(not(feature = "std"))] + let re = nostd_compat(re); + #[cfg(feature = "std")] + let re = re?; // Propagate any errors. Ok(CompiledRegex::compiled(move |test| run_test(&re, test))) }