Skip to content

Commit 4c11e23

Browse files
committed
cli: auto-configure and auto-build in vix tests when CTest is missing
- Automatically run `vix check --tests` if build directory does not exist - Auto-build when CTestTestfile.cmake is missing - Re-resolve preset build directory after auto-build - Keeps same preset to avoid mismatched build dirs - Makes `vix tests` work on fresh clone without manual build step Improves developer experience and makes tests command self-healing.
1 parent 94e3335 commit 4c11e23

1 file changed

Lines changed: 58 additions & 9 deletions

File tree

src/commands/TestsCommand.cpp

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <sstream>
3232
#include <optional>
3333
#include <cstdlib>
34+
#include <algorithm>
3435

3536
#include <nlohmann/json.hpp>
3637

@@ -351,6 +352,38 @@ namespace
351352
return vix::cli::process::normalize_exit_code(raw);
352353
}
353354

355+
static int ensure_project_configured_and_built(
356+
const vix::commands::TestsCommand::detail::Options &opt,
357+
const std::string &presetName)
358+
{
359+
info("CTest is not ready. Auto-configure/auto-build via `vix check`.");
360+
361+
std::vector<std::string> forwarded = opt.forwarded;
362+
363+
auto has_flag = [&](const std::string &flag) -> bool
364+
{
365+
for (const auto &a : forwarded)
366+
{
367+
if (a == flag)
368+
return true;
369+
}
370+
return false;
371+
};
372+
373+
// Ensure tests are enabled
374+
if (!has_flag("--tests"))
375+
forwarded.push_back("--tests");
376+
377+
// Ensure the same preset is used for check/build
378+
if (!value_after_flag(forwarded, "--preset") && !value_after_flag(forwarded, "-p"))
379+
{
380+
forwarded.push_back("--preset");
381+
forwarded.push_back(presetName);
382+
}
383+
384+
return vix::commands::CheckCommand::run(forwarded);
385+
}
386+
354387
static int run_ctest(const vix::commands::TestsCommand::detail::Options &opt)
355388
{
356389
const std::string presetName = resolve_preset_name(opt);
@@ -359,20 +392,36 @@ namespace
359392
std::error_code ec;
360393
if (!fs::exists(buildDir, ec) || !fs::is_directory(buildDir, ec))
361394
{
362-
error("Build directory does not exist.");
363-
hint("Run: vix check (or vix build) first to configure/build the project.");
364-
step(buildDir.string());
365-
return 1;
395+
const int checkCode = ensure_project_configured_and_built(opt, presetName);
396+
if (checkCode != 0)
397+
return checkCode;
398+
399+
const fs::path newBuildDir = resolve_build_dir_or_fallback(opt.projectDir, presetName);
400+
401+
std::error_code ec2;
402+
if (!fs::exists(newBuildDir, ec2) || !fs::is_directory(newBuildDir, ec2))
403+
{
404+
error("Build directory still does not exist after auto-build.");
405+
step(newBuildDir.string());
406+
return 1;
407+
}
366408
}
367409

368410
const fs::path ctestFile = buildDir / "CTestTestfile.cmake";
369411
if (!fs::exists(ctestFile, ec) || ec)
370412
{
371-
error("No CTest configuration found in build directory.");
372-
hint("This project is not configured/built yet (or tests are disabled).");
373-
hint("Run first: vix check (or vix build), then re-run: vix tests");
374-
step(std::string("Expected file: ") + ctestFile.string());
375-
return 1;
413+
const int checkCode = ensure_project_configured_and_built(opt, presetName);
414+
if (checkCode != 0)
415+
return checkCode;
416+
417+
std::error_code ec2;
418+
if (!fs::exists(ctestFile, ec2) || ec2)
419+
{
420+
error("CTest is still not available after auto-build.");
421+
hint("Tests may be disabled in the project (enable_testing/add_test missing).");
422+
step(std::string("Expected file: ") + ctestFile.string());
423+
return 1;
424+
}
376425
}
377426

378427
info("Running tests (CTest).");

0 commit comments

Comments
 (0)