2023-12-10 03:47:43 +08:00
|
|
|
use pcre2::utf32::Regex;
|
|
|
|
|
|
|
|
use crate::parse_constants::{
|
|
|
|
ERROR_BAD_VAR_CHAR1, ERROR_BRACKETED_VARIABLE1, ERROR_BRACKETED_VARIABLE_QUOTED1,
|
|
|
|
ERROR_NOT_ARGV_AT, ERROR_NOT_ARGV_COUNT, ERROR_NOT_ARGV_STAR, ERROR_NOT_PID, ERROR_NOT_STATUS,
|
|
|
|
ERROR_NO_VAR_NAME,
|
|
|
|
};
|
|
|
|
use crate::parse_util::parse_util_detect_errors;
|
2024-01-04 03:57:28 +08:00
|
|
|
use crate::tests::prelude::*;
|
2023-12-10 03:47:43 +08:00
|
|
|
use crate::wchar::prelude::*;
|
|
|
|
use crate::wchar_ext::WExt;
|
|
|
|
|
2024-01-04 03:57:28 +08:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn test_error_messages() {
|
|
|
|
test_init();
|
2023-12-10 03:47:43 +08:00
|
|
|
// Given a format string, returns a list of non-empty strings separated by format specifiers. The
|
|
|
|
// format specifiers themselves are omitted.
|
|
|
|
fn separate_by_format_specifiers(format: &wstr) -> Vec<&wstr> {
|
2024-01-19 13:09:20 +08:00
|
|
|
let format_specifier_regex = Regex::new(L!(r"%l?[cds]").as_char_slice()).unwrap();
|
2023-12-10 03:47:43 +08:00
|
|
|
let mut result = vec![];
|
|
|
|
let mut offset = 0;
|
|
|
|
for mtch in format_specifier_regex.find_iter(format.as_char_slice()) {
|
|
|
|
let mtch = mtch.unwrap();
|
|
|
|
result.push(&format[offset..mtch.start()]);
|
|
|
|
offset = mtch.end();
|
|
|
|
}
|
2024-01-19 13:09:20 +08:00
|
|
|
result.push(&format[offset..]);
|
2023-12-10 03:47:43 +08:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a format string 'format', return true if the string may have been produced by that format
|
|
|
|
// string. We do this by splitting the format string around the format specifiers, and then ensuring
|
|
|
|
// that each of the remaining chunks is found (in order) in the string.
|
|
|
|
fn string_matches_format(s: &wstr, format: &wstr) -> bool {
|
|
|
|
let components = separate_by_format_specifiers(format);
|
2024-01-19 13:09:20 +08:00
|
|
|
assert!(!components.is_empty());
|
2023-12-10 03:47:43 +08:00
|
|
|
let mut idx = 0;
|
|
|
|
for component in components {
|
|
|
|
let Some(relpos) = s[idx..].find(component) else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
idx += relpos + component.len();
|
|
|
|
assert!(idx <= s.len());
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! validate {
|
|
|
|
($src:expr, $error_text_format:expr) => {
|
|
|
|
let mut errors = vec![];
|
|
|
|
let res = parse_util_detect_errors(L!($src), Some(&mut errors), false);
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert!(
|
|
|
|
string_matches_format(&errors[0].text, L!($error_text_format)),
|
2024-01-20 18:28:14 +08:00
|
|
|
"command '{}' is expected to match error pattern '{}' but is '{}'",
|
|
|
|
$src,
|
|
|
|
$error_text_format,
|
|
|
|
&errors[0].text
|
2023-12-10 03:47:43 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
validate!("echo $^", ERROR_BAD_VAR_CHAR1);
|
|
|
|
validate!("echo foo${a}bar", ERROR_BRACKETED_VARIABLE1);
|
|
|
|
validate!("echo foo\"${a}\"bar", ERROR_BRACKETED_VARIABLE_QUOTED1);
|
|
|
|
validate!("echo foo\"${\"bar", ERROR_BAD_VAR_CHAR1);
|
|
|
|
validate!("echo $?", ERROR_NOT_STATUS);
|
|
|
|
validate!("echo $$", ERROR_NOT_PID);
|
|
|
|
validate!("echo $#", ERROR_NOT_ARGV_COUNT);
|
|
|
|
validate!("echo $@", ERROR_NOT_ARGV_AT);
|
|
|
|
validate!("echo $*", ERROR_NOT_ARGV_STAR);
|
|
|
|
validate!("echo $", ERROR_NO_VAR_NAME);
|
|
|
|
validate!("echo foo\"$\"bar", ERROR_NO_VAR_NAME);
|
|
|
|
validate!("echo \"foo\"$\"bar\"", ERROR_NO_VAR_NAME);
|
|
|
|
validate!("echo foo $ bar", ERROR_NO_VAR_NAME);
|
2024-01-04 03:57:28 +08:00
|
|
|
}
|