253 lines
15 KiB
C++
253 lines
15 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
#include <stdlib.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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string ReadFile(const fs::path& path) {
|
||
|
|
std::ifstream in(path, std::ios::binary);
|
||
|
|
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string BuildIt(Configuration& cfg) {
|
||
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||
|
|
std::mutex depMutex;
|
||
|
|
return Build(cfg, depResults, depMutex).result;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::map<fs::path, fs::file_time_type> Mtimes(const fs::path& root) {
|
||
|
|
std::map<fs::path, fs::file_time_type> times;
|
||
|
|
for (const fs::directory_entry& entry : fs::recursive_directory_iterator(root)) {
|
||
|
|
if (entry.is_regular_file()) times.emplace(entry.path(), entry.last_write_time());
|
||
|
|
}
|
||
|
|
return times;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// cfg.flatpak.share puts files the program reads under /app/share rather
|
||
|
|
// than next to it, where cfg.files goes: a directory with everything in it
|
||
|
|
// and single files, each at the path under share/ it names. Crafter.Review
|
||
|
|
// finds its process/ directory at <its directory>/../share/crafter-review,
|
||
|
|
// and the Flatpak's files/share is the writer's own, which it prunes, so
|
||
|
|
// before this there was no way to put it there. The fixture program reads
|
||
|
|
// its data the same way, and is run from files/bin as -r runs it. The
|
||
|
|
// entries are kept while listed: a build that changed nothing writes
|
||
|
|
// nothing, an edited source is copied again, a removed one goes, and so does
|
||
|
|
// an entry no longer listed. The build only places them: what is exported of
|
||
|
|
// them is flatpak build-finish's to decide, and build-export's to check, as
|
||
|
|
// for any app's files. A target outside share/, a missing source, and two
|
||
|
|
// files for one path, or a file where another needs a directory, are
|
||
|
|
// refused, naming what clashed, a field's own file among them. So is a
|
||
|
|
// directory that holds what the build writes, files/ or the app's
|
||
|
|
// directory, where install and bundle put another copy of files/, which
|
||
|
|
// would be copied into itself, as written and where links lead, so the
|
||
|
|
// directory of a project whose bin/ is a link elsewhere as well; and one
|
||
|
|
// that lies in them, the build's own output. The
|
||
|
|
// app's desktop entry is held to the field's checks whichever placed it, so
|
||
|
|
// one a share entry places with another Icon= is refused. Anything else is
|
||
|
|
// placed like any other file: a second desktop entry, an add-on's metainfo,
|
||
|
|
// the legacy appdata/, any icon, of the app's or not, and the app's D-Bus
|
||
|
|
// services, MIME packages, search providers and KRunner plugins, which no
|
||
|
|
// field of their own carries.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-share";
|
||
|
|
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);
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
const fs::path fixture = fs::current_path() / "tests" / "FlatpakShare" / "fixture";
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
// The data is edited and deleted below, so it is staged.
|
||
|
|
const fs::path data = scratch / "data";
|
||
|
|
fs::copy(fixture / "data", data, fs::copy_options::recursive);
|
||
|
|
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "share-app";
|
||
|
|
app.outputName = "share";
|
||
|
|
app.target = HostTarget();
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
app.flatpak.appId = "net.example.Share";
|
||
|
|
app.flatpak.metainfo = data / "app.metainfo.xml";
|
||
|
|
const FlatpakShareEntry process = { data / "process", "demo/process" };
|
||
|
|
const FlatpakShareEntry license = { data / "LICENSE", "licenses/net.example.Share/LICENSE" };
|
||
|
|
app.flatpak.share = { process, license };
|
||
|
|
{
|
||
|
|
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 fs::path root = app.BinDir();
|
||
|
|
const fs::path share = root / "files" / "share";
|
||
|
|
Check(ReadFile(share / "demo" / "process" / "lens.md") == ReadFile(data / "process" / "lens.md"), "a directory entry is copied under its target");
|
||
|
|
Check(fs::is_regular_file(share / "demo" / "process" / "steps" / "one.md"), "with everything in it");
|
||
|
|
Check(ReadFile(share / "licenses" / "net.example.Share" / "LICENSE") == ReadFile(data / "LICENSE"), "a file entry is copied to its target");
|
||
|
|
Check(fs::is_regular_file(share / "metainfo" / "net.example.Share.metainfo.xml"), "the metainfo is still placed beside them");
|
||
|
|
Check(!fs::exists(root / "export"), "the build writes no export/, which build-finish does");
|
||
|
|
|
||
|
|
const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30));
|
||
|
|
Check(run.exitCode == 0 && run.output == "the lens as first written\n", std::format("the program finds its data at <its directory>/../share (exit {}): {}", run.exitCode, run.output));
|
||
|
|
|
||
|
|
const std::map<fs::path, fs::file_time_type> before = Mtimes(share);
|
||
|
|
Check(BuildIt(app).empty(), "the second build succeeds");
|
||
|
|
Check(Mtimes(share) == before, "a build that changed nothing leaves files/share alone");
|
||
|
|
|
||
|
|
std::ofstream(data / "process" / "lens.md", std::ios::trunc) << "the lens as edited\n";
|
||
|
|
fs::remove(data / "process" / "steps" / "one.md");
|
||
|
|
Check(BuildIt(app).empty(), "the build after the edits succeeds");
|
||
|
|
Check(ReadFile(share / "demo" / "process" / "lens.md") == "the lens as edited\n", "an edited source is copied again");
|
||
|
|
Check(!fs::exists(share / "demo" / "process" / "steps"), "a removed source goes, and the directory it leaves empty with it");
|
||
|
|
|
||
|
|
app.flatpak.share = { process };
|
||
|
|
Check(BuildIt(app).empty(), "the build without the licence entry succeeds");
|
||
|
|
Check(!fs::exists(share / "licenses"), "an entry no longer listed goes");
|
||
|
|
Check(fs::exists(share / "demo" / "process" / "lens.md"), "the one still listed stays");
|
||
|
|
|
||
|
|
// A directory entry that holds an icon of the app, among others.
|
||
|
|
const fs::path theme = data / "theme" / "hicolor" / "scalable";
|
||
|
|
fs::create_directories(theme / "apps");
|
||
|
|
fs::create_directories(theme / "actions");
|
||
|
|
std::ofstream(theme / "apps" / "net.example.Share.svg") << "<svg/>\n";
|
||
|
|
std::ofstream(theme / "actions" / "demo-action.svg") << "<svg/>\n";
|
||
|
|
|
||
|
|
struct Refusal {
|
||
|
|
std::string_view what;
|
||
|
|
std::vector<FlatpakShareEntry> share;
|
||
|
|
std::string_view reason;
|
||
|
|
};
|
||
|
|
// The app's directory, where install and bundle make build-dir/files of
|
||
|
|
// files/ again, as they would have by now.
|
||
|
|
fs::create_directories(FlatpakAppDir(app) / "build-dir" / "files");
|
||
|
|
const std::array<Refusal, 12> refusals = {{
|
||
|
|
{"an absolute target", {{data / "LICENSE", "/etc/LICENSE"}}, "has to be a path inside /app/share"},
|
||
|
|
{"a target leaving share/", {{data / "LICENSE", "../bin/LICENSE"}}, "has to be a path inside /app/share"},
|
||
|
|
{"share/ itself", {{data / "LICENSE", "."}}, "has to be a path inside /app/share"},
|
||
|
|
{"a missing source", {{data / "NOTICE", "licenses/NOTICE"}}, "does not exist"},
|
||
|
|
{"two files for one path", {{data / "LICENSE", "x/LICENSE"}, {data / "app.metainfo.xml", "x/LICENSE"}}, "both go to share/x/LICENSE"},
|
||
|
|
{"a file where a directory goes", {process, {data / "LICENSE", "demo/process"}}, "would make share/demo/process a file"},
|
||
|
|
{"a directory where a file went", {{data / "LICENSE", "demo/process"}, process}, "needs share/demo/process to be a directory"},
|
||
|
|
{"a file over the metainfo cfg.flatpak.metainfo places", {{data / "LICENSE", "metainfo/net.example.Share.metainfo.xml"}}, "both go to share/metainfo/net.example.Share.metainfo.xml"},
|
||
|
|
{"the project's directory, which holds files/share", {{fixture, "project"}}, "so a share entry would copy it into itself"},
|
||
|
|
{"the bin dir's files/", {{root / "files", "build"}}, "so a share entry would copy it into itself"},
|
||
|
|
{"the project's build/, which holds the app's directory", {{fixture / "build", "build"}}, "so a share entry would copy it into itself"},
|
||
|
|
{"a directory inside the app's directory", {{FlatpakAppDir(app) / "build-dir", "app"}}, "which the build writes and writes again"},
|
||
|
|
}};
|
||
|
|
for (const Refusal& refusal : refusals) {
|
||
|
|
app.flatpak.share = refusal.share;
|
||
|
|
const std::string result = BuildIt(app);
|
||
|
|
Check(result.contains(refusal.reason), std::format("{} is refused, saying '{}': {}", refusal.what, refusal.reason, result));
|
||
|
|
}
|
||
|
|
|
||
|
|
// A project whose bin/ is a link elsewhere holds its files/ as written
|
||
|
|
// and not once the link is followed, and its own directory is refused
|
||
|
|
// all the same: its walk would otherwise copy build/, and with it the
|
||
|
|
// app's build-dir, deeper with every install.
|
||
|
|
const fs::path linked = scratch / "linked";
|
||
|
|
fs::create_directories(linked);
|
||
|
|
fs::create_directories(scratch / "linked-bin");
|
||
|
|
fs::copy_file(fixture / "main.cpp", linked / "main.cpp");
|
||
|
|
fs::create_directory_symlink(scratch / "linked-bin", linked / "bin");
|
||
|
|
Configuration other;
|
||
|
|
other.path = linked;
|
||
|
|
other.name = "share-linked";
|
||
|
|
other.outputName = "linked";
|
||
|
|
other.target = HostTarget();
|
||
|
|
other.packaging = Packaging::Flatpak;
|
||
|
|
other.flatpak.appId = "net.example.ShareLinked";
|
||
|
|
other.flatpak.share = { {linked, "project"} };
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> ifaces = {};
|
||
|
|
std::array<fs::path, 1> impls = { "main" };
|
||
|
|
other.GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
const std::string linkedRefused = BuildIt(other);
|
||
|
|
Check(linkedRefused.contains("so a share entry would copy it into itself"), std::format("the directory of a project whose bin/ is a link is refused: {}", linkedRefused));
|
||
|
|
|
||
|
|
// The app's desktop entry is checked where it lands, whichever placed
|
||
|
|
// it: one a share entry puts at applications/<app-id>.desktop is held to
|
||
|
|
// its Icon= as the field's is.
|
||
|
|
std::ofstream(data / "wrong-icon.desktop") << "[Desktop Entry]\nType=Application\nName=Share\nExec=share\nIcon=other\n";
|
||
|
|
app.flatpak.share = { {data / "wrong-icon.desktop", "applications/net.example.Share.desktop"} };
|
||
|
|
const std::string wrongIcon = BuildIt(app);
|
||
|
|
Check(wrongIcon.contains("has to be the app-id"), std::format("a share entry's desktop entry of the app with another Icon= is refused: {}", wrongIcon));
|
||
|
|
|
||
|
|
// Anything else is placed as it is, for flatpak to export and check as
|
||
|
|
// it does any app's: a second desktop entry, an add-on's metainfo, the
|
||
|
|
// legacy appdata/, the app's icon where the icons field puts none,
|
||
|
|
// inside a directory entry too, an icon of something else, an action
|
||
|
|
// icon say, and the app's symbolic one or one named with a '+'.
|
||
|
|
std::ofstream(data / "editor.desktop") << "[Desktop Entry]\nType=Application\nName=Share Editor\nExec=share --edit\nIcon=net.example.Share\n";
|
||
|
|
app.flatpak.share = {
|
||
|
|
{data / "editor.desktop", "applications/net.example.Share.Editor.desktop"},
|
||
|
|
{data / "app.metainfo.xml", "metainfo/net.example.Share.Plugin.metainfo.xml"},
|
||
|
|
{data / "app.metainfo.xml", "appdata/net.example.Share.appdata.xml"},
|
||
|
|
{data / "LICENSE", "icons/hicolor/256x256/apps/net.example.Share.png"},
|
||
|
|
{data / "theme", "icons"},
|
||
|
|
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.SharePlus.svg"},
|
||
|
|
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.Share-symbolic.svg"},
|
||
|
|
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.Share+x.svg"},
|
||
|
|
};
|
||
|
|
const std::string placed = BuildIt(app);
|
||
|
|
Check(placed.empty(), std::format("a second desktop entry, other metainfo and any icon build: {}", placed));
|
||
|
|
const fs::path apps = share / "icons" / "hicolor" / "scalable" / "apps";
|
||
|
|
for (const fs::path& place : {fs::path("applications/net.example.Share.Editor.desktop"), fs::path("metainfo/net.example.Share.Plugin.metainfo.xml"), fs::path("appdata/net.example.Share.appdata.xml"), fs::path("icons/hicolor/256x256/apps/net.example.Share.png"), fs::path("icons/hicolor/scalable/apps/net.example.Share.svg"), fs::path("icons/hicolor/scalable/actions/demo-action.svg")}) {
|
||
|
|
Check(fs::is_regular_file(share / place), std::format("share/{} is placed", place.string()));
|
||
|
|
}
|
||
|
|
Check(fs::is_regular_file(apps / "net.example.SharePlus.svg") && fs::is_regular_file(apps / "net.example.Share-symbolic.svg") && fs::is_regular_file(apps / "net.example.Share+x.svg"), "and so are the app's icons the icons field does not make");
|
||
|
|
|
||
|
|
// So are the files of the app's that no field carries: D-Bus
|
||
|
|
// activation, a MIME type, a search provider, a KRunner plugin.
|
||
|
|
const std::array<fs::path, 4> others = {
|
||
|
|
"dbus-1/services/net.example.Share.service",
|
||
|
|
"mime/packages/net.example.Share.xml",
|
||
|
|
"gnome-shell/search-providers/net.example.Share.search-provider.ini",
|
||
|
|
"krunner/dbusplugins/net.example.Share.desktop",
|
||
|
|
};
|
||
|
|
app.flatpak.share.clear();
|
||
|
|
for (const fs::path& place : others) app.flatpak.share.push_back({data / "LICENSE", place});
|
||
|
|
const std::string othersBuilt = BuildIt(app);
|
||
|
|
Check(othersBuilt.empty(), std::format("the app's D-Bus service, MIME package, search provider and KRunner plugin build: {}", othersBuilt));
|
||
|
|
for (const fs::path& place : others) Check(fs::is_regular_file(share / place), std::format("share/{} is placed", place.string()));
|
||
|
|
|
||
|
|
// A source directory written with a trailing separator, "process/", is
|
||
|
|
// the directory: each file lands at its path inside it, under the target.
|
||
|
|
app.flatpak.share = { {data / "process" / "", "slashed"} };
|
||
|
|
Check(BuildIt(app).empty() && fs::is_regular_file(share / "slashed" / "lens.md"), "a source directory written with a trailing separator is copied under its target");
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|