test: a program built on a shared library starts

SharedLibraryRuns builds an executable on a LibraryDynamic dependency
and runs it, from its fixture and again from a directory whose name
holds a comma. Before the fix it failed at start with "libgreet.so:
cannot open shared object file".
This commit is contained in:
Jorijn van der Graaf 2026-11-01 19:24:02 +01:00
commit 0cf2f629eb
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 138 additions and 0 deletions

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Greet;
import std;
export std::string Greeting() {
return "hello from a shared library";
}

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Greet;
int main() {
std::println("{}", Greeting());
return 0;
}

View file

@ -0,0 +1,118 @@
// 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;
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
std::string BuildIt(Configuration& cfg) {
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
return Build(cfg, depResults, depMutex).result;
}
// The fixture's program and library again, built from a directory whose
// name holds a comma. clang splits a -Wl, value at every comma, so a run
// path passed as -Wl,-rpath,<dir> came apart there: the link failed, or
// recorded the wrong directory. Why it did not start; empty when it did.
std::string FromCommaDirectory(const fs::path& fixture) {
const fs::path dir = fs::temp_directory_path() / "crafter-build-shared,library";
std::error_code ec;
fs::remove_all(dir, ec);
fs::create_directories(dir / "greet");
fs::copy_file(fixture / "main.cpp", dir / "main.cpp");
fs::copy_file(fixture / "greet" / "Greet.cppm", dir / "greet" / "Greet.cppm");
Configuration greet;
greet.path = dir / "greet";
greet.name = "greet";
greet.outputName = "greet";
greet.target = HostTarget();
greet.type = ConfigurationType::LibraryDynamic;
{
std::array<fs::path, 1> ifaces = { "Greet" };
std::array<fs::path, 0> impls = {};
greet.GetInterfacesAndImplementations(ifaces, impls);
}
Configuration app;
app.path = dir;
app.name = "greet-app";
app.outputName = "greet-app";
app.target = HostTarget();
app.dependencies = { &greet };
{
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 1> impls = { "main" };
app.GetInterfacesAndImplementations(ifaces, impls);
}
std::string why = BuildIt(app);
if (why.empty()) {
const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30));
if (run.exitCode != 0 || !run.output.contains("hello from a shared library")) why = std::format("it did not start (exit {}): {}", run.exitCode, run.output);
}
fs::remove_all(dir, ec);
return why;
}
}
// An executable built on a LibraryDynamic dependency starts: the loader
// finds lib<name>.so, which the executable's link took from the library's
// bin dir, through the run path the link records for that directory.
// Before, the link took the library and recorded nowhere to find it, so the
// program failed at start with "cannot open shared object file".
int main() {
std::error_code ec;
const fs::path fixture = fs::current_path() / "tests" / "SharedLibraryRuns" / "fixture";
fs::remove_all(fixture / "bin", ec);
fs::remove_all(fixture / "build", ec);
fs::remove_all(fixture / "greet" / "bin", ec);
fs::remove_all(fixture / "greet" / "build", ec);
auto greet = std::make_unique<Configuration>();
greet->path = fixture / "greet";
greet->name = "greet";
greet->outputName = "greet";
greet->target = HostTarget();
greet->type = ConfigurationType::LibraryDynamic;
{
std::array<fs::path, 1> ifaces = { "Greet" };
std::array<fs::path, 0> impls = {};
greet->GetInterfacesAndImplementations(ifaces, impls);
}
Configuration app;
app.path = fixture;
app.name = "greet-app";
app.outputName = "greet-app";
app.target = HostTarget();
app.dependencies = { greet.get() };
{
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 1> impls = { "main" };
app.GetInterfacesAndImplementations(ifaces, impls);
}
if (const std::string failed = BuildIt(app); !failed.empty()) {
std::println(std::cerr, "FAIL: build failed: {}", failed);
return 1;
}
const std::string comma = FromCommaDirectory(fixture);
Check(comma.empty(), std::format("built from a directory whose name holds a comma, it starts: {}", comma));
const auto raw = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30));
Check(raw.exitCode == 0 && raw.output.contains("hello from a shared library"), std::format("the program starts (exit {}): {}", raw.exitCode, raw.output));
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}