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
159BOOST_AUTO_TEST_CASE( CommandWithSingleQuotes )
160{
161#ifndef __WXMSW__
162 wxString cmd = wxS( "echo \"it's working\"" );
163 wxString output;
164 int result = executeViaShell( cmd, output );
165
167 BOOST_CHECK_MESSAGE( output.Contains( wxS( "it's working" ) ),
168 "Should handle single quotes in output, got: " + output );
169#endif
170}
171
172
173#ifdef __WXMSW__
174BOOST_AUTO_TEST_CASE( CommandWithQuotedArgument )
175{
176 // Regression test for a Windows-only bug where a user-supplied command containing
177 // double-quoted arguments with spaces was corrupted by the argv-array form of wxExecute,
178 // which re-escapes embedded " as \" (see wxExecuteImpl in wxWidgets' src/msw/utilsexc.cpp).
179 // cmd.exe does not honour backslash-escaped quotes, so this broke any quoted executable
180 // path containing spaces (e.g. "C:\Program Files\KiCad\10.0\bin\python.exe" --version).
181 // Under the buggy code path, cmd.exe's `echo` would emit \"hello world\"; with the fix
182 // the user's quoting is preserved and the output contains the original "hello world".
183 wxString cmd = wxS( "echo \"hello world\"" );
184 wxString output;
185 int result = executeViaShell( cmd, output );
186
188 BOOST_CHECK_MESSAGE( output.Contains( wxS( "\"hello world\"" ) ),
189 "Quoted argument should pass through verbatim, got: " + output );
190}
191#endif
192
193
202BOOST_AUTO_TEST_CASE( AbsolutePathWithSpaces )
203{
204 wxString baseDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
205 + wxS( "kicad test 24226 " ) + wxString::Format( wxS( "%d" ), (int) getpid() );
206
207 BOOST_REQUIRE( wxFileName::Mkdir( baseDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
208
209 wxString sentinel = wxS( "SENTINEL_24226" );
210
211 // A second spaced absolute path passed as an argument exercises the nested-quote shape
212 // cmd /d /s /c ""exe path" "arg path"". The script only emits the sentinel when it receives
213 // this argument intact, so argument quoting is validated, not just the executable path.
214 wxString argPath = baseDir + wxFileName::GetPathSeparator() + wxS( "script input.txt" );
215 wxFile argFile;
216 BOOST_REQUIRE( argFile.Create( argPath, true ) );
217 argFile.Close();
218
219 wxFile script;
220
221#ifdef __WXMSW__
222 // Write a batch file at a spaced absolute path and invoke it through its quoted absolute path.
223 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.bat" );
224 BOOST_REQUIRE( script.Create( scriptPath, true ) );
225 BOOST_REQUIRE( script.Write( wxS( "@echo off\r\nif \"%~1\"==\"" ) + argPath + wxS( "\" echo " )
226 + sentinel + wxS( "\r\n" ) ) );
227 script.Close();
228
229 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
230#else
231 // Write a shell script at a spaced absolute path and invoke it through its quoted absolute path.
232 wxString scriptPath = baseDir + wxFileName::GetPathSeparator() + wxS( "print sentinel.sh" );
233 BOOST_REQUIRE( script.Create( scriptPath, true ) );
234 BOOST_REQUIRE( script.Write( wxS( "#!/bin/sh\n[ \"$1\" = \"" ) + argPath + wxS( "\" ] && echo " )
235 + sentinel + wxS( "\n" ) ) );
236 script.Close();
237 BOOST_REQUIRE( wxFileName( scriptPath ).SetPermissions( wxPOSIX_USER_READ | wxPOSIX_USER_WRITE
238 | wxPOSIX_USER_EXECUTE ) );
239
240 wxString cmd = wxS( "\"" ) + scriptPath + wxS( "\" \"" ) + argPath + wxS( "\"" );
241#endif
242
243 wxString output;
244 int result = executeViaShell( cmd, output );
245
247 BOOST_CHECK_MESSAGE( output.Contains( sentinel ),
248 "Quoted absolute paths with spaces should reach the interpreter intact, "
249 "got: " + output );
250
251 wxRemoveFile( argPath );
252 wxRemoveFile( scriptPath );
253 wxRmdir( baseDir );
254}
255
256
int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
Definition gestfich.cpp:271
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)
nlohmann::json output
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")