From f189e6412651022f1466c5768856adfb89c8f469 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 7 Feb 2025 21:10:03 -0500 Subject: [PATCH] Tests: Add cases covering our mkdtemp code paths * `cmSystemTools::MakeTempDirectory` is our own code. * `uv_fs_mkdtemp` requires patching to backport to platforms not supported by upstream libuv. --- Tests/CMakeLib/CMakeLists.txt | 1 + Tests/CMakeLib/testSystemTools.cxx | 23 +++++++++++++++ Tests/CMakeLib/testUVPatches.cxx | 46 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 Tests/CMakeLib/testUVPatches.cxx diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index dda162245e..19794b8193 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -33,6 +33,7 @@ set(CMakeLib_TESTS testFindPackageCommand.cxx testUVHandlePtr.cxx testUVJobServerClient.cxx + testUVPatches.cxx testUVProcessChain.cxx testUVRAII.cxx testUVStreambuf.cxx diff --git a/Tests/CMakeLib/testSystemTools.cxx b/Tests/CMakeLib/testSystemTools.cxx index cb4fb6cedd..3d932a41e1 100644 --- a/Tests/CMakeLib/testSystemTools.cxx +++ b/Tests/CMakeLib/testSystemTools.cxx @@ -96,11 +96,34 @@ static bool testStrVersCmp() return true; } +static bool testMakeTempDirectory() +{ + std::cout << "testMakeTempDirectory()\n"; + + static std::string const kTemplate = "testMakeTempDirectory-XXXXXX"; + std::string tempDir = kTemplate; + cmsys::Status status = cmSystemTools::MakeTempDirectory(tempDir); + if (!status) { + std::cout << "cmSystemTools::MakeTempDirectory failed on \"" << tempDir + << "\": " << status.GetString() << '\n'; + return false; + } + if (!cmSystemTools::FileIsDirectory(tempDir)) { + std::cout << "cmSystemTools::MakeTempDirectory did not create \"" + << tempDir << '\n'; + return false; + } + cmSystemTools::RemoveADirectory(tempDir); + ASSERT_TRUE(tempDir != kTemplate); + return true; +} + int testSystemTools(int /*unused*/, char* /*unused*/[]) { return runTests({ testUpperCase, testVersionCompare, testStrVersCmp, + testMakeTempDirectory, }); } diff --git a/Tests/CMakeLib/testUVPatches.cxx b/Tests/CMakeLib/testUVPatches.cxx new file mode 100644 index 0000000000..06f8d1f237 --- /dev/null +++ b/Tests/CMakeLib/testUVPatches.cxx @@ -0,0 +1,46 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include // IWYU pragma: keep + +#include + +#include + +#include "cmSystemTools.h" + +#include "testCommon.h" + +static bool test_uv_fs_mkdtemp() +{ + std::cout << "test_uv_fs_mkdtemp()\n"; + static std::string const kTemplate = "test-uv_fs_mkdtemp-XXXXXX"; + std::string tempDir; + uv_fs_t tempDirReq; + tempDirReq.data = &tempDir; + uv_loop_t* loop = uv_default_loop(); + int r = + uv_fs_mkdtemp(loop, &tempDirReq, kTemplate.c_str(), [](uv_fs_t* req) { + if (req->data && req->path) { + *static_cast(req->data) = req->path; + } + }); + ASSERT_EQUAL(r, 0); + uv_run(loop, UV_RUN_DEFAULT); + uv_fs_req_cleanup(&tempDirReq); + if (!cmSystemTools::FileIsDirectory(tempDir)) { + std::cout << "cmSystemTools::MakeTempDirectory did not create \"" + << tempDir << '\n'; + return false; + } + cmSystemTools::RemoveADirectory(tempDir); + ASSERT_TRUE(tempDir != kTemplate); + return true; +} + +int testUVPatches(int /*unused*/, char* /*unused*/[]) +{ + return runTests({ + test_uv_fs_mkdtemp, + }); +}