ddnet/src/test/fs.cpp
heinrich5991 fb27b83d79 Add function to remove directories
Add tests. Unify behavior of `fs_remove` across operating systems to
only remove files, not directories. Previously on Linux, it would also
delete directories.
2021-05-14 01:00:41 +02:00

51 lines
1.4 KiB
C++

#include "test.h"
#include <gtest/gtest.h>
#include <base/system.h>
TEST(Filesystem, CreateCloseDelete)
{
CTestInfo Info;
IOHANDLE File = io_open(Info.m_aFilename, IOFLAG_WRITE);
ASSERT_TRUE(File);
EXPECT_FALSE(io_close(File));
EXPECT_FALSE(fs_remove(Info.m_aFilename));
}
TEST(Filesystem, CreateDeleteDirectory)
{
CTestInfo Info;
char aFilename[128];
str_format(aFilename, sizeof(aFilename), "%s/test.txt", Info.m_aFilename);
EXPECT_FALSE(fs_makedir(Info.m_aFilename));
IOHANDLE File = io_open(aFilename, IOFLAG_WRITE);
ASSERT_TRUE(File);
EXPECT_FALSE(io_close(File));
// Directory removal fails if there are any files left in the directory.
EXPECT_TRUE(fs_removedir(Info.m_aFilename));
EXPECT_FALSE(fs_remove(aFilename));
EXPECT_FALSE(fs_removedir(Info.m_aFilename));
}
TEST(Filesystem, CantDeleteDirectoryWithRemove)
{
CTestInfo Info;
EXPECT_FALSE(fs_makedir(Info.m_aFilename));
EXPECT_TRUE(fs_remove(Info.m_aFilename)); // Cannot remove directory with file removal function.
EXPECT_FALSE(fs_removedir(Info.m_aFilename));
}
TEST(Filesystem, CantDeleteFileWithRemoveDirectory)
{
CTestInfo Info;
IOHANDLE File = io_open(Info.m_aFilename, IOFLAG_WRITE);
ASSERT_TRUE(File);
EXPECT_FALSE(io_close(File));
EXPECT_TRUE(fs_removedir(Info.m_aFilename)); // Cannot remove file with directory removal function.
EXPECT_FALSE(fs_remove(Info.m_aFilename));
}