From 3b71571e79415b82e10b3543ffd3992772f5b5aa Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 18 Jun 2018 09:32:07 +0200 Subject: [PATCH 1/5] Use exit code 2 to indicate error Exit code 1 was shared to indicate both "no results" and "error." Use status code 2 to indicate errors, similar to grep's behavior. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 761348f3b..e374338b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ fn main() { Ok(_) => process::exit(0), Err(err) => { eprintln!("{}", err); - process::exit(1); + process::exit(2); } } } From 3b937ec2061284d74d859364bc8b5be7b7decafc Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 18 Jun 2018 21:09:01 +0200 Subject: [PATCH 2/5] Add exit code integration tests --- tests/tests.rs | 30 ++++++++++++++++++++++++++++++ tests/workdir.rs | 9 +++++++++ 2 files changed, 39 insertions(+) diff --git a/tests/tests.rs b/tests/tests.rs index 0ee4995b2..856ab6eaa 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2191,3 +2191,33 @@ fn type_list() { // This can change over time, so just make sure we print something. assert!(!lines.is_empty()); } + +// See: https://github.com/BurntSushi/ripgrep/issues/948 +sherlock!( + feature_948_match, + ".", + ".", + |wd: WorkDir, mut cmd: Command| { + wd.assert_exit_code(0, &mut cmd); + } +); + +// See: https://github.com/BurntSushi/ripgrep/issues/948 +sherlock!( + feature_948_no_match, + "6d28e48b5224a42b167e{10}", + ".", + |wd: WorkDir, mut cmd: Command| { + wd.assert_exit_code(1, &mut cmd); + } +); + +// See: https://github.com/BurntSushi/ripgrep/issues/948 +sherlock!( + feature_948_error, + "*", + ".", + |wd: WorkDir, mut cmd: Command| { + wd.assert_exit_code(2, &mut cmd); + } +); diff --git a/tests/workdir.rs b/tests/workdir.rs index 3c47e9483..9f9a59e45 100644 --- a/tests/workdir.rs +++ b/tests/workdir.rs @@ -273,6 +273,15 @@ impl WorkDir { } } + /// Runs the given command and asserts that its exit code matches expected exit code. + pub fn assert_exit_code(&self, expected_code: i32, cmd: &mut process::Command) { + let code = cmd.status() + .expect("no status") + .code() + .expect("no exit code"); + assert_eq!(expected_code, code); + } + /// Runs the given command and asserts that something was printed to /// stderr. pub fn assert_non_empty_stderr(&self, cmd: &mut process::Command) { From 469547138590295d295188c7b258b49d69b78d4e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 18 Jun 2018 21:14:16 +0200 Subject: [PATCH 3/5] Add assertion failure message --- tests/workdir.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/workdir.rs b/tests/workdir.rs index 9f9a59e45..cf978885d 100644 --- a/tests/workdir.rs +++ b/tests/workdir.rs @@ -279,7 +279,15 @@ impl WorkDir { .expect("no status") .code() .expect("no exit code"); - assert_eq!(expected_code, code); + assert_eq!( + expected_code, code, + "\n\n===== {:?} =====\n\ + expected exit code did not match\ + \n\nexpected: {}\ + \n\nfound: {}\ + \n\n=====\n", + cmd, expected_code, code + ); } /// Runs the given command and asserts that something was printed to From fcaf6cce59120e578229d37b491718c275b03b5f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 19 Jun 2018 08:00:06 +0200 Subject: [PATCH 4/5] Replace trivial expect() with unwrap() --- tests/workdir.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/workdir.rs b/tests/workdir.rs index cf978885d..99f0bf3fb 100644 --- a/tests/workdir.rs +++ b/tests/workdir.rs @@ -261,24 +261,26 @@ impl WorkDir { pub fn assert_err(&self, cmd: &mut process::Command) { let o = cmd.output().unwrap(); if o.status.success() { - panic!("\n\n===== {:?} =====\n\ - command succeeded but expected failure!\ - \n\ncwd: {}\ - \n\nstatus: {}\ - \n\nstdout: {}\n\nstderr: {}\ - \n\n=====\n", - cmd, self.dir.display(), o.status, - String::from_utf8_lossy(&o.stdout), - String::from_utf8_lossy(&o.stderr)); + panic!( + "\n\n===== {:?} =====\n\ + command succeeded but expected failure!\ + \n\ncwd: {}\ + \n\nstatus: {}\ + \n\nstdout: {}\n\nstderr: {}\ + \n\n=====\n", + cmd, + self.dir.display(), + o.status, + String::from_utf8_lossy(&o.stdout), + String::from_utf8_lossy(&o.stderr) + ); } } /// Runs the given command and asserts that its exit code matches expected exit code. pub fn assert_exit_code(&self, expected_code: i32, cmd: &mut process::Command) { - let code = cmd.status() - .expect("no status") - .code() - .expect("no exit code"); + let code = cmd.status().unwrap().code().unwrap(); + assert_eq!( expected_code, code, "\n\n===== {:?} =====\n\ From 319d219d8dc5d8f2ced28bfc859e6bda67f6442d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 19 Jun 2018 08:01:59 +0200 Subject: [PATCH 5/5] Update test names to be more descriptive --- tests/tests.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index 856ab6eaa..f3cc8f06c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2194,7 +2194,7 @@ fn type_list() { // See: https://github.com/BurntSushi/ripgrep/issues/948 sherlock!( - feature_948_match, + exit_code_match_success, ".", ".", |wd: WorkDir, mut cmd: Command| { @@ -2204,7 +2204,7 @@ sherlock!( // See: https://github.com/BurntSushi/ripgrep/issues/948 sherlock!( - feature_948_no_match, + exit_code_no_match, "6d28e48b5224a42b167e{10}", ".", |wd: WorkDir, mut cmd: Command| { @@ -2214,7 +2214,7 @@ sherlock!( // See: https://github.com/BurntSushi/ripgrep/issues/948 sherlock!( - feature_948_error, + exit_code_error, "*", ".", |wd: WorkDir, mut cmd: Command| {