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 <algorithm>
22#include <filesystem>
23#include <fstream>
24#include <ranges>
25#include <utility>
26#include <vector>
27
28#include <wx/ffile.h>
29
30#include <mock_pgm_base.h>
31#include <richio.h>
35#include <pegtl/contrib/analyze.hpp>
36
37#include <env_vars.h>
38#include <pgm_base.h>
46
47
48BOOST_AUTO_TEST_SUITE( LibraryTables )
49
50
52{
53 BOOST_REQUIRE( tao::pegtl::analyze< LIBRARY_TABLE_GRAMMAR::LIB_TABLE_FILE >( 1 ) == 0 );
54}
55
56
58{
60 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> result = parser.ParseBuffer( "" );
61 BOOST_REQUIRE( !result.has_value() );
62}
63
64
65BOOST_AUTO_TEST_CASE( ParseFromFile )
66{
67 std::vector<std::string> cases = {
68 "sym-lib-table",
69 "fp-lib-table"
70 };
71
72 std::filesystem::path p( KI_TEST::GetTestDataRootDir() );
73 p.append( "libraries/" );
74
76
77 for( const std::string& path : cases )
78 {
79 p.remove_filename();
80 p.append( path );
81
82 auto result = parser.Parse( p );
83
84 BOOST_REQUIRE( result.has_value() );
85 }
86}
87
88
89BOOST_AUTO_TEST_CASE( ParseAndConstruct )
90{
91 struct TESTCASE
92 {
93 wxString filename;
94 wxString expected_error;
95 size_t expected_rows;
96 bool check_formatted = true;
97 };
98
99 std::vector<TESTCASE> cases = {
100 { .filename = "sym-lib-table", .expected_rows = 224 },
101 { .filename = "fp-lib-table", .expected_rows = 146 },
102 { .filename = "nested-symbols", .expected_rows = 6 },
103 { .filename = "nested-disabled", .expected_rows = 4 },
104 { .filename = "nested-hidden", .expected_rows = 4 },
105 { .filename = "cycle", .expected_rows = 2 },
106 { .filename = "sym-hand-edited", .expected_rows = 2, .check_formatted = false },
107 { .filename = "corrupted", .expected_error = "Syntax error at line 6, column 9" },
108 { .filename = "truncated", .expected_error = "Syntax error at line 11, column 1" }
109 };
110
111 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
112 fn.AppendDir( "libraries" );
113
114 for( const auto& [filename, expected_error, expected_rows, check_formatted] : cases )
115 {
116 BOOST_TEST_CONTEXT( filename )
117 {
118 fn.SetName( filename );
120
121 BOOST_REQUIRE( table.IsOk() == ( expected_error.IsEmpty() ) );
122
123 BOOST_REQUIRE_MESSAGE( table.Rows().size() == expected_rows,
124 wxString::Format( "Expected %zu rows but got %zu",
125 expected_rows, table.Rows().size() ) );
126
127 BOOST_REQUIRE_MESSAGE( table.ErrorDescription() == expected_error,
128 wxString::Format( "Expected error '%s' but got '%s'",
129 expected_error, table.ErrorDescription() ) );
130
131 // Non-parsed tables can't be formatted
132 if( !table.IsOk() || !check_formatted )
133 continue;
134
135 std::ifstream inFp;
136 inFp.open( fn.GetFullPath().fn_str() );
137 BOOST_REQUIRE( inFp.is_open() );
138
139 std::stringstream inBuf;
140 inBuf << inFp.rdbuf();
141 std::string inData = inBuf.str();
142
143 STRING_FORMATTER formatter;
144 table.Format( &formatter );
146 KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
147
148 if( formatter.GetString().compare( inData ) != 0 )
149 {
150 BOOST_TEST_MESSAGE( "--- original ---" );
151 BOOST_TEST_MESSAGE( inData );
152 BOOST_TEST_MESSAGE( "--- formatted ---" );
153 BOOST_TEST_MESSAGE( formatter.GetString() );
154 }
155
156 BOOST_REQUIRE( formatter.GetString().compare( inData ) == 0 );
157 }
158 }
159}
160
162{
163 LIBRARY_MANAGER manager;
164 manager.LoadGlobalTables();
165
166 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ).size() == 3 );
167 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::FOOTPRINT ).size() == 146 );
168}
169
170
171// Regression coverage for the LoadGlobalTables guard: when a global library
172// table file does not exist on disk, loadTables() never inserts an entry for
173// that type into m_tables, so Table( aType, GLOBAL ) must return std::nullopt
174// instead of an entry holding a null pointer. The cleanupRemovedPCMLibraries
175// lambda inside LoadGlobalTables relies on this contract via .value_or(
176// nullptr ) and must not dereference the result. Exercising the full PCM
177// cleanup path requires extensive environment setup (3RD_PARTY env var plus
178// PCM auto-remove enabled in user settings), so we test the underlying
179// contract directly here.
180BOOST_AUTO_TEST_CASE( ManagerTableReturnsNulloptForUnloadedType )
181{
182 LIBRARY_MANAGER manager;
183
185 BOOST_REQUIRE( !result.has_value() );
186
187 LIBRARY_TABLE* table = result.value_or( nullptr );
188 BOOST_REQUIRE( table == nullptr );
189}
190
191
192BOOST_AUTO_TEST_CASE( NestedTablesDisabledHidden )
193{
194 // Test that disabled and hidden nested library table rows are parsed correctly
195 // This is a regression test for https://gitlab.com/kicad/code/kicad/-/issues/22784
196 // Note that full end-to-end testing requires the library manager to process the tables,
197 // but the parse test verifies the flag is correctly read from disk.
198
199 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
200 fn.AppendDir( "libraries" );
201
202 // Test with the disabled nested table
203 fn.SetName( "nested-disabled" );
204 LIBRARY_TABLE disabledTable( fn, LIBRARY_TABLE_SCOPE::GLOBAL );
205 BOOST_REQUIRE( disabledTable.IsOk() );
206 BOOST_REQUIRE_MESSAGE( disabledTable.Rows().size() == 4,
207 wxString::Format( "Expected 4 rows but got %zu",
208 disabledTable.Rows().size() ) );
209
210 // Verify the disabled flag is parsed correctly on the nested table row
211 bool foundDisabledRow = false;
212
213 for( const LIBRARY_TABLE_ROW& row : disabledTable.Rows() )
214 {
215 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
216 {
217 BOOST_REQUIRE_MESSAGE( row.Disabled(),
218 "Nested table row should have disabled flag set" );
219 foundDisabledRow = true;
220 }
221 }
222
223 BOOST_REQUIRE_MESSAGE( foundDisabledRow,
224 "Disabled nested table row not found in parsed table" );
225
226 // Test hidden nested table has same behavior
227 fn.SetName( "nested-hidden" );
229 BOOST_REQUIRE( hiddenTable.IsOk() );
230 BOOST_REQUIRE_MESSAGE( hiddenTable.Rows().size() == 4,
231 wxString::Format( "Expected 4 rows but got %zu",
232 hiddenTable.Rows().size() ) );
233
234 bool foundHiddenRow = false;
235
236 for( const LIBRARY_TABLE_ROW& row : hiddenTable.Rows() )
237 {
238 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
239 {
240 BOOST_REQUIRE_MESSAGE( row.Hidden(),
241 "Nested table row should have hidden flag set" );
242 foundHiddenRow = true;
243 }
244 }
245
246 BOOST_REQUIRE_MESSAGE( foundHiddenRow,
247 "Hidden nested table row not found in parsed table" );
248}
249
250
260BOOST_AUTO_TEST_CASE( InsertRowPreservesExistingRowPointers )
261{
262 LIBRARY_TABLE table( true, wxEmptyString, LIBRARY_TABLE_SCOPE::PROJECT );
264
265 // Seed with a few rows and snapshot pointers plus the expected URIs.
266 std::vector<const LIBRARY_TABLE_ROW*> seededPointers;
267 std::vector<wxString> seededUris;
268
269 for( int i = 0; i < 4; ++i )
270 {
271 LIBRARY_TABLE_ROW& row = table.InsertRow();
272 row.SetNickname( wxString::Format( wxS( "seed_%d" ), i ) );
273 row.SetURI( wxString::Format( wxS( "${KIPRJMOD}/libs/seed_%d.kicad_sym" ), i ) );
274 row.SetType( wxS( "KiCad" ) );
275
276 seededPointers.push_back( &row );
277 seededUris.push_back( row.URI() );
278 }
279
280 // Insert additional rows to force container growth that would reallocate
281 // a std::vector, and verify the seeded pointers continue to resolve to the
282 // same logical rows (same nickname and URI).
283 for( int i = 0; i < 64; ++i )
284 {
285 LIBRARY_TABLE_ROW& row = table.InsertRow();
286 row.SetNickname( wxString::Format( wxS( "extra_%d" ), i ) );
287 row.SetURI( wxString::Format( wxS( "${KIPRJMOD}/libs/extra_%d.kicad_sym" ), i ) );
288 row.SetType( wxS( "KiCad" ) );
289
290 for( size_t j = 0; j < seededPointers.size(); ++j )
291 {
292 BOOST_REQUIRE_MESSAGE(
293 seededPointers[j]->URI() == seededUris[j],
294 wxString::Format(
295 wxS( "Seed row %zu pointer was invalidated after inserting %d rows: "
296 "expected URI '%s', got '%s'" ),
297 j, i + 1, seededUris[j], seededPointers[j]->URI() ) );
298 }
299 }
300}
301
302
316BOOST_AUTO_TEST_CASE( IsPcmManagedRow_URITemplateMatching )
317{
318 struct CASE
319 {
320 wxString uri;
321 bool expectedPcmManaged;
322 wxString description;
323 };
324
325 std::vector<CASE> cases = {
326 { wxS( "${KICAD10_3RD_PARTY}/symbols/foo/foo.kicad_sym" ), true,
327 wxS( "Versioned 3RD_PARTY template should be recognised as PCM-managed" ) },
328 { wxS( "${KICAD9_3RD_PARTY}/symbols/legacy/legacy.kicad_sym" ), true,
329 wxS( "Legacy versioned 3RD_PARTY template should still match the wildcard" ) },
330 { wxS( "${KICAD10_3RD_PARTY}/footprints/bar/bar.pretty" ), true,
331 wxS( "Footprint library using 3RD_PARTY template should match" ) },
332 { wxS( "${KICAD10_3RD_PARTY}/design_blocks/baz/baz.kicad_blocks" ), true,
333 wxS( "Design block library using 3RD_PARTY template should match" ) },
334 { wxS( "${KICAD_USER_LIB}/symbols/test.kicad_sym" ), false,
335 wxS( "Row using a different env var must not be flagged as PCM-managed" ) },
336 { wxS( "${KIPRJMOD}/libs/local.kicad_sym" ), false,
337 wxS( "Project-relative row must not be flagged as PCM-managed" ) },
338 { wxS( "/abs/path/to/lib.kicad_sym" ), false,
339 wxS( "Absolute path row must not be flagged as PCM-managed" ) },
340 { wxS( "${}" ), false,
341 wxS( "Malformed empty var name must not match" ) },
342 { wxS( "${KICAD10_3RD_PARTY_EXTRA}/foo" ), false,
343 wxS( "Similar-but-different var name must not match" ) },
344 // Issue #23476: a user who repurposes KICADn_3RD_PARTY to point at their own
345 // library collection adds libraries directly under that root (no PCM category
346 // folder). Such rows must not be treated as PCM-managed or auto-remove deletes
347 // them.
348 { wxS( "${KICAD10_3RD_PARTY}/mylib.kicad_sym" ), false,
349 wxS( "User library directly under repurposed 3RD_PARTY root must not match" ) },
350 { wxS( "${KICAD10_3RD_PARTY}/MyLibs/mylib.kicad_sym" ), false,
351 wxS( "User library under a non-PCM subfolder of 3RD_PARTY must not match" ) },
352 { wxS( "${KICAD10_3RD_PARTY}/eagle/imported.pretty" ), false,
353 wxS( "User footprint library under a non-PCM subfolder must not match" ) },
354 { wxS( "${KICAD10_3RD_PARTY}/symbols" ), false,
355 wxS( "3RD_PARTY/symbols with no nested package must not match" ) },
356 { wxS( "${KICAD10_3RD_PARTY}/symbols/mylib.kicad_sym" ), false,
357 wxS( "User symbol library directly in symbols folder (no package level) must not match" ) },
358 { wxS( "${KICAD10_3RD_PARTY}symbols/foo/foo.kicad_sym" ), false,
359 wxS( "Missing separator after env var must not match" ) },
360 { wxS( "${KICAD10_3RD_PARTY}/design_blocks/baz/baz.kicad_dbl" ), false,
361 wxS( "Wrong design-block library extension must not match" ) },
362 { wxS( "${KICAD10_3RD_PARTY}/symbols/foo/foo.txt" ), false,
363 wxS( "Non-library file in PCM symbols tree must not match" ) },
364 { wxS( "${KICAD10_3RD_PARTY}\\symbols\\foo\\foo.kicad_sym" ), false,
365 wxS( "Backslash-separated URI is never emitted by PCM and must not match" ) },
366 { wxS( "${KICAD10_3RD_PARTY}/symbols//foo.kicad_sym" ), false,
367 wxS( "Empty package-id component must not match" ) },
368 { wxS( "${KICAD10_3RD_PARTY}/symbols/foo/.kicad_sym" ), false,
369 wxS( "Extension-only leaf with empty library stem must not match" ) },
370 };
371
372 for( const CASE& c : cases )
373 {
375 row.SetURI( c.uri );
376
378
380 actual == c.expectedPcmManaged,
381 wxString::Format( wxS( "%s: URI='%s' expected=%d actual=%d" ),
382 c.description, c.uri, c.expectedPcmManaged ? 1 : 0,
383 actual ? 1 : 0 ) );
384 }
385}
386
387
388BOOST_AUTO_TEST_CASE( ReadOnlyTable )
389{
390 // Create a temporary copy of a library table and make it read-only
391 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
392 fn.AppendDir( "libraries" );
393 fn.SetName( "sym-lib-table" );
394
395 wxFileName tmpFn = wxFileName::CreateTempFileName( "kicad_test_ro_" );
396 wxCopyFile( fn.GetFullPath(), tmpFn.GetFullPath() );
397
398 // Verify a writable table is not read-only
399 {
400 LIBRARY_TABLE writableTable( tmpFn, LIBRARY_TABLE_SCOPE::GLOBAL );
401 BOOST_REQUIRE( writableTable.IsOk() );
402 BOOST_REQUIRE( !writableTable.IsReadOnly() );
403 }
404
405 // Make the file read-only
406 tmpFn.SetPermissions( wxS_IRUSR | wxS_IRGRP | wxS_IROTH );
407
408 // CI containers commonly run as root, where access(W_OK) succeeds regardless of the
409 // permission bits, so a read-only file still reports as writable. IsReadOnly() uses the
410 // same IsFileWritable() check, so when the file remains writable here the read-only
411 // assertions cannot hold and are not meaningful.
412 if( wxFileName( tmpFn.GetFullPath() ).IsFileWritable() )
413 {
414 BOOST_TEST_MESSAGE( "Skipping read-only table checks; file remains writable despite "
415 "read-only permissions (running as root?)" );
416 }
417 else
418 {
420 BOOST_REQUIRE( roTable.IsOk() );
421 BOOST_REQUIRE( roTable.IsReadOnly() );
422
423 // Save should return an error for read-only tables
424 LIBRARY_RESULT<void> result = roTable.Save();
425 BOOST_REQUIRE( !result.has_value() );
426 }
427
428 // Clean up
429 tmpFn.SetPermissions( wxS_IRUSR | wxS_IWUSR );
430 wxRemoveFile( tmpFn.GetFullPath() );
431}
432
433
434BOOST_AUTO_TEST_CASE( LibOverrideSettings )
435{
436 // Test that LIB_OVERRIDE serialization in KICAD_SETTINGS works via the
437 // LIBRARY_MANAGER override API.
438 LIBRARY_MANAGER manager;
439
440 wxString tablePath = wxT( "/some/read-only/path/sym-lib-table" );
441 wxString nickname1 = wxT( "LibA" );
442 wxString nickname2 = wxT( "LibB" );
443
444 // Set an override
445 manager.SetLibOverride( tablePath, nickname1, true, false );
446 manager.SetLibOverride( tablePath, nickname2, false, true );
447
448 // Verify overrides via settings
450 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
451
452 BOOST_REQUIRE( settings != nullptr );
453 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 1 );
454 BOOST_REQUIRE( settings->m_LibOverrides[tablePath].count( nickname1 ) == 1 );
455 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname1].disabled == true );
456 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname1].hidden == false );
457 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname2].disabled == false );
458 BOOST_REQUIRE( settings->m_LibOverrides[tablePath][nickname2].hidden == true );
459
460 // Clear override (both disabled and hidden are false)
461 manager.ClearLibOverride( tablePath, nickname1 );
462 BOOST_REQUIRE( settings->m_LibOverrides[tablePath].count( nickname1 ) == 0 );
463
464 // Clear last entry should remove the table key too
465 manager.ClearLibOverride( tablePath, nickname2 );
466 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 0 );
467
468 // SetLibOverride with both false should also clear
469 manager.SetLibOverride( tablePath, nickname1, true, false );
470 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 1 );
471 manager.SetLibOverride( tablePath, nickname1, false, false );
472 BOOST_REQUIRE( settings->m_LibOverrides.count( tablePath ) == 0 );
473}
474
475
482BOOST_AUTO_TEST_CASE( CreateGlobalTableEmptyWhenNoStockTable )
483{
485
486 if( stockPath.IsFileReadable() )
487 {
488 BOOST_TEST_MESSAGE( "Skipping: stock design-block-lib-table exists; test only applies when "
489 "no stock table is installed" );
490 return;
491 }
492
493 const wxString tablePath =
495
496 // RAII guard: restore the global table file to its original state after the test.
497 struct FILE_RESTORE
498 {
499 wxString path;
500 bool existed = false;
501 wxString contents;
502
503 explicit FILE_RESTORE( const wxString& aPath ) : path( aPath )
504 {
505 existed = wxFileName::FileExists( path );
506
507 if( existed )
508 {
509 wxFFile in( path, wxT( "rb" ) );
510 in.ReadAll( &contents );
511 }
512 }
513
514 ~FILE_RESTORE()
515 {
516 if( existed )
517 {
518 wxFFile out( path, wxT( "wb" ) );
519 out.Write( contents );
520 }
521 else if( wxFileName::FileExists( path ) )
522 {
523 wxRemoveFile( path );
524 }
525 }
526 } restore( tablePath );
527
529
530 LIBRARY_TABLE reloaded( wxFileName( tablePath ), LIBRARY_TABLE_SCOPE::GLOBAL );
531
532 BOOST_REQUIRE( reloaded.IsOk() );
533 BOOST_CHECK_MESSAGE( reloaded.Rows().empty(),
534 "CreateGlobalTable must not write a dangling row when the stock table is absent" );
535}
536
537
544BOOST_AUTO_TEST_CASE( StockTableReferenceURIHonorsExternalDefinition )
545{
546 const wxString templateVar = ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) );
548
549 BOOST_REQUIRE( common != nullptr );
550
551 ENV_VAR_MAP& vars = common->m_Env.vars;
552
553 // Preserve and restore the original entry so neighbouring tests are unaffected.
554 const bool hadEntry = vars.count( templateVar ) > 0;
555 const ENV_VAR_ITEM savedEntry = hadEntry ? vars[templateVar] : ENV_VAR_ITEM();
556
559 {
560 ENV_VAR_ITEM& entry = vars[templateVar];
561
562 entry.SetDefinedExternally( false );
565
566 entry.SetDefinedExternally( true );
569 }
570
571 if( hadEntry )
572 vars[templateVar] = savedEntry;
573 else
574 vars.erase( templateVar );
575}
576
577
579static LIBRARY_TABLE makeImportedSymbolTable( const std::vector<std::pair<wxString, wxString>>& aUserRows )
580{
581 LIBRARY_TABLE table( true, wxEmptyString, LIBRARY_TABLE_SCOPE::GLOBAL );
583
584 for( const auto& [nickname, uri] : aUserRows )
585 {
586 LIBRARY_TABLE_ROW& row = table.InsertRow();
587 row.SetNickname( nickname );
588 row.SetURI( uri );
589 row.SetType( wxS( "KiCad" ) );
590 }
591
592 return table;
593}
594
595
596static size_t countChainedKiCadRows( const LIBRARY_TABLE& aTable )
597{
598 return std::ranges::count_if( aTable.Rows(),
599 []( const LIBRARY_TABLE_ROW& aRow )
600 {
601 return aRow.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME
602 && aRow.Nickname() == wxS( "KiCad" );
603 } );
604}
605
606
612BOOST_AUTO_TEST_CASE( MigrateBuiltInLibraries_NoStockRefsAddsNothing )
613{
614 const wxString stockPath = wxS( "${KICAD10_SYMBOL_DIR}/sym-lib-table" );
615
617 { wxS( "MyParts" ), wxS( "${KIPRJMOD}/../libs/MyParts.kicad_sym" ) },
618 { wxS( "MyPassives" ), wxS( "/home/user/kicad/MyPassives.kicad_sym" ) },
619 } );
620
621 const size_t rowsBefore = table.Rows().size();
622
624 table, LIBRARY_TABLE_TYPE::SYMBOL, stockPath, true );
625
626 BOOST_CHECK_MESSAGE( !modified, "Table with no stock references should not be modified" );
627 BOOST_CHECK_EQUAL( table.Rows().size(), rowsBefore );
629}
630
631
633BOOST_AUTO_TEST_CASE( MigrateBuiltInLibraries_DirectStockRowsBecomeChained )
634{
635 const wxString stockPath = wxS( "${KICAD10_SYMBOL_DIR}/sym-lib-table" );
636
638 { wxS( "Device" ), wxS( "${KICAD9_SYMBOL_DIR}/Device.kicad_sym" ) },
639 { wxS( "MyParts" ), wxS( "${KIPRJMOD}/../libs/MyParts.kicad_sym" ) },
640 } );
641
643 table, LIBRARY_TABLE_TYPE::SYMBOL, stockPath, true );
644
645 BOOST_CHECK( modified );
647
648 // The user's own row must survive and the direct stock row must be gone.
649 BOOST_CHECK( table.Row( wxS( "MyParts" ) ).has_value() );
650 BOOST_CHECK( !table.Row( wxS( "Device" ) ).has_value() );
651}
652
653
655BOOST_AUTO_TEST_CASE( MigrateBuiltInLibraries_ChainedRowMigratedInPlace )
656{
657 const wxString stockPath = wxS( "${KICAD10_SYMBOL_DIR}/sym-lib-table" );
658
659 LIBRARY_TABLE table( true, wxEmptyString, LIBRARY_TABLE_SCOPE::GLOBAL );
661
662 LIBRARY_TABLE_ROW& chained = table.InsertRow();
664 chained.SetNickname( wxS( "KiCad" ) );
665 chained.SetURI( wxS( "${KICAD9_SYMBOL_DIR}/sym-lib-table" ) );
666
667 LIBRARY_TABLE_ROW& mine = table.InsertRow();
668 mine.SetNickname( wxS( "MyParts" ) );
669 mine.SetURI( wxS( "${KIPRJMOD}/../libs/MyParts.kicad_sym" ) );
670 mine.SetType( wxS( "KiCad" ) );
671
673 table, LIBRARY_TABLE_TYPE::SYMBOL, stockPath, true );
674
675 BOOST_CHECK( modified );
677
678 auto migrated = table.Row( wxS( "KiCad" ) );
679 BOOST_REQUIRE( migrated.has_value() );
680 BOOST_CHECK_EQUAL( ( *migrated )->URI(), stockPath );
681}
682
683
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...
static wxString DefaultGlobalTablePath(LIBRARY_TABLE_TYPE aType)
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 CreateGlobalTable(LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries)
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:553
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
static KICOMMON_API bool MigrateBuiltInLibraries(LIBRARY_TABLE &aTable, LIBRARY_TABLE_TYPE aType, const wxString &aStockPath, bool aStockPathValid)
Migrates built-in (stock) library references in an imported global library table to the current versi...
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:78
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
static size_t countChainedKiCadRows(const LIBRARY_TABLE &aTable)
BOOST_AUTO_TEST_CASE(Grammar)
static LIBRARY_TABLE makeImportedSymbolTable(const std::vector< std::pair< wxString, wxString > > &aUserRows)
Builds an in-memory symbol library table seeded with the given user rows.
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")