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.
This commit is contained in:
Brad King 2025-02-07 21:10:03 -05:00
parent ec24f910d8
commit f189e64126
3 changed files with 70 additions and 0 deletions

View File

@ -33,6 +33,7 @@ set(CMakeLib_TESTS
testFindPackageCommand.cxx
testUVHandlePtr.cxx
testUVJobServerClient.cxx
testUVPatches.cxx
testUVProcessChain.cxx
testUVRAII.cxx
testUVStreambuf.cxx

View File

@ -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,
});
}

View File

@ -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 <cmConfigure.h> // IWYU pragma: keep
#include <string>
#include <cm3p/uv.h>
#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<std::string*>(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,
});
}