KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_jobs_runner.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <gestfich.h>
23
24#include <wx/process.h>
25#include <wx/txtstrm.h>
26#include <wx/filename.h>
27#include <wx/dir.h>
28#include <wx/file.h>
29
30#ifdef _WIN32
31#include <process.h>
32#endif
33
34
35BOOST_AUTO_TEST_SUITE( JobsRunner )
36
37
38
42static int executeViaShell( const wxString& aCmd, wxString& aOutput )
43{
44 wxProcess process;
45 process.Redirect();
46
48
49 wxInputStream* inputStream = process.GetInputStream();
50
51 if( inputStream )
52 {
53 wxTextInputStream textStream( *inputStream );
54
55 while( !inputStream->Eof() )
56 {
57 wxString line = textStream.ReadLine();
58
59 if( !line.IsEmpty() || !inputStream->Eof() )
60 {
61 if( !aOutput.IsEmpty() )
62 aOutput += wxS( "\n" );
63
64 aOutput += line;
65 }
66 }
67 }
68
69 return result;
70}
71
72
73BOOST_AUTO_TEST_CASE( SimpleCommand )
74{
75 wxString output;
76 int result = executeViaShell( wxS( "echo hello" ), output );
77
79 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
80}
81
82#ifdef _WIN32
83#define getpid _getpid
84#endif
85
86BOOST_AUTO_TEST_CASE( GlobExpansion )
87{
88 wxString tempDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
89 + wxS( "kicad_test_glob_" ) + wxString::Format( wxS( "%d" ), (int) getpid() );
90
91 BOOST_REQUIRE( wxFileName::Mkdir( tempDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
92
93 wxFile file1;
94 wxFile file2;
95 wxString path1 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_a.txt" );
96 wxString path2 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_b.txt" );
97
98 file1.Create( path1 );
99 file1.Write( wxS( "content_a" ) );
100 file1.Close();
101
102 file2.Create( path2 );
103 file2.Write( wxS( "content_b" ) );
104 file2.Close();
105
106 // Use ls with glob. Without shell interpretation, the glob would not expand.
107#ifdef __WXMSW__
108 wxString cmd = wxString::Format( wxS( "dir /b \"%s\\*.txt\"" ), tempDir );
109#else
110 wxString cmd = wxString::Format( wxS( "ls %s/*.txt" ), tempDir );
111#endif
112
113 wxString output;
114 int result = executeViaShell( cmd, output );
115
117 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_a.txt" ) ),
118 "Glob should expand to include file_a.txt, got: " + output );
119 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_b.txt" ) ),
120 "Glob should expand to include file_b.txt, got: " + output );
121
122 wxRemoveFile( path1 );
123 wxRemoveFile( path2 );
124 wxRmdir( tempDir );
125}
126
127
129{
130#ifdef __WXMSW__
131 wxString cmd = wxS( "echo hello world | findstr hello" );
132#else
133 wxString cmd = wxS( "echo hello world | grep hello" );
134#endif
135
136 wxString output;
137 int result = executeViaShell( cmd, output );
138
140 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
141}
142
143
144BOOST_AUTO_TEST_CASE( ExitCodePropagation )
145{
146#ifdef __WXMSW__
147 wxString cmd = wxS( "cmd /c exit 42" );
148#else
149 wxString cmd = wxS( "exit 42" );
150#endif
151
152 wxString output;
153 int result = executeViaShell( cmd, output );
154
156}
157
158
159// Single-quote passthrough is a POSIX shell contract; cmd.exe has no equivalent, so the case is
160// not registered on Windows rather than registered with an empty body.
161#ifndef __WXMSW__
162BOOST_AUTO_TEST_CASE( CommandWithSingleQuotes )
163{
164 wxString cmd = wxS( "echo \"it's working\"" );
165 wxString output;
166 int result = executeViaShell( cmd, output );
167
169 BOOST_CHECK_MESSAGE( output.Contains( wxS( "it's working" ) ),
170 "Should handle single quotes in output, got: " + output );
171}
172#endif
173
174
175#ifdef __WXMSW__
176BOOST_AUTO_TEST_CASE( CommandWithQuotedArgument )
177{
178 // Regression test for a Windows-only bug where a user-supplied command containing
179 // double-quoted arguments with spaces was corrupted by the argv-array form of wxExecute,
180 // which re-escapes embedded " as \" (see wxExecuteImpl in wxWidgets' src/msw/utilsexc.cpp).
181 // cmd.exe does not honour backslash-escaped quotes, so this broke any quoted executable
182 // path containing spaces (e.g. "C:\Program Files\KiCad\10.0\bin\python.exe" --version).
183 // Under the buggy code path, cmd.exe's `echo` would emit \"hello world\"; with the fix
184 // the user's quoting is preserved and the output contains the original "hello world".
185 wxString cmd = wxS( "echo \"hello world\"" );
186 wxString output;
187 int result = executeViaShell( cmd, output );
188
190 BOOST_CHECK_MESSAGE( output.Contains( wxS( "\"hello world\"" ) ),
191 "Quoted argument should pass through verbatim, got: " + output );
192}
193#endif
194
195
204BOOST_AUTO_TEST_CASE( AbsolutePathWithSpaces )
205{
206 wxString baseDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
207 + wxS( "kicad test 24226 " ) + wxString::Format( wxS( "%d" ), (int) getpid() );
208
209 BOOST_REQUIRE( wxFileName::Mkdir( baseDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
210
211 wxString sentinel = wxS( "SENTINEL_24226" );
212
213 // A second spaced absolute path passed as an argument exercises the nested-quote shape
214 // cmd /d /s /c ""exe path" "arg path"". The script only emits the sentinel when it receives
215 // this argument intact, so argument quoting is validated, not just the executable path.
216 wxString argPath = baseDir + wxFileName::GetPathSeparator() + wxS( "script input.txt" );
217 wxFile argFile;
218 BOOST_REQUIRE( argFile.Create( argPath, true ) );
219 argFile.Close();
220
221 wxFile script;
222
223#ifdef __WXMSW__
224 // Write a batch file at a spaced absolute path and invoke it through its quoted absolute path.
225 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.bat" );
226 BOOST_REQUIRE( script.Create( scriptPath, true ) );
227 BOOST_REQUIRE( script.Write( wxS( "@echo off\r\nif \"%~1\"==\"" ) + argPath + wxS( "\" echo " )
228 + sentinel + wxS( "\r\n" ) ) );
229 script.Close();
230
231 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
232#else
233 // Write a shell script at a spaced absolute path and invoke it through its quoted absolute path.
234 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.sh" );
235 BOOST_REQUIRE( script.Create( scriptPath, true ) );
236 BOOST_REQUIRE( script.Write( wxS( "#!/bin/sh\n[ \"$1\" = \"" ) + argPath + wxS( "\" ] && echo " )
237 + sentinel + wxS( "\n" ) ) );
238 script.Close();
239 BOOST_REQUIRE( wxFileName( scriptPath ).SetPermissions( wxPOSIX_USER_READ | wxPOSIX_USER_WRITE
240 | wxPOSIX_USER_EXECUTE ) );
241
242 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
243#endif
244
245 wxString output;
246 int result = executeViaShell( cmd, output );
247
249 BOOST_CHECK_MESSAGE( output.Contains( sentinel ),
250 "Quoted absolute paths with spaces should reach the interpreter intact, "
251 "got: " + output );
252
253 wxRemoveFile( argPath );
254 wxRemoveFile( scriptPath );
255 wxRmdir( baseDir );
256}
257
258
int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
Definition gestfich.cpp:272
static PGM_BASE * process
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static int executeViaShell(const wxString &aCmd, wxString &aOutput)
Drive the production ExecuteCommandThroughShell (the exact function jobsets use) and capture its stdo...
BOOST_AUTO_TEST_CASE(SimpleCommand)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")