KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_history_autosave.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 3
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 <board.h>
23#include <local_history.h>
24#include <pgm_base.h>
25#include <project.h>
28
29#include <git2.h>
30
31#include <vector>
32
33#include <wx/datetime.h>
34#include <wx/dir.h>
35#include <wx/ffile.h>
36#include <wx/filefn.h>
37#include <wx/filename.h>
38#include <wx/stdpaths.h>
39
40
41namespace
42{
43// single_top handles libgit2 init in production; the QA harness does not, so tests driving
44// LOCAL_HISTORY must manage it themselves.
45struct LIBGIT2_SCOPE
46{
47 LIBGIT2_SCOPE() { git_libgit2_init(); }
48 ~LIBGIT2_SCOPE() { git_libgit2_shutdown(); }
49};
50
51
52struct SCOPED_BOOL_OVERRIDE
53{
54 explicit SCOPED_BOOL_OVERRIDE( bool& aFlag ) : m_flag( aFlag ), m_original( aFlag ) {}
55 ~SCOPED_BOOL_OVERRIDE() { m_flag = m_original; }
56
57 bool& m_flag;
58 bool m_original;
59};
60
61
62// Restore the backup location on destruction so a thrown BOOST_REQUIRE cannot leak the
63// override into later tests.
64struct SCOPED_BACKUP_LOCATION_OVERRIDE
65{
66 explicit SCOPED_BACKUP_LOCATION_OVERRIDE( BACKUP_LOCATION& aLocation ) :
67 m_location( aLocation ), m_original( aLocation )
68 {
69 }
70
71 ~SCOPED_BACKUP_LOCATION_OVERRIDE() { m_location = m_original; }
72
73 BACKUP_LOCATION& m_location;
74 BACKUP_LOCATION m_original;
75};
76
77
78// Load a project into the settings manager and unload it on destruction, keeping the global
79// active-project state isolated even when a test aborts partway through.
80struct SCOPED_PROJECT_LOAD
81{
82 SCOPED_PROJECT_LOAD( SETTINGS_MANAGER& aMgr, const wxString& aProjectFile ) : m_mgr( aMgr )
83 {
84 m_mgr.LoadProject( aProjectFile.ToStdString() );
85 }
86
87 ~SCOPED_PROJECT_LOAD() { m_mgr.UnloadProject( &m_mgr.Prj(), false ); }
88
89 SETTINGS_MANAGER& m_mgr;
90};
91
92
93// Recursively remove the directory on destruction so test failures (which throw out of
94// BOOST_REQUIRE) do not leak temp directories.
95struct SCOPED_TEMP_DIR
96{
97 explicit SCOPED_TEMP_DIR( const wxString& aPrefix )
98 {
99 wxString base = wxStandardPaths::Get().GetTempDir();
100 m_path = base + wxFileName::GetPathSeparator() + aPrefix
101 + wxString::Format( wxS( "_%lu_%ld" ),
102 static_cast<unsigned long>( ::wxGetProcessId() ),
103 static_cast<long>( wxDateTime::UNow().GetTicks() ) );
104 wxFileName::Mkdir( m_path, 0777, wxPATH_MKDIR_FULL );
105 }
106
107 ~SCOPED_TEMP_DIR()
108 {
109 if( !m_path.IsEmpty() && wxDirExists( m_path ) )
110 wxFileName::Rmdir( m_path, wxPATH_RMDIR_RECURSIVE );
111 }
112
113 const wxString& Path() const { return m_path; }
114
115 wxString m_path;
116};
117
118
119void writeTextFile( const wxString& aPath, const wxString& aContents )
120{
121 wxFFile f( aPath, wxT( "w" ) );
122 BOOST_REQUIRE( f.IsOpened() );
123 f.Write( aContents );
124 f.Close();
125}
126} // namespace
127
128
129BOOST_AUTO_TEST_SUITE( PcbHistoryAutosave )
130
131
132
141BOOST_AUTO_TEST_CASE( SaveToHistoryWithNullProjectDoesNotCrash )
142{
143 BOARD board;
144 std::vector<HISTORY_FILE_DATA> fileData;
145
146 BOOST_REQUIRE( board.GetProject() == nullptr );
147
148 BOOST_CHECK_NO_THROW( board.SaveToHistory( wxS( "/tmp/anywhere" ), fileData ) );
149 BOOST_CHECK( fileData.empty() );
150}
151
152
157BOOST_AUTO_TEST_CASE( SaveToHistoryUnsavedBoardProducesNothing )
158{
160
161 wxString tempDir = wxStandardPaths::Get().GetTempDir();
162 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxS( "pcb_autosave.kicad_pro" );
163
164 mgr.LoadProject( projectPath.ToStdString() );
165
166 BOARD board;
167 board.SetProject( &mgr.Prj() );
168
169 std::vector<HISTORY_FILE_DATA> fileData;
170
171 BOOST_REQUIRE( board.GetFileName().IsEmpty() );
172 BOOST_CHECK_NO_THROW( board.SaveToHistory( mgr.Prj().GetProjectPath(), fileData ) );
173 BOOST_CHECK( fileData.empty() );
174
175 // Detach project before BOARD destruction so design settings ownership unwinds cleanly
176 board.ClearProject();
177 mgr.UnloadProject( &mgr.Prj(), false );
178}
179
180
188BOOST_AUTO_TEST_CASE( NoSnapshotWithoutProjectFile )
189{
190 LIBGIT2_SCOPE libgit;
191
192 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
193 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
194 backupEnabled = true;
195
196 SCOPED_TEMP_DIR notAProject( wxS( "kicad_qa_no_project" ) );
197 const wxString& path = notAProject.Path();
198
199 // Drop a board file in but no .kicad_pro - this mirrors saving a board to /tmp
200 // from standalone pcbnew.
201 wxString boardPath = path + wxFileName::GetPathSeparator() + wxS( "stray.kicad_pcb" );
202 writeTextFile( boardPath, wxS( "(kicad_pcb (version 20240108))\n" ) );
203
204 LOCAL_HISTORY history;
205
206 BOOST_CHECK( !history.Init( path ) );
207 BOOST_CHECK( !history.CommitFullProjectSnapshot( path, wxS( "PCB Save" ) ) );
208 BOOST_CHECK( !history.TagSave( path, wxS( "pcb" ) ) );
209
210 // No .history directory should have been created.
211 wxString historyDir = path + wxFileName::GetPathSeparator() + wxS( ".history" );
212 BOOST_CHECK( !wxDirExists( historyDir ) );
213}
214
215
223BOOST_AUTO_TEST_CASE( CommitFullProjectSnapshotHandlesSubdirectories )
224{
225 LIBGIT2_SCOPE libgit;
226
227 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
228 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
229 backupEnabled = true;
230
231 SCOPED_TEMP_DIR project( wxS( "kicad_qa_subdirs" ) );
232 const wxString& path = project.Path();
233
234 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "subdirs.kicad_pro" ), wxS( "{}\n" ) );
235 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "subdirs.kicad_pcb" ),
236 wxS( "(kicad_pcb (version 20240108))\n" ) );
237
238 // Create a subdirectory whose contents will likely be collected before files at the
239 // project root, exercising the subdirectory-first iteration order.
240 wxString subDir = path + wxFileName::GetPathSeparator() + wxS( "libs" );
241 BOOST_REQUIRE( wxFileName::Mkdir( subDir, 0777, wxPATH_MKDIR_FULL ) );
242 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS( "fp.kicad_mod" ),
243 wxS( "(footprint test)\n" ) );
244
245 LOCAL_HISTORY history;
246 BOOST_REQUIRE( history.CommitFullProjectSnapshot( path, wxS( "Initial" ) ) );
247
248 // History must have been created at the project root, not at the subdirectory.
249 wxString historyDir = path + wxFileName::GetPathSeparator() + wxS( ".history" );
250 BOOST_CHECK( wxDirExists( historyDir ) );
251 BOOST_CHECK( !wxDirExists( subDir + wxFileName::GetPathSeparator() + wxS( ".history" ) ) );
252
253 wxString headBefore = history.GetHeadHash( path );
254 BOOST_REQUIRE( !headBefore.IsEmpty() );
255
256 // Mutate a file in the subdirectory to ensure the next snapshot has work to do.
257 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS( "fp.kicad_mod" ),
258 wxS( "(footprint test (modified))\n" ) );
259
260 // CommitFullProjectSnapshot must commit using the project root, not derive a wrong
261 // root from the first file in the recursive collection.
262 BOOST_CHECK( history.CommitFullProjectSnapshot( path, wxS( "PCB Save" ) ) );
263
264 wxString headAfter = history.GetHeadHash( path );
265 BOOST_REQUIRE( !headAfter.IsEmpty() );
266 BOOST_CHECK( headBefore != headAfter );
267}
268
269
270// Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24016
271BOOST_AUTO_TEST_CASE( RestoreCommitPreservesZipBackupsDirectory )
272{
273 LIBGIT2_SCOPE libgit;
274
275 // LOCAL_HISTORY early-exits when backups are disabled.
276 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
277 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
278 backupEnabled = true;
279
280 SCOPED_TEMP_DIR tempProject( wxS( "kicad_qa_issue24016" ) );
281 const wxString& projectPath = tempProject.Path();
282
283 wxString boardPath =
284 projectPath + wxFileName::GetPathSeparator() + wxS( "issue24016.kicad_pcb" );
285 writeTextFile( boardPath, wxS( "(kicad_pcb (version 20240108))\n" ) );
286
287 // LOCAL_HISTORY refuses to operate on directories without a project file, so seed
288 // the fixture with a minimal one to mirror a real KiCad project layout.
289 wxString projectFile =
290 projectPath + wxFileName::GetPathSeparator() + wxS( "issue24016.kicad_pro" );
291 writeTextFile( projectFile, wxS( "{}\n" ) );
292
293 LOCAL_HISTORY history;
294 BOOST_REQUIRE( history.CommitFullProjectSnapshot( projectPath, wxS( "Initial" ) ) );
295
296 wxString headHash = history.GetHeadHash( projectPath );
297 BOOST_REQUIRE( !headHash.IsEmpty() );
298
299 // Mirror SETTINGS_MANAGER::BackupProject output: a sibling "<name>-backups" directory
300 // containing one or more .zip archives.
301 wxString backupsDir =
302 projectPath + wxFileName::GetPathSeparator() + wxS( "issue24016-backups" );
303 BOOST_REQUIRE( wxFileName::Mkdir( backupsDir, 0777, wxPATH_MKDIR_FULL ) );
304
305 wxString zipPath = backupsDir + wxFileName::GetPathSeparator()
306 + wxS( "issue24016-2026-04-22_120000.zip" );
307 writeTextFile( zipPath, wxS( "pretend-zip-contents" ) );
308
309 writeTextFile( boardPath, wxS( "(kicad_pcb (version 20240108) (dirty yes))\n" ) );
310
311 BOOST_REQUIRE( history.RestoreCommit( projectPath, headHash, nullptr ) );
312
313 BOOST_CHECK_MESSAGE( wxDirExists( backupsDir ),
314 "zip backups directory must survive RestoreCommit" );
315 BOOST_CHECK_MESSAGE( wxFileExists( zipPath ),
316 ".zip archive inside the backups directory must survive RestoreCommit" );
317}
318
319
333BOOST_AUTO_TEST_CASE( RestoreCommitPreservesNestedProject )
334{
335 LIBGIT2_SCOPE libgit;
336
337 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
338 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
339 backupEnabled = true;
340
341 SCOPED_TEMP_DIR tempProject( wxS( "kicad_qa_nested_project" ) );
342 const wxString& projectPath = tempProject.Path();
343
344 // Parent project (projectA): minimal .kicad_pro plus a board file.
345 wxString parentPro = projectPath + wxFileName::GetPathSeparator() + wxS( "projectA.kicad_pro" );
346 wxString parentPcb = projectPath + wxFileName::GetPathSeparator() + wxS( "projectA.kicad_pcb" );
347 writeTextFile( parentPro, wxS( "{}\n" ) );
348 writeTextFile( parentPcb, wxS( "(kicad_pcb (version 20240108))\n" ) );
349
350 LOCAL_HISTORY history;
351 BOOST_REQUIRE( history.CommitFullProjectSnapshot( projectPath, wxS( "Initial" ) ) );
352
353 wxString headHash = history.GetHeadHash( projectPath );
354 BOOST_REQUIRE( !headHash.IsEmpty() );
355
356 // Now drop a nested project under projectA/. The user's scenario was that this nested
357 // project was added AFTER the parent's snapshot was committed - so its files are not in
358 // the restored commit, and a naive restore would propose them for deletion.
359 wxString nestedDir = projectPath + wxFileName::GetPathSeparator() + wxS( "projectB" );
360 BOOST_REQUIRE( wxFileName::Mkdir( nestedDir, 0777, wxPATH_MKDIR_FULL ) );
361
362 wxString nestedPro = nestedDir + wxFileName::GetPathSeparator() + wxS( "projectB.kicad_pro" );
363 wxString nestedPcb = nestedDir + wxFileName::GetPathSeparator() + wxS( "projectB.kicad_pcb" );
364 wxString nestedSch = nestedDir + wxFileName::GetPathSeparator() + wxS( "projectB.kicad_sch" );
365 writeTextFile( nestedPro, wxS( "{ \"nested\": true }\n" ) );
366 writeTextFile( nestedPcb, wxS( "(kicad_pcb (version 20240108) (nested yes))\n" ) );
367 writeTextFile( nestedSch, wxS( "(kicad_sch (version 20240108))\n" ) );
368
369 // Modify the parent board so the restore actually has work to do.
370 writeTextFile( parentPcb, wxS( "(kicad_pcb (version 20240108) (dirty yes))\n" ) );
371
372 BOOST_REQUIRE( history.RestoreCommit( projectPath, headHash, nullptr ) );
373
374 // The nested project's directory and every one of its files MUST survive the restore.
375 BOOST_CHECK_MESSAGE( wxDirExists( nestedDir ),
376 "nested project directory must survive RestoreCommit" );
377 BOOST_CHECK_MESSAGE( wxFileExists( nestedPro ),
378 "nested .kicad_pro must survive RestoreCommit" );
379 BOOST_CHECK_MESSAGE( wxFileExists( nestedPcb ),
380 "nested .kicad_pcb must survive RestoreCommit" );
381 BOOST_CHECK_MESSAGE( wxFileExists( nestedSch ),
382 "nested .kicad_sch must survive RestoreCommit" );
383
384 // Parent project files were correctly restored.
385 BOOST_CHECK( wxFileExists( parentPro ) );
386 BOOST_CHECK( wxFileExists( parentPcb ) );
387}
388
389
395BOOST_AUTO_TEST_CASE( RestoreCommitRetainsTimestampedBackup )
396{
397 LIBGIT2_SCOPE libgit;
398
399 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
400 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
401 backupEnabled = true;
402
403 SCOPED_TEMP_DIR tempProject( wxS( "kicad_qa_retained_backup" ) );
404 const wxString& projectPath = tempProject.Path();
405
406 wxString boardPath = projectPath + wxFileName::GetPathSeparator() + wxS( "rb.kicad_pcb" );
407 wxString projectFile = projectPath + wxFileName::GetPathSeparator() + wxS( "rb.kicad_pro" );
408 writeTextFile( projectFile, wxS( "{}\n" ) );
409 writeTextFile( boardPath, wxS( "(kicad_pcb (version 20240108))\n" ) );
410
411 LOCAL_HISTORY history;
412 BOOST_REQUIRE( history.CommitFullProjectSnapshot( projectPath, wxS( "Initial" ) ) );
413
414 wxString headHash = history.GetHeadHash( projectPath );
415 BOOST_REQUIRE( !headHash.IsEmpty() );
416
417 // Mutate the board so restore has work to do (and produces a backup).
418 writeTextFile( boardPath, wxS( "(kicad_pcb (version 20240108) (dirty yes))\n" ) );
419
420 BOOST_REQUIRE( history.RestoreCommit( projectPath, headHash, nullptr ) );
421
422 // Backups land at a SIBLING path (aProjectPath + "_restore_backup_<ts>"), so look in
423 // the parent directory of the project. Same convention as the legacy "_restore_backup".
424 wxString parentDir = wxFileName( projectPath ).GetPath();
425 wxString leafPrefix = wxFileName( projectPath ).GetFullName() + wxS( "_restore_backup_" );
426
427 wxDir dir( parentDir );
428 BOOST_REQUIRE( dir.IsOpened() );
429
430 wxString retainedBackup;
431 bool foundLegacyBackup = false;
432
433 wxString name;
434 for( bool cont = dir.GetFirst( &name, wxEmptyString, wxDIR_DIRS ); cont;
435 cont = dir.GetNext( &name ) )
436 {
437 if( name.StartsWith( leafPrefix ) )
438 {
439 retainedBackup = parentDir + wxFileName::GetPathSeparator() + name;
440
441 // No colons - Windows path-safe.
442 BOOST_CHECK_MESSAGE( name.Find( ':' ) == wxNOT_FOUND,
443 "retained backup directory name must not contain ':' "
444 "(Windows-illegal in path components)" );
445 }
446 else if( name == wxFileName( projectPath ).GetFullName() + wxS( "_restore_backup" ) )
447 {
448 foundLegacyBackup = true;
449 }
450 }
451
453 !retainedBackup.IsEmpty(),
454 "RestoreCommit must retain a timestamped _restore_backup_<ts>/ sibling directory" );
456 !foundLegacyBackup,
457 "RestoreCommit must not leave the legacy non-timestamped _restore_backup/ behind" );
458
459 // Clean up the retained backup so the test does not leak files into /tmp.
460 if( !retainedBackup.IsEmpty() && wxDirExists( retainedBackup ) )
461 wxFileName::Rmdir( retainedBackup, wxPATH_RMDIR_RECURSIVE );
462}
463
464
468BOOST_AUTO_TEST_CASE( CommitFullProjectSnapshotExcludesNonKiCadFiles )
469{
470 LIBGIT2_SCOPE libgit;
471
472 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
473 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
474 backupEnabled = true;
475
476 SCOPED_TEMP_DIR project( wxS( "kicad_qa_privacy" ) );
477 const wxString& path = project.Path();
478
479 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pro" ), wxS( "{}\n" ) );
480 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pcb" ),
481 wxS( "(kicad_pcb (version 20240108))\n" ) );
482 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_sch" ),
483 wxS( "(kicad_sch (version 20240108))\n" ) );
484
485 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "passwords.txt" ), wxS( "secret\n" ) );
486 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "datasheet.pdf" ), wxS( "fake pdf bytes\n" ) );
487 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "notes.md" ), wxS( "personal notes\n" ) );
488
489 wxString subDir = path + wxFileName::GetPathSeparator() + wxS( "docs" );
490 BOOST_REQUIRE( wxFileName::Mkdir( subDir, 0777, wxPATH_MKDIR_FULL ) );
491 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS( "manual.txt" ), wxS( "irrelevant\n" ) );
492
493 LOCAL_HISTORY history;
494 BOOST_REQUIRE( history.CommitFullProjectSnapshot( path, wxS( "Close" ) ) );
495
496 wxString hist = path + wxFileName::GetPathSeparator() + wxS( ".history" );
497 git_repository* repo = nullptr;
498 BOOST_REQUIRE_EQUAL( git_repository_open( &repo, hist.mb_str().data() ), 0 );
499
500 git_oid head_oid;
501 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &head_oid, repo, "HEAD" ), 0 );
502
503 git_commit* head = nullptr;
504 BOOST_REQUIRE_EQUAL( git_commit_lookup( &head, repo, &head_oid ), 0 );
505
506 git_tree* tree = nullptr;
507 BOOST_REQUIRE_EQUAL( git_commit_tree( &tree, head ), 0 );
508
509 std::vector<std::string> committedPaths;
510 git_tree_walk(
511 tree, GIT_TREEWALK_PRE,
512 []( const char* root, const git_tree_entry* entry, void* payload ) -> int
513 {
514 auto* paths = static_cast<std::vector<std::string>*>( payload );
515
516 if( git_tree_entry_type( entry ) == GIT_OBJECT_BLOB )
517 paths->push_back( std::string( root ) + git_tree_entry_name( entry ) );
518
519 return 0;
520 },
521 &committedPaths );
522
523 git_tree_free( tree );
524 git_commit_free( head );
525 git_repository_free( repo );
526
527 auto contains = [&]( const std::string& s )
528 {
529 return std::find( committedPaths.begin(), committedPaths.end(), s ) != committedPaths.end();
530 };
531
532 BOOST_CHECK_MESSAGE( contains( "p.kicad_pro" ), "kicad_pro must be committed" );
533 BOOST_CHECK_MESSAGE( contains( "p.kicad_pcb" ), "kicad_pcb must be committed" );
534 BOOST_CHECK_MESSAGE( contains( "p.kicad_sch" ), "kicad_sch must be committed" );
535
536 BOOST_CHECK_MESSAGE( !contains( "passwords.txt" ), "passwords.txt must NOT appear in history" );
537 BOOST_CHECK_MESSAGE( !contains( "datasheet.pdf" ), "datasheet.pdf must NOT appear in history" );
538 BOOST_CHECK_MESSAGE( !contains( "notes.md" ), "notes.md must NOT appear in history" );
539 BOOST_CHECK_MESSAGE( !contains( "docs/manual.txt" ), "subdirectory user content must NOT appear in history" );
540}
541
542
547BOOST_AUTO_TEST_CASE( FirstAutosaveSkipsCommitWhenStagedMatchesDisk )
548{
549 LIBGIT2_SCOPE libgit;
550
551 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
552 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
553 backupEnabled = true;
554
555 SCOPED_TEMP_DIR project( wxS( "kicad_qa_first_idle_autosave" ) );
556 const wxString& path = project.Path();
557
558 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pro" ), wxS( "{}\n" ) );
559 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pcb" ),
560 wxS( "(kicad_pcb (version 20240108))\n" ) );
561
562 LOCAL_HISTORY history;
563
564 // Mutating this between calls simulates the user editing the in-memory document.
565 std::string inMemoryContent = "(kicad_pcb (version 20240108))\n";
566
567 auto saver = [&inMemoryContent]( const wxString&, std::vector<HISTORY_FILE_DATA>& aFileData )
568 {
569 HISTORY_FILE_DATA entry;
570 entry.relativePath = wxS( "p.kicad_pcb" );
571 entry.content = inMemoryContent;
572 aFileData.push_back( std::move( entry ) );
573 };
574
575 history.RegisterSaver( &history, saver );
576
577 // Saver output matches disk, must skip (autosave path: empty tagFileType).
578 BOOST_REQUIRE( history.RunRegisteredSaversAndCommit( path, wxS( "Autosave" ), wxEmptyString ) );
579 history.WaitForPendingSave();
580
581 wxString histDir = path + wxFileName::GetPathSeparator() + wxS( ".history" );
582 BOOST_CHECK( wxDirExists( histDir ) );
583
584 wxString head = history.GetHeadHash( path );
585 BOOST_CHECK_MESSAGE( head.IsEmpty(), "no untagged HEAD should exist after an idle first save" );
586
587 // Real edit, must commit.
588 inMemoryContent = "(kicad_pcb (version 20240108) (edited yes))\n";
589
590 BOOST_REQUIRE( history.RunRegisteredSaversAndCommit( path, wxS( "Autosave" ), wxEmptyString ) );
591 history.WaitForPendingSave();
592
593 head = history.GetHeadHash( path );
594 BOOST_CHECK_MESSAGE( !head.IsEmpty(), "in-memory edits diverging from disk must produce a commit" );
595
596 history.UnregisterSaver( &history );
597}
598
599
605BOOST_AUTO_TEST_CASE( FirstManualSaveAlwaysCommitsOnFreshProject )
606{
607 LIBGIT2_SCOPE libgit;
608
609 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
610 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
611 backupEnabled = true;
612
613 SCOPED_TEMP_DIR project( wxS( "kicad_qa_first_manual_save" ) );
614 const wxString& path = project.Path();
615
616 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pro" ), wxS( "{}\n" ) );
617 writeTextFile( path + wxFileName::GetPathSeparator() + wxS( "p.kicad_pcb" ),
618 wxS( "(kicad_pcb (version 20240108))\n" ) );
619
620 LOCAL_HISTORY history;
621
622 std::string inMemoryContent = "(kicad_pcb (version 20240108))\n";
623
624 auto saver = [&inMemoryContent]( const wxString&, std::vector<HISTORY_FILE_DATA>& aFileData )
625 {
626 HISTORY_FILE_DATA entry;
627 entry.relativePath = wxS( "p.kicad_pcb" );
628 entry.content = inMemoryContent;
629 aFileData.push_back( std::move( entry ) );
630 };
631
632 history.RegisterSaver( &history, saver );
633
634 // Manual save (non-empty tagFileType) on fresh project: commit even when staged matches disk.
635 BOOST_REQUIRE( history.RunRegisteredSaversAndCommit( path, wxS( "Manual Save" ), wxS( "pcb" ) ) );
636
637 wxString head = history.GetHeadHash( path );
638 BOOST_CHECK_MESSAGE( !head.IsEmpty(), "manual save on a fresh project must commit even when staged matches disk" );
639
640 history.UnregisterSaver( &history );
641}
642
643
644// A content-identical autosave with a newer mtime (cloud-sync touch) must not be flagged
645// stale, or recovery prompts fire on every open with nothing to restore (issue 24126).
646BOOST_AUTO_TEST_CASE( CloudSyncTouchedAutosaveFalselyFlaggedStale )
647{
648 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
649 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
650 backupEnabled = true;
651
653 SCOPED_BACKUP_LOCATION_OVERRIDE restoreLocation( location );
655
656 SCOPED_TEMP_DIR project( wxS( "kicad_qa_cloudsync_autosave" ) );
657 const wxString& path = project.Path();
658 const wxString sep = wxFileName::GetPathSeparator();
659
660 // FindStaleAutosaveFiles resolves the autosave root through the active project, so it must
661 // be loaded; the .kicad_pro must exist on disk first for LoadProject to succeed.
662 writeTextFile( path + sep + wxS( "p.kicad_pro" ), wxS( "{}\n" ) );
663
665 SCOPED_PROJECT_LOAD loadedProject( mgr, path + sep + wxS( "p.kicad_pro" ) );
666
667 const wxString sourcePath = path + sep + wxS( "p.kicad_pcb" );
668 const wxString autosavePath = path + sep + wxS( "_autosave-p.kicad_pcb" );
669 const wxString boardContent = wxS( "(kicad_pcb (version 20240108))\n" );
670
671 // Byte-identical source and "_autosave-" companion, mirroring a clean save.
672 writeTextFile( sourcePath, boardContent );
673 writeTextFile( autosavePath, boardContent );
674
675 LOCAL_HISTORY history;
676
677 // Cloud-sync touch gives the identical autosave a strictly newer mtime.
678 wxDateTime srcMtime = wxFileName( sourcePath ).GetModificationTime();
679 wxDateTime newerMtime = srcMtime + wxTimeSpan::Seconds( 60 );
680 BOOST_REQUIRE( wxFileName( autosavePath ).SetTimes( &newerMtime, &newerMtime, nullptr ) );
681
682 std::vector<wxString> exts{ wxS( "kicad_pcb" ) };
683 auto stale = history.FindStaleAutosaveFiles( path, exts );
684
685 BOOST_CHECK_MESSAGE( stale.empty(),
686 "Content-identical autosave with newer mtime was flagged stale, "
687 "triggering a spurious recovery prompt (issue 24126)" );
688}
689
690
691// Counterpart to the above: an autosave whose content genuinely differs (a real unsaved edit)
692// must still be flagged stale so recovery continues to fire.
693BOOST_AUTO_TEST_CASE( DivergentAutosaveStillFlaggedStale )
694{
695 bool& backupEnabled = Pgm().GetCommonSettings()->m_Backup.enabled;
696 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
697 backupEnabled = true;
698
700 SCOPED_BACKUP_LOCATION_OVERRIDE restoreLocation( location );
702
703 SCOPED_TEMP_DIR project( wxS( "kicad_qa_divergent_autosave" ) );
704 const wxString& path = project.Path();
705 const wxString sep = wxFileName::GetPathSeparator();
706
707 writeTextFile( path + sep + wxS( "p.kicad_pro" ), wxS( "{}\n" ) );
708
710 SCOPED_PROJECT_LOAD loadedProject( mgr, path + sep + wxS( "p.kicad_pro" ) );
711
712 const wxString sourcePath = path + sep + wxS( "p.kicad_pcb" );
713 const wxString autosavePath = path + sep + wxS( "_autosave-p.kicad_pcb" );
714
715 // Autosave content diverges from the source, a genuine unsaved edit to recover.
716 writeTextFile( sourcePath, wxS( "(kicad_pcb (version 20240108))\n" ) );
717 writeTextFile( autosavePath, wxS( "(kicad_pcb (version 20240108) (edited yes))\n" ) );
718
719 LOCAL_HISTORY history;
720
721 wxDateTime srcMtime = wxFileName( sourcePath ).GetModificationTime();
722 wxDateTime newerMtime = srcMtime + wxTimeSpan::Seconds( 60 );
723 BOOST_REQUIRE( wxFileName( autosavePath ).SetTimes( &newerMtime, &newerMtime, nullptr ) );
724
725 std::vector<wxString> exts{ wxS( "kicad_pcb" ) };
726 auto stale = history.FindStaleAutosaveFiles( path, exts );
727
728 BOOST_CHECK_MESSAGE( stale.size() == 1,
729 "Genuinely divergent autosave must still be flagged stale for recovery" );
730}
731
732
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
void SaveToHistory(const wxString &aProjectPath, std::vector< HISTORY_FILE_DATA > &aFileData)
Serialize board into HISTORY_FILE_DATA for non-blocking history commit.
Definition board.cpp:4035
void SetProject(PROJECT *aProject, bool aReferenceOnly=false)
Link a board to a given project.
Definition board.cpp:211
const wxString & GetFileName() const
Definition board.h:409
void ClearProject()
Definition board.cpp:252
PROJECT * GetProject() const
Definition board.h:650
AUTO_BACKUP m_Backup
Simple local history manager built on libgit2.
bool TagSave(const wxString &aProjectPath, const wxString &aFileType)
Tag a manual save in the local history repository.
bool RunRegisteredSaversAndCommit(const wxString &aProjectPath, const wxString &aTitle, const wxString &aTagFileType=wxEmptyString)
Run all registered savers and, if any staged changes differ from HEAD, create a commit.
std::vector< std::pair< wxString, wxString > > FindStaleAutosaveFiles(const wxString &aProjectPath, const std::vector< wxString > &aExtensions) const
Enumerate autosave files newer than their corresponding source files for the project at aProjectPath,...
wxString GetHeadHash(const wxString &aProjectPath)
Return the current head commit hash.
bool RestoreCommit(const wxString &aProjectPath, const wxString &aHash, wxWindow *aParent=nullptr)
Restore the project files to the state recorded by the given commit hash.
void WaitForPendingSave()
Block until any pending background save completes.
void RegisterSaver(const void *aSaverObject, const std::function< void(const wxString &, std::vector< HISTORY_FILE_DATA > &)> &aSaver)
Register a saver callback invoked during autosave history commits.
bool Init(const wxString &aProjectPath)
Initialize the local history repository for the given project path.
bool CommitFullProjectSnapshot(const wxString &aProjectPath, const wxString &aTitle)
Commit a snapshot of the entire project directory (excluding the .history directory and ignored trans...
void UnregisterSaver(const void *aSaverObject)
Unregister a previously registered saver callback.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:528
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:183
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
BACKUP_LOCATION
@ PROJECT_DIR
Inside the project directory (default)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
BACKUP_LOCATION location
Where backups, history, and autosave files live.
bool enabled
Automatically back up the project when files are saved.
Data produced by a registered saver on the UI thread, consumed by either the background local-history...
std::string content
Serialized content (mutually exclusive with sourcePath)
wxString relativePath
Destination path relative to the project root.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SaveToHistoryWithNullProjectDoesNotCrash)
Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23737.
std::string path
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
VECTOR2I location