124 lines
5.7 KiB
C++
124 lines
5.7 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <sys/file.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// `crafter-build install` installs the Flatpak a project was built as for
|
||
|
|
// this user: from the project's repo, through a user remote named after the
|
||
|
|
// app-id, updating what an earlier install put there, and waiting while
|
||
|
|
// another install holds that remote. The test installs into a private user
|
||
|
|
// installation under the fixture's build/ by way of FLATPAK_USER_DIR, which
|
||
|
|
// the host helper passes on from the sandbox too, so the user's own
|
||
|
|
// installation is never touched; the runtime comes from the system
|
||
|
|
// installation, so a machine without Platform 26.08 there skips, as one
|
||
|
|
// without the host's flatpak does. The app is not run: that would make
|
||
|
|
// ~/.var/app/<app-id> in the real home.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-install";
|
||
|
|
std::error_code ec;
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
fs::create_directories(scratch);
|
||
|
|
const fs::path info = scratch / "flatpak-info";
|
||
|
|
std::ofstream(info) << "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
|
||
|
|
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
|
||
|
|
|
||
|
|
const fs::path fixture = fs::current_path() / "tests" / "FlatpakInstall" / "fixture";
|
||
|
|
if (const std::string why = HostFlatpakUnavailable(fixture); !why.empty()) {
|
||
|
|
std::println(std::cerr, "SKIP: {}", why);
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
|
||
|
|
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
if (!GetToolchain().libcxxDir.empty()) {
|
||
|
|
std::println(std::cerr, "SKIP: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak");
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
if (RunHostFlatpak("info --system --show-ref org.freedesktop.Platform//26.08") != 0) {
|
||
|
|
std::println(std::cerr, "SKIP: the system installation has no org.freedesktop.Platform//26.08 for a private user installation to use");
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
const fs::path installation = fixture / "build" / "user-installation";
|
||
|
|
fs::create_directories(installation);
|
||
|
|
setenv("FLATPAK_USER_DIR", installation.string().c_str(), 1);
|
||
|
|
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "install-app";
|
||
|
|
app.outputName = "installed";
|
||
|
|
app.target = HostTarget();
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
app.flatpak.appId = "net.example.Installed";
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> ifaces = {};
|
||
|
|
std::array<fs::path, 1> impls = { "main" };
|
||
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
{
|
||
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||
|
|
std::mutex depMutex;
|
||
|
|
if (const BuildResult r = Build(app, depResults, depMutex); !r.result.empty()) {
|
||
|
|
std::println(std::cerr, "FAIL: build failed: {}", r.result);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const std::string installed = InstallFlatpakApp(app);
|
||
|
|
Check(installed.empty(), std::format("the app is installed: {}", installed));
|
||
|
|
Check(RunHostFlatpak("info --user --show-ref net.example.Installed") == 0, "the user installation has the app");
|
||
|
|
// On disk as well, where the private installation keeps it: a check
|
||
|
|
// through flatpak alone would pass with the app in the real one.
|
||
|
|
Check(fs::exists(installation / "app" / "net.example.Installed"), "and it is the private installation");
|
||
|
|
const std::string again = InstallFlatpakApp(app);
|
||
|
|
Check(again.empty(), std::format("installing again updates it: {}", again));
|
||
|
|
|
||
|
|
// The remote is the installation's, every checkout's that builds this
|
||
|
|
// app-id, so an install waits while another holds it, rather than install
|
||
|
|
// from the repo that one pointed it at, and installs once it is free. The
|
||
|
|
// lock follows the installation, the private one here, and not
|
||
|
|
// crafter-build's cache: another crafter-build runs with an XDG_CACHE_HOME
|
||
|
|
// of its own, natively or in the Flatpak, so this one's is moved into the
|
||
|
|
// scratch directory first, and the install still waits.
|
||
|
|
{
|
||
|
|
setenv("XDG_CACHE_HOME", (scratch / "cache").string().c_str(), 1);
|
||
|
|
const fs::path remotes = installation / "crafter.build" / "flatpak-remotes";
|
||
|
|
fs::create_directories(remotes, ec);
|
||
|
|
const std::int32_t held = open((remotes / "crafter-build-net.example.Installed.lock").c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644);
|
||
|
|
Check(held >= 0 && flock(held, LOCK_EX) == 0, "the test holds the app's remote");
|
||
|
|
std::future<std::string> third = std::async(std::launch::async, [&app] { return InstallFlatpakApp(app); });
|
||
|
|
Check(third.wait_for(std::chrono::seconds(5)) == std::future_status::timeout, "an install waits while another holds the app's remote");
|
||
|
|
if (held >= 0) close(held);
|
||
|
|
const std::string waited = third.get();
|
||
|
|
Check(waited.empty(), std::format("and installs once it is free: {}", waited));
|
||
|
|
}
|
||
|
|
|
||
|
|
RunHostFlatpak("uninstall --user --noninteractive net.example.Installed");
|
||
|
|
fs::remove_all(installation, ec);
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|