20#include <boost/test/unit_test.hpp>
31#include <wx/process.h>
32#include <wx/txtstrm.h>
33#include <wx/filename.h>
36#include <wx/stdpaths.h>
57 wxInputStream* inputStream =
process.GetInputStream();
61 wxTextInputStream textStream( *inputStream );
63 while( !inputStream->Eof() )
65 wxString line = textStream.ReadLine();
67 if( !line.IsEmpty() || !inputStream->Eof() )
69 if( !aOutput.IsEmpty() )
70 aOutput += wxS(
"\n" );
96 wxString tempDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
97 + wxS(
"kicad_test_glob_" ) + wxString::Format( wxS(
"%d" ), (
int) getpid() );
99 BOOST_REQUIRE( wxFileName::Mkdir( tempDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
103 wxString path1 = tempDir + wxFileName::GetPathSeparator() + wxS(
"file_a.txt" );
104 wxString path2 = tempDir + wxFileName::GetPathSeparator() + wxS(
"file_b.txt" );
106 file1.Create( path1 );
107 file1.Write( wxS(
"content_a" ) );
110 file2.Create( path2 );
111 file2.Write( wxS(
"content_b" ) );
116 wxString cmd = wxString::Format( wxS(
"dir /b \"%s\\*.txt\"" ), tempDir );
118 wxString cmd = wxString::Format( wxS(
"ls %s/*.txt" ), tempDir );
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 );
130 wxRemoveFile( path1 );
131 wxRemoveFile( path2 );
139 wxString cmd = wxS(
"echo hello world | findstr hello" );
141 wxString cmd = wxS(
"echo hello world | grep hello" );
155 wxString cmd = wxS(
"cmd /c exit 42" );
157 wxString cmd = wxS(
"exit 42" );
172 wxString cmd = wxS(
"echo \"it's working\"" );
177 BOOST_CHECK_MESSAGE( output.Contains( wxS(
"it's working" ) ),
178 "Should handle single quotes in output, got: " + output );
193 wxString cmd = wxS(
"echo \"hello world\"" );
198 BOOST_CHECK_MESSAGE( output.Contains( wxS(
"\"hello world\"" ) ),
199 "Quoted argument should pass through verbatim, got: " + output );
214 wxString baseDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
215 + wxS(
"kicad test 24226 " ) + wxString::Format( wxS(
"%d" ), (
int) getpid() );
217 BOOST_REQUIRE( wxFileName::Mkdir( baseDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
219 wxString sentinel = wxS(
"SENTINEL_24226" );
224 wxString argPath = baseDir + wxFileName::GetPathSeparator() + wxS(
"script input.txt" );
233 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS(
"print sentinel.bat" );
235 BOOST_REQUIRE( script.Write( wxS(
"@echo off\r\nif \"%~1\"==\"" ) + argPath + wxS(
"\" echo " )
236 + sentinel + wxS(
"\r\n" ) ) );
239 wxString cmd = wxS(
"\"" ) + scriptPath + wxS(
"\" \"" ) + argPath + wxS(
"\"" );
242 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS(
"print sentinel.sh" );
244 BOOST_REQUIRE( script.Write( wxS(
"#!/bin/sh\n[ \"$1\" = \"" ) + argPath + wxS(
"\" ] && echo " )
245 + sentinel + wxS(
"\n" ) ) );
247 BOOST_REQUIRE( wxFileName( scriptPath ).SetPermissions( wxPOSIX_USER_READ | wxPOSIX_USER_WRITE
248 | wxPOSIX_USER_EXECUTE ) );
250 wxString cmd = wxS(
"\"" ) + scriptPath + wxS(
"\" \"" ) + argPath + wxS(
"\"" );
257 BOOST_CHECK_MESSAGE( output.Contains( sentinel ),
258 "Quoted absolute paths with spaces should reach the interpreter intact, "
261 wxRemoveFile( argPath );
262 wxRemoveFile( scriptPath );
270 wxFileName exe( wxStandardPaths::Get().GetExecutablePath() );
275 wxString cmd = wxS(
"where " ) + exe.GetName();
277 wxString cmd = wxS(
"command -v " ) + exe.GetFullName();
284 BOOST_CHECK_MESSAGE( output.Contains( exe.GetFullPath() ),
285 "A bare name should resolve beside the KiCad executable, got: " + output );
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" );
300 BOOST_REQUIRE( wxFileName::Mkdir( macOsDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
305 BOOST_REQUIRE( wxFileName::Mkdir( pythonDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
310 BOOST_CHECK( wxFileName( dirs[0], wxEmptyString ).SameAs( wxFileName( macOsDir, wxEmptyString ) ) );
312 wxFileName( dirs[1], wxEmptyString ).SameAs( wxFileName( pythonDir, wxEmptyString ) ),
313 "The bundled Python directory should be found, got: " + dirs[1] );
321 wxString sep = wxFileName::GetPathSeparator();
322 wxString base = wxFileName::GetTempDir() + sep + wxS(
"kicad_test_exec_vars_" )
323 + wxString::Format( wxS(
"%d" ), (
int) getpid() );
326 BOOST_REQUIRE( wxFileName::Mkdir( base, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
328 for(
const wxString& ext : { wxS(
"kicad_pro" ), wxS(
"kicad_sch" ), wxS(
"kicad_pcb" ) } )
330 wxString
name = wxS(
"custom_fields." ) + ext;
339 wxString cmd = wxS(
"${KICAD_PROJECT_PCB}|${KICAD_PROJECT_SCH}" );
342 wxString pcb = expanded.BeforeFirst(
'|' );
343 wxString sch = expanded.AfterFirst(
'|' );
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" ) ) ) );
350 wxSetEnv( wxS(
"KICAD_TEST_PY" ), wxS(
"${KICAD_PYTHON}" ) );
353 wxUnsetEnv( wxS(
"KICAD_TEST_PY" ) );
356 wxSetEnv( wxS(
"KICAD_PYTHON" ), wxS(
"user_python" ) );
358 wxUnsetEnv( wxS(
"KICAD_PYTHON" ) );
361 BOOST_REQUIRE( wxRenameFile( base + sep + wxS(
"custom_fields.kicad_pcb" ),
362 base + sep + wxS(
"renamed.kicad_pcb" ) ) );
364 BOOST_CHECK( wxFileName( pcb ).SameAs( wxFileName( base + sep + wxS(
"renamed.kicad_pcb" ) ) ) );
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" );
383 wxString
name = wxS(
"python.exe" );
385 wxString
name = wxS(
"python3" );
388 BOOST_REQUIRE( wxFileName::Mkdir( exeDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
389 BOOST_REQUIRE( wxFileName::Mkdir( pyDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
static std::vector< wxString > GetToolDirectories(const wxString &aExecutableDir)
Return the directories holding tools shipped alongside KiCad, most specific first.
Container for project specific data.
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,...
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
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.