From 0f9f986acec5b7e2a2fd1275fbe07b302bb7620f Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Fri, 2 Jan 2026 11:33:42 +0100 Subject: [PATCH] test: add --output --- tests/test-chat-jinja.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/test-chat-jinja.cpp b/tests/test-chat-jinja.cpp index 86fe8f1f15..b22c8a56d5 100644 --- a/tests/test-chat-jinja.cpp +++ b/tests/test-chat-jinja.cpp @@ -17,13 +17,15 @@ using json = nlohmann::json; void run_multiple(std::string dir_path, bool stop_on_first_failure, json input); -void run_single(std::string contents, json input); +void run_single(std::string contents, json input, const std::string & output_path = ""); std::string HELP = R"( Usage: test-chat-jinja [OPTIONS] PATH_TO_TEMPLATE Options: + -h, --help Show this help message and exit. --json Path to the JSON input file. --stop-on-first-fail Stop testing on the first failure (default: false). + --output Path to output results (only for single template runs). If PATH_TO_TEMPLATE is a file, runs that single template. If PATH_TO_TEMPLATE is a directory, runs all .jinja files in that directory. )"; @@ -65,6 +67,7 @@ int main(int argc, char ** argv) { std::string tmpl_path; std::string json_path; + std::string output_path; bool stop_on_first_fail = false; for (size_t i = 1; i < args.size(); i++) { @@ -76,6 +79,9 @@ int main(int argc, char ** argv) { i++; } else if (args[i] == "--stop-on-first-fail") { stop_on_first_fail = true; + } else if (args[i] == "--output" && i + 1 < args.size()) { + output_path = args[i + 1]; + i++; } else if (tmpl_path.empty()) { tmpl_path = args[i]; } else { @@ -114,7 +120,7 @@ int main(int argc, char ** argv) { std::string contents = std::string( std::istreambuf_iterator(infile), std::istreambuf_iterator()); - run_single(contents, input_json); + run_single(contents, input_json, output_path); } else { std::cerr << "Error: PATH_TO_TEMPLATE is not a valid file or directory: " << tmpl_path << "\n"; return 1; @@ -158,7 +164,7 @@ void run_multiple(std::string dir_path, bool stop_on_first_fail, json input) { } -void run_single(std::string contents, json input) { +void run_single(std::string contents, json input, const std::string & output_path) { jinja::enable_debug(true); // lexing @@ -185,4 +191,15 @@ void run_single(std::string contents, json input) { for (const auto & part : parts->as_string().parts) { std::cout << (part.is_input ? "DATA" : "TMPL") << ": " << part.val << "\n"; } + + if (!output_path.empty()) { + std::ofstream outfile(output_path); + if (!outfile) { + throw std::runtime_error("Could not open output file: " + output_path); + } + for (const auto & part : parts->as_string().parts) { + outfile << part.val; + } + std::cout << "\n=== OUTPUT WRITTEN TO " << output_path << " ===\n"; + } }