1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/exec/exec.cpp
3 // Purpose: test wxExecute()
4 // Author: Francesco Montorsi
5 // (based on console sample TestExecute() function)
8 // Copyright: (c) 2009 Francesco Montorsi
9 // (c) 2013 Rob Bresalier, Vadim Zeitlin
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
24 #include "wx/process.h"
25 #include "wx/sstream.h"
28 #define COMMAND "echo hi"
29 #define ASYNC_COMMAND "xclock"
30 #define SHELL_COMMAND "echo hi from shell>/dev/null"
31 #define COMMAND_NO_OUTPUT "echo -n"
32 #elif defined(__WINDOWS__)
33 #define COMMAND "cmd.exe /c \"echo hi\""
34 #define ASYNC_COMMAND "notepad"
35 #define SHELL_COMMAND "echo hi > nul:"
36 #define COMMAND_NO_OUTPUT COMMAND " > nul:"
38 #error "no command to exec"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 class ExecTestCase
: public CppUnit::TestCase
51 CPPUNIT_TEST_SUITE( ExecTestCase
);
52 CPPUNIT_TEST( TestShell
);
53 CPPUNIT_TEST( TestExecute
);
54 CPPUNIT_TEST( TestProcess
);
55 CPPUNIT_TEST_SUITE_END();
61 DECLARE_NO_COPY_CLASS(ExecTestCase
)
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( ExecTestCase
);
67 // also include in its own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExecTestCase
, "ExecTestCase" );
71 void ExecTestCase::TestShell()
73 CPPUNIT_ASSERT( wxShell(SHELL_COMMAND
) );
76 void ExecTestCase::TestExecute()
78 // test sync exec (with a command not producing any output to avoid
79 // interfering with the test):
80 CPPUNIT_ASSERT( wxExecute(COMMAND_NO_OUTPUT
, wxEXEC_SYNC
) == 0 );
83 long pid
= wxExecute(ASYNC_COMMAND
, wxEXEC_ASYNC
);
84 CPPUNIT_ASSERT( pid
!= 0 );
86 // NOTE: under Windows the first wxKill() invocation with wxSIGTERM
87 // may fail if the system is fast and the ASYNC_COMMAND app
88 // doesn't manage to create its HWND before our wxKill() is
89 // executed; in that case we "fall back" to the second invocation
90 // with wxSIGKILL (which should always succeed)
91 CPPUNIT_ASSERT( wxKill(pid
, wxSIGTERM
) == 0 ||
92 wxKill(pid
, wxSIGKILL
) == 0 );
94 // test running COMMAND again, but this time with redirection:
95 wxArrayString stdout_arr
;
96 CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND
, stdout_arr
, wxEXEC_SYNC
) );
97 CPPUNIT_ASSERT_EQUAL( "hi", stdout_arr
[0] );
100 void ExecTestCase::TestProcess()
102 // test wxExecute with wxProcess
103 wxProcess
*proc
= new wxProcess
;
104 long pid
= wxExecute(ASYNC_COMMAND
, wxEXEC_ASYNC
, proc
);
105 CPPUNIT_ASSERT( proc
->GetPid() == pid
&& pid
!= 0 );
107 // we're not going to process the wxEVT_END_PROCESS event,
108 // so the proc instance will auto-delete itself after we kill
109 // the asynch process:
110 CPPUNIT_ASSERT( wxKill(pid
, wxSIGTERM
) == 0 ||
111 wxKill(pid
, wxSIGKILL
) == 0 );
114 // test wxExecute with wxProcess and REDIRECTION
115 wxProcess
*proc2
= new wxProcess
;
117 CPPUNIT_ASSERT( wxExecute(COMMAND
, wxEXEC_SYNC
, proc2
) == 0 );
119 wxStringOutputStream stdout_stream
;
120 CPPUNIT_ASSERT( proc2
->GetInputStream() );
121 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF
,
122 proc2
->GetInputStream()->Read(stdout_stream
).GetLastError() );
124 wxString
str(stdout_stream
.GetString());
125 CPPUNIT_ASSERT_EQUAL( "hi", str
.Trim() );