Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test match using regular expressions #40

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ features = [
assert_cmd = "2.0.14"
tempfile = "3.10.1"
mockall = "0.12.1"
regex = "1.10.4"
63 changes: 28 additions & 35 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod utils;
use regex::bytes::Regex;
use utils::start_listener_process;

use assert_cmd::Command;
Expand All @@ -9,6 +10,15 @@ const MOCK_PROCESS_NAME: &str = "mock_process";
#[cfg(windows)]
const MOCK_PROCESS_NAME: &str = "mock_process.exe";

// test helper
fn assert_match(data: &[u8], msg: &str, port: u16) {
let re = Regex::new(&format!(
r"{msg} process '(\/tmp\/\.tmp\w+\/)?{MOCK_PROCESS_NAME}' listening on port {port}\n"
))
.unwrap();
assert!(re.is_match(data));
}

#[test]
fn test_basic_kill_no_process() {
let mut cmd = Command::cargo_bin("killport").unwrap();
Expand All @@ -23,13 +33,10 @@ fn test_basic_kill_no_process() {
fn test_basic_kill_process() {
let tempdir = tempdir().unwrap();
let tempdir_path = tempdir.path();
let mut child = start_listener_process(tempdir_path, 8080);

let mut child = start_listener_process(tempdir_path, 8081);
let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8080"]).assert().success().stdout(format!(
"Successfully killed process '{MOCK_PROCESS_NAME}' listening on port 8080\n"
));

let command = cmd.args(&["8081"]).assert().success();
assert_match(&command.get_output().stdout, "Successfully killed", 8081);
// Clean up
let _ = child.kill();
let _ = child.wait();
Expand All @@ -42,15 +49,10 @@ fn test_signal_handling() {
let tempdir_path = tempdir.path();

for signal in ["sighup", "sigint", "sigkill"].iter() {
let mut child = start_listener_process(tempdir_path, 8081);
let mut child = start_listener_process(tempdir_path, 8082);
let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8081", "-s", signal])
.assert()
.success()
.stdout(format!(
"Successfully killed process '{MOCK_PROCESS_NAME}' listening on port 8081\n"
));

let command = cmd.args(&["8082", "-s", signal]).assert().success();
assert_match(&command.get_output().stdout, "Successfully killed", 8082);
// Clean up
let _ = child.kill();
let _ = child.wait();
Expand All @@ -64,53 +66,44 @@ fn test_mode_option() {
let tempdir_path = tempdir.path();

for mode in ["auto", "process"].iter() {
let mut child = start_listener_process(tempdir_path, 8082);
let mut child = start_listener_process(tempdir_path, 8083);
let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8082", "--mode", mode])
.assert()
.success()
.stdout(format!(
"Successfully killed process '{MOCK_PROCESS_NAME}' listening on port 8082\n"
));
let command = cmd.args(&["8083", "--mode", mode]).assert().success();
assert_match(&command.get_output().stdout, "Successfully killed", 8083);
// Clean up
let _ = child.kill();
let _ = child.wait();
}

let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8082", "--mode", "auto"])
cmd.args(&["8083", "--mode", "auto"])
.assert()
.success()
.stdout(format!("No service found using port 8082\n"));
.stdout(format!("No service found using port 8083\n"));

let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8082", "--mode", "process"])
cmd.args(&["8083", "--mode", "process"])
.assert()
.success()
.stdout(format!("No process found using port 8082\n"));
.stdout(format!("No process found using port 8083\n"));

let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8082", "--mode", "container"])
cmd.args(&["8083", "--mode", "container"])
.assert()
.success()
.stdout(format!("No container found using port 8082\n"));
.stdout(format!("No container found using port 8083\n"));
}

/// Tests the `--dry-run` option to ensure no actual killing of the process.
#[test]
fn test_dry_run_option() {
let tempdir = tempdir().unwrap();
let tempdir_path = tempdir.path();
let mut child = start_listener_process(tempdir_path, 8083);
let mut child = start_listener_process(tempdir_path, 8084);

let mut cmd = Command::cargo_bin("killport").unwrap();
cmd.args(&["8083", "--dry-run"])
.assert()
.success()
.stdout(format!(
"Would kill process '{MOCK_PROCESS_NAME}' listening on port 8083\n"
));

let command = cmd.args(&["8084", "--dry-run"]).assert().success();
assert_match(&command.get_output().stdout, "Would kill", 8084);
// Clean up
let _ = child.kill();
let _ = child.wait();
Expand Down
Loading