X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ca5016d4ddae7f9a2d72a16326e9339f8d3e8748..1ec4e9c2b72a01a7ba7da569e0f8d04a81aaba10:/tests/exec/exec.cpp?ds=sidebyside diff --git a/tests/exec/exec.cpp b/tests/exec/exec.cpp index 566ca72044..4355808d3f 100644 --- a/tests/exec/exec.cpp +++ b/tests/exec/exec.cpp @@ -25,13 +25,13 @@ #ifdef __UNIX__ #define COMMAND "echo hi" #define ASYNC_COMMAND "xclock" - #define SHELL_COMMAND "echo hi from shell" - #define REDIRECT_COMMAND "cat -n Makefile" -#elif defined(__WXMSW__) + #define SHELL_COMMAND "echo hi from shell>/dev/null" + #define COMMAND_NO_OUTPUT "echo -n" +#elif defined(__WINDOWS__) #define COMMAND "cmd.exe /c \"echo hi\"" #define ASYNC_COMMAND "notepad" - #define SHELL_COMMAND "echo hi" - #define REDIRECT_COMMAND COMMAND + #define SHELL_COMMAND "echo hi > nul:" + #define COMMAND_NO_OUTPUT COMMAND " > nul:" #else #error "no command to exec" #endif // OS @@ -62,7 +62,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( ExecTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExecTestCase, "ExecTestCase" ); @@ -73,18 +73,26 @@ void ExecTestCase::TestShell() void ExecTestCase::TestExecute() { - // test sync exec: - CPPUNIT_ASSERT( wxExecute(COMMAND, wxEXEC_SYNC) == 0 ); + // test sync exec (with a command not producing any output to avoid + // interfering with the test): + CPPUNIT_ASSERT( wxExecute(COMMAND_NO_OUTPUT, wxEXEC_SYNC) == 0 ); // test asynch exec long pid = wxExecute(ASYNC_COMMAND, wxEXEC_ASYNC); CPPUNIT_ASSERT( pid != 0 ); - CPPUNIT_ASSERT( wxKill(pid) == 0 ); + + // NOTE: under Windows the first wxKill() invocation with wxSIGTERM + // may fail if the system is fast and the ASYNC_COMMAND app + // doesn't manage to create its HWND before our wxKill() is + // executed; in that case we "fall back" to the second invocation + // with wxSIGKILL (which should always succeed) + CPPUNIT_ASSERT( wxKill(pid, wxSIGTERM) == 0 || + wxKill(pid, wxSIGKILL) == 0 ); // test running COMMAND again, but this time with redirection: - wxArrayString stdout; - CPPUNIT_ASSERT( wxExecute(COMMAND, stdout, wxEXEC_SYNC) == 0 ); - CPPUNIT_ASSERT( stdout[0] == "hi" ); + wxArrayString stdout_arr; + CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND, stdout_arr, wxEXEC_SYNC) ); + CPPUNIT_ASSERT_EQUAL( "hi", stdout_arr[0] ); } void ExecTestCase::TestProcess() @@ -93,23 +101,25 @@ void ExecTestCase::TestProcess() wxProcess *proc = new wxProcess; long pid = wxExecute(ASYNC_COMMAND, wxEXEC_ASYNC, proc); CPPUNIT_ASSERT( proc->GetPid() == pid && pid != 0 ); - + // we're not going to process the wxEVT_END_PROCESS event, // so the proc instance will auto-delete itself after we kill // the asynch process: - CPPUNIT_ASSERT( wxKill(pid) == 0 ); + CPPUNIT_ASSERT( wxKill(pid, wxSIGTERM) == 0 || + wxKill(pid, wxSIGKILL) == 0 ); + - // test wxExecute with wxProcess and REDIRECTION wxProcess *proc2 = new wxProcess; proc2->Redirect(); CPPUNIT_ASSERT( wxExecute(COMMAND, wxEXEC_SYNC, proc2) == 0 ); - - wxStringOutputStream stdout; + + wxStringOutputStream stdout_stream; CPPUNIT_ASSERT( proc2->GetInputStream() ); - CPPUNIT_ASSERT( proc2->GetInputStream()->Read(stdout).GetLastError() == wxSTREAM_EOF ); - - wxString str(stdout.GetString()); + CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, + proc2->GetInputStream()->Read(stdout_stream).GetLastError() ); + + wxString str(stdout_stream.GetString()); CPPUNIT_ASSERT_EQUAL( "hi", str.Trim() ); }