CTest: Allow setting exit code in ctest scripts
This commit is contained in:
parent
47277616d8
commit
46a0c04284
@ -7,6 +7,7 @@
|
|||||||
#include "cmExecutionStatus.h"
|
#include "cmExecutionStatus.h"
|
||||||
#include "cmMakefile.h"
|
#include "cmMakefile.h"
|
||||||
#include "cmMessageType.h"
|
#include "cmMessageType.h"
|
||||||
|
#include "cmMessenger.h"
|
||||||
#include "cmStringAlgorithms.h"
|
#include "cmStringAlgorithms.h"
|
||||||
#include "cmSystemTools.h"
|
#include "cmSystemTools.h"
|
||||||
|
|
||||||
@ -94,10 +95,12 @@ bool cmCTestEmptyBinaryDirectoryCommand(std::vector<std::string> const& args,
|
|||||||
|
|
||||||
std::string err;
|
std::string err;
|
||||||
if (!EmptyBinaryDirectory(args[0], err)) {
|
if (!EmptyBinaryDirectory(args[0], err)) {
|
||||||
status.GetMakefile().IssueMessage(
|
cmMakefile& mf = status.GetMakefile();
|
||||||
|
mf.GetMessenger()->DisplayMessage(
|
||||||
MessageType::FATAL_ERROR,
|
MessageType::FATAL_ERROR,
|
||||||
cmStrCat("Did not remove the binary directory:\n ", args[0],
|
cmStrCat("Did not remove the binary directory:\n ", args[0],
|
||||||
"\nbecause:\n ", err));
|
"\nbecause:\n ", err),
|
||||||
|
mf.GetBacktrace());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,10 +63,7 @@ int cmCTestScriptHandler::ProcessHandler()
|
|||||||
res |= this->RunConfigurationScript(this->ConfigurationScripts[i],
|
res |= this->RunConfigurationScript(this->ConfigurationScripts[i],
|
||||||
this->ScriptProcessScope[i]);
|
this->ScriptProcessScope[i]);
|
||||||
}
|
}
|
||||||
if (res) {
|
return res;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmCTestScriptHandler::UpdateElapsedTime()
|
void cmCTestScriptHandler::UpdateElapsedTime()
|
||||||
@ -180,6 +177,8 @@ void cmCTestScriptHandler::CreateCMake()
|
|||||||
this->CMake->SetHomeOutputDirectory("");
|
this->CMake->SetHomeOutputDirectory("");
|
||||||
this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
|
this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
|
||||||
this->CMake->AddCMakePaths();
|
this->CMake->AddCMakePaths();
|
||||||
|
this->CMake->SetWorkingMode(cmake::SCRIPT_MODE,
|
||||||
|
cmake::CommandFailureAction::EXIT_CODE);
|
||||||
this->GlobalGenerator =
|
this->GlobalGenerator =
|
||||||
cm::make_unique<cmGlobalGenerator>(this->CMake.get());
|
cm::make_unique<cmGlobalGenerator>(this->CMake.get());
|
||||||
|
|
||||||
@ -289,7 +288,7 @@ int cmCTestScriptHandler::ReadInScript(std::string const& total_script_arg)
|
|||||||
cmSystemTools::GetErrorOccurredFlag()) {
|
cmSystemTools::GetErrorOccurredFlag()) {
|
||||||
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
||||||
"Error in read:" << systemFile << "\n");
|
"Error in read:" << systemFile << "\n");
|
||||||
return 2;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add definitions of variables passed in on the command line:
|
// Add definitions of variables passed in on the command line:
|
||||||
@ -299,16 +298,20 @@ int cmCTestScriptHandler::ReadInScript(std::string const& total_script_arg)
|
|||||||
this->Makefile->AddDefinition(d.first, d.second);
|
this->Makefile->AddDefinition(d.first, d.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
// finally read in the script
|
// finally read in the script
|
||||||
if (!this->Makefile->ReadListFile(script) ||
|
if (!this->Makefile->ReadListFile(script) ||
|
||||||
cmSystemTools::GetErrorOccurredFlag()) {
|
cmSystemTools::GetErrorOccurredFlag()) {
|
||||||
// Reset the error flag so that it can run more than
|
// Reset the error flag so that it can run more than
|
||||||
// one script with an error when you use ctest_run_script.
|
// one script with an error when you use ctest_run_script.
|
||||||
cmSystemTools::ResetErrorOccurredFlag();
|
cmSystemTools::ResetErrorOccurredFlag();
|
||||||
return 2;
|
res = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return this->CMake->HasScriptModeExitCode()
|
||||||
|
? this->CMake->GetScriptModeExitCode()
|
||||||
|
: res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// run a specific script
|
// run a specific script
|
||||||
|
@ -525,7 +525,8 @@ bool cmMakefile::ExecuteCommand(cmListFileFunction const& lff,
|
|||||||
this->IssueMessage(MessageType::FATAL_ERROR, error);
|
this->IssueMessage(MessageType::FATAL_ERROR, error);
|
||||||
}
|
}
|
||||||
result = false;
|
result = false;
|
||||||
if (this->GetCMakeInstance()->GetWorkingMode() != cmake::NORMAL_MODE) {
|
if (this->GetCMakeInstance()->GetCommandFailureAction() ==
|
||||||
|
cmake::CommandFailureAction::FATAL_ERROR) {
|
||||||
cmSystemTools::SetFatalErrorOccurred();
|
cmSystemTools::SetFatalErrorOccurred();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -659,7 +659,8 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
|
|||||||
GetProjectCommandsInScriptMode(state->GetState());
|
GetProjectCommandsInScriptMode(state->GetState());
|
||||||
// Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
|
// Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
|
||||||
// set to $PWD for -P mode.
|
// set to $PWD for -P mode.
|
||||||
state->SetWorkingMode(SCRIPT_MODE);
|
state->SetWorkingMode(SCRIPT_MODE,
|
||||||
|
cmake::CommandFailureAction::FATAL_ERROR);
|
||||||
state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
|
state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
|
||||||
state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
|
state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
|
||||||
state->ReadListFile(args, path);
|
state->ReadListFile(args, path);
|
||||||
@ -1561,7 +1562,8 @@ void cmake::SetArgs(std::vector<std::string> const& args)
|
|||||||
presetsGraph.PrintAllPresets();
|
presetsGraph.PrintAllPresets();
|
||||||
}
|
}
|
||||||
|
|
||||||
this->SetWorkingMode(WorkingMode::HELP_MODE);
|
this->SetWorkingMode(WorkingMode::HELP_MODE,
|
||||||
|
cmake::CommandFailureAction::FATAL_ERROR);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +129,16 @@ public:
|
|||||||
FIND_PACKAGE_MODE
|
FIND_PACKAGE_MODE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class CommandFailureAction
|
||||||
|
{
|
||||||
|
// When a command fails to execute, treat it as a fatal error.
|
||||||
|
FATAL_ERROR,
|
||||||
|
|
||||||
|
// When a command fails to execute, continue execution, but set the exit
|
||||||
|
// code accordingly.
|
||||||
|
EXIT_CODE,
|
||||||
|
};
|
||||||
|
|
||||||
using TraceFormat = cmTraceEnums::TraceOutputFormat;
|
using TraceFormat = cmTraceEnums::TraceOutputFormat;
|
||||||
|
|
||||||
struct GeneratorInfo
|
struct GeneratorInfo
|
||||||
@ -441,8 +451,18 @@ public:
|
|||||||
//! Do all the checks before running configure
|
//! Do all the checks before running configure
|
||||||
int DoPreConfigureChecks();
|
int DoPreConfigureChecks();
|
||||||
|
|
||||||
void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
|
void SetWorkingMode(WorkingMode mode, CommandFailureAction policy)
|
||||||
WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
|
{
|
||||||
|
this->CurrentWorkingMode = mode;
|
||||||
|
this->CurrentCommandFailureAction = policy;
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkingMode GetWorkingMode() const { return this->CurrentWorkingMode; }
|
||||||
|
|
||||||
|
CommandFailureAction GetCommandFailureAction() const
|
||||||
|
{
|
||||||
|
return this->CurrentCommandFailureAction;
|
||||||
|
}
|
||||||
|
|
||||||
//! Debug the try compile stuff by not deleting the files
|
//! Debug the try compile stuff by not deleting the files
|
||||||
bool GetDebugTryCompile() const { return this->DebugTryCompile; }
|
bool GetDebugTryCompile() const { return this->DebugTryCompile; }
|
||||||
@ -780,6 +800,8 @@ private:
|
|||||||
std::string CMakeWorkingDirectory;
|
std::string CMakeWorkingDirectory;
|
||||||
ProgressCallbackType ProgressCallback;
|
ProgressCallbackType ProgressCallback;
|
||||||
WorkingMode CurrentWorkingMode = NORMAL_MODE;
|
WorkingMode CurrentWorkingMode = NORMAL_MODE;
|
||||||
|
CommandFailureAction CurrentCommandFailureAction =
|
||||||
|
CommandFailureAction::FATAL_ERROR;
|
||||||
bool DebugOutput = false;
|
bool DebugOutput = false;
|
||||||
bool DebugFindOutput = false;
|
bool DebugFindOutput = false;
|
||||||
bool Trace = false;
|
bool Trace = false;
|
||||||
|
@ -396,6 +396,9 @@ int do_cmake(int ac, char const* const* av)
|
|||||||
mode = cmState::FindPackage;
|
mode = cmState::FindPackage;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
auto const failurePolicy = workingMode == cmake::NORMAL_MODE
|
||||||
|
? cmake::CommandFailureAction::EXIT_CODE
|
||||||
|
: cmake::CommandFailureAction::FATAL_ERROR;
|
||||||
cmake cm(role, mode);
|
cmake cm(role, mode);
|
||||||
cm.SetHomeDirectory("");
|
cm.SetHomeDirectory("");
|
||||||
cm.SetHomeOutputDirectory("");
|
cm.SetHomeOutputDirectory("");
|
||||||
@ -406,7 +409,7 @@ int do_cmake(int ac, char const* const* av)
|
|||||||
cm.SetProgressCallback([&cm](std::string const& msg, float prog) {
|
cm.SetProgressCallback([&cm](std::string const& msg, float prog) {
|
||||||
cmakemainProgressCallback(msg, prog, &cm);
|
cmakemainProgressCallback(msg, prog, &cm);
|
||||||
});
|
});
|
||||||
cm.SetWorkingMode(workingMode);
|
cm.SetWorkingMode(workingMode, failurePolicy);
|
||||||
|
|
||||||
int res = cm.Run(parsedArgs, view_only);
|
int res = cm.Run(parsedArgs, view_only);
|
||||||
if (list_cached || list_all_cached) {
|
if (list_cached || list_all_cached) {
|
||||||
@ -988,7 +991,8 @@ int do_install(int ac, char const* const* av)
|
|||||||
cm.SetHomeDirectory("");
|
cm.SetHomeDirectory("");
|
||||||
cm.SetHomeOutputDirectory("");
|
cm.SetHomeOutputDirectory("");
|
||||||
cm.SetDebugOutputOn(verbose);
|
cm.SetDebugOutputOn(verbose);
|
||||||
cm.SetWorkingMode(cmake::SCRIPT_MODE);
|
cm.SetWorkingMode(cmake::SCRIPT_MODE,
|
||||||
|
cmake::CommandFailureAction::FATAL_ERROR);
|
||||||
ret_ = int(bool(cm.Run(cmd)));
|
ret_ = int(bool(cm.Run(cmd)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -672,6 +672,7 @@ add_RunCMake_test(ctest_update)
|
|||||||
add_RunCMake_test(ctest_upload)
|
add_RunCMake_test(ctest_upload)
|
||||||
add_RunCMake_test(ctest_environment)
|
add_RunCMake_test(ctest_environment)
|
||||||
add_RunCMake_test(ctest_empty_binary_directory)
|
add_RunCMake_test(ctest_empty_binary_directory)
|
||||||
|
add_RunCMake_test(ctest_exit)
|
||||||
add_RunCMake_test(ctest_fixtures)
|
add_RunCMake_test(ctest_fixtures)
|
||||||
if(CMAKE_GENERATOR MATCHES "Make|Ninja")
|
if(CMAKE_GENERATOR MATCHES "Make|Ninja")
|
||||||
add_RunCMake_test(ctest_instrumentation)
|
add_RunCMake_test(ctest_instrumentation)
|
||||||
|
1
Tests/RunCMake/ctest_exit/3-result.txt
Normal file
1
Tests/RunCMake/ctest_exit/3-result.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
3
|
3
Tests/RunCMake/ctest_exit/3-stderr.txt
Normal file
3
Tests/RunCMake/ctest_exit/3-stderr.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
^CMake Error at [^
|
||||||
|
]*/Tests/RunCMake/ctest_exit/exit1.cmake:2 \(message\):
|
||||||
|
send error$
|
1
Tests/RunCMake/ctest_exit/7-result.txt
Normal file
1
Tests/RunCMake/ctest_exit/7-result.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
7
|
3
Tests/RunCMake/ctest_exit/7-stderr.txt
Normal file
3
Tests/RunCMake/ctest_exit/7-stderr.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
^CMake Error at [^
|
||||||
|
]*/Tests/RunCMake/ctest_exit/exit1.cmake:2 \(message\):
|
||||||
|
send error$
|
45
Tests/RunCMake/ctest_exit/RunCMakeTest.cmake
Normal file
45
Tests/RunCMake/ctest_exit/RunCMakeTest.cmake
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
include(RunCMake)
|
||||||
|
|
||||||
|
message(STATUS "Multiple -S options:")
|
||||||
|
|
||||||
|
run_cmake_command(3 ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit1.cmake
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit2.cmake
|
||||||
|
)
|
||||||
|
|
||||||
|
run_cmake_command(7 ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit4.cmake
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit1.cmake
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit2.cmake
|
||||||
|
)
|
||||||
|
|
||||||
|
message(STATUS "Multiple -SP options:")
|
||||||
|
|
||||||
|
run_cmake_command(3 ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit1.cmake
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit2.cmake
|
||||||
|
)
|
||||||
|
|
||||||
|
run_cmake_command(7 ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit4.cmake
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit1.cmake
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit2.cmake
|
||||||
|
)
|
||||||
|
|
||||||
|
message(STATUS "Mixed -S and -SP options:")
|
||||||
|
|
||||||
|
run_cmake_command(7 ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit4.cmake
|
||||||
|
-SP ${RunCMake_SOURCE_DIR}/exit1.cmake
|
||||||
|
-S ${RunCMake_SOURCE_DIR}/exit2.cmake
|
||||||
|
)
|
||||||
|
|
||||||
|
message(STATUS "ctest_run_script:")
|
||||||
|
|
||||||
|
configure_file(
|
||||||
|
${RunCMake_SOURCE_DIR}/test.cmake.in
|
||||||
|
${RunCMake_BINARY_DIR}/test.cmake @ONLY)
|
||||||
|
|
||||||
|
run_cmake_command(Script ${CMAKE_CTEST_COMMAND} -V
|
||||||
|
-S ${RunCMake_BINARY_DIR}/test.cmake
|
||||||
|
)
|
1
Tests/RunCMake/ctest_exit/Script-result.txt
Normal file
1
Tests/RunCMake/ctest_exit/Script-result.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
(-1|255)
|
8
Tests/RunCMake/ctest_exit/Script-stderr.txt
Normal file
8
Tests/RunCMake/ctest_exit/Script-stderr.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
^CMake Error at [^
|
||||||
|
]*/Tests/RunCMake/ctest_exit/exit1.cmake:2 \(message\):
|
||||||
|
send error
|
||||||
|
|
||||||
|
|
||||||
|
CMake Error at [^
|
||||||
|
]*/Tests/RunCMake/ctest_exit/exit1.cmake:2 \(message\):
|
||||||
|
send error$
|
3
Tests/RunCMake/ctest_exit/exit1.cmake
Normal file
3
Tests/RunCMake/ctest_exit/exit1.cmake
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
message(SEND_ERROR "send error")
|
||||||
|
cmake_language(EXIT 1)
|
2
Tests/RunCMake/ctest_exit/exit2.cmake
Normal file
2
Tests/RunCMake/ctest_exit/exit2.cmake
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
cmake_language(EXIT 2)
|
2
Tests/RunCMake/ctest_exit/exit4.cmake
Normal file
2
Tests/RunCMake/ctest_exit/exit4.cmake
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
cmake_language(EXIT 4)
|
23
Tests/RunCMake/ctest_exit/test.cmake.in
Normal file
23
Tests/RunCMake/ctest_exit/test.cmake.in
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
ctest_run_script(
|
||||||
|
"@RunCMake_SOURCE_DIR@/exit1.cmake"
|
||||||
|
"@RunCMake_SOURCE_DIR@/exit4.cmake"
|
||||||
|
RETURN_VALUE ret
|
||||||
|
)
|
||||||
|
|
||||||
|
if(NOT ret EQUAL 4)
|
||||||
|
message(FATAL_ERROR "Expected ret == 4, got ${ret}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
unset(ret)
|
||||||
|
|
||||||
|
ctest_run_script(NEW_PROCESS
|
||||||
|
"@RunCMake_SOURCE_DIR@/exit1.cmake"
|
||||||
|
"@RunCMake_SOURCE_DIR@/exit4.cmake"
|
||||||
|
RETURN_VALUE ret
|
||||||
|
)
|
||||||
|
|
||||||
|
if(NOT ret EQUAL 4)
|
||||||
|
message(FATAL_ERROR "Expected ret == 4, got ${ret}")
|
||||||
|
endif()
|
Loading…
Reference in New Issue
Block a user