KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_library_tables.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 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <filesystem>
22#include <fstream>
23
24#include <mock_pgm_base.h>
25#include <richio.h>
29#include <pegtl/contrib/analyze.hpp>
30
31#include <env_vars.h>
32#include <pgm_base.h>
39
40
41BOOST_AUTO_TEST_SUITE( LibraryTables )
42
43
45{
46 BOOST_REQUIRE( tao::pegtl::analyze< LIBRARY_TABLE_GRAMMAR::LIB_TABLE_FILE >( 1 ) == 0 );
47}
48
49
51{
53 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> result = parser.ParseBuffer( "" );
54 BOOST_REQUIRE( !result.has_value() );
55}
56
57
58BOOST_AUTO_TEST_CASE( ParseFromFile )
59{
60 std::vector<std::string> cases = {
61 "sym-lib-table",
62 "fp-lib-table"
63 };
64
65 std::filesystem::path p( KI_TEST::GetTestDataRootDir() );
66 p.append( "libraries/" );
67
69
70 for( const std::string& path : cases )
71 {
72 p.remove_filename();
73 p.append( path );
74
75 auto result = parser.Parse( p );
76
77 BOOST_REQUIRE( result.has_value() );
78 }
79}
80
81
82BOOST_AUTO_TEST_CASE( ParseAndConstruct )
83{
84 struct TESTCASE
85 {
86 wxString filename;
87 wxString expected_error;
88 size_t expected_rows;
89 bool check_formatted = true;
90 };
91
92 std::vector<TESTCASE> cases = {
93 { .filename = "sym-lib-table", .expected_rows = 224 },
94 { .filename = "fp-lib-table", .expected_rows = 146 },
95 { .filename = "nested-symbols", .expected_rows = 6 },
96 { .filename = "nested-disabled", .expected_rows = 4 },
97 { .filename = "nested-hidden", .expected_rows = 4 },
98 { .filename = "cycle", .expected_rows = 2 },
99 { .filename = "sym-hand-edited", .expected_rows = 2, .check_formatted = false },
100 { .filename = "corrupted", .expected_error = "Syntax error at line 6, column 9" },
101 { .filename = "truncated", .expected_error = "Syntax error at line 11, column 1" }
102 };
103
104 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
105 fn.AppendDir( "libraries" );
106
107 for( const auto& [filename, expected_error, expected_rows, check_formatted] : cases )
108 {
109 BOOST_TEST_CONTEXT( filename )
110 {
111 fn.SetName( filename );
113
114 BOOST_REQUIRE( table.IsOk() == ( expected_error.IsEmpty() ) );
115
116 BOOST_REQUIRE_MESSAGE( table.Rows().size() == expected_rows,
117 wxString::Format( "Expected %zu rows but got %zu",
118 expected_rows, table.Rows().size() ) );
119
120 BOOST_REQUIRE_MESSAGE( table.ErrorDescription() == expected_error,
121 wxString::Format( "Expected error '%s' but got '%s'",
122 expected_error, table.ErrorDescription() ) );
123
124 // Non-parsed tables can't be formatted
125 if( !table.IsOk() || !check_formatted )
126 continue;
127
128 std::ifstream inFp;
129 inFp.open( fn.GetFullPath().fn_str() );
130 BOOST_REQUIRE( inFp.is_open() );
131
132 std::stringstream inBuf;
133 inBuf << inFp.rdbuf();
134 std::string inData = inBuf.str();
135
136 STRING_FORMATTER formatter;
137 table.Format( &formatter );
139 KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
140
141 if( formatter.GetString().compare( inData ) != 0 )
142 {
143 BOOST_TEST_MESSAGE( "--- original ---" );
144 BOOST_TEST_MESSAGE( inData );
145 BOOST_TEST_MESSAGE( "--- formatted ---" );
146 BOOST_TEST_MESSAGE( formatter.GetString() );
147 }
148
149 BOOST_REQUIRE( formatter.GetString().compare( inData ) == 0 );
150 }
151 }
152}
153
155{
156 LIBRARY_MANAGER manager;
157 manager.LoadGlobalTables();
158
159 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ).size() == 3 );
160 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::FOOTPRINT ).size() == 146 );
161}
162
163
164// Regression coverage for the LoadGlobalTables guard: when a global library
165// table file does not exist on disk, loadTables() never inserts an entry for
166// that type into m_tables, so Table( aType, GLOBAL ) must return std::nullopt
167// instead of an entry holding a null pointer. The cleanupRemovedPCMLibraries
168// lambda inside LoadGlobalTables relies on this contract via .value_or(
169// nullptr ) and must not dereference the result. Exercising the full PCM
170// cleanup path requires extensive environment setup (3RD_PARTY env var plus
171// PCM auto-remove enabled in user settings), so we test the underlying
172// contract directly here.
173BOOST_AUTO_TEST_CASE( ManagerTableReturnsNulloptForUnloadedType )
174{
175 LIBRARY_MANAGER manager;
176
178 BOOST_REQUIRE( !result.has_value() );
179
180 LIBRARY_TABLE* table = result.value_or( nullptr );
181 BOOST_REQUIRE( table == nullptr );
182}
183
184
185BOOST_AUTO_TEST_CASE( NestedTablesDisabledHidden )
186{
187 // Test that disabled and hidden nested library table rows are parsed correctly
188 // This is a regression test for https://gitlab.com/kicad/code/kicad/-/issues/22784
189 // Note that full end-to-end testing requires the library manager to process the tables,
190 // but the parse test verifies the flag is correctly read from disk.
191
192 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
193 fn.AppendDir( "libraries" );
194
195 // Test with the disabled nested table
196 fn.SetName( "nested-disabled" );
197 LIBRARY_TABLE disabledTable( fn, LIBRARY_TABLE_SCOPE::GLOBAL );
198 BOOST_REQUIRE( disabledTable.IsOk() );
199 BOOST_REQUIRE_MESSAGE( disabledTable.Rows().size() == 4,
200 wxString::Format( "Expected 4 rows but got %zu",
201 disabledTable.Rows().size() ) );
202
203 // Verify the disabled flag is parsed correctly on the nested table row
204 bool foundDisabledRow = false;
205
206 for( const LIBRARY_TABLE_ROW& row : disabledTable.Rows() )
207 {
208 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
209 {
210 BOOST_REQUIRE_MESSAGE( row.Disabled(),
211 "Nested table row should have disabled flag set" );
212 foundDisabledRow = true;
213 }
214 }
215
216 BOOST_REQUIRE_MESSAGE( foundDisabledRow,
217 "Disabled nested table row not found in parsed table" );
218
219 // Test hidden nested table has same behavior
220 fn.SetName( "nested-hidden" );
222 BOOST_REQUIRE( hiddenTable.IsOk() );
223 BOOST_REQUIRE_MESSAGE( hiddenTable.Rows().size() == 4,
224 wxString::Format( "Expected 4 rows but got %zu",
225 hiddenTable.Rows().size() ) );
226
227 bool foundHiddenRow = false;
228
229 for( const LIBRARY_TABLE_ROW& row : hiddenTable.Rows() )
230 {
231 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
232 {
233 BOOST_REQUIRE_MESSAGE( row.Hidden(),
234 "Nested table row should have hidden flag set" );
235 foundHiddenRow = true;
236 }
237 }
238
239 BOOST_REQUIRE_MESSAGE( foundHiddenRow,
240 "Hidden nested table row not found in parsed table" );
241}
242
243
253BOOST_AUTO_TEST_CASE( InsertRowPreservesExistingRowPointers )
254{
255 LIBRARY_TABLE table( true, wxEmptyString, LIBRARY_TABLE_SCOPE::PROJECT );
257
258 // Seed with a few rows and snapshot pointers plus the expected URIs.
259 std::vector<const LIBRARY_TABLE_ROW*> seededPointers;
260 std::vector<wxString> seededUris;
261
262 for( int i = 0; i < 4; ++i )
263 {
264 LIBRARY_TABLE_ROW& row = table.InsertRow();
265 row.SetNickname( wxString::Format( wxS( "seed_%d" ), i ) );
266 row.SetURI( wxString::Format( wxS( "${KIPRJMOD}/libs/seed_%d.kicad_sym" ), i ) );
267 row.SetType( wxS( "KiCad" ) );
268
269 seededPointers.push_back( &row );
270 seededUris.push_back( row.URI() );
271 }
272
273 // Insert additional rows to force container growth that would reallocate
274 // a std::vector, and verify the seeded pointers continue to resolve to the
275 // same logical rows (same nickname and URI).
276 for( int i = 0; i < 64; ++i )
277 {
278 LIBRARY_TABLE_ROW& row = table.InsertRow();
279 row.SetNickname( wxString::Format( wxS( "extra_%d" ), i ) );
280 row.SetURI( wxString::Format( wxS( "${KIPRJMOD}/libs/extra_%d.kicad_sym" ), i ) );
281 row.SetType( wxS( "KiCad" ) );
282
283 for( size_t j = 0; j < seededPointers.size(); ++j )
284 {
285 BOOST_REQUIRE_MESSAGE(
286 seededPointers[j]->URI() == seededUris[j],
287 wxString::Format(
288 wxS( "Seed row %zu pointer was invalidated after inserting %d rows: "
289 "expected URI '%s', got '%s'" ),
290 j, i + 1, seededUris[j], seededPointers[j]->URI() ) );
291 }
292 }
293}
294
295
309BOOST_AUTO_TEST_CASE( IsPcmManagedRow_URITemplateMatching )
310{
311 struct CASE
312 {
313 wxString uri;
314 bool expectedPcmManaged;
315 wxString description;
316 };
317
318 std::vector<CASE> cases = {
319 { wxS( "${KICAD10_3RD_PARTY}/symbols/foo/foo.kicad_sym" ), true,
320 wxS( "Versioned 3RD_PARTY template should be recognised as PCM-managed" ) },
321 { wxS( "${KICAD9_3RD_PARTY}/symbols/legacy/legacy.kicad_sym" ), true,
322 wxS( "Legacy versioned 3RD_PARTY template should still match the wildcard" ) },
323 { wxS( "${KICAD10_3RD_PARTY}/footprints/bar/bar.pretty" ), true,
324 wxS( "Footprint library using 3RD_PARTY template should match" ) },
325 { wxS( "${KICAD_USER_LIB}/symbols/test.kicad_sym" ), false,
326 wxS( "Row using a different env var must not be flagged as PCM-managed" ) },
327 { wxS( "${KIPRJMOD}/libs/local.kicad_sym" ), false,
328 wxS( "Project-relative row must not be flagged as PCM-managed" ) },
329 { wxS( "/abs/path/to/lib.kicad_sym" ), false,
330 wxS( "Absolute path row must not be flagged as PCM-managed" ) },
331 { wxS( "${}" ), false,
332 wxS( "Malformed empty var name must not match" ) },
333 { wxS( "${KICAD10_3RD_PARTY_EXTRA}/foo" ), false,
334 wxS( "Similar-but-different var name must not match" ) },
335 };
336
337 for( const CASE& c : cases )
338 {
340 row.SetURI( c.uri );
341
343
345 actual == c.expectedPcmManaged,
346 wxString::Format( wxS( "%s: URI='%s' expected=%d actual=%d" ),
347 c.description, c.uri, c.expectedPcmManaged ? 1 : 0,
348 actual ? 1 : 0 ) );
349 }
350}
351
352
353BOOST_AUTO_TEST_CASE( ReadOnlyTable )
354{
355 // Create a temporary copy of a library table and make it read-only
356 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
357 fn.AppendDir( "libraries" );
358 fn.SetName( "sym-lib-table" );
359
360 wxFileName tmpFn = wxFileName::CreateTempFileName( "kicad_test_ro_" );
361 wxCopyFile( fn.GetFullPath(), tmpFn.GetFullPath() );
362
363 // Verify a writable table is not read-only
364 {
365 LIBRARY_TABLE writableTable( tmpFn, LIBRARY_TABLE_SCOPE::GLOBAL );
366 BOOST_REQUIRE( writableTable.IsOk() );
367 BOOST_REQUIRE( !writableTable.IsReadOnly() );
368 }
369
370 // Make the file read-only
371 tmpFn.SetPermissions( wxS_IRUSR | wxS_IRGRP | wxS_IROTH );
372
373 // CI containers commonly run as root, where access(W_OK) succeeds regardless of the
374 // permission bits, so a read-only file still reports as writable. IsReadOnly() uses the
375 // same IsFileWritable() check, so when the file remains writable here the read-only
376 // assertions cannot hold and are not meaningful.
377 if( wxFileName( tmpFn.GetFullPath() ).IsFileWritable() )
378 {
379 BOOST_TEST_MESSAGE( "Skipping read-only table checks; file remains writable despite "
380 "read-only permissions (running as root?)" );
381 }
382 else
383 {
385 BOOST_REQUIRE( roTable.IsOk() );
386 BOOST_REQUIRE( roTable.IsReadOnly() );
387
388 // Save should return an error for read-only tables
389 LIBRARY_RESULT<void> result = roTable.Save();
390 BOOST_REQUIRE( !result.has_value() );
391 }
392
393 // Clean up
394 tmpFn.SetPermissions( wxS_IRUSR | wxS_IWUSR );
395 wxRemoveFile( tmpFn.GetFullPath() );
396}
397
398
399BOOST_AUTO_TEST_CASE( LibOverrideSettings )
400{
401 // Test that LIB_OVERRIDE serialization in KICAD_SETTINGS works via the
402 // LIBRARY_MANAGER override API.
403 LIBRARY_MANAGER manager;
404
405 wxString tablePath = wxT( "/some/read-only/path/sym-lib-table" );
406 wxString nickname1 = wxT( "LibA" );
407 wxString nickname2 = wxT( "LibB" );
408
409 // Set an override
410 manager.SetLibOverride( tablePath, nickname1, true, false );
411 manager.SetLibOverride( tablePath, nickname2, false, true );
412
413 // Verify overrides via settings
415 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
416
417 BOOST_REQUIRE( settings != nullptr );
418 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 1 );
419 BOOST_REQUIRE( settings->m_LibOverrides[tablePath].count( nickname1 ) == 1 );
420 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname1].disabled == true );
421 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname1].hidden == false );
422 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname2].disabled == false );
423 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname2].hidden == true );
424
425 // Clear override (both disabled and hidden are false)
426 manager.ClearLibOverride( tablePath, nickname1 );
427 BOOST_REQUIRE( settings->m_LibOverrides[tablePath].count( nickname1 ) == 0 );
428
429 // Clear last entry should remove the table key too
430 manager.ClearLibOverride( tablePath, nickname2 );
431 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 0 );
432
433 // SetLibOverride with both false should also clear
434 manager.SetLibOverride( tablePath, nickname1, true, false );
435 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 1 );
436 manager.SetLibOverride( tablePath, nickname1, false, false );
437 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 0 );
438}
439
440
447BOOST_AUTO_TEST_CASE( StockTableReferenceURIHonorsExternalDefinition )
448{
449 const wxString templateVar = ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) );
451
452 BOOST_REQUIRE( common != nullptr );
453
454 ENV_VAR_MAP& vars = common->m_Env.vars;
455
456 // Preserve and restore the original entry so neighbouring tests are unaffected.
457 const bool hadEntry = vars.count( templateVar ) > 0;
458 const ENV_VAR_ITEM savedEntry = hadEntry ? vars[templateVar] : ENV_VAR_ITEM();
459
462 {
463 ENV_VAR_ITEM& entry = vars[templateVar];
464
465 entry.SetDefinedExternally( false );
468
469 entry.SetDefinedExternally( true );
472 }
473
474 if( hadEntry )
475 vars[templateVar] = savedEntry;
476 else
477 vars.erase( templateVar );
478}
479
480
KiCad uses environment variables internally for determining the base paths for libraries,...
void SetDefinedExternally(bool aIsDefinedExternally=true)
std::map< wxString, std::map< wxString, LIB_OVERRIDE > > m_LibOverrides
Overrides for libraries in read-only nested tables.
static wxString StockTableTokenizedURI(LIBRARY_TABLE_TYPE aType)
void ClearLibOverride(const wxString &aTablePath, const wxString &aNickname)
Removes any override for a library that no longer needs one.
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void SetLibOverride(const wxString &aTablePath, const wxString &aNickname, bool aDisabled, bool aHidden)
Set a user override for a library in a read-only nested table.
static bool IsPcmManagedRow(const LIBRARY_TABLE_ROW &aRow)
Return true if a library table row was added by the Plugin and Content Manager.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
static wxString StockTableReferenceURI(LIBRARY_TABLE_TYPE aType)
static wxString StockTablePath(LIBRARY_TABLE_TYPE aType)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > ParseBuffer(const std::string &aBuffer)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
const wxString & URI() const
LIBRARY_RESULT< void > Save()
bool IsReadOnly() const
Returns true if the underlying file exists but is not writable.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
bool IsOk() const
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:126
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:418
std::string & MutableString()
Definition richio.h:446
const std::string & GetString()
Definition richio.h:441
Functions related to environment variables, including help functions.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_TYPE
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:77
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
std::string GetTestDataRootDir()
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_AUTO_TEST_CASE(Grammar)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
std::vector< std::vector< std::string > > table
BOOST_TEST_CONTEXT("Test Clearance")
int actual
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")