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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <boost/test/unit_test.hpp>
25
26#include <wx/process.h>
27#include <wx/txtstrm.h>
28#include <wx/filename.h>
29#include <wx/dir.h>
30#include <wx/file.h>
31
32#ifdef _WIN32
33#include <process.h>
34#endif
35
36
37BOOST_AUTO_TEST_SUITE( JobsRunner )
38
39
40
44static int executeViaShell( const wxString& aCmd, wxString& aOutput )
45{
46 wxProcess process;
47 process.Redirect();
48
49#ifdef __WXMSW__
50 const wxString shell = wxS( "cmd.exe" );
51 const wxString shellFlag = wxS( "/c" );
52#else
53 const wxString shell = wxS( "/bin/sh" );
54 const wxString shellFlag = wxS( "-c" );
55#endif
56
57 const wchar_t* argv[] = { shell.wc_str(), shellFlag.wc_str(), aCmd.wc_str(), nullptr };
58
59 int result = static_cast<int>(
60 wxExecute( const_cast<wchar_t**>( argv ), wxEXEC_SYNC, &process ) );
61
62 wxInputStream* inputStream = process.GetInputStream();
63
64 if( inputStream )
65 {
66 wxTextInputStream textStream( *inputStream );
67
68 while( !inputStream->Eof() )
69 {
70 wxString line = textStream.ReadLine();
71
72 if( !line.IsEmpty() || !inputStream->Eof() )
73 {
74 if( !aOutput.IsEmpty() )
75 aOutput += wxS( "\n" );
76
77 aOutput += line;
78 }
79 }
80 }
81
82 return result;
83}
84
85
86BOOST_AUTO_TEST_CASE( SimpleCommand )
87{
88 wxString output;
89 int result = executeViaShell( wxS( "echo hello" ), output );
90
92 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
93}
94
95#ifdef _WIN32
96#define getpid _getpid
97#endif
98
99BOOST_AUTO_TEST_CASE( GlobExpansion )
100{
101 wxString tempDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
102 + wxS( "kicad_test_glob_" ) + wxString::Format( wxS( "%d" ), (int) getpid() );
103
104 BOOST_REQUIRE( wxFileName::Mkdir( tempDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
105
106 wxFile file1;
107 wxFile file2;
108 wxString path1 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_a.txt" );
109 wxString path2 = tempDir + wxFileName::GetPathSeparator() + wxS( "file_b.txt" );
110
111 file1.Create( path1 );
112 file1.Write( wxS( "content_a" ) );
113 file1.Close();
114
115 file2.Create( path2 );
116 file2.Write( wxS( "content_b" ) );
117 file2.Close();
118
119 // Use ls with glob. Without shell interpretation, the glob would not expand.
120#ifdef __WXMSW__
121 wxString cmd = wxString::Format( wxS( "dir /b \"%s\\*.txt\"" ), tempDir );
122#else
123 wxString cmd = wxString::Format( wxS( "ls %s/*.txt" ), tempDir );
124#endif
125
126 wxString output;
127 int result = executeViaShell( cmd, output );
128
130 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_a.txt" ) ),
131 "Glob should expand to include file_a.txt, got: " + output );
132 BOOST_CHECK_MESSAGE( output.Contains( wxS( "file_b.txt" ) ),
133 "Glob should expand to include file_b.txt, got: " + output );
134
135 wxRemoveFile( path1 );
136 wxRemoveFile( path2 );
137 wxRmdir( tempDir );
138}
139
140
142{
143#ifdef __WXMSW__
144 wxString cmd = wxS( "echo hello world | findstr hello" );
145#else
146 wxString cmd = wxS( "echo hello world | grep hello" );
147#endif
148
149 wxString output;
150 int result = executeViaShell( cmd, output );
151
153 BOOST_CHECK( output.Contains( wxS( "hello" ) ) );
154}
155
156
157BOOST_AUTO_TEST_CASE( ExitCodePropagation )
158{
159#ifdef __WXMSW__
160 wxString cmd = wxS( "cmd /c exit 42" );
161#else
162 wxString cmd = wxS( "exit 42" );
163#endif
164
165 wxString output;
166 int result = executeViaShell( cmd, output );
167
169}
170
171
172BOOST_AUTO_TEST_CASE( CommandWithSingleQuotes )
173{
174#ifndef __WXMSW__
175 wxString cmd = wxS( "echo \"it's working\"" );
176 wxString output;
177 int result = executeViaShell( cmd, output );
178
180 BOOST_CHECK_MESSAGE( output.Contains( wxS( "it's working" ) ),
181 "Should handle single quotes in output, got: " + output );
182#endif
183}
184
185
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)
Helper that mirrors the shell-wrapping execution pattern used in JOBS_RUNNER::runSpecialExecute.
BOOST_AUTO_TEST_CASE(SimpleCommand)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")