Crafter.Build/tests/ImportCycle/main.cpp

79 lines
3.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// Interface units that import each other are refused before their
// configuration builds anything, with an error naming them. No order builds
// them. Before the refusal such a build hung with no compiler running, each
// unit waiting for the units it imports, which is how this test failed then:
// by running into its timeout.
//
// Three ways round: two partitions that import each other, a partition that
// imports its own module, whose primary interface is built after all of its
// partitions, and two modules of one configuration that import each other.
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
// Builds the interfaces of the fixture in `dir` as a library and returns
// Build()'s error. On a copy outside the checkout, made anew each run: a
// build that gets past the refusal has made its directories before it
// fails, and in the checkout the next run would find them there and
// report them against a refusal that works.
std::string BuildError(std::string_view dir, std::span<fs::path> interfaces) {
const fs::path staged = fs::temp_directory_path() / "crafter-build-import-cycle" / dir;
fs::remove_all(staged);
fs::create_directories(staged.parent_path());
fs::copy(fs::current_path() / "tests" / "ImportCycle" / "fixture" / dir, staged, fs::copy_options::recursive);
Configuration cfg;
cfg.path = staged;
cfg.name = std::format("cycle-{}", dir);
cfg.outputName = cfg.name;
cfg.target = HostTarget();
cfg.type = ConfigurationType::LibraryStatic;
std::array<fs::path, 0> implementations = {};
cfg.GetInterfacesAndImplementations(interfaces, implementations);
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const BuildResult built = Build(cfg, depResults, depMutex);
// Refused like a graph with a module built twice: before a directory
// is made for it.
Check(!fs::exists(cfg.BuildDir()) && !fs::exists(cfg.BinDir()), std::format("{}: the refused build left {} or {} behind", dir, cfg.BuildDir().string(), cfg.BinDir().string()));
return built.result;
}
}
int main() {
{
std::array<fs::path, 3> interfaces = { "Ring", "Ring-First", "Ring-Second" };
const std::string error = BuildError("partitions", interfaces);
Check(error.contains("Ring:First -> Ring:Second -> Ring:First"), std::format("partitions that import each other are named in the order they wait in, got: '{}'", error));
// The scanner reads imports the compiler does not see, so the error
// has to say where a round nobody wrote can come from.
Check(error.contains("string literal") && error.contains("#if 0"), std::format("the error says which text the scanner reads as an import, got: '{}'", error));
}
{
std::array<fs::path, 2> interfaces = { "Own", "Own-Part" };
const std::string error = BuildError("own-module", interfaces);
Check(error.contains("Own -> Own:Part -> Own"), std::format("a partition that imports its own module is named with it, got: '{}'", error));
}
{
std::array<fs::path, 2> interfaces = { "East", "West" };
const std::string error = BuildError("siblings", interfaces);
Check(error.contains("East -> West -> East"), std::format("modules that import each other are named, got: '{}'", error));
}
return Failures == 0 ? 0 : 1;
}