KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unit_test_utils.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
21
22#include <fstream>
23
24#include <wx/filename.h>
25#include <wx/utils.h>
26
27#if defined( __WXGTK__ )
28#include <gdk/gdk.h> // For gdk_display_get_default()
29#endif
30
31std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aPt )
32{
33 os << "WXPOINT[ x=\"" << aPt.x << "\" y=\"" << aPt.y << "\" ]";
34 return os;
35}
36
37
39{
40 const char* env = std::getenv( "KICAD_TEST_EESCHEMA_DATA_DIR" );
41 std::string fn;
42
43 if( !env )
44 {
45 // Use the compiled-in location of the data dir
46 // (i.e. where the files were at build time)
47 fn = GetTestDataRootDir();
48 fn += "eeschema";
49 }
50 else
51 {
52 // Use whatever was given in the env var
53 fn = env;
54 }
55
56 // Ensure the string ends in / to force a directory interpretation
57 fn += "/";
58
59 return fn;
60}
61
62
63#ifndef QA_DATA_ROOT
64#define QA_DATA_ROOT "???"
65#endif
66
68{
69 const char* env = std::getenv( "QA_DATA_ROOT" );
70 std::string fn;
71
72 if( !env )
73 {
74 // Use the compiled-in location of the data dir
75 // (i.e. where the files were at build time)
76 fn = QA_DATA_ROOT;
77 }
78 else
79 {
80 // Use whatever was given in the env var
81 fn = env;
82 }
83
84 // Ensure the string ends in / to force a directory interpretation
85 fn += "/";
86
87 return fn;
88}
89
90
91std::vector<uint8_t> KI_TEST::LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes )
92{
93 std::ifstream fileStream( aFilePath, std::ios::binary );
94
95 if( !fileStream )
96 throw std::runtime_error( "Failed to open file: " + aFilePath );
97
98 fileStream.seekg( 0, std::ios::end );
99 const size_t fileSize = static_cast<size_t>( fileStream.tellg() );
100 fileStream.seekg( 0, std::ios::beg );
101
102 const size_t bytesToRead = aLoadBytes.value_or( fileSize );
103
104 if( bytesToRead > fileSize )
105 throw std::runtime_error( "Requested " + std::to_string( bytesToRead ) + " bytes but file is "
106 + std::to_string( fileSize ) + " bytes: " + aFilePath );
107
108 std::vector<uint8_t> data( bytesToRead );
109 fileStream.read( reinterpret_cast<char*>( data.data() ), bytesToRead );
110
111 if( static_cast<size_t>( fileStream.gcount() ) != bytesToRead )
112 throw std::runtime_error( "Short read from file: " + aFilePath );
113
114 return data;
115}
116
117
118std::string KI_TEST::LoadStringData( const wxString& aPath )
119{
120 const std::vector<uint8_t> data = KI_TEST::LoadBinaryData( aPath.ToStdString() );
121
122 return std::string( data.begin(), data.end() );
123}
124
125
127{
128 if( !wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
129 {
130 wxString path( GetTestDataRootDir() );
131 path += wxT( "/config/" );
132 wxSetEnv( wxT( "KICAD_CONFIG_HOME" ), path );
133 wxSetEnv( wxT( "KICAD_CONFIG_HOME_IS_QA" ), wxT( "1" ) );
134
135 if( wxFileName fn( path ); fn.DirExists() )
136 {
137 // Under normal circumstances, don't let QA test runs write back to the config dir
138 // to avoid churn. Disable this if you want to update the QA schema.
139 wxSetEnv( wxT( "KICAD_INHIBIT_SETTINGS_WRITES" ), wxT( "1" ) );
140 }
141 }
142}
143
144
146{
147 // On Linux/GTK, clipboard operations require a display connection.
148 // This function checks if a display is available without spamming GDK-Critical errors
149 // like wxDisplay::GetCount() does
150 // (see https://github.com/wxWidgets/wxWidgets/issues/26967)
151#ifdef __WXGTK__
152 GdkDisplay* display = gdk_display_get_default();
153 return display != nullptr;
154#endif
155
156 // Other platforms don't have this restriction, so always return true
157 return true;
158}
std::string GetTestDataRootDir()
std::vector< uint8_t > LoadBinaryData(const std::string &aFilePath, std::optional< size_t > aLoadBytes=std::nullopt)
Load the contents of a file into a vector of bytes.
std::string LoadStringData(const wxString &aPath)
Load the contents of a file into a string.
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool CanDoDisplayTests()
Some tests on some platforms require a display connection to run.
void SetMockConfigDir()
std::string path
std::ostream & boost_test_print_type(std::ostream &os, wxPoint const &aPt)
Boost print helper for wxPoint.
#define QA_DATA_ROOT