21#include <boost/test/unit_test.hpp>
32#include <wx/filename.h>
47void writeFileContents(
const wxString& aPath,
const std::string& aContent )
49 wxFFile fp( aPath, wxT(
"wb" ) );
52 if( !aContent.empty() )
53 BOOST_REQUIRE( fp.Write( aContent.data(), aContent.size() ) == aContent.size() );
61unsigned countSiblingTemps(
const wxString& aTargetPath )
63 wxFileName fn( aTargetPath );
64 wxString dir = fn.GetPath();
65 wxString pattern = fn.GetFullName() + wxT(
".kicad-save-*" );
66 wxArrayString matches;
67 wxDir::GetAllFiles( dir, &matches, pattern, wxDIR_FILES );
68 return matches.GetCount();
79 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
83 f.
Print( 0,
"(kicad_test (content \"hello\"))\n" );
91 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
101 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
102 const std::string original =
"(original \"do not lose me\")\n";
103 writeFileContents( target, original );
108 f.
Print( 0,
"(partial " );
112 throw std::runtime_error(
"simulated serializer failure" );
114 std::runtime_error );
118 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
127 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
128 const std::string original =
"(original \"keep me\")\n";
129 writeFileContents( target, original );
133 f.
Print( 0,
"(uncommitted content)\n" );
137 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
146 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
147 const std::string original =
"original streaming content\n";
148 writeFileContents( target, original );
153 f.
Print( 0,
"partial write before the crash " );
154 throw std::runtime_error(
"simulated exporter failure" );
156 std::runtime_error );
160 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
168 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
169 const std::string original =
"original export content\n";
170 writeFileContents( target, original );
174 f.
Print( 0,
"uncommitted streaming content\n" );
178 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
185 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
186 const std::string payload =
"{\"setting\":42}\n";
194 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
203 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
204 const std::string original =
"unchanged\n";
205 writeFileContents( target, original );
210 wxString bogus = target + wxT(
"/subdir/cannot-create" );
225 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
227 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
234 f.
Print( 0,
"(doomed save)\n" );
242 BOOST_REQUIRE_MESSAGE( threw,
"Finish() must throw IO_ERROR when atomic commit fails" );
244 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
251 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
253 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
260 f.
Print( 0,
"doomed streaming save\n" );
268 BOOST_REQUIRE_MESSAGE( threw,
"Finish() must throw IO_ERROR when atomic commit fails" );
270 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
279 const wxString sep = wxFileName::GetPathSeparator();
283 const wxString target = tempDir.
PathStr() + sep + wxT(
"stream-ok" );
285 f.
Print( 0,
"saved once\n" );
293 const wxString target = tempDir.
PathStr() + sep + wxT(
"stream-fail" );
294 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
297 f.
Print( 0,
"doomed streaming save\n" );
305 const wxString target = tempDir.
PathStr() + sep + wxT(
"prettified-ok" );
307 f.
Print( 0,
"(saved once)\n" );
315 const wxString target = tempDir.
PathStr() + sep + wxT(
"prettified-fail" );
316 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
319 f.
Print( 0,
"(doomed pretty save)\n" );
332 model.SetEmptyLayout();
335 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
336 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
340 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
350 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
352 BOOST_REQUIRE( wxFileName::Mkdir( target, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
358 f.
Print( 0,
"(implicit doomed)\n" );
362 BOOST_REQUIRE_EQUAL( logCounter.
GetCount(), 0u );
364 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
372 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
377 f.
Print( 0,
"(new_file content)\n" );
384 BOOST_REQUIRE_EQUAL( countSiblingTemps( target ), 0u );
388#if !defined( _WIN32 )
396 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
397 const std::string original =
"original\n";
398 writeFileContents( target, original );
400 BOOST_REQUIRE_EQUAL( chmod( target.fn_str(), 0400 ), 0 );
402 const std::string payload =
"replacement\n";
409 BOOST_REQUIRE_EQUAL( stat( target.fn_str(), &st ), 0 );
410 BOOST_REQUIRE_EQUAL( st.st_mode & 0777, 0400 );
421 const wxString referent = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"referent" );
422 const wxString linkPath = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"link" );
423 const std::string original =
"referent original\n";
424 writeFileContents( referent, original );
426 BOOST_REQUIRE_EQUAL( symlink( referent.fn_str(), linkPath.fn_str() ), 0 );
428 const std::string payload =
"replacement via symlink\n";
435 BOOST_REQUIRE_EQUAL( lstat( linkPath.fn_str(), &st ), 0 );
450 const wxString target = tempDir.
PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
451 const std::string original =
"original\n";
452 writeFileContents( target, original );
453 BOOST_REQUIRE_EQUAL( chmod( target.fn_str(), 0400 ), 0 );
458 wxString bogus = target + wxT(
"/cannot-create" );
463 BOOST_REQUIRE_EQUAL( stat( target.fn_str(), &st ), 0 );
464 BOOST_REQUIRE_EQUAL( st.st_mode & 0777, 0400 );
475 const wxString target = tempDir.PathStr() + wxFileName::GetPathSeparator() + wxT(
"target" );
476 const std::string original =
"original\n";
477 writeFileContents( target, original );
480 FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN ) );
482 const std::string payload =
"replacement\n";
488 DWORD attrs = GetFileAttributesW( target.wc_str() );
496 SetFileAttributesW( target.wc_str(), FILE_ATTRIBUTE_NORMAL );
Handle the graphic items list to draw/plot the frame and title block.
static DS_DATA_MODEL & GetTheInstance()
Return the instance of DS_DATA_MODEL used in the application.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
A scoped application of a wxLog target that counts error-level messages.
unsigned GetCount() const
wxString PathStr() const
Get the path to the temporary directory as a wxString.
std::string LoadStringData(const wxString &aPath)
Load the contents of a file into a string.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(PrettifiedFormatter_HappyPath)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
#define CHECK_WX_ASSERT(STATEMENT)
A test macro to check a wxASSERT is thrown.