test(linux): check the resolutions parsed from xrandr output

Move the parsing of resolutions() into resolutions_from_xrandr(), which takes the xrandr output, so the test asserts the returned list instead of the captured mode lines. resolutions() runs xrandr and calls it; the parsing is unchanged.
This commit is contained in:
L4XB committed 2026-10-23 10:20:53 +02:00
1 parent 03f720c4d9
commit 854beb1af3
1 file changed
+20 -22
+20 -22
View File
@@ -1940,17 +1940,23 @@ fn get_xrandr_conn_pat(name: &str) -> String {
) )
} }
// The mode lines below an output. A mode name can have a suffix after its size, like the "i" of
// "1920x1080i", so a TV's interlaced mode does not end the list.
const XRANDR_MODES_PAT: &str = r"(?P<resolutions>(\s*\d+x\d+\S*\s+\d+.*\n)+)";
pub fn resolutions(name: &str) -> Vec<Resolution> { pub fn resolutions(name: &str) -> Vec<Resolution> {
let resolutions_pat = XRANDR_MODES_PAT; match run_cmds("xrandr --query | tr -s ' '") {
Ok(xrandr_output) => resolutions_from_xrandr(name, &xrandr_output),
Err(e) => {
log::error!("Failed to run xrandr query, {}", e);
vec![]
}
}
}
fn resolutions_from_xrandr(name: &str, xrandr_output: &str) -> Vec<Resolution> {
// A mode name can have a suffix after its size, like the "i" of "1920x1080i", so a TV's
// interlaced mode does not end the list.
let resolutions_pat = r"(?P<resolutions>(\s*\d+x\d+\S*\s+\d+.*\n)+)";
let connected_pat = get_xrandr_conn_pat(name); let connected_pat = get_xrandr_conn_pat(name);
let mut v = vec![]; let mut v = vec![];
if let Ok(re) = Regex::new(&format!("{}{}", connected_pat, resolutions_pat)) { if let Ok(re) = Regex::new(&format!("{}{}", connected_pat, resolutions_pat)) {
match run_cmds("xrandr --query | tr -s ' '") {
Ok(xrandr_output) => {
// There'are different kinds of xrandr output. // There'are different kinds of xrandr output.
/* /*
1. 1.
@@ -1974,7 +1980,7 @@ pub fn resolutions(name: &str) -> Vec<Resolution> {
rdp0 connected primary 1920x1080+0+0 0mm x 0mm rdp0 connected primary 1920x1080+0+0 0mm x 0mm
*/ */
if let Some(caps) = re.captures(&xrandr_output) { if let Some(caps) = re.captures(xrandr_output) {
if let Some(resolutions) = caps.name("resolutions") { if let Some(resolutions) = caps.name("resolutions") {
let resolution_pat = let resolution_pat =
r"\s*(?P<width>\d+)x(?P<height>\d+)\s+(?P<rates>(\d+\.\d+\D*)+)\s*\n"; r"\s*(?P<width>\d+)x(?P<height>\d+)\s+(?P<rates>(\d+\.\d+\D*)+)\s*\n";
@@ -1983,8 +1989,7 @@ pub fn resolutions(name: &str) -> Vec<Resolution> {
return vec![]; return vec![];
}; };
for resolution_caps in resolution_re.captures_iter(resolutions.as_str()) { for resolution_caps in resolution_re.captures_iter(resolutions.as_str()) {
if let Some((width, height)) = if let Some((width, height)) = get_width_height_from_captures(&resolution_caps)
get_width_height_from_captures(&resolution_caps)
{ {
let resolution = Resolution { let resolution = Resolution {
width, width,
@@ -1999,9 +2004,6 @@ pub fn resolutions(name: &str) -> Vec<Resolution> {
} }
} }
} }
Err(e) => log::error!("Failed to run xrandr query, {}", e),
}
}
v v
} }
@@ -2051,15 +2053,11 @@ HDMI-0 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis
640x480 75.00 59.94 640x480 75.00 59.94
DP-0 disconnected (normal left inverted right x axis y axis) DP-0 disconnected (normal left inverted right x axis y axis)
"; ";
let re = Regex::new(&format!( let sizes: Vec<_> = resolutions_from_xrandr("HDMI-0", xrandr_output)
"{}{}", .iter()
get_xrandr_conn_pat("HDMI-0"), .map(|r| (r.width, r.height))
XRANDR_MODES_PAT .collect();
)) assert_eq!(sizes, vec![(1920, 1080), (1280, 720), (640, 480)]);
.unwrap();
let caps = re.captures(xrandr_output).unwrap();
assert!(caps["resolutions"].contains(" 1280x720 "));
assert!(caps["resolutions"].contains(" 640x480 "));
} }
} }