Crafter.Build/tests/ModuleInterface/main.cpp
Jorijn van der Graaf 7ff426b2f0
Some checks failed
CI / build-test-release (push) Failing after 5m36s
SPDX license update
Canonical LGPL-3.0-only text, GPL-3.0 companion, SPDX headers on all
first-party sources, MIT for examples/. Vendored lib/ untouched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-22 18:19:17 +02:00

68 lines
2.5 KiB
C++

//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;
// Build an executable that imports a project-local .cppm module, then run
// it and check the produced output. Exercises the within-project module
// dependency path (main.cpp imports Greeter, resolved through cfg.interfaces).
int main() {
Configuration cfg;
cfg.path = fs::current_path() / "tests" / "ModuleInterface" / "fixture";
cfg.name = "greeter-app";
cfg.outputName = "greeter-app";
cfg.target = HostTarget();
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 1> ifaces = { "Greeter" };
std::array<fs::path, 1> impls = { "main" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
// Implementation should have picked up the import Greeter; through the
// interface list resolution in GetInterfacesAndImplementations.
if (cfg.implementations.size() != 1) {
std::println(std::cerr, "expected 1 implementation, got {}", cfg.implementations.size());
return 1;
}
if (cfg.implementations[0].moduleDependencies.size() != 1) {
std::println(std::cerr,
"expected main.cpp to depend on 1 module, got {}",
cfg.implementations[0].moduleDependencies.size());
return 1;
}
if (cfg.implementations[0].moduleDependencies[0]->name != "Greeter") {
std::println(std::cerr,
"expected dep 'Greeter', got '{}'",
cfg.implementations[0].moduleDependencies[0]->name);
return 1;
}
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
BuildResult r = Build(cfg, depResults, depMutex);
if (!r.result.empty()) {
std::println(std::cerr, "build failed: {}", r.result);
return 1;
}
fs::path bin = cfg.BinDir() / "greeter-app";
if (!fs::exists(bin)) {
std::println(std::cerr, "binary not produced at {}", bin.string());
return 1;
}
auto run = RunCommandWithTimeout(bin.string(), std::chrono::seconds(10));
if (run.exitCode != 0 || run.timedOut || run.crashed) {
std::println(std::cerr, "binary did not exit cleanly: exit={} output={}",
run.exitCode, run.output);
return 1;
}
if (run.output != "ok-from-module") {
std::println(std::cerr, "unexpected output: '{}'", run.output);
return 1;
}
return 0;
}