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,6 +39,7 @@ 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) {
auto do_find = [&](const std::string & folder) {
std::vector<std::string> files; std::vector<std::string> files;
// Note: once we can use C++17 this becomes: // Note: once we can use C++17 this becomes:
// for (const auto & entry : std::filesystem::directory_iterator(folder)) // for (const auto & entry : std::filesystem::directory_iterator(folder))
@ -58,6 +59,12 @@ static std::vector<std::string> find_files(const std::string & folder, const std
closedir(dir); closedir(dir);
} }
return files; return files;
};
auto files = do_find(folder);
if (files.empty()) {
files = do_find("../" + folder);
}
return files;
} }
static std::string read_file(const std::string &path) { static std::string read_file(const std::string &path) {
@ -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);
} }