From 04deda1d2a750fe609f3f2ab02e3068e1d152fb3 Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Tue, 23 Nov 2021 20:06:47 +0100 Subject: CTest: Extract common implementation of add_subdirectory and subdirs The two functions have basically the same functionality in a CTest context, however the add_subdirectory implementation was missing code for changing the current working directory similarly to how it was done for subdirs. This commit extracts the common code and also fixes that bug. Issue: #22921 --- Source/CTest/cmCTestTestHandler.cxx | 93 +++++++++++++++---------------------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index 02db0c6..fb9954f 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -81,6 +81,42 @@ public: cmCTestTestHandler* TestHandler; }; +bool ReadSubdirectory(std::string fname, cmExecutionStatus& status) +{ + if (!cmSystemTools::FileExists(fname)) { + // No subdirectory? So what... + return true; + } + bool readit = false; + { + cmWorkingDirectory workdir(fname); + if (workdir.Failed()) { + status.SetError("Failed to change directory to " + fname + " : " + + std::strerror(workdir.GetLastResult())); + return false; + } + const char* testFilename; + if (cmSystemTools::FileExists("CTestTestfile.cmake")) { + // does the CTestTestfile.cmake exist ? + testFilename = "CTestTestfile.cmake"; + } else if (cmSystemTools::FileExists("DartTestfile.txt")) { + // does the DartTestfile.txt exist ? + testFilename = "DartTestfile.txt"; + } else { + // No CTestTestfile? Who cares... + return true; + } + fname += "/"; + fname += testFilename; + readit = status.GetMakefile().ReadDependentFile(fname); + } + if (!readit) { + status.SetError(cmStrCat("Could not find include file: ", fname)); + return false; + } + return true; +} + bool cmCTestSubdirCommand(std::vector const& args, cmExecutionStatus& status) { @@ -98,35 +134,7 @@ bool cmCTestSubdirCommand(std::vector const& args, fname = cmStrCat(cwd, '/', arg); } - if (!cmSystemTools::FileIsDirectory(fname)) { - // No subdirectory? So what... - continue; - } - bool readit = false; - { - cmWorkingDirectory workdir(fname); - if (workdir.Failed()) { - status.SetError("Failed to change directory to " + fname + " : " + - std::strerror(workdir.GetLastResult())); - return false; - } - const char* testFilename; - if (cmSystemTools::FileExists("CTestTestfile.cmake")) { - // does the CTestTestfile.cmake exist ? - testFilename = "CTestTestfile.cmake"; - } else if (cmSystemTools::FileExists("DartTestfile.txt")) { - // does the DartTestfile.txt exist ? - testFilename = "DartTestfile.txt"; - } else { - // No CTestTestfile? Who cares... - continue; - } - fname += "/"; - fname += testFilename; - readit = status.GetMakefile().ReadDependentFile(fname); - } - if (!readit) { - status.SetError(cmStrCat("Could not load include file: ", fname)); + if (!ReadSubdirectory(std::move(fname), status)) { return false; } } @@ -144,32 +152,7 @@ bool cmCTestAddSubdirectoryCommand(std::vector const& args, std::string fname = cmStrCat(cmSystemTools::GetCurrentWorkingDirectory(), '/', args[0]); - if (!cmSystemTools::FileExists(fname)) { - // No subdirectory? So what... - return true; - } - bool readit = false; - { - const char* testFilename; - if (cmSystemTools::FileExists("CTestTestfile.cmake")) { - // does the CTestTestfile.cmake exist ? - testFilename = "CTestTestfile.cmake"; - } else if (cmSystemTools::FileExists("DartTestfile.txt")) { - // does the DartTestfile.txt exist ? - testFilename = "DartTestfile.txt"; - } else { - // No CTestTestfile? Who cares... - return true; - } - fname += "/"; - fname += testFilename; - readit = status.GetMakefile().ReadDependentFile(fname); - } - if (!readit) { - status.SetError(cmStrCat("Could not find include file: ", fname)); - return false; - } - return true; + return ReadSubdirectory(std::move(fname), status); } class cmCTestAddTestCommand : public cmCTestCommand -- cgit v0.12