1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Introduce proper error handling for file streams

This commit is contained in:
sfan5 2024-05-08 20:37:10 +02:00
parent c38e0d05bf
commit 39fd9b93c3
23 changed files with 235 additions and 149 deletions

View file

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "modchannels.h"
#include "util/numeric.h"
#include "porting.h"
#include "debug.h"
content_t t_CONTENT_STONE;
content_t t_CONTENT_GRASS;
@ -348,11 +349,14 @@ void TestBase::runTest(const char *name, std::function<void()> &&test)
rawstream << " at " << e.file << ":" << e.line << std::endl;
rawstream << "[FAIL] ";
num_tests_failed++;
} catch (std::exception &e) {
}
#if CATCH_UNHANDLED_EXCEPTIONS == 1
catch (std::exception &e) {
rawstream << "Caught unhandled exception: " << e.what() << std::endl;
rawstream << "[FAIL] ";
num_tests_failed++;
}
#endif
num_tests_run++;
u64 tdiff = porting::getTimeMs() - t1;
rawstream << name << " - " << tdiff << "ms" << std::endl;

View file

@ -80,13 +80,13 @@ void TestBan::testCreate()
BanManager bm(m_testbm);
}
UASSERT(std::ifstream(m_testbm, std::ios::binary).is_open());
UASSERT(fs::IsFile(m_testbm));
// test manual save
{
BanManager bm(m_testbm2);
bm.save();
UASSERT(std::ifstream(m_testbm2, std::ios::binary).is_open());
UASSERT(fs::IsFile(m_testbm2));
}
}

View file

@ -41,6 +41,7 @@ public:
void testRemoveRelativePathComponent();
void testSafeWriteToFile();
void testCopyFileContents();
void testNonExist();
};
static TestFileSys g_test_instance;
@ -54,6 +55,7 @@ void TestFileSys::runTests(IGameDef *gamedef)
TEST(testRemoveRelativePathComponent);
TEST(testSafeWriteToFile);
TEST(testCopyFileContents);
TEST(testNonExist);
}
////////////////////////////////////////////////////////////////////////////////
@ -311,3 +313,27 @@ void TestFileSys::testCopyFileContents()
UASSERT(fs::ReadFile(file2, contents_actual));
UASSERTEQ(auto, contents_actual, test_data);
}
void TestFileSys::testNonExist()
{
const auto path = getTestTempFile();
fs::DeleteSingleFileOrEmptyDirectory(path);
UASSERT(!fs::IsFile(path));
UASSERT(!fs::IsDir(path));
UASSERT(!fs::IsExecutable(path));
std::string s;
UASSERT(!fs::ReadFile(path, s));
UASSERT(s.empty());
UASSERT(!fs::Rename(path, getTestTempFile()));
std::filebuf buf;
// with logging enabled to test that code path
UASSERT(!fs::OpenStream(buf, path.c_str(), std::ios::in, false, true));
UASSERT(!buf.is_open());
auto ifs = open_ifstream(path.c_str(), false);
UASSERT(!ifs.good());
}