// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® #include #include #include #include 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 ReadFile(const fs::path& path) { std::ifstream in(path, std::ios::binary); return std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); } // Every file under `root` with its size and mtime. std::map> Snapshot(const fs::path& root) { std::map> files; for (const fs::directory_entry& entry : fs::recursive_directory_iterator(root)) { if (entry.is_regular_file()) files.emplace(entry.path(), std::pair{entry.file_size(), entry.last_write_time()}); } return files; } } // `crafter-build bundle` turns the Flatpak a project was built as into one // file that `flatpak install --user` takes on another machine: the host's // flatpak build-finish finishes the files/ the build laid out, linked into // a build directory of the app's, flatpak build-export puts that into the // project's repo, and the bundle is written from there, next to the bin // dir under the same variant name. build-finish exports the desktop entry // and the icon files/share holds, and the build's own files/ is left as it // was, linked rather than copied. Bundling again with nothing changed works // as the first time did, and a bundle waits while another export holds the // repo, which every variant of the app shares. It needs the host's flatpak // and skips without it, which is the case in flatpak-builder's build // sandbox, where CI runs the suite. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-bundle"; 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" / "FlatpakBundle" / "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; } fs::remove_all(fixture / "bin", ec); fs::remove_all(fixture / "build", ec); Configuration app; app.path = fixture; app.name = "bundle-app"; app.outputName = "bundled"; app.target = HostTarget(); app.packaging = Packaging::Flatpak; app.flatpak.appId = "net.example.Bundled"; std::ofstream(scratch / "app.desktop") << "[Desktop Entry]\nType=Application\nName=Bundled\nExec=bundled\nIcon=net.example.Bundled\nCategories=Utility;\n"; std::ofstream(scratch / "icon.svg") << "\n"; app.flatpak.desktopFile = scratch / "app.desktop"; app.flatpak.icons = { scratch / "icon.svg" }; { std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); } { std::unordered_map> 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 auto built = Snapshot(app.BinDir() / "files"); const std::string first = BundleFlatpakApp(app); Check(first.empty(), std::format("the bundle is written: {}", first)); const fs::path finished = FlatpakAppDir(app) / "build-dir"; const fs::path exported = finished / "export" / "share"; Check(fs::exists(exported / "applications" / "net.example.Bundled.desktop") && fs::exists(exported / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Bundled.svg"), "build-finish exports the desktop entry and the icon"); Check(ReadFile(finished / "metadata").contains("\ncommand=bundled\n"), std::format("and makes the program the app's command:\n{}", ReadFile(finished / "metadata"))); Check(fs::equivalent(finished / "files" / "bin" / "bundled", ExpectedOutput(app), ec), "files/ is linked into the build directory, not copied"); Check(Snapshot(app.BinDir() / "files") == built && !fs::exists(app.BinDir() / "metadata") && !fs::exists(app.BinDir() / "export"), "and the build's own files/ is left as it was"); const fs::path bundle = FlatpakBundlePath(app); Check(bundle.parent_path() == app.BinDir().parent_path() && bundle.filename() == std::format("{}.flatpak", app.VariantId()), std::format("it is named after the variant, next to its bin dir: {}", bundle.string())); Check(fs::is_regular_file(bundle) && fs::file_size(bundle, ec) > 0, "the bundle exists"); Check(fs::exists(FlatpakAppDir(app) / "repo" / "config"), "the app went into the project's repo"); const std::string second = BundleFlatpakApp(app); Check(second.empty(), std::format("bundling again with nothing changed works: {}", second)); // While another export holds the app's repo, every variant's being one, // a bundle waits for it rather than bundle what that one exported, and // is written once it is free. Held here as another crafter-build would. { const std::int32_t held = open((FlatpakAppDir(app) / ".lock").c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644); Check(held >= 0 && flock(held, LOCK_EX) == 0, "the test holds the app's repo"); std::future third = std::async(std::launch::async, [&app] { return BundleFlatpakApp(app); }); Check(third.wait_for(std::chrono::seconds(5)) == std::future_status::timeout, "a bundle waits while another export holds the app's repo"); if (held >= 0) close(held); const std::string waited = third.get(); Check(waited.empty(), std::format("and is written once the repo is free: {}", waited)); } fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }