From 7cc45cfabc7e8e0fbf3a11a577f91026a470e425 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:54:30 +0000 Subject: [PATCH] test: Add `test_exception` --- test/CMakeLists.txt | 2 +- test/test_exception.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/test_exception.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f61e5d2..74e8f82 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -set(test_names test_subprocess test_cat test_env test_err_redirection test_split test_main test_ret_code) +set(test_names test_subprocess test_cat test_env test_err_redirection test_exception test_split test_main test_ret_code) set(test_files env_script.sh write_err.sh write_err.txt) diff --git a/test/test_exception.cc b/test/test_exception.cc new file mode 100644 index 0000000..51f4fb1 --- /dev/null +++ b/test/test_exception.cc @@ -0,0 +1,27 @@ +#include +#include +#include + +namespace sp = subprocess; + +void test_exception() +{ + bool caught = false; + try { + auto p = sp::Popen("invalid_command"); + assert(false); // Expected to throw + } catch (sp::CalledProcessError& e) { +#ifdef __USING_WINDOWS__ + assert(std::strstr(e.what(), "CreateProcess failed: The system cannot find the file specified.")); +#else + assert(std::strstr(e.what(), "execve failed: No such file or directory")); +#endif + caught = true; + } + assert(caught); +} + +int main() { + test_exception(); + return 0; +}