KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_library_tables.cpp File Reference
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <utility>
#include <vector>
#include <wx/ffile.h>
#include <mock_pgm_base.h>
#include <richio.h>
#include <io/kicad/kicad_io_utils.h>
#include <qa_utils/file_utils.h>
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <settings/settings_manager.h>
#include <pegtl/contrib/analyze.hpp>
#include <env_vars.h>
#include <pgm_base.h>
#include <settings/common_settings.h>
#include <libraries/library_manager.h>
#include <libraries/library_table.h>
#include <libraries/library_table_parser.h>
#include <libraries/library_table_grammar.h>
#include <settings/kicad_settings.h>
#include <startwizard/startwizard_provider_libraries.h>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (Grammar)
 
 BOOST_AUTO_TEST_CASE (EmptyString)
 
 BOOST_AUTO_TEST_CASE (ParseFromFile)
 
 BOOST_AUTO_TEST_CASE (ParseAndConstruct)
 
 BOOST_AUTO_TEST_CASE (Manager)
 
 BOOST_AUTO_TEST_CASE (ManagerTableReturnsNulloptForUnloadedType)
 
 BOOST_AUTO_TEST_CASE (NestedTablesDisabledHidden)
 
 BOOST_AUTO_TEST_CASE (InsertRowPreservesExistingRowPointers)
 Regression test: inserting rows into a loaded LIBRARY_TABLE must not invalidate pointers or references to previously-captured rows.
 
 BOOST_AUTO_TEST_CASE (IsPcmManagedRow_URITemplateMatching)
 Regression test for the PCM auto-remove identification predicate.
 
 BOOST_AUTO_TEST_CASE (ReadOnlyTable)
 
 BOOST_AUTO_TEST_CASE (RowsAddedToANewProjectTableAreEnumerated)
 Regression test for a new project-scope library not appearing in the symbol editor library tree until KiCad was restarted.
 
 BOOST_AUTO_TEST_CASE (LibOverrideSettings)
 
 BOOST_AUTO_TEST_CASE (CreateGlobalTableEmptyWhenNoStockTable)
 Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24626.
 
 BOOST_AUTO_TEST_CASE (StockTableReferenceURIHonorsExternalDefinition)
 The stock-table reference URI written into a freshly created global table must only use the env-var token when the template-dir variable is defined externally (the relocatable-install case from https://gitlab.com/kicad/code/kicad/-/issues/23081).
 
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.
 
static size_t countChainedKiCadRows (const LIBRARY_TABLE &aTable)
 
 BOOST_AUTO_TEST_CASE (MigrateBuiltInLibraries_NoStockRefsAddsNothing)
 Regression test for issue 24594.
 
 BOOST_AUTO_TEST_CASE (MigrateBuiltInLibraries_DirectStockRowsBecomeChained)
 Direct stock rows are removed and replaced by a single chained reference to the latest stock.
 
 BOOST_AUTO_TEST_CASE (MigrateBuiltInLibraries_ChainedRowMigratedInPlace)
 An existing chained reference is repointed at the latest stock without a second one being added.
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/17]

BOOST_AUTO_TEST_CASE ( CreateGlobalTableEmptyWhenNoStockTable )

Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24626.

When KiCad ships no stock design-block-lib-table, CreateGlobalTable must produce a valid empty table rather than a table with a dangling row pointing at the missing file.

Definition at line 545 of file test_library_tables.cpp.

References BOOST_REQUIRE(), BOOST_TEST_MESSAGE(), LIBRARY_MANAGER::CreateGlobalTable(), LIBRARY_MANAGER::DefaultGlobalTablePath(), DESIGN_BLOCK, GLOBAL, LIBRARY_TABLE::IsOk(), path, LIBRARY_TABLE::Rows(), and LIBRARY_MANAGER::StockTablePath().

◆ BOOST_AUTO_TEST_CASE() [2/17]

BOOST_AUTO_TEST_CASE ( EmptyString )

◆ BOOST_AUTO_TEST_CASE() [3/17]

BOOST_AUTO_TEST_CASE ( Grammar )

Definition at line 51 of file test_library_tables.cpp.

References BOOST_AUTO_TEST_CASE(), and BOOST_REQUIRE().

◆ BOOST_AUTO_TEST_CASE() [4/17]

BOOST_AUTO_TEST_CASE ( InsertRowPreservesExistingRowPointers )

Regression test: inserting rows into a loaded LIBRARY_TABLE must not invalidate pointers or references to previously-captured rows.

The remote symbol import path (EnsureRemoteLibraryEntry) calls InsertRow() at runtime while LIB_DATA instances and LIBRARY_MANAGER::m_rowCache hold raw pointers into the rows container. A std::vector-backed container would reallocate on growth and leave those pointers dangling, which produced an intermittent std::bad_alloc crash deep in KIwxExpandEnvVars when the next symbol placement tried to read the stale URI.

Definition at line 260 of file test_library_tables.cpp.

References PROJECT, LIBRARY_TABLE_ROW::SetNickname(), LIBRARY_TABLE_ROW::SetType(), LIBRARY_TABLE_ROW::SetURI(), SYMBOL, table, and LIBRARY_TABLE_ROW::URI().

◆ BOOST_AUTO_TEST_CASE() [5/17]

BOOST_AUTO_TEST_CASE ( IsPcmManagedRow_URITemplateMatching )

Regression test for the PCM auto-remove identification predicate.

Rows inserted by the PCM traverser reference ${KICADn_3RD_PARTY} directly, so matching on the URI template uniquely identifies them and avoids cleaning up user-added libraries whose expanded absolute paths happen to be descendants of the 3RD_PARTY directory via a different env var (e.g. ${KICAD_USER_LIB} pointing to ${KICAD10_3RD_PARTY}/V10).

