Skip to content

Commit 1899552

Browse files
committed
cli: align test build directory resolution with embedded presets
1 parent 6974b52 commit 1899552

1 file changed

Lines changed: 20 additions & 177 deletions

File tree

src/commands/TestsCommand.cpp

Lines changed: 20 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -307,115 +307,21 @@ namespace
307307
return "dev-ninja";
308308
}
309309

310-
static fs::path normalize_binary_dir(const fs::path &projectDir, const std::string &binaryDirRaw)
311-
{
312-
std::string s = binaryDirRaw;
313-
314-
const std::string tokenSourceDir = "${sourceDir}";
315-
const std::string proj = projectDir.generic_string();
316-
317-
for (;;)
318-
{
319-
const auto pos = s.find(tokenSourceDir);
320-
if (pos == std::string::npos)
321-
break;
322-
323-
s.replace(pos, tokenSourceDir.size(), proj);
324-
}
325-
326-
while (s.find("//") != std::string::npos)
327-
s.replace(s.find("//"), 2, "/");
328-
329-
fs::path p = fs::path(s);
330-
331-
if (p.is_relative())
332-
p = projectDir / p;
333-
334-
std::error_code ec;
335-
return fs::weakly_canonical(p, ec);
336-
}
337-
338-
static fs::path resolve_build_dir_or_fallback(
310+
static fs::path resolve_build_dir_from_preset(
339311
const fs::path &projectDir,
340312
const std::string &presetName)
341313
{
342-
const fs::path presetsPath = projectDir / "CMakePresets.json";
314+
std::string dirName = "build";
343315

344-
auto fallback_build_dir = [&]() -> fs::path
345-
{
346-
std::error_code ec2;
347-
return fs::weakly_canonical(projectDir / "build", ec2);
348-
};
316+
if (presetName == "dev")
317+
dirName = "build-dev";
318+
else if (presetName == "dev-ninja")
319+
dirName = "build-ninja";
320+
else if (presetName == "release")
321+
dirName = "build-release";
349322

350323
std::error_code ec;
351-
if (!fs::exists(presetsPath, ec))
352-
{
353-
const std::vector<fs::path> candidates = {
354-
projectDir / "out" / presetName,
355-
projectDir / "out",
356-
projectDir / "bld" / presetName,
357-
projectDir / "bld",
358-
projectDir / ("cmake-build-" + presetName),
359-
projectDir / "build",
360-
};
361-
362-
for (const auto &c : candidates)
363-
{
364-
if (fs::exists(c, ec) && fs::is_directory(c, ec))
365-
{
366-
std::error_code ec2;
367-
return fs::weakly_canonical(c, ec2);
368-
}
369-
}
370-
371-
return fallback_build_dir();
372-
}
373-
374-
std::ifstream in(presetsPath);
375-
if (!in)
376-
return fallback_build_dir();
377-
378-
nlohmann::json j;
379-
try
380-
{
381-
in >> j;
382-
}
383-
catch (...)
384-
{
385-
return fallback_build_dir();
386-
}
387-
388-
try
389-
{
390-
if (!j.contains("configurePresets") || !j["configurePresets"].is_array())
391-
return fallback_build_dir();
392-
393-
for (const auto &p : j["configurePresets"])
394-
{
395-
if (!p.is_object())
396-
continue;
397-
398-
const std::string name = p.value("name", "");
399-
if (name != presetName)
400-
continue;
401-
402-
const std::string binaryDir = p.value("binaryDir", "");
403-
if (!binaryDir.empty())
404-
return normalize_binary_dir(projectDir, binaryDir);
405-
406-
const std::string buildDirectory = p.value("buildDirectory", "");
407-
if (!buildDirectory.empty())
408-
return normalize_binary_dir(projectDir, buildDirectory);
409-
410-
return fallback_build_dir();
411-
}
412-
413-
return fallback_build_dir();
414-
}
415-
catch (...)
416-
{
417-
return fallback_build_dir();
418-
}
324+
return fs::weakly_canonical(projectDir / dirName, ec);
419325
}
420326

421327
struct ScopedCwd
@@ -490,57 +396,6 @@ namespace
490396
return vix::cli::process::normalize_exit_code(raw);
491397
}
492398

