Files
capy-bearer/scripts/test_file_temp_native.cpp
T

74 lines
3.0 KiB
C++

#include <filesystem>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include "src/lib/types.cpp"
#include "src/lib/dvalue.cpp"
#include "src/lib/functionlib.cpp"
int main(int argc, char** argv)
{
if(argc != 2) return 2;
std::filesystem::path root(argv[1]);
bool ok = true;
auto need = [&](bool condition, const char* name) {
if(!condition) { std::cerr << name << '\n'; ok = false; }
};
auto create = [&](const char* name) {
auto path = root / name;
std::ofstream(path) << "owned";
return path.string();
};
Request request;
auto first = create("first");
request.resources.temporary_files.push_back(first);
request.cleanup_temporary_files();
need(!std::filesystem::exists(first), "explicit cleanup removes file");
need(request.resources.temporary_files.empty(), "cleanup clears ownership");
std::ofstream(first) << "unowned";
request.cleanup_temporary_files();
need(std::filesystem::exists(first), "repeated cleanup does not retain old ownership");
auto reused = create("reused-request");
request.resources.temporary_files.push_back(reused);
request.cleanup_temporary_files();
need(!std::filesystem::exists(reused), "reused Request cleans newly owned file");
need(std::filesystem::exists(first), "reused Request preserves previously released path");
need(request.resources.temporary_files.empty(), "reused Request clears new ownership");
auto original = create("original");
auto destination = (root / "destination").string();
request.resources.temporary_files.push_back(original);
std::filesystem::rename(original, destination);
std::ofstream(original) << "replacement";
request.cleanup_temporary_files();
need(!std::filesystem::exists(original), "replacement at owned path is removed");
need(std::filesystem::exists(destination), "renamed destination survives");
auto directory = create("directory");
request.resources.temporary_files.push_back(directory);
::unlink(directory.c_str());
std::filesystem::create_directory(directory);
std::ofstream(std::filesystem::path(directory) / "child") << "keep";
request.cleanup_temporary_files();
need(std::filesystem::is_directory(directory), "cleanup leaves converted directory");
need(std::filesystem::exists(std::filesystem::path(directory) / "child"), "cleanup is not recursive");
need(request.resources.temporary_files.empty(), "failed unlink clears ownership");
auto survivor = create("other-request");
Request other;
other.resources.temporary_files.push_back(survivor);
auto scoped = create("destructor");
{
Request scope;
scope.resources.temporary_files.push_back(scoped);
scope.resources.temporary_files.push_back((root / "missing").string());
}
need(!std::filesystem::exists(scoped), "destructor removes owned file");
need(std::filesystem::exists(survivor), "destructor preserves separate request file");
other.cleanup_temporary_files();
need(!std::filesystem::exists(survivor), "separate request cleans its own file");
if(ok) std::cout << "file_temp native cleanup passed\n";
return ok ? 0 : 1;
}