Fix test on Windows

This commit is contained in:
Ross Morsali 2026-02-06 15:54:50 +01:00
parent c8f7a8aa51
commit e4e84b967c
4 changed files with 50 additions and 10 deletions

View File

@ -0,0 +1 @@
---

View File

@ -0,0 +1,3 @@
Start testing: Feb 06 15:52 Romance Standard Time
----------------------------------------------------------
End testing: Feb 06 15:52 Romance Standard Time

View File

@ -115,7 +115,7 @@ if (WHISPER_BUILD_EXAMPLES)
set(TEST_TARGET test-stream-pcm)
add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
target_compile_definitions(${TEST_TARGET} PRIVATE
WHISPER_STREAM_PCM_PATH="${CMAKE_BINARY_DIR}/bin/whisper-stream-pcm"
WHISPER_STREAM_PCM_PATH="$<TARGET_FILE:whisper-stream-pcm>"
WHISPER_TEST_MODEL_PATH="${PROJECT_SOURCE_DIR}/models/for-tests-ggml-tiny.en.bin")
add_test(NAME ${TEST_TARGET} COMMAND ${TEST_TARGET})
set_tests_properties(${TEST_TARGET} PROPERTIES LABELS "tiny;stream")

View File

@ -1,10 +1,14 @@
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#if defined(_WIN32)
#include <process.h>
#endif
#ifndef WHISPER_STREAM_PCM_PATH
#error "WHISPER_STREAM_PCM_PATH is not defined"
#endif
@ -29,7 +33,8 @@ int main() {
std::vector<float> zeros(n_samples, 0.0f);
const auto pcm_path = temp_pcm_path();
auto pcm_path = temp_pcm_path();
pcm_path.make_preferred();
std::ofstream out(pcm_path, std::ios::binary);
if (!out.is_open()) {
fprintf(stderr, "failed to open temp PCM path: %s\n", pcm_path.string().c_str());
@ -39,17 +44,48 @@ int main() {
out.write(reinterpret_cast<const char *>(zeros.data()), zeros.size() * sizeof(float));
out.close();
const std::string stream_bin = WHISPER_STREAM_PCM_PATH;
const std::string model_path = WHISPER_TEST_MODEL_PATH;
const std::string stream_bin = std::filesystem::path(WHISPER_STREAM_PCM_PATH).make_preferred().string();
const std::string model_path = std::filesystem::path(WHISPER_TEST_MODEL_PATH).make_preferred().string();
std::vector<std::string> args = {
stream_bin,
"-m", model_path,
"--input", pcm_path.string(),
"--format", "f32",
"--sample-rate", "16000",
"--step", "500",
"--length", "2000",
"-t", "1",
"-ng",
};
int rc = 1;
#if defined(_WIN32)
std::vector<const char *> argv;
argv.reserve(args.size() + 1);
for (const auto & arg : args) {
argv.push_back(arg.c_str());
}
argv.push_back(nullptr);
rc = _spawnv(_P_WAIT, stream_bin.c_str(), argv.data());
if (rc == -1) {
fprintf(stderr, "failed to spawn whisper-stream-pcm: %s\n", std::strerror(errno));
rc = 1;
}
#else
std::string cmd;
cmd.reserve(1024);
cmd += "\"" + stream_bin + "\"";
cmd += " -m \"" + model_path + "\"";
cmd += " --input \"" + pcm_path.string() + "\"";
cmd += " --format f32 --sample-rate 16000 --step 500 --length 2000 -t 1 -ng";
const int rc = std::system(cmd.c_str());
for (const auto & arg : args) {
if (!cmd.empty()) {
cmd += " ";
}
cmd += "\"";
cmd += arg;
cmd += "\"";
}
rc = std::system(cmd.c_str());
#endif
std::error_code ec;
std::filesystem::remove(pcm_path, ec);