Files
mandy/runtime/test/test_temp_dir.h
thiefyzheng b17397f0dd tests: derive every test's scratch folder from test_temp_dir::temp_dir, not GetTempPath (TEST-TEMPDIR-STRIPPED-ENV)
With TMP, TEMP and USERPROFILE all unset, GetTempPathW returns C:\Windows\, and the throwing create_directories/copy_file/ofstream on C:\Windows\leonida_* aborts with 0xc0000409 before the test body runs. runtime/test/test_temp_dir.h grows one entry, testfiles::temp_dir(): LEONIDA_TEST_DIR when set, the current directory when the temp environment is stripped (Windows), else the standard temp resolution. Fifty-two test files and the tiny_game fixture that called GetTempPathA/W or temp_directory_path directly now use it; d3d12_queue_handover_test.cpp also checks the _sopen_s result in capture_stderr instead of silently returning empty stderr. One runtime source function needed the same fallback because tests exercise it: tls_host_clone_dir in runtime/src/pe_loader.cpp (the loader_tls tests). Rebased onto current main after its D3D-448 additions to d3d12_mesh_test.cpp (3-way; the mesh test keeps both). Verification on a Windows VM: stripped environment - the previously crashing d3d12 tests exit 77 (no usable Vulkan device) instead of aborting, file_system_test runs all 505 checks and loader_test all 95 with zero failures; normal environment - identical outcomes; LEONIDA_TEST_DIR honored under a fully stripped environment (the scratch folder observed mid-run inside it, cleaned up after the pass). The finding is whoalin1's (coordination issue #14, 02:37:44Z).

AI-assisted.
2026-11-28 19:01:06 +00:00

89 lines
3.6 KiB
C++

// Scratch folders for tests that write files (a translator-style shader library, an index) under the temp directory.
//
// The folder name carries the process id. Under one fixed name, every copy of a test running at the same time (ctest
// -j, or two suites sharing one Wine prefix or one TMPDIR) used the same folder, and one copy's remove_all deleted the
// modules another copy was still loading: with four copies of d3d12_compute at once, 6 of 40 runs failed at PSO
// creation or in copy_file, while 125 runs one at a time all passed (Apple M3 Max, MoltenVK 1.4.3 under Wine 11).
#pragma once
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <string_view>
#include <system_error>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
namespace testfiles {
// The system temporary directory, or LEONIDA_TEST_DIR when it is set. GetTempPath falls back to the Windows
// directory when TMP, TEMP and USERPROFILE are all absent; a test must not try to write scratch files there.
inline std::filesystem::path temp_dir() {
if (const char* dir = std::getenv("LEONIDA_TEST_DIR"); dir && *dir) return std::filesystem::path(dir) / "";
#ifdef _WIN32
const char* tmp = std::getenv("TMP");
const char* temp = std::getenv("TEMP");
const char* profile = std::getenv("USERPROFILE");
if ((!tmp || !*tmp) && (!temp || !*temp) && (!profile || !*profile)) {
std::error_code ec;
auto cwd = std::filesystem::current_path(ec);
if (!ec) return cwd / "";
return std::filesystem::path(".") / "";
}
#endif
std::error_code ec;
auto dir = std::filesystem::temp_directory_path(ec);
return ec ? std::filesystem::path(".") / "" : dir;
}
// <temp>/<name>_<pid>: a folder no other running process uses.
inline std::filesystem::path process_temp_dir(std::string_view name) {
#ifdef _WIN32
const long pid = long(_getpid());
#else
const long pid = long(getpid());
#endif
return temp_dir() / (std::string(name) + "_" + std::to_string(pid));
}
// process_temp_dir(name), emptied and created on construction (a crashed run may have left the same name behind) and
// removed with its contents on destruction, so early returns don't leave one folder per failed run.
class ScratchDir {
public:
explicit ScratchDir(std::string_view name) : path_(process_temp_dir(name)) {
std::error_code ec;
std::filesystem::remove_all(path_, ec);
std::filesystem::create_directories(path_, ec);
if (ec) std::fprintf(stderr, "cannot create %s: %s\n", path_.string().c_str(), ec.message().c_str());
}
~ScratchDir() {
std::error_code ec;
std::filesystem::remove_all(path_, ec);
}
ScratchDir(const ScratchDir&) = delete;
ScratchDir& operator=(const ScratchDir&) = delete;
const std::filesystem::path& path() const { return path_; }
private:
std::filesystem::path path_;
};
// Copies `from` (a file the build compiled into the test, LEONIDA_TEST_*_SPV) to `to`, overwriting it. Returns false
// and says why when it can't: std::filesystem::copy_file would throw instead, and the uncaught exception ends the
// test without a message (abort; exit code 3 under Wine), which reads as a crash rather than a missing file.
inline bool copy_file(const std::filesystem::path& from, const std::filesystem::path& to) {
std::error_code ec;
std::filesystem::copy_file(from, to, std::filesystem::copy_options::overwrite_existing, ec);
if (ec) std::fprintf(stderr, "cannot copy %s to %s: %s\n", from.string().c_str(), to.string().c_str(),
ec.message().c_str());
return !ec;
}
} // namespace testfiles