Files
Descent3/cfile/tests/cfile_tests.cpp
Jason Yundt 9d08314986 Consolidate case-sensitive filesystem functions
Descent 3 is case-insensitive. It doesn’t matter if a file is named
“ppics.hog” or “PPPICS.HOG”. Descent 3 will load the file regardless. In
order to accomplish this, Descent 3 has to have special code for
case-sensitive filesystems. That code must take a fake case-insensitive
path and turn it into a real case-sensitive path.

Before this change, there was multiple chunks of code that helped turn
fake case-insensitive paths into real case-sensitive paths. There was
cf_FindRealFileNameCaseInsenstive(), mve_FindMovieFileRealName() and a
chunk of code in open_file_in_directory() that only exists if __LINUX__
is defined. This removes each of those pieces of code and replaces them
with a new cf_LocatePath() function.

Using the new cf_LocatePath() function has two main advantages over the
old way of doing things. First, having a single function is simpler than
having three different pieces of code. Second, the new cf_LocatePath()
function will make it easier to create a future commit. That future
commit will make Descent 3 look for files in more than just the -setdir
directory. Having a single function that’s responsible for determining
the true path of a file will make it much easier to create that future
commit.
2024-09-29 12:51:15 -04:00

117 lines
3.9 KiB
C++

/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <filesystem>
#include <vector>
#include <gtest/gtest.h>
#include "cfile.h"
void set_base_directory_to_cwd() {
cf_SetBaseDirectory(std::filesystem::current_path());
}
TEST(D3, CFileIO) {
set_base_directory_to_cwd();
int lib_handle = cf_OpenLibrary("TestDir/test.hog");
CFILE *file_handle = cfopen("lowercase.txt", "rb");
char buf[5];
cf_ReadString(buf, 5, file_handle);
EXPECT_STREQ(buf, "TEST");
cf_Rewind(file_handle);
EXPECT_EQ(cf_ReadByte(file_handle), 84);
cf_Rewind(file_handle);
EXPECT_EQ(cf_ReadShort(file_handle), 17748);
cf_Rewind(file_handle);
EXPECT_EQ(cf_ReadInt(file_handle), 1414743380);
cf_Rewind(file_handle);
cf_CloseLibrary(lib_handle);
}
TEST(D3, CFileLibrary) {
set_base_directory_to_cwd();
// First pass - without search path in "TestDir" (i.e. not search actual files in directory)
// Second pass - with search path (files in directory goes first)
for (int i = 0; i < 2; i++) {
if (i != 0) {
EXPECT_EQ(cf_SetSearchPath("TestDir"), true);
}
int lib_handle = cf_OpenLibrary("TestDir/test.hog");
EXPECT_NE(lib_handle, 0);
CFILE *file_handle = cf_OpenFileInLibrary("lowercase.txt", lib_handle);
EXPECT_NE(file_handle, nullptr);
file_handle = cfopen("lowercase.txt", "rb");
EXPECT_NE(file_handle, nullptr);
// Length in hog is 4, in directory is 0
EXPECT_EQ(cfilelength(file_handle), i == 0 ? 4 : 0);
EXPECT_EQ(cf_CalculateFileCRC(file_handle), i == 0 ? 4008350648 : 0);
cf_ClearAllSearchPaths();
cf_CloseLibrary(lib_handle);
}
// Try to search on non-initialized paths and files
CFILE *file_handle = cfopen("lowercase.txt", "rb");
EXPECT_EQ(file_handle, nullptr);
}
TEST(D3, CFileLocatePath) {
const std::vector<std::filesystem::path> test_paths = {
std::filesystem::path("TestDir") / "CamelCase.txt",
std::filesystem::path("TestDir") / "lowercase.txt",
std::filesystem::path("TestDir") / "UPPERCASE.TXT",
};
std::filesystem::path filename_new = cf_LocatePath(std::filesystem::path("no-exist-dir") / "no-exist-file.txt");
EXPECT_TRUE(filename_new.empty());
filename_new = cf_LocatePath("no-exist-file.txt");
EXPECT_TRUE(filename_new.empty());
auto cwd = std::filesystem::current_path();
for (auto const &item : test_paths) {
auto directory = cwd / item.parent_path();
cf_SetBaseDirectory(directory);
std::filesystem::path file = item.filename();
std::string file_lc = item.filename().u8string();
std::transform(file_lc.begin(), file_lc.end(), file_lc.begin(), ::tolower);
std::string file_uc = item.filename().u8string();
std::transform(file_uc.begin(), file_uc.end(), file_uc.begin(), ::toupper);
EXPECT_FALSE(cf_LocatePath(file_lc).empty());
EXPECT_FALSE(cf_LocatePath(file_uc).empty());
// Now try case-insensitive path with non-existing path.
// Expected not found on case-sensitive fs.
file_lc = item.u8string();
std::transform(file_lc.begin(), file_lc.end(), file_lc.begin(), ::tolower);
file_uc = item.u8string();
std::transform(file_uc.begin(), file_uc.end(), file_uc.begin(), ::toupper);
EXPECT_TRUE(cf_LocatePath(file_lc).empty());
EXPECT_TRUE(cf_LocatePath(file_uc).empty());
}
}