tests: fix test-chat-template run from build

This commit is contained in:
ochafik 2024-09-28 23:31:23 +01:00
parent ad6719e2a7
commit 22493c8e9e

View file

@ -39,23 +39,30 @@ static void assert_equals(const T & expected, const T & actual) {
} }
static std::vector<std::string> find_files(const std::string & folder, const std::string & ext) { static std::vector<std::string> find_files(const std::string & folder, const std::string & ext) {
std::vector<std::string> files; auto do_find = [&](const std::string & folder) {
// Note: once we can use C++17 this becomes: std::vector<std::string> files;
// for (const auto & entry : std::filesystem::directory_iterator(folder)) // Note: once we can use C++17 this becomes:
// if (entry.path().extension() == ext) files.push_back(entry.path().string()); // for (const auto & entry : std::filesystem::directory_iterator(folder))
DIR* dir = opendir(folder.c_str()); // if (entry.path().extension() == ext) files.push_back(entry.path().string());
if (dir != nullptr) { DIR* dir = opendir(folder.c_str());
struct dirent* entry; if (dir != nullptr) {
while ((entry = readdir(dir)) != nullptr) { struct dirent* entry;
if (entry->d_type == DT_REG) { // If it's a regular file while ((entry = readdir(dir)) != nullptr) {
std::string filename = entry->d_name; if (entry->d_type == DT_REG) { // If it's a regular file
if (filename.length() >= ext.length() && std::string filename = entry->d_name;
filename.compare(filename.length() - ext.length(), ext.length(), ext) == 0) { if (filename.length() >= ext.length() &&
files.push_back(folder + "/" + filename); filename.compare(filename.length() - ext.length(), ext.length(), ext) == 0) {
files.push_back(folder + "/" + filename);
}
} }
} }
closedir(dir);
} }
closedir(dir); return files;
};
auto files = do_find(folder);
if (files.empty()) {
files = do_find("../" + folder);
} }
return files; return files;
} }
@ -110,7 +117,11 @@ static void test_jinja_templates() {
ctx.at("eos_token")); ctx.at("eos_token"));
auto golden_file = get_golden_file(tmpl_file, ctx_file); auto golden_file = get_golden_file(tmpl_file, ctx_file);
if (!std::ifstream(golden_file).is_open()) { std::string expected;
try {
expected = read_file(golden_file);
} catch (const std::runtime_error & e) {
// No golden file.
continue; continue;
} }
found_goldens = true; found_goldens = true;
@ -128,7 +139,6 @@ static void test_jinja_templates() {
} catch (const std::runtime_error & e) { } catch (const std::runtime_error & e) {
actual = "ERROR: " + std::string(e.what()); actual = "ERROR: " + std::string(e.what());
} }
auto expected = read_file(golden_file);
assert_equals(expected, actual); assert_equals(expected, actual);
} }