62 void SeedLoadError(
const wxString& aNickname )
72 void SeedLoadedLibrary(
const wxString& aNickname,
const wxString& aUri )
74 LIBRARY_TABLE_ROW* row = m_rows.emplace_back( std::make_unique<LIBRARY_TABLE_ROW>() ).get();
79 LIB_DATA& data = m_libraries[aNickname];
81 data.
plugin = std::make_unique<PCB_IO_KICAD_SEXPR>();
86 std::vector<std::unique_ptr<LIBRARY_TABLE_ROW>> m_rows;
91wxString getResistorLibPath()
96 fn.AppendDir( wxS(
"libraries" ) );
97 fn.AppendDir( wxS(
"Resistor_SMD.pretty" ) );
118 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
120 adapter.SeedLoadError( wxS(
"BadLib" ) );
125 BOOST_CHECK_EQUAL( adapter.IsFootprintLibWritable( wxS(
"NeverSeen" ) ),
false );
141 if( ::geteuid() == 0 )
151 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_save_readonly.pretty" );
154 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
156 const wxString nickname = wxS(
"scratch" );
157 adapter.SeedLoadedLibrary( nickname, libPath.string() );
159 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
167 std::filesystem::path savedFile = libPath /
"readonly_fp.kicad_mod";
172 std::filesystem::permissions( savedFile,
173 std::filesystem::perms::owner_write | std::filesystem::perms::group_write
174 | std::filesystem::perms::others_write,
175 std::filesystem::perm_options::remove );
177 BOOST_CHECK_THROW( adapter.SaveFootprint( nickname, fp ),
IO_ERROR );
189 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_cached_nets.pretty" );
192 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
194 const wxString nickname = wxS(
"CachedNets" );
195 adapter.SeedLoadedLibrary( nickname, libPath.string() );
196 adapter.RefreshLibraryIfChanged( nickname );
198 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
203 board->Add( footprint );
204 footprint->
SetFPID(
LIB_ID( nickname, wxS(
"cached_nets" ) ) );
219 std::unique_ptr<FOOTPRINT> loaded( adapter.LoadFootprint( nickname, wxS(
"cached_nets" ),
false ) );
233 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_concurrent.pretty" );
235 for(
const auto& entry : std::filesystem::directory_iterator(
236 std::filesystem::path( getResistorLibPath().
ToStdString() ) ) )
238 if( entry.is_regular_file() )
239 std::filesystem::copy_file( entry.path(), libPath / entry.path().filename() );
243 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
245 const wxString nickname = wxS(
"Resistor_SMD" );
246 adapter.SeedLoadedLibrary( nickname, libPath.string() );
249 const wxString stableFp = wxS(
"R_0603_1608Metric" );
251 BOOST_REQUIRE( adapter.FootprintExists( nickname, stableFp ) );
253 constexpr int readerCount = 6;
254 constexpr int iterations = 40;
256 std::atomic<bool> sawMissing{
false };
257 std::atomic<bool> sawNullLoad{
false };
258 std::atomic<int> savedCount{ 0 };
261 std::atomic<int> ready{ 0 };
262 std::atomic<bool> go{
false };
264 auto waitForStart = [&]()
266 ready.fetch_add( 1 );
269 std::this_thread::yield();
272 std::vector<std::thread> workers;
273 workers.reserve( readerCount + 1 );
275 for(
int t = 0; t < readerCount; ++t )
277 workers.emplace_back(
282 for(
int i = 0; i < iterations; ++i )
285 adapter.IsWritable( nickname );
287 if( !adapter.FootprintExists( nickname, stableFp ) )
290 std::unique_ptr<FOOTPRINT> fp{ adapter.LoadFootprint( nickname, stableFp,
false ) };
298 workers.emplace_back(
301 std::unique_ptr<FOOTPRINT> seed{ adapter.LoadFootprint( nickname, stableFp,
false ) };
311 for(
int i = 0; i < iterations; ++i )
314 seed->SetFPID(
LIB_ID( nickname, wxString::Format( wxS(
"scratch_%d" ), i % 4 ) ) );
319 savedCount.fetch_add( 1 );
328 while( ready.load() < readerCount + 1 )
329 std::this_thread::yield();
333 for( std::thread& worker : workers )
349 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_stale.pretty" );
351 const std::filesystem::path
source =
352 std::filesystem::path( getResistorLibPath().
ToStdString() ) /
"R_0402_1005Metric.kicad_mod";
354 std::filesystem::copy_file(
source, libPath /
"R_0402_1005Metric.kicad_mod" );
356 const wxString nickname = wxS(
"StaleCheck" );
359 std::ofstream
table( tmpTable.
Path() /
"fp-lib-table" );
360 table <<
"(fp_lib_table\n (version 7)\n";
361 table <<
" (lib (name \"" << nickname.ToStdString() <<
"\")(type \"KiCad\")(uri \""
362 << libPath.string() <<
"\")(options \"\")(descr \"\"))\n)\n";
368 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
369 adapter.SeedLoadedLibrary( nickname, libPath.string() );
371 adapter.RefreshLibraryIfChanged( nickname );
374 std::filesystem::copy_file(
source, libPath /
"ZZ_PulledFootprint.kicad_mod" );
376 adapter.RefreshChangedLibraries();
380 for(
FOOTPRINT* fp : adapter.GetFootprints( nickname,
true ) )
382 if( fp && fp->GetFPID().GetLibItemName().wx_str() == wxS(
"ZZ_PulledFootprint" ) )
386 BOOST_CHECK_MESSAGE( found,
"a footprint added to the library on disk is missing from the listing" );
396 const std::filesystem::path libPathA = tmpA.
CreateChildDir(
"kicad_qa_adapter_skip_a.pretty" );
397 const std::filesystem::path libPathB = tmpB.
CreateChildDir(
"kicad_qa_adapter_skip_b.pretty" );
399 const std::filesystem::path
source =
400 std::filesystem::path( getResistorLibPath().
ToStdString() ) /
"R_0402_1005Metric.kicad_mod";
402 std::filesystem::copy_file(
source, libPathA /
"R_0402_1005Metric.kicad_mod" );
404 for(
int i = 0; i < 8; ++i )
406 std::filesystem::copy_file(
source, libPathB / (
"R_" + std::to_string( i ) +
".kicad_mod" ) );
409 const wxString nickA = wxS(
"SkipCheckA" );
410 const wxString nickB = wxS(
"SkipCheckB" );
412 const std::filesystem::path tablePath = tmpTable.
Path() /
"fp-lib-table";
415 std::ofstream
table( tablePath );
416 table <<
"(fp_lib_table\n (version 7)\n";
417 table <<
" (lib (name \"" << nickA.ToStdString() <<
"\")(type \"KiCad\")(uri \"" << libPathA.string()
418 <<
"\")(options \"\")(descr \"\"))\n";
419 table <<
" (lib (name \"" << nickB.ToStdString() <<
"\")(type \"KiCad\")(uri \"" << libPathB.string()
420 <<
"\")(options \"\")(descr \"\"))\n)\n";
426 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
427 adapter.SeedLoadedLibrary( nickA, libPathA.string() );
428 adapter.SeedLoadedLibrary( nickB, libPathB.string() );
430 adapter.RefreshLibraryIfChanged( nickA );
431 adapter.RefreshLibraryIfChanged( nickB );
438 std::vector<FOOTPRINT*> untouched = adapter.GetFootprints( nickB,
true );
440 std::filesystem::copy_file(
source, libPathA /
"ZZ_Added.kicad_mod" );
442 adapter.RefreshChangedLibraries();
445 BOOST_CHECK_MESSAGE( adapter.GetFootprints( nickB,
true ) == untouched,
"an unchanged library was re-enumerated" );
459 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_rt.pretty" );
461 const std::filesystem::path
source =
462 std::filesystem::path( getResistorLibPath().
ToStdString() ) /
"R_0402_1005Metric.kicad_mod";
463 const std::filesystem::path target = libPath /
"R_0402_1005Metric.kicad_mod";
465 std::filesystem::copy_file(
source, target );
467 const wxString nickname = wxS(
"RoundTrip" );
468 const wxString fpName = wxS(
"R_0402_1005Metric" );
471 std::ofstream
table( tmpTable.
Path() /
"fp-lib-table" );
472 table <<
"(fp_lib_table\n (version 7)\n";
473 table <<
" (lib (name \"" << nickname.ToStdString() <<
"\")(type \"KiCad\")(uri \""
474 << libPath.string() <<
"\")(options \"\")(descr \"\"))\n)\n";
478 [](
const std::filesystem::path& aPath )
480 std::set<std::string> ids;
481 std::ifstream in( aPath );
484 while( std::getline( in, line ) )
486 size_t pos = line.find(
"(uuid \"" );
488 if( pos != std::string::npos )
489 ids.insert( line.substr( pos + 7, 36 ) );
495 const std::set<std::string> before = fileUuids( target );
500 TEST_FOOTPRINT_LIBRARY_ADAPTER adapter( manager );
501 adapter.SeedLoadedLibrary( nickname, libPath.string() );
504 adapter.RefreshLibraryIfChanged( nickname );
506 std::unique_ptr<FOOTPRINT> edited( adapter.LoadFootprint( nickname, fpName,
true ) );
511 const std::set<std::string> after = fileUuids( target );
513 std::vector<std::string> common;
514 std::set_intersection( before.begin(), before.end(), after.begin(), after.end(),
515 std::back_inserter( common ) );
520 BOOST_CHECK_MESSAGE( common.size() == after.size(),
521 "a load/save round trip through the editor path rewrote UUIDs in the .kicad_mod" );
533 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"kicad_qa_adapter_removed_lib.pretty" );
535 const std::filesystem::path
source =
536 std::filesystem::path( getResistorLibPath().
ToStdString() ) /
"R_0402_1005Metric.kicad_mod";
538 std::filesystem::copy_file(
source, libPath /
"R_0402_1005Metric.kicad_mod" );
540 const wxString nickname = wxS(
"RemovedCheck" );
543 std::ofstream
table( tmpTable.
Path() /
"fp-lib-table" );
544 table <<
"(fp_lib_table\n (version 7)\n";
545 table <<
" (lib (name \"" << nickname.ToStdString() <<
"\")(type \"KiCad\")(uri \"" << libPath.string()
546 <<
"\")(options \"\")(descr \"\"))\n)\n";
553 std::make_unique<TEST_FOOTPRINT_LIBRARY_ADAPTER>( manager ) );
557 TEST_FOOTPRINT_LIBRARY_ADAPTER& adapter = *
static_cast<TEST_FOOTPRINT_LIBRARY_ADAPTER*
>( *adapterOpt );
559 adapter.SeedLoadedLibrary( nickname, libPath.string() );
560 adapter.RefreshLibraryIfChanged( nickname );
565 std::ofstream
table( tmpTable.
Path() /
"fp-lib-table" );
566 table <<
"(fp_lib_table\n (version 7)\n)\n";
571 FOOTPRINT* fp = adapter.LoadFootprint( nickname, wxS(
"R_0402_1005Metric" ),
false );
572 BOOST_CHECK_MESSAGE( !fp,
"a library removed from the table must not keep serving preloaded "
584 wxFileName
source( getResistorLibPath(), wxS(
"R_0402_1005Metric.kicad_mod" ) );
586 source.AppendDir( wxS(
"Lifecycle_SMD.pretty" ) );
588 const std::filesystem::path libPath = tmpLib.
CreateChildDir(
"Lifecycle_SMD.pretty" );
589 std::filesystem::copy_file(
source.GetFullPath().ToStdString(), libPath /
"R_0402_1005Metric.kicad_mod" );
592 const wxString lib = wxString::FromUTF8( libPath.string() );
594 std::unique_ptr<FOOTPRINT> footprint( plugin->FootprintLoad( lib, wxS(
"R_0402_1005Metric" ),
true ) );
597 const PCB_FIELD* lifecycle = footprint->GetField( wxS(
"Lifecycle" ) );
601 plugin->FootprintSave( lib, footprint.get() );
604 std::unique_ptr<FOOTPRINT> saved( reader->FootprintLoad( lib, wxS(
"R_0402_1005Metric" ),
true ) );
605 BOOST_REQUIRE( saved && saved->GetField( wxS(
"Lifecycle" ) ) );
constexpr EDA_IU_SCALE pcbIUScale
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
wxString AsString() const
wxString PathStr() const
Get the path to the temporary directory as a wxString.
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
std::optional< LIBRARY_MANAGER_ADAPTER * > Adapter(LIBRARY_TABLE_TYPE aType) const
void RegisterAdapter(LIBRARY_TABLE_TYPE aType, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > &&aAdapter)
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
A logical library item identifier and consists of various portions much like a URI.
LSET is a set of PCB_LAYER_IDs.
Handle the data for a net.
static NETINFO_ITEM * OrphanedItem()
NETINFO_ITEM meaning that there was no net assigned for an item, as there was no board storing net li...
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
@ KICAD_SEXP
S-expression Pcbnew file format.
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a PCB_IO which the caller can use to import, export, save, or load design documents.
static std::string ToStdString(const wxString &aStr)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::unique_ptr< IO_BASE > plugin
Never destroyed while holding the map's mutex; a plugin's worker thread may be blocked on it.
const LIBRARY_TABLE_ROW * row
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
VECTOR2< int32_t > VECTOR2I