mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-08 09:05:34 +00:00
Add explicit Unicode mode to all regex patterns for proper Unicode handling
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
This commit is contained in:
parent
9c92c7e345
commit
48254a93e1
4 changed files with 47 additions and 16 deletions
|
|
@ -71,9 +71,9 @@ fn run(params: &mut RunParams, test_data_glob: &str) -> Result<()> {
|
|||
(".", test_data_glob)
|
||||
};
|
||||
|
||||
// Convert glob pattern to a simple matcher
|
||||
// Convert glob pattern to a simple matcher with Unicode support
|
||||
let pattern_regex = pattern.replace("*", ".*");
|
||||
let regex = regex::Regex::new(&format!("^{}$", pattern_regex))?;
|
||||
let regex = regex::Regex::new(&format!("(?u)^{}$", pattern_regex))?;
|
||||
|
||||
// Walk the directory and collect matching files
|
||||
for entry in WalkDir::new(base_dir).min_depth(1).max_depth(1) {
|
||||
|
|
|
|||
|
|
@ -218,7 +218,14 @@ impl TestEnvironment {
|
|||
|
||||
/// Search for a pattern in files (like grep)
|
||||
pub fn grep_files(&mut self, pattern: &str, files: &[String]) -> Result<()> {
|
||||
let regex = Regex::new(pattern)
|
||||
// Enable Unicode mode for proper Unicode character matching in grep patterns
|
||||
let pattern_with_flags = if pattern.starts_with("(?") {
|
||||
// Pattern already has flags, don't add more
|
||||
pattern.to_string()
|
||||
} else {
|
||||
format!("(?u){}", pattern)
|
||||
};
|
||||
let regex = Regex::new(&pattern_with_flags)
|
||||
.map_err(|e| Error::command_error("grep", format!("Invalid regex: {}", e)))?;
|
||||
|
||||
let mut _found_matches = false;
|
||||
|
|
|
|||
|
|
@ -236,7 +236,9 @@ impl TestEnvironment {
|
|||
|| expected_substituted.contains('*')
|
||||
|| expected_substituted.contains('.')
|
||||
{
|
||||
let regex_pattern = format!("(?s){}", expected_substituted); // (?s) enables DOTALL mode
|
||||
// Enable DOTALL mode (?s) for . to match newlines
|
||||
// Enable Unicode mode (?u) for proper Unicode character matching
|
||||
let regex_pattern = format!("(?su){}", expected_substituted);
|
||||
let regex = Regex::new(®ex_pattern)
|
||||
.map_err(|e| Error::command_error(output_type, format!("Invalid regex: {}", e)))?;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ stdout "Success ✓"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Unicode stdout exact match should work: {:?}",
|
||||
|
|
@ -42,7 +42,7 @@ stderr "Error ✗"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Unicode stderr exact match should work: {:?}",
|
||||
|
|
@ -65,7 +65,7 @@ stdout "Success .* completed"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Unicode in regex pattern should work: {:?}",
|
||||
|
|
@ -88,7 +88,7 @@ stdout "Tests: ✓ ✗ ⚠ ℹ"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Multiple Unicode characters should work: {:?}",
|
||||
|
|
@ -111,12 +111,8 @@ stdout "Status: 🎉 Success!"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Emoji in output should work: {:?}",
|
||||
result
|
||||
);
|
||||
|
||||
assert!(result.is_ok(), "Emoji in output should work: {:?}", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -134,7 +130,7 @@ stdout "Result: ✓ \(success\)"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Unicode with regex special chars should work: {:?}",
|
||||
|
|
@ -157,10 +153,36 @@ stdout "A.B"
|
|||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Dot in regex should match Unicode character: {:?}",
|
||||
result
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_grep_with_unicode() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let testdata_dir = temp_dir.path().join("testdata");
|
||||
fs::create_dir(&testdata_dir).unwrap();
|
||||
|
||||
// Test grep command with Unicode content
|
||||
let test_content = r#"grep "Success.*✓" test.txt
|
||||
stdout "test.txt:1: Success ✓"
|
||||
|
||||
-- test.txt --
|
||||
Success ✓
|
||||
"#;
|
||||
|
||||
let test_file = testdata_dir.join("grep_unicode_test.txt");
|
||||
fs::write(&test_file, test_content).unwrap();
|
||||
|
||||
let result = testscript::run(testdata_dir.to_string_lossy()).execute();
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Grep with Unicode should work: {:?}",
|
||||
result
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue