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
23
24#include <common.h>
25#include <gestfich.h>
27#include <paths.h>
28#include <project.h>
30
31#include <wx/process.h>
32#include <wx/txtstrm.h>
33#include <wx/filename.h>
34#include <wx/dir.h>
35#include <wx/file.h>
36#include <wx/stdpaths.h>
37
38#ifdef _WIN32
39#include <process.h>
40#endif
41
42
43BOOST_AUTO_TEST_SUITE( JobsRunner )
44
45
46
50static int executeViaShell( const wxString& aCmd, wxString& aOutput )
51{
52 wxProcess process;
53 process.Redirect();
54
56
57 wxInputStream* inputStream = process.GetInputStream();
58
59 if( inputStream )
60 {
61 wxTextInputStream textStream( *inputStream );
62
63 while( !inputStream->Eof() )
64 {
65 wxString line = textStream.ReadLine();
66
67 if( !line.IsEmpty() || !inputStream->Eof() )
68 {
69 if( !aOutput.IsEmpty() )
70 aOutput += wxS( "\n" );
71
72 aOutput += line;
73 }
74 }
75 }
76
77 return result;
78}
79
80
81BOOST_AUTO_TEST_CASE( SimpleCommand )
82{
83 wxString output;
84 int result = executeViaShell( wxS( "echo hello" ), output );
85
87 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
88}
89
90#ifdef _WIN32
91#define getpid _getpid
92#endif
93
94BOOST_AUTO_TEST_CASE( GlobExpansion )
95{
96 wxString tempDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
97 + wxS( "kicad_test_glob_" ) + wxString::Format( wxS( "%d" ), (int) getpid() );
98
99 BOOST_REQUIRE( wxFileName::Mkdir( tempDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
100
101 wxFile file1;
102 wxFile file2;
103 wxString path1 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_a.txt" );
104 wxString path2 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_b.txt" );
105
106 file1.Create( path1 );
107 file1.Write( wxS( "content_a" ) );
108 file1.Close();
109
110 file2.Create( path2 );
111 file2.Write( wxS( "content_b" ) );
112 file2.Close();
113
114 // Use ls with glob. Without shell interpretation, the glob would not expand.
115#ifdef __WXMSW__
116 wxString cmd = wxString::Format( wxS( "dir /b \"%s\\*.txt\"" ), tempDir );
117#else
118 wxString cmd = wxString::Format( wxS( "ls %s/*.txt" ), tempDir );
119#endif
120
121 wxString output;
122 int result = executeViaShell( cmd, output );
123
125 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_a.txt" ) ),
126 "Glob should expand to include file_a.txt, got: " + output );
127 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_b.txt" ) ),
128 "Glob should expand to include file_b.txt, got: " + output );
129
130 wxRemoveFile( path1 );
131 wxRemoveFile( path2 );
132 wxRmdir( tempDir );
133}
134
135
137{
138#ifdef __WXMSW__
139 wxString cmd = wxS( "echo hello world | findstr hello" );
140#else
141 wxString cmd = wxS( "echo hello world | grep hello" );
142#endif
143
144 wxString output;
145 int result = executeViaShell( cmd, output );
146
148 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
149}
150
151
152BOOST_AUTO_TEST_CASE( ExitCodePropagation )
153{
154#ifdef __WXMSW__
155 wxString cmd = wxS( "cmd /c exit 42" );
156#else
157 wxString cmd = wxS( "exit 42" );
158#endif
159
160 wxString output;
161 int result = executeViaShell( cmd, output );
162
164}
165
166
167// Single-quote passthrough is a POSIX shell contract; cmd.exe has no equivalent, so the case is
168// not registered on Windows rather than registered with an empty body.
169#ifndef __WXMSW__
170BOOST_AUTO_TEST_CASE( CommandWithSingleQuotes )
171{
172 wxString cmd = wxS( "echo \"it's working\"" );
173 wxString output;
174 int result = executeViaShell( cmd, output );
175
177 BOOST_CHECK_MESSAGE( output.Contains( wxS( "it's working" ) ),
178 "Should handle single quotes in output, got: " + output );
179}
180#endif
181
182
183#ifdef __WXMSW__
184BOOST_AUTO_TEST_CASE( CommandWithQuotedArgument )
185{
186 // Regression test for a Windows-only bug where a user-supplied command containing
187 // double-quoted arguments with spaces was corrupted by the argv-array form of wxExecute,
188 // which re-escapes embedded " as \" (see wxExecuteImpl in wxWidgets' src/msw/utilsexc.cpp).
189 // cmd.exe does not honour backslash-escaped quotes, so this broke any quoted executable
190 // path containing spaces (e.g. "C:\Program Files\KiCad\10.0\bin\python.exe" --version).
191 // Under the buggy code path, cmd.exe's `echo` would emit \"hello world\"; with the fix
192 // the user's quoting is preserved and the output contains the original "hello world".
193 wxString cmd = wxS( "echo \"hello world\"" );
194 wxString output;
195 int result = executeViaShell( cmd, output );
196
198 BOOST_CHECK_MESSAGE( output.Contains( wxS( "\"hello world\"" ) ),
199 "Quoted argument should pass through verbatim, got: " + output );
200}
201#endif
202
203
212BOOST_AUTO_TEST_CASE( AbsolutePathWithSpaces )
213{
214 wxString baseDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
215 + wxS( "kicad test 24226 " ) + wxString::Format( wxS( "%d" ), (int) getpid() );
216
217 BOOST_REQUIRE( wxFileName::Mkdir( baseDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
218
219 wxString sentinel = wxS( "SENTINEL_24226" );
220
221 // A second spaced absolute path passed as an argument exercises the nested-quote shape
222 // cmd /d /s /c ""exe path" "arg path"". The script only emits the sentinel when it receives
223 // this argument intact, so argument quoting is validated, not just the executable path.
224 wxString argPath = baseDir + wxFileName::GetPathSeparator() + wxS( "script input.txt" );
225 wxFile argFile;
226 BOOST_REQUIRE( argFile.Create( argPath, true ) );
227 argFile.Close();
228
229 wxFile script;
230
231#ifdef __WXMSW__
232 // Write a batch file at a spaced absolute path and invoke it through its quoted absolute path.
233 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.bat" );
234 BOOST_REQUIRE( script.Create( scriptPath, true ) );
235 BOOST_REQUIRE( script.Write( wxS( "@echo off\r\nif \"%~1\"==\"" ) + argPath + wxS( "\" echo " )
236 + sentinel + wxS( "\r\n" ) ) );
237 script.Close();
238
239 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
240#else
241 // Write a shell script at a spaced absolute path and invoke it through its quoted absolute path.
242 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.sh" );
243 BOOST_REQUIRE( script.Create( scriptPath, true ) );
244 BOOST_REQUIRE( script.Write( wxS( "#!/bin/sh\n[ \"$1\" = \"" ) + argPath + wxS( "\" ] && echo " )
245 + sentinel + wxS( "\n" ) ) );
246 script.Close();
247 BOOST_REQUIRE( wxFileName( scriptPath ).SetPermissions( wxPOSIX_USER_READ | wxPOSIX_USER_WRITE
248 | wxPOSIX_USER_EXECUTE ) );
249
250 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
251#endif
252
253 wxString output;
254 int result = executeViaShell( cmd, output );
255
257 BOOST_CHECK_MESSAGE( output.Contains( sentinel ),
258 "Quoted absolute paths with spaces should reach the interpreter intact, "
259 "got: " + output );
260
261 wxRemoveFile( argPath );
262 wxRemoveFile( scriptPath );
263 wxRmdir( baseDir );
264}
265
266
267// Only the Windows branch put the executable directory on the child PATH (#21603)
268BOOST_AUTO_TEST_CASE( ExecutableDirectoryOnPath )
269{
270 wxFileName exe( wxStandardPaths::Get().GetExecutablePath() );
271
272 BOOST_REQUIRE( exe.IsFileExecutable() );
273
274#ifdef __WXMSW__
275 wxString cmd = wxS( "where " ) + exe.GetName();
276#else
277 wxString cmd = wxS( "command -v " ) + exe.GetFullName();
278#endif
279
280 wxString output;
281 int result = executeViaShell( cmd, output );
282
284 BOOST_CHECK_MESSAGE( output.Contains( exe.GetFullPath() ),
285 "A bare name should resolve beside the KiCad executable, got: " + output );
286}
287
288
289// A macOS bundle keeps Python under Contents/Frameworks, not in Contents/MacOS
290BOOST_AUTO_TEST_CASE( BundledPythonDirectory )
291{
292 wxString sep = wxFileName::GetPathSeparator();
293 wxString base = wxFileName::GetTempDir() + sep + wxS( "kicad_test_bundle_" )
294 + wxString::Format( wxS( "%d" ), (int) getpid() );
295 wxString contents = base + sep + wxS( "KiCad.app" ) + sep + wxS( "Contents" );
296 wxString macOsDir = contents + sep + wxS( "MacOS" );
297 wxString pythonDir = contents + sep + wxS( "Frameworks" ) + sep + wxS( "Python.framework" )
298 + sep + wxS( "Versions" ) + sep + wxS( "Current" ) + sep + wxS( "bin" );
299
300 BOOST_REQUIRE( wxFileName::Mkdir( macOsDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
301
302 // An install shipping no framework must not gain a phantom entry
303 BOOST_CHECK_EQUAL( PATHS::GetToolDirectories( macOsDir ).size(), 1 );
304
305 BOOST_REQUIRE( wxFileName::Mkdir( pythonDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
306
307 std::vector<wxString> dirs = PATHS::GetToolDirectories( macOsDir );
308
309 BOOST_REQUIRE_EQUAL( dirs.size(), 2 );
310 BOOST_CHECK( wxFileName( dirs[0], wxEmptyString ).SameAs( wxFileName( macOsDir, wxEmptyString ) ) );
311 BOOST_CHECK_MESSAGE(
312 wxFileName( dirs[1], wxEmptyString ).SameAs( wxFileName( pythonDir, wxEmptyString ) ),
313 "The bundled Python directory should be found, got: " + dirs[1] );
314
315 RmDirRecursive( base );
316}
317
318
319BOOST_AUTO_TEST_CASE( ExecuteBuiltinVariables )
320{
321 wxString sep = wxFileName::GetPathSeparator();
322 wxString base = wxFileName::GetTempDir() + sep + wxS( "kicad_test_exec_vars_" )
323 + wxString::Format( wxS( "%d" ), (int) getpid() );
324 wxString src = wxString::FromUTF8( KI_TEST::GetTestDataRootDir() ) + sep + wxS( "pcbnew" ) + sep;
325
326 BOOST_REQUIRE( wxFileName::Mkdir( base, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
327
328 for( const wxString& ext : { wxS( "kicad_pro" ), wxS( "kicad_sch" ), wxS( "kicad_pcb" ) } )
329 {
330 wxString name = wxS( "custom_fields." ) + ext;
331 BOOST_REQUIRE( wxCopyFile( src + name, base + sep + name ) );
332 }
333
334 {
336 BOOST_REQUIRE( mgr.LoadProject( base + sep + wxS( "custom_fields.kicad_pro" ), false ) );
337
338 PROJECT& prj = mgr.Prj();
339 wxString cmd = wxS( "${KICAD_PROJECT_PCB}|${KICAD_PROJECT_SCH}" );
340
341 wxString expanded = ExpandExecuteCommand( cmd, &prj );
342 wxString pcb = expanded.BeforeFirst( '|' );
343 wxString sch = expanded.AfterFirst( '|' );
344
345 BOOST_CHECK( wxFileName( pcb ).SameAs( wxFileName( base + sep + wxS( "custom_fields.kicad_pcb" ) ) ) );
346 BOOST_CHECK( wxFileName( sch ).SameAs( wxFileName( base + sep + wxS( "custom_fields.kicad_sch" ) ) ) );
347 BOOST_CHECK( !ExpandExecuteCommand( wxS( "${KICAD_PYTHON}" ), &prj ).StartsWith( wxS( "${" ) ) );
348
349 // A user variable can refer to a built-in
350 wxSetEnv( wxS( "KICAD_TEST_PY" ), wxS( "${KICAD_PYTHON}" ) );
351 BOOST_CHECK_EQUAL( ExpandExecuteCommand( wxS( "${KICAD_TEST_PY}" ), &prj ),
352 ExpandExecuteCommand( wxS( "${KICAD_PYTHON}" ), &prj ) );
353 wxUnsetEnv( wxS( "KICAD_TEST_PY" ) );
354
355 // A user definition wins so a portable jobset can still be overridden
356 wxSetEnv( wxS( "KICAD_PYTHON" ), wxS( "user_python" ) );
357 BOOST_CHECK_EQUAL( ExpandExecuteCommand( wxS( "${KICAD_PYTHON}" ), &prj ), wxS( "user_python" ) );
358 wxUnsetEnv( wxS( "KICAD_PYTHON" ) );
359
360 // A renamed board is still found when it is the only one
361 BOOST_REQUIRE( wxRenameFile( base + sep + wxS( "custom_fields.kicad_pcb" ),
362 base + sep + wxS( "renamed.kicad_pcb" ) ) );
363 pcb = ExpandExecuteCommand( wxS( "${KICAD_PROJECT_PCB}" ), &prj );
364 BOOST_CHECK( wxFileName( pcb ).SameAs( wxFileName( base + sep + wxS( "renamed.kicad_pcb" ) ) ) );
365
366 mgr.UnloadProject( &prj, false );
367 }
368
369 RmDirRecursive( base );
370}
371
372
373// The bundled interpreter is found in a later tool directory, not only beside the executable
374BOOST_AUTO_TEST_CASE( BundledPythonLookup )
375{
376 wxString sep = wxFileName::GetPathSeparator();
377 wxString base = wxFileName::GetTempDir() + sep + wxS( "kicad_test_py_" )
378 + wxString::Format( wxS( "%d" ), (int) getpid() );
379 wxString exeDir = base + sep + wxS( "exe" );
380 wxString pyDir = base + sep + wxS( "py" );
381
382#ifdef __WXMSW__
383 wxString name = wxS( "python.exe" );
384#else
385 wxString name = wxS( "python3" );
386#endif
387
388 BOOST_REQUIRE( wxFileName::Mkdir( exeDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
389 BOOST_REQUIRE( wxFileName::Mkdir( pyDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
390
391 BOOST_CHECK( FindBundledPython( { exeDir, pyDir } ).IsEmpty() );
392
393 BOOST_REQUIRE( wxFile( pyDir + sep + name, wxFile::write ).IsOpened() );
394 BOOST_CHECK_EQUAL( FindBundledPython( { exeDir, pyDir } ), pyDir + sep + name );
395
396 RmDirRecursive( base );
397}
398
399
const char * name
static std::vector< wxString > GetToolDirectories(const wxString &aExecutableDir)
Return the directories holding tools shipped alongside KiCad, most specific first.
Definition paths.cpp:689
Container for project specific data.
Definition project.h:63
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
Definition gestfich.cpp:334
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
Definition gestfich.cpp:512
wxString ExpandExecuteCommand(const wxString &aCommand, const PROJECT *aProject)
Expand an Execute Command string, including the built-in variables.
wxString FindBundledPython(const std::vector< wxString > &aToolDirs)
Find the interpreter shipped with KiCad in the given tool directories.
std::string GetTestDataRootDir()
static PGM_BASE * process
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
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.