493-
static std::string project_name_from_dir(const fs::path &projectDir)
494-
{
495-
std::string name = projectDir.filename().string();
496-
if (name.empty())
497-
name = "app";
498-
return name;
499-
}
500-
501-
static int ensure_project_configured_and_built(
502-
const vix::commands::TestsCommand::detail::Options &opt,
503-
const std::string &presetName)
504-
{
505-
std::vector<std::string> forwarded = opt.forwarded;
506-
507-
if (!has_flag(forwarded, "--tests"))
508-
forwarded.push_back("--tests");
509-
510-
if (!value_after_flag(forwarded, "--preset") &&
511-
!value_after_flag(forwarded, "-p"))
512-
{
513-
forwarded.push_back("--preset");
514-
forwarded.push_back(presetName);
515-
}
516-
517-
const std::string projectName = project_name_from_dir(opt.projectDir);
518-
const std::string testFlag = "-D" + projectName + "_BUILD_TESTS=ON";
519-
520-
bool hasDashDash = false;
521-
bool hasTestFlag = false;
522-
523-
for (const auto &arg : forwarded)
524-
{
525-
if (arg == "--")
526-
hasDashDash = true;
527-
528-
if (arg == testFlag)
529-
hasTestFlag = true;
530-
}
531-
532-
if (!hasTestFlag)
533-
{
534-
if (!hasDashDash)
535-
forwarded.push_back("--");
536-
537-
forwarded.push_back(testFlag);
538-
}
539-
540-
ScopedSilenceStdStreams silence;
541-
return vix::commands::CheckCommand::run(forwarded);
542-
}
543-
544399
static bool file_is_executable(const fs::path &p)
545400
{
546401
std::error_code ec;
@@ -710,54 +565,42 @@ namespace
710565
static int run_native_tests(const vix::commands::TestsCommand::detail::Options &opt)
711566
{
712567
const std::string presetName = resolve_preset_name(opt);
713-
const fs::path buildDir = resolve_build_dir_or_fallback(opt.projectDir, presetName);
568+
const fs::path buildDir = resolve_build_dir_from_preset(opt.projectDir, presetName);
714569

715570
std::error_code ec;
716571
if (!fs::exists(buildDir, ec) || !fs::is_directory(buildDir, ec))
717572
{
718-
const int checkCode = ensure_project_configured_and_built(opt, presetName);
719-
if (checkCode != 0)
720-
return checkCode;
573+
return 2;
721574
}
722575

723-
const fs::path refreshedBuildDir = resolve_build_dir_or_fallback(opt.projectDir, presetName);
724-
const auto runner = find_native_test_runner(refreshedBuildDir);
576+
const auto runner = find_native_test_runner(buildDir);
725577

726578
if (!runner)
727579
return 2;
728580

729581
std::vector<std::string> argv;
730582
argv.push_back(runner->string());
731583

732-
return run_in_dir(refreshedBuildDir, argv);
584+
return run_in_dir(buildDir, argv);
733585
}
734586

735587
static int run_ctest(const vix::commands::TestsCommand::detail::Options &opt)
736588
{
737589
const std::string presetName = resolve_preset_name(opt);
738-
const fs::path buildDir = resolve_build_dir_or_fallback(opt.projectDir, presetName);
590+
const fs::path buildDir = resolve_build_dir_from_preset(opt.projectDir, presetName);
739591

740592
std::error_code ec;
741593
if (!fs::exists(buildDir, ec) || !fs::is_directory(buildDir, ec))
742594
{
743-
const int checkCode = ensure_project_configured_and_built(opt, presetName);
744-
if (checkCode != 0)
745-
return checkCode;
746-
}
747-
748-
const fs::path refreshedBuildDir = resolve_build_dir_or_fallback(opt.projectDir, presetName);
749-
750-
if (!ctest_file_exists(refreshedBuildDir))
751-
{
752-
const int checkCode = ensure_project_configured_and_built(opt, presetName);
753-
if (checkCode != 0)
754-
return checkCode;
595+
error("No build directory found for tests.");
596+
hint("Run vix build with tests enabled first.");
597+
return 1;
755598
}
756599

757-
if (!ctest_file_exists(refreshedBuildDir))
600+
if (!ctest_file_exists(buildDir))
758601
{
759602
error("No tests available.");
760-
hint("Tests are disabled for this project in the current CMake configuration.");
603+
hint("Tests were not generated in the existing build directory.");
761604
return 1;
762605
}
763606

@@ -767,7 +610,7 @@ namespace
767610
for (const auto &a : opt.ctestArgs)
768611
argv.push_back(a);
769612

770-
return run_in_dir(refreshedBuildDir, argv);
613+
return run_in_dir(buildDir, argv);
771614
}
772615

773616
static int run_tests_once(const vix::commands::TestsCommand::detail::Options &opt)

0 commit comments

Comments
 (0)