Prior to this fix, cleanupRemovedPCMLibraries did a raw prefix check on the expanded path, mis-identifying overlapping user libraries as PCM-managed and silently deleting their rows any time the file was temporarily absent. The user-visible symptom was "Could not create the library file" when adding a new library through the symbol editor.

Definition at line 316 of file test_library_tables.cpp.

References actual, LIBRARY_MANAGER::IsPcmManagedRow(), and LIBRARY_TABLE_ROW::SetURI().

◆ BOOST_AUTO_TEST_CASE() [6/17]

◆ BOOST_AUTO_TEST_CASE() [7/17]

BOOST_AUTO_TEST_CASE ( Manager )

◆ BOOST_AUTO_TEST_CASE() [8/17]

BOOST_AUTO_TEST_CASE ( ManagerTableReturnsNulloptForUnloadedType )

◆ BOOST_AUTO_TEST_CASE() [9/17]

BOOST_AUTO_TEST_CASE ( MigrateBuiltInLibraries_ChainedRowMigratedInPlace )

◆ BOOST_AUTO_TEST_CASE() [10/17]

BOOST_AUTO_TEST_CASE ( MigrateBuiltInLibraries_DirectStockRowsBecomeChained )

Direct stock rows are removed and replaced by a single chained reference to the latest stock.

Definition at line 696 of file test_library_tables.cpp.

References BOOST_CHECK_EQUAL(), countChainedKiCadRows(), makeImportedSymbolTable(), STARTWIZARD_PROVIDER_LIBRARIES::MigrateBuiltInLibraries(), SYMBOL, and table.

◆ BOOST_AUTO_TEST_CASE() [11/17]

BOOST_AUTO_TEST_CASE ( MigrateBuiltInLibraries_NoStockRefsAddsNothing )

Regression test for issue 24594.

A user who removed every built-in KiCad library in the previous version selected "Import tables" + "Migrate built-in libraries"; migration must not silently add the stock libraries back into their table.

Definition at line 675 of file test_library_tables.cpp.

References BOOST_CHECK_EQUAL(), countChainedKiCadRows(), makeImportedSymbolTable(), STARTWIZARD_PROVIDER_LIBRARIES::MigrateBuiltInLibraries(), SYMBOL, and table.

◆ BOOST_AUTO_TEST_CASE() [12/17]

BOOST_AUTO_TEST_CASE ( NestedTablesDisabledHidden )

◆ BOOST_AUTO_TEST_CASE() [13/17]

◆ BOOST_AUTO_TEST_CASE() [14/17]

BOOST_AUTO_TEST_CASE ( ParseFromFile )

◆ BOOST_AUTO_TEST_CASE() [15/17]

◆ BOOST_AUTO_TEST_CASE() [16/17]

BOOST_AUTO_TEST_CASE ( RowsAddedToANewProjectTableAreEnumerated )

Regression test for a new project-scope library not appearing in the symbol editor library tree until KiCad was restarted.

A project with no sym-lib-table has no table on disk, so the manager builds one on demand for a path that does not exist. The table constructor treats a missing file as a parse failure and flags the table invalid, and Rows() drops every row of an invalid table. A library added to such a project was written to disk but stayed invisible to everything that enumerates rows, which is why it only turned up after a restart.

Definition at line 446 of file test_library_tables.cpp.

References BOOST_CHECK_EQUAL(), BOOST_REQUIRE(), PGM_BASE::GetSettingsManager(), SETTINGS_MANAGER::LoadProject(), Pgm(), SETTINGS_MANAGER::Prj(), PROJECT, LIBRARY_MANAGER::Rows(), LIBRARY_TABLE_ROW::SetNickname(), LIBRARY_TABLE_ROW::SetType(), LIBRARY_TABLE_ROW::SetURI(), SYMBOL, LIBRARY_MANAGER::Table(), table, and SETTINGS_MANAGER::UnloadProject().

◆ BOOST_AUTO_TEST_CASE() [17/17]

BOOST_AUTO_TEST_CASE ( StockTableReferenceURIHonorsExternalDefinition )

The stock-table reference URI written into a freshly created global table must only use the env-var token when the template-dir variable is defined externally (the relocatable-install case from https://gitlab.com/kicad/code/kicad/-/issues/23081).

When the variable is at its built-in default the standard, resolved absolute path is preserved.

Definition at line 607 of file test_library_tables.cpp.

References BOOST_CHECK_EQUAL(), BOOST_REQUIRE(), DESIGN_BLOCK, FOOTPRINT, PGM_BASE::GetCommonSettings(), ENV_VAR::GetVersionedEnvVarName(), COMMON_SETTINGS::m_Env, Pgm(), ENV_VAR_ITEM::SetDefinedExternally(), LIBRARY_MANAGER::StockTablePath(), LIBRARY_MANAGER::StockTableReferenceURI(), LIBRARY_MANAGER::StockTableTokenizedURI(), SYMBOL, and COMMON_SETTINGS::ENVIRONMENT::vars.

◆ countChainedKiCadRows()

static size_t countChainedKiCadRows ( const LIBRARY_TABLE & aTable)
static

◆ makeImportedSymbolTable()

static LIBRARY_TABLE makeImportedSymbolTable ( const std::vector< std::pair< wxString, wxString > > & aUserRows)
static

Builds an in-memory symbol library table seeded with the given user rows.

Definition at line 642 of file test_library_tables.cpp.

References GLOBAL, LIBRARY_TABLE_ROW::SetNickname(), LIBRARY_TABLE_ROW::SetType(), LIBRARY_TABLE_ROW::SetURI(), SYMBOL, and table.

Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().