From 6151c6fe006d0e474d8c1f86d49a6a827edf7dbf Mon Sep 17 00:00:00 2001 From: Andrea Manzini Date: Sun, 26 May 2024 18:26:46 +0200 Subject: [PATCH] Test match using regular expressions --- Cargo.lock | 1 + Cargo.toml | 1 + tests/integration_test.rs | 38 ++++++++++++++++++-------------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 814e943..6edffcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -765,6 +765,7 @@ dependencies = [ "mockall", "nix", "procfs", + "regex", "tempfile", "tokio", "windows-sys 0.52.0", diff --git a/Cargo.toml b/Cargo.toml index 98d84f8..09883e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,3 +40,4 @@ features = [ assert_cmd = "2.0.14" tempfile = "3.10.1" mockall = "0.12.1" +regex = "1.10.4" diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 6ee506d..67b2c52 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -4,6 +4,8 @@ use utils::start_listener_process; use assert_cmd::Command; use tempfile::tempdir; +use regex::bytes::Regex; + #[test] fn test_basic_kill_no_process() { let mut cmd = Command::cargo_bin("killport").unwrap(); @@ -21,12 +23,11 @@ fn test_basic_kill_process() { let mut child = start_listener_process(tempdir_path, 8080); let mut cmd = Command::cargo_bin("killport").unwrap(); - cmd.args(&["8080"]) + let command=cmd.args(&["8080"]) .assert() - .success() - .stdout("Successfully killed process 'mock_process' listening on port 8080\n"); - - // Clean up + .success(); + let re = Regex::new(r"Successfully killed process '(\/tmp\/\.tmp\w+\/)?mock_process' listening on port 8080\n").unwrap(); + assert!(re.is_match(&command.get_output().stdout)); let _ = child.kill(); let _ = child.wait(); } @@ -40,13 +41,11 @@ fn test_signal_handling() { for signal in ["sighup", "sigint", "sigkill"].iter() { let mut child = start_listener_process(tempdir_path, 8081); let mut cmd = Command::cargo_bin("killport").unwrap(); - cmd.args(&["8081", "-s", signal]) + let command=cmd.args(&["8081", "-s", signal]) .assert() - .success() - .stdout(format!( - "Successfully killed process 'mock_process' listening on port 8081\n" - )); - + .success(); + let re=Regex::new(r"Successfully killed process '(\/tmp\/\.tmp\w+\/)?mock_process' listening on port 8081\n").unwrap(); + assert!(re.is_match(&command.get_output().stdout)); // Clean up let _ = child.kill(); let _ = child.wait(); @@ -62,12 +61,11 @@ fn test_mode_option() { for mode in ["auto", "process"].iter() { let mut child = start_listener_process(tempdir_path, 8082); let mut cmd = Command::cargo_bin("killport").unwrap(); - cmd.args(&["8082", "--mode", mode]) + let command=cmd.args(&["8082", "--mode", mode]) .assert() - .success() - .stdout(format!( - "Successfully killed process 'mock_process' listening on port 8082\n" - )); + .success(); + let re=Regex::new(r"Successfully killed process '(\/tmp\/\.tmp\w+\/)?mock_process' listening on port 8082\n").unwrap(); + assert!(re.is_match(&command.get_output().stdout)); // Clean up let _ = child.kill(); let _ = child.wait(); @@ -100,11 +98,11 @@ fn test_dry_run_option() { let mut child = start_listener_process(tempdir_path, 8083); let mut cmd = Command::cargo_bin("killport").unwrap(); - cmd.args(&["8083", "--dry-run"]) + let command=cmd.args(&["8083", "--dry-run"]) .assert() - .success() - .stdout("Would kill process 'mock_process' listening on port 8083\n"); - + .success(); + let re=Regex::new(r"Would kill process '(\/tmp\/\.tmp\w+\/)?mock_process' listening on port 8083\n").unwrap(); + assert!(re.is_match(&command.get_output().stdout)); // Clean up let _ = child.kill(); let _ = child.wait();