test-jinja: use common subproc

This commit is contained in:
Xuan Son Nguyen
2026-07-25 02:04:56 +02:00
parent c063f10698
commit e6d477bcae
3 changed files with 27 additions and 18 deletions
+12 -4
View File
@@ -1,5 +1,3 @@
#include "common.h"
#include "subproc.h"
bool common_subproc::is_supported() {
@@ -75,6 +73,13 @@ FILE * common_subproc::stderr_file() {
return is_created ? subprocess_stderr(&proc) : nullptr;
}
void common_subproc::close_stdin() {
if (is_created && proc.stdin_file) {
fclose(proc.stdin_file);
proc.stdin_file = nullptr;
}
}
void common_subproc::terminate() {
if (has_handle()) {
subprocess_terminate(&proc);
@@ -100,8 +105,8 @@ bool common_subproc::create(
int,
const std::vector<std::string> &,
const char *) {
GGML_UNUSED(proc);
GGML_UNUSED(is_created);
(void)(proc);
(void)(is_created);
return false;
}
@@ -125,6 +130,9 @@ FILE * common_subproc::stderr_file() {
return nullptr;
}
void common_subproc::close_stdin() {
}
void common_subproc::terminate() {
}
+4
View File
@@ -41,6 +41,10 @@ struct common_subproc {
FILE * stdout_file();
FILE * stderr_file();
// close stdin and detach it from the process, so a later join()/destroy() won't double-close it;
// use this after writing all input to signal EOF to the child while it's still running
void close_stdin();
void terminate();
// wait for the process to exit, release the underlying handle and return its exit code
+11 -14
View File
@@ -4,7 +4,7 @@
#include <cstdlib>
#include <nlohmann/json.hpp>
#include <sheredom/subprocess.h>
#include "subproc.h"
#include "jinja/runtime.h"
#include "jinja/parser.h"
@@ -2135,21 +2135,20 @@ static void test_template_py(testing & t, const std::string & name, const std::s
const char * python_executable = "python3";
#endif
const char * command_line[] = {python_executable, "-c", py_script.c_str(), NULL};
std::vector<std::string> args = {python_executable, "-c", py_script, };
struct subprocess_s subprocess;
common_subproc subprocess;
int options = subprocess_option_combined_stdout_stderr
| subprocess_option_no_window
| subprocess_option_inherit_environment
| subprocess_option_search_user_path;
int result = subprocess_create(command_line, options, &subprocess);
if (result != 0) {
t.log("Failed to create subprocess, error code: " + std::to_string(result));
if (!subprocess.create(args, options)) {
t.log("Failed to create subprocess");
t.assert_true("subprocess creation", false);
return;
}
FILE * p_stdin = subprocess_stdin(&subprocess);
FILE * p_stdin = subprocess.stdin_file();
// Write input
std::string input = merged.dump();
@@ -2157,24 +2156,22 @@ static void test_template_py(testing & t, const std::string & name, const std::s
if (written != input.size()) {
t.log("Failed to write complete input to subprocess stdin");
t.assert_true("subprocess stdin write", false);
subprocess_destroy(&subprocess);
subprocess.close_stdin();
subprocess.join();
return;
}
fflush(p_stdin);
fclose(p_stdin); // Close stdin to signal EOF to the Python process
subprocess.stdin_file = nullptr;
subprocess.close_stdin(); // Close stdin to signal EOF to the Python process
// Read output
std::string output;
char buffer[1024];
FILE * p_stdout = subprocess_stdout(&subprocess);
FILE * p_stdout = subprocess.stdout_file();
while (fgets(buffer, sizeof(buffer), p_stdout)) {
output += buffer;
}
int process_return;
subprocess_join(&subprocess, &process_return);
subprocess_destroy(&subprocess);
int process_return = subprocess.join();
if (process_return != 0) {
t.log("Python script failed with exit code: " + std::to_string(process_return));