From c7349240804c3cbac98ae7d2ffe3b6f5f9bd8ef5 Mon Sep 17 00:00:00 2001 From: Qingsong Chen Date: Thu, 29 Jan 2026 09:25:02 +0000 Subject: [PATCH] Enable regex matching in run_cmd_and_expect --- test/nixos/common/framework/src/session.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/nixos/common/framework/src/session.rs b/test/nixos/common/framework/src/session.rs index 04f4a4448..45201b7b6 100644 --- a/test/nixos/common/framework/src/session.rs +++ b/test/nixos/common/framework/src/session.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use rexpect::session::PtySession; +use rexpect::{reader::Regex, session::PtySession}; use super::Error; @@ -153,9 +153,9 @@ impl Session { Ok(()) } - /// Executes a command and verifies its output contains expected text. + /// Executes a command and verifies its output matches expected regex. /// - /// This method runs the command and checks that the specified string appears + /// This method runs the command and checks that the specified regex appears /// in the output. This is useful for validating command results. /// /// Returns an error if: @@ -178,6 +178,9 @@ impl Session { /// // Verify system information /// nixos_shell.run_cmd_and_expect("cat /etc/os-release", "NixOS")?; /// + /// // Verify output matches regex + /// nixos_shell.run_cmd_and_expect_regex("echo 'Hello, World!'", "(?m)^H.*!$")?; + /// /// Ok(()) /// } /// ``` @@ -191,7 +194,7 @@ impl Session { Ok(unread) => { let cleaned_unread = String::from_utf8_lossy(&strip_ansi_escapes::strip(&unread)).to_string(); - if !cleaned_unread.contains(expected) { + if !Regex::new(expected)?.is_match(&cleaned_unread) { println!("=== Unexpected Output ==="); println!("Expected: {}", expected); println!("Output before prompt:\n{}", cleaned_unread);