MCPcopy Create free account
hub / github.com/catchorg/Catch2 / setUpGuardFile

Function setUpGuardFile

extras/catch_amalgamated.cpp:1313–1355  ·  view source on GitHub ↗

Creates empty file at path. The path must be writable, we do not try to create directories in path because that's hard in C++14.

Source from the content-addressed store, hash-verified

1311 // Creates empty file at path. The path must be writable, we do not
1312 // try to create directories in path because that's hard in C++14.
1313 void setUpGuardFile( std::string const& guardFilePath ) {
1314 if ( !guardFilePath.empty() ) {
1315#if defined( _MSC_VER )
1316 std::FILE* file = nullptr;
1317 if ( fopen_s( &file, guardFilePath.c_str(), "w" ) ) {
1318 char msgBuffer[100];
1319 const auto err = errno;
1320 std::string errMsg;
1321 if ( !strerror_s( msgBuffer, err ) ) {
1322 errMsg = msgBuffer;
1323 } else {
1324 errMsg = "Could not translate errno to a string";
1325 }
1326
1327#else
1328 std::FILE* file = std::fopen( guardFilePath.c_str(), "w" );
1329 if ( !file ) {
1330 const auto err = errno;
1331 const char* errMsg = std::strerror( err );
1332#endif
1333
1334 CATCH_RUNTIME_ERROR( "Could not open the exit guard file '"
1335 << guardFilePath << "' because '"
1336 << errMsg << "' (" << err << ')' );
1337 }
1338 const int ret = std::fclose( file );
1339 CATCH_ENFORCE(
1340 ret == 0,
1341 "Error when closing the exit guard file: " << ret );
1342 }
1343 }
1344
1345 // Removes file at path. Assuming we created it in setUpGuardFile.
1346 void tearDownGuardFile( std::string const& guardFilePath ) {
1347 if ( !guardFilePath.empty() ) {
1348 const int ret = std::remove( guardFilePath.c_str() );
1349 CATCH_ENFORCE(
1350 ret == 0,
1351 "Error when removing the exit guard file: " << ret );
1352 }
1353 }
1354
1355 } // anon namespace
1356
1357 Session::Session() {
1358 static bool alreadyInstantiated = false;

Callers 1

runInternalMethod · 0.70

Calls 1

emptyMethod · 0.45

Tested by

no test coverage detected