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"
26 #include "wx/evtloop.h"
28 #include "wx/filename.h"
29 #include "wx/mstream.h"
30 #include "wx/scopeguard.h"
31 #include "wx/txtstrm.h"
35 #define COMMAND "echo hi"
36 #define COMMAND_STDERR "cat nonexistentfile"
37 #define ASYNC_COMMAND "xclock"
38 #define SHELL_COMMAND "echo hi from shell>/dev/null"
39 #define COMMAND_NO_OUTPUT "echo -n"
40 #elif defined(__WINDOWS__)
41 #define COMMAND "cmd.exe /c \"echo hi\""
42 #define COMMAND_STDERR "cmd.exe /c \"type nonexistentfile\""
43 #define ASYNC_COMMAND "notepad"
44 #define SHELL_COMMAND "echo hi > nul:"
45 #define COMMAND_NO_OUTPUT COMMAND " > nul:"
47 #error "no command to exec"
50 #define SLEEP_END_STRING "Done sleeping"
54 enum AsyncExecLoopExitEnum
56 AsyncExec_DontExitLoop
,
59 } // anonymous namespace
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 class ExecTestCase
: public CppUnit::TestCase
71 CPPUNIT_TEST_SUITE( ExecTestCase
);
72 CPPUNIT_TEST( TestShell
);
73 CPPUNIT_TEST( TestExecute
);
74 CPPUNIT_TEST( TestProcess
);
75 CPPUNIT_TEST( TestAsync
);
76 CPPUNIT_TEST( TestAsyncRedirect
);
77 CPPUNIT_TEST( TestOverlappedSyncExecute
);
78 CPPUNIT_TEST_SUITE_END();
84 void TestAsyncRedirect();
85 void TestOverlappedSyncExecute();
87 // Helper: create an executable file sleeping for the given amount of
88 // seconds with the specified base name.
90 // Returns the name of the file.
91 static wxString
CreateSleepFile(const wxString
& basename
, int seconds
);
93 // Return the full command, to be passed to wxExecute(), launching the
94 // specified script file.
95 static wxString
MakeShellCommand(const wxString
& filename
);
98 // Helper of TestAsyncRedirect(): tests that the output of the given
99 // command on the given stream contains the expected string.
100 enum CheckStream
{ Check_Stdout
, Check_Stderr
};
102 void DoTestAsyncRedirect(const wxString
& command
,
104 const char* expectedContaining
);
106 // This class is used as a helper in order to run wxExecute(ASYNC)
107 // inside of an event loop.
108 class AsyncInEventLoop
: public wxTimer
111 AsyncInEventLoop() { }
113 long DoExecute(AsyncExecLoopExitEnum forceExitLoop_
,
114 const wxString
& command_
,
115 int flags_
= wxEXEC_ASYNC
,
116 wxProcess
* callback_
= NULL
)
118 forceExitLoop
= forceExitLoop_
;
121 callback
= callback_
;
125 // Trigger the timer to go off inside the event loop
126 // so that we can run wxExecute there.
129 // Run the event loop.
132 return wxExecuteReturnCode
;
137 // Run wxExecute inside the event loop.
138 wxExecuteReturnCode
= wxExecute(command
, flags
, callback
);
140 if (forceExitLoop
== AsyncExec_ExitLoop
)
142 wxEventLoop::GetActive()->Exit();
147 AsyncExecLoopExitEnum forceExitLoop
;
151 long wxExecuteReturnCode
;
154 DECLARE_NO_COPY_CLASS(ExecTestCase
)
157 // register in the unnamed registry so that these tests are run by default
158 CPPUNIT_TEST_SUITE_REGISTRATION( ExecTestCase
);
160 // also include in its own registry so that these tests can be run alone
161 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExecTestCase
, "ExecTestCase" );
164 void ExecTestCase::TestShell()
166 CPPUNIT_ASSERT( wxShell(SHELL_COMMAND
) );
169 void ExecTestCase::TestExecute()
171 AsyncInEventLoop asyncInEventLoop
;
175 // asyncInEventLoop.DoExecute will perform the
176 // call to wxExecute(ASYNC) in an event loop, as required by
177 // console test (and this same event loop will also
178 // be used in GUI test too, even though not required, just to have
180 long pid
= asyncInEventLoop
.DoExecute(AsyncExec_ExitLoop
, // Force exit of event loop right
181 // after the call to wxExecute()
182 ASYNC_COMMAND
, wxEXEC_ASYNC
);
183 CPPUNIT_ASSERT( pid
!= 0 );
185 // NOTE: under Windows the first wxKill() invocation with wxSIGTERM
186 // may fail if the system is fast and the ASYNC_COMMAND app
187 // doesn't manage to create its HWND before our wxKill() is
188 // executed; in that case we "fall back" to the second invocation
189 // with wxSIGKILL (which should always succeed)
190 CPPUNIT_ASSERT( wxKill(pid
, wxSIGTERM
) == 0 ||
191 wxKill(pid
, wxSIGKILL
) == 0 );
195 // Test the sync execution case with/without wxEXEC_NOEVENTS flag
196 // because we use either an event loop or wxSelectDispatcher
197 // depending on this flag, and we want to test both cases.
198 for (useNoeventsFlag
= 0; useNoeventsFlag
<=1 ; ++useNoeventsFlag
)
200 int execFlags
= wxEXEC_SYNC
;
204 execFlags
|= wxEXEC_NOEVENTS
;
207 // test sync exec (with a command not producing any output to avoid
208 // interfering with the test):
209 CPPUNIT_ASSERT( wxExecute(COMMAND_NO_OUTPUT
, execFlags
) == 0 );
211 // test running COMMAND again, but this time with redirection:
212 // and the expected data is on stdout.
213 wxArrayString stdout_arr
;
214 CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND
, stdout_arr
, execFlags
) );
215 CPPUNIT_ASSERT_EQUAL( "hi", stdout_arr
[0] );
217 // test running COMMAND_STDERR with redirection and the expected data
219 wxArrayString stderr_arr
;
221 CPPUNIT_ASSERT( wxExecute(COMMAND_STDERR
, stdout_arr
, stderr_arr
, execFlags
) != 0 );
223 // Check that there is something on stderr.
224 // In Unix systems, the 'cat' command has the name of the file it could not
225 // find in the error output.
226 // In Windows, the 'type' command outputs the following when it can't find
228 // "The system cannot find the file specified"
229 // In both cases, we expect the word 'file' to be in the stderr.
230 CPPUNIT_ASSERT( stderr_arr
[0].Contains("file") );
234 void ExecTestCase::TestProcess()
236 AsyncInEventLoop asyncInEventLoop
;
238 // test wxExecute with wxProcess
239 wxProcess
*proc
= new wxProcess
;
241 // asyncInEventLoop.DoExecute will perform the
242 // call to wxExecute(ASYNC) in an event loop, as required by
243 // console test (and this same event loop will also
244 // be used in GUI test too, even though not required, just to have
246 long pid
= asyncInEventLoop
.DoExecute(AsyncExec_ExitLoop
, // Force exit of event loop right
247 // after the call to wxExecute()
248 ASYNC_COMMAND
, wxEXEC_ASYNC
, proc
);
249 CPPUNIT_ASSERT( proc
->GetPid() == pid
&& pid
!= 0 );
251 // we're not going to process the wxEVT_END_PROCESS event,
252 // so the proc instance will auto-delete itself after we kill
253 // the asynch process:
254 CPPUNIT_ASSERT( wxKill(pid
, wxSIGTERM
) == 0 ||
255 wxKill(pid
, wxSIGKILL
) == 0 );
258 // test wxExecute with wxProcess and REDIRECTION
260 // Test the sync execution case with/without wxEXEC_NOEVENTS flag
261 // because we use either an event loop or wxSelectDispatcher
262 // depending on this flag, and we want to test both cases.
264 // First the default case, dispatching the events while waiting.
268 CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND
, wxEXEC_SYNC
, &proc2
) );
270 wxStringOutputStream procOutput
;
271 CPPUNIT_ASSERT( proc2
.GetInputStream() );
272 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF
,
273 proc2
.GetInputStream()->Read(procOutput
).GetLastError() );
275 wxString output
= procOutput
.GetString();
276 CPPUNIT_ASSERT_EQUAL( "hi", output
.Trim() );
279 // And now without event dispatching.
283 CPPUNIT_ASSERT_EQUAL( 0,
284 wxExecute(COMMAND
, wxEXEC_SYNC
| wxEXEC_NOEVENTS
, &proc2
) );
286 wxStringOutputStream procOutput
;
287 CPPUNIT_ASSERT( proc2
.GetInputStream() );
288 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF
,
289 proc2
.GetInputStream()->Read(procOutput
).GetLastError() );
291 wxString output
= procOutput
.GetString();
292 CPPUNIT_ASSERT_EQUAL( "hi", output
.Trim() );
297 // This class exits the event loop associated with it when the child process
299 class TestAsyncProcess
: public wxProcess
302 wxEXPLICIT
TestAsyncProcess()
306 // may be overridden to be notified about process termination
307 virtual void OnTerminate(int WXUNUSED(pid
), int WXUNUSED(status
))
309 wxEventLoop::GetActive()->ScheduleExit();
313 wxDECLARE_NO_COPY_CLASS(TestAsyncProcess
);
316 void ExecTestCase::TestAsync()
318 // Test asynchronous execution with no redirection, just to make sure we
319 // get the OnTerminate() call.
320 TestAsyncProcess proc
;
321 AsyncInEventLoop asyncInEventLoop
;
323 CPPUNIT_ASSERT( asyncInEventLoop
.DoExecute(
324 AsyncExec_DontExitLoop
, // proc is expected (inside of its OnTerminate())
325 // to trigger the exit of the event loop.
326 COMMAND_NO_OUTPUT
, wxEXEC_ASYNC
, &proc
) != 0 );
330 ExecTestCase::DoTestAsyncRedirect(const wxString
& command
,
332 const char* expectedContaining
)
334 AsyncInEventLoop asyncInEventLoop
;
335 TestAsyncProcess proc
;
339 CPPUNIT_ASSERT( asyncInEventLoop
.DoExecute(
340 AsyncExec_DontExitLoop
, // proc is expected (inside of its OnTerminate())
341 // to trigger the exit of the event loop.
342 command
, wxEXEC_ASYNC
, &proc
) != 0 );
344 wxInputStream
*streamToCheck
= NULL
;
348 streamToCheck
= proc
.GetInputStream();
352 streamToCheck
= proc
.GetErrorStream();
356 wxTextInputStream
tis(*streamToCheck
);
358 // Check that the first line of output contains what we expect.
359 CPPUNIT_ASSERT( tis
.ReadLine().Contains(expectedContaining
) );
362 void ExecTestCase::TestAsyncRedirect()
364 // Test redirection with reading from the input stream after process termination.
365 DoTestAsyncRedirect(COMMAND
, Check_Stdout
, "hi");
367 // Test redirection with reading from the error stream after process termination.
368 DoTestAsyncRedirect(COMMAND_STDERR
, Check_Stderr
, "file");
372 wxString
ExecTestCase::CreateSleepFile(const wxString
& basename
, int seconds
)
375 static const char* const scriptExt
= ".sh";
377 // The script text is a format string with a single "%d" appearing in it
378 // which will be replaced by the number of seconds to sleep below.
379 static const char* const scriptText
=
381 "echo " SLEEP_END_STRING
"\n";
382 #elif defined(__WINDOWS__)
383 static const char* const scriptExt
= ".bat";
385 // Notice that we need to ping N+1 times for it to take N seconds as the
386 // first ping is sent out immediately, without waiting a second.
387 static const char* const scriptText
=
388 "@ ping 127.0.0.1 -n 1 > nul\n"
389 "@ ping 127.0.0.1 -n %d > nul\n"
390 "@ echo " SLEEP_END_STRING
"\n";
392 #error "Need code to create sleep file for this platform"
395 const wxString fnSleep
= wxFileName(".", basename
, scriptExt
).GetFullPath();
400 fileSleep
.Create(fnSleep
, true, wxS_IRUSR
| wxS_IWUSR
| wxS_IXUSR
)
403 fileSleep
.Write(wxString::Format(scriptText
, seconds
));
409 wxString
ExecTestCase::MakeShellCommand(const wxString
& filename
)
414 command
= "/bin/sh " + filename
;
415 #elif defined(__WINDOWS__)
416 command
= wxString::Format("cmd.exe /c \"%s\"", filename
);
418 #error "Need to code to launch shell for this platform"
424 void ExecTestCase::TestOverlappedSyncExecute()
426 // Windows Synchronous wxExecute implementation does not currently
427 // support overlapped event loops. It is still using wxYield, which is
428 // not nestable. Therefore, this test would fail in Windows.
429 // If someday somebody changes that in Windows, they could use this
430 // test to verify it.
432 // Because MSW is not yet ready for this test, it may make sense to
433 // separate it out to its own test suite, so we could register it under
434 // "fixme" for Windows, but a real test for Unix. But that is more work,
435 // so just #ifndefing it here for now.
437 // Too bad you can't just register one test case of a test suite as a
440 // Simple helper delaying the call to wxExecute(): instead of running it
441 // immediately, it runs it when we re-enter the event loop.
442 class DelayedExecuteTimer
: public wxTimer
445 DelayedExecuteTimer(const wxString
& command
, wxArrayString
& outputArray
)
446 : m_command(command
),
447 m_outputArray(outputArray
)
449 // The exact delay doesn't matter, anything short enough will do.
453 virtual void Notify()
455 wxExecute(m_command
, m_outputArray
);
460 wxArrayString
& m_outputArray
;
463 // Create two scripts with one of them taking longer than the other one to
465 const wxString shortSleepFile
= CreateSleepFile("shortsleep", 1);
466 wxON_BLOCK_EXIT1( wxRemoveFile
, shortSleepFile
);
467 const wxString longSleepFile
= CreateSleepFile("longsleep", 2);
468 wxON_BLOCK_EXIT1( wxRemoveFile
, longSleepFile
);
470 const wxString shortSleepCommand
= MakeShellCommand(shortSleepFile
);
471 const wxString longSleepCommand
= MakeShellCommand(longSleepFile
);
473 // Collect the child process output
474 wxArrayString shortSleepOutput
,
477 // Test that launching a process taking a longer time to run while the
478 // shorter process is running works, i.e. that our outer wxExecute()
479 // doesn't return until both process terminate.
480 DelayedExecuteTimer
delayLongSleep(longSleepCommand
, longSleepOutput
);
481 wxExecute(shortSleepCommand
, shortSleepOutput
);
482 CPPUNIT_ASSERT( !shortSleepOutput
.empty() );
483 CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING
, shortSleepOutput
.Last() );
485 CPPUNIT_ASSERT( !longSleepOutput
.empty() );
486 CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING
, longSleepOutput
.Last() );
488 // And also that, vice versa, running a short-lived child process that both
489 // starts and ends while a longer-lived parent process is still running
491 DelayedExecuteTimer
delayShortSleep(shortSleepCommand
, shortSleepOutput
);
492 wxExecute(longSleepCommand
, longSleepOutput
);
493 CPPUNIT_ASSERT( !shortSleepOutput
.empty() );
494 CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING
, shortSleepOutput
.Last() );
496 CPPUNIT_ASSERT( !longSleepOutput
.empty() );
497 CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING
, longSleepOutput
.Last() );
498 #endif // !__WINDOWS__