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