Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/uu/pgrep/src/process_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,20 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
pub fn find_matching_pids(settings: &Settings) -> UResult<Vec<ProcessInformation>> {
let mut pids = collect_matched_pids(settings)?;

let is_long_match = if settings.exact {
settings
.regex
.as_str()
.trim_matches('^')
.trim_matches('$')
.len()
> 15
} else {
settings.regex.as_str().len() > 15
};

if pids.is_empty() {
if !settings.full && settings.regex.as_str().len() > 15 {
if !settings.full && is_long_match {
let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\
Try `{} -f' option to match against the complete command line.", uucore::util_name());
return Err(USimpleError::new(1, msg));
Expand Down
29 changes: 28 additions & 1 deletion tests/by-util/test_pgrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ fn test_pidfile_fcntl_locked() {

// spawn a flock process that locks the file
let mut flock_process = Command::new("flock")
.arg("--fcntl")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change ?

.arg(temp_file.path())
.arg("sleep")
.arg("2")
Expand Down Expand Up @@ -718,3 +717,31 @@ fn test_env_multiple_filters() {
// Multiple filters use OR logic
new_ucmd!().arg("--env=PATH,NONEXISTENT").succeeds();
}

#[test]
#[cfg(target_os = "linux")]
fn test_exact_long_pattern_no_match() {
new_ucmd!()
.arg("-x")
.arg("12345678901234")
.fails()
.code_is(1);
}

#[test]
fn test_pattern_longer_than_15_characters() {
new_ucmd!()
.arg("1234567890123456")
.fails()
.code_is(1)
.stderr_contains("longer than 15 characters");
}

#[test]
#[cfg(target_os = "linux")]
fn test_pool_workqueue_release() {
new_ucmd!()
.arg("pool_workqueue_release")
.succeeds()
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
}
Loading