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, 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
25
26#include <exception>
27#include <filesystem>
28#include <fstream>
29
30#include <wx/utils.h>
31
32std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aPt )
33{
34 os << "WXPOINT[ x=\"" << aPt.x << "\" y=\"" << aPt.y << "\" ]";
35 return os;
36}
37
38
40{
41 const char* env = std::getenv( "KICAD_TEST_EESCHEMA_DATA_DIR" );
42 std::string fn;
43
44 if( !env )
45 {
46 // Use the compiled-in location of the data dir
47 // (i.e. where the files were at build time)
48 fn = GetTestDataRootDir();
49 fn += "eeschema";
50 }
51 else
52 {
53 // Use whatever was given in the env var
54 fn = env;
55 }
56
57 // Ensure the string ends in / to force a directory interpretation
58 fn += "/";
59
60 return fn;
61}
62
63
64#ifndef QA_DATA_ROOT
65#define QA_DATA_ROOT "???"
66#endif
67
69{
70 const char* env = std::getenv( "QA_DATA_ROOT" );
71 std::string fn;
72
73 if( !env )
74 {
75 // Use the compiled-in location of the data dir
76 // (i.e. where the files were at build time)
77 fn = QA_DATA_ROOT;
78 }
79 else
80 {
81 // Use whatever was given in the env var
82 fn = env;
83 }
84
85 // Ensure the string ends in / to force a directory interpretation
86 fn += "/";
87
88 return fn;
89}
90
91
92std::vector<uint8_t> KI_TEST::LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes )
93{
94 std::ifstream fileStream( aFilePath, std::ios::binary );
95
96 if( !fileStream )
97 throw std::runtime_error( "Failed to open file: " + aFilePath );
98
99 fileStream.seekg( 0, std::ios::end );
100 const size_t fileSize = static_cast<size_t>( fileStream.tellg() );
101 fileStream.seekg( 0, std::ios::beg );
102
103 const size_t bytesToRead = aLoadBytes.value_or( fileSize );
104
105 if( bytesToRead > fileSize )
106 throw std::runtime_error( "Requested " + std::to_string( bytesToRead ) + " bytes but file is "
107 + std::to_string( fileSize ) + " bytes: " + aFilePath );
108
109 std::vector<uint8_t> data( bytesToRead );
110 fileStream.read( reinterpret_cast<char*>( data.data() ), bytesToRead );
111
112 if( static_cast<size_t>( fileStream.gcount() ) != bytesToRead )
113 throw std::runtime_error( "Short read from file: " + aFilePath );
114
115 return data;
116}
117
118
120{
121 if( !wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
122 {
123 wxString path( GetTestDataRootDir() );
124 path += wxT( "/config/" );
125 wxSetEnv( wxT( "KICAD_CONFIG_HOME" ), path );
126 wxSetEnv( wxT( "KICAD_CONFIG_HOME_IS_QA" ), wxT( "1" ) );
127 }
128}
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 GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
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