1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestRunTest.h"
#include <algorithm>
#include <chrono>
#include <cstddef> // IWYU pragma: keep
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <ratio>
#include <sstream>
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include "cmsys/RegularExpression.hxx"
#include "cmCTest.h"
#include "cmCTestMemCheckHandler.h"
#include "cmCTestMultiProcessHandler.h"
#include "cmDuration.h"
#include "cmInstrumentation.h"
#include "cmProcess.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUVHandlePtr.h"
#include "cmWorkingDirectory.h"
cmCTestRunTest::cmCTestRunTest(cmCTestMultiProcessHandler& multiHandler,
int index)
: MultiTestHandler(multiHandler)
, Index(index)
, CTest(MultiTestHandler.CTest)
, TestHandler(MultiTestHandler.TestHandler)
, TestProperties(MultiTestHandler.Properties[Index])
{
}
void cmCTestRunTest::CheckOutput(std::string const& line)
{
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->GetIndex() << ": " << line << std::endl);
// Check for special CTest XML tags in this line of output.
// If any are found, this line is excluded from ProcessOutput.
if (!line.empty() && line.find("<CTest") != std::string::npos) {
bool ctest_tag_found = false;
if (this->TestHandler->CustomCompletionStatusRegex.find(line)) {
ctest_tag_found = true;
this->TestResult.CustomCompletionStatus =
this->TestHandler->CustomCompletionStatusRegex.match(1);
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->GetIndex() << ": "
<< "Test Details changed to '"
<< this->TestResult.CustomCompletionStatus
<< "'" << std::endl);
} else if (this->TestHandler->CustomLabelRegex.find(line)) {
ctest_tag_found = true;
auto label = this->TestHandler->CustomLabelRegex.match(1);
auto& labels = this->TestProperties->Labels;
if (std::find(labels.begin(), labels.end(), label) == labels.end()) {
labels.push_back(label);
std::sort(labels.begin(), labels.end());
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->GetIndex()
<< ": "
<< "Test Label added: '" << label << "'" << std::endl);
}
}
if (ctest_tag_found) {
return;
}
}
this->ProcessOutput += line;
this->ProcessOutput += "\n";
// Check for TIMEOUT_AFTER_MATCH property.
if (!this->TestProperties->TimeoutRegularExpressions.empty()) {
for (auto& reg : this->TestProperties->TimeoutRegularExpressions) {
if (reg.first.find(this->ProcessOutput)) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->GetIndex()
<< ": "
<< "Test timeout changed to "
<< std::chrono::duration_cast<std::chrono::seconds>(
this->TestProperties->AlternateTimeout)
.count()
<< std::endl);
this->TestProcess->ResetStartTime();
this->TestProcess->ChangeTimeout(
this->TestProperties->AlternateTimeout);
this->TestProperties->TimeoutRegularExpressions.clear();
break;
}
}
}
}
cmCTestRunTest::EndTestResult cmCTestRunTest::EndTest(size_t completed,
size_t total,
bool started)
{
this->WriteLogOutputTop(completed, total);
std::string reason;
bool passed = true;
cmProcess::State res =
started ? this->TestProcess->GetProcessStatus() : cmProcess::State::Error;
std::int64_t retVal = this->TestProcess->GetExitValue();
bool forceFail = false;
bool forceSkip = false;
bool skipped = false;
bool outputTestErrorsToConsole = false;
if (!this->TestProperties->RequiredRegularExpressions.empty() &&
this->FailedDependencies.empty()) {
bool found = false;
for (auto& pass : this->TestProperties->RequiredRegularExpressions) {
if (pass.first.find(this->ProcessOutput)) {
found = true;
reason = cmStrCat("Required regular expression found. Regex=[",
pass.second, ']');
break;
}
}
if (!found) {
reason = "Required regular expression not found. Regex=[";
for (auto& pass : this->TestProperties->RequiredRegularExpressions) {
reason += pass.second;
reason += "\n";
}
reason += "]";
forceFail = true;
}
}
if (!this->TestProperties->ErrorRegularExpressions.empty() &&
this->FailedDependencies.empty()) {
for (auto& fail : this->TestProperties->ErrorRegularExpressions) {
if (fail.first.find(this->ProcessOutput)) {
reason = cmStrCat("Error regular expression found in output. Regex=[",
fail.second, ']');
forceFail = true;
break;
}
}
}
if (!this->TestProperties->SkipRegularExpressions.empty() &&
this->FailedDependencies.empty()) {
for (auto& skip : this->TestProperties->SkipRegularExpressions) {
if (skip.first.find(this->ProcessOutput)) {
reason = cmStrCat("Skip regular expression found in output. Regex=[",
skip.second, ']');
forceSkip = true;
break;
}
}
}
std::string resourceSpecParseError;
if (!this->TestProperties->GeneratedResourceSpecFile.empty()) {
this->MultiTestHandler.ResourceSpecFile =
this->TestProperties->GeneratedResourceSpecFile;
if (!this->MultiTestHandler.InitResourceAllocator(
resourceSpecParseError)) {
reason = "Invalid resource spec file";
forceFail = true;
} else {
this->MultiTestHandler.CheckResourceAvailability();
}
}
std::ostringstream outputStream;
if (res == cmProcess::State::Exited) {
bool success = !forceFail &&
(retVal == 0 ||
!this->TestProperties->RequiredRegularExpressions.empty());
if ((this->TestProperties->SkipReturnCode >= 0 &&
this->TestProperties->SkipReturnCode == retVal) ||
forceSkip) {
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
std::ostringstream s;
if (forceSkip) {
s << "SKIP_REGULAR_EXPRESSION_MATCHED";
} else {
s << "SKIP_RETURN_CODE=" << this->TestProperties->SkipReturnCode;
}
this->TestResult.CompletionStatus = s.str();
outputStream << "***Skipped ";
skipped = true;
} else if (success != this->TestProperties->WillFail) {
this->TestResult.Status = cmCTestTestHandler::COMPLETED;
outputStream << " Passed ";
} else {
this->TestResult.Status = cmCTestTestHandler::FAILED;
outputStream << "***Failed " << reason;
outputTestErrorsToConsole =
this->CTest->GetOutputTestOutputOnTestFailure();
}
} else if (res == cmProcess::State::Expired) {
outputStream << "***Timeout ";
if (this->TestProperties->TimeoutSignal &&
this->TestProcess->GetTerminationStyle() ==
cmProcess::Termination::Custom) {
outputStream << "(" << this->TestProperties->TimeoutSignal->Name << ") ";
}
this->TestResult.Status = cmCTestTestHandler::TIMEOUT;
outputTestErrorsToConsole =
this->CTest->GetOutputTestOutputOnTestFailure();
} else if (res == cmProcess::State::Exception) {
outputTestErrorsToConsole =
this->CTest->GetOutputTestOutputOnTestFailure();
outputStream << "***Exception: ";
this->TestResult.ExceptionStatus =
this->TestProcess->GetExitExceptionString();
switch (this->TestProcess->GetExitException()) {
case cmProcess::Exception::Fault:
outputStream << "SegFault";
this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
break;
case cmProcess::Exception::Illegal:
outputStream << "Illegal";
this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
break;
case cmProcess::Exception::Interrupt:
outputStream << "Interrupt";
this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
break;
case cmProcess::Exception::Numerical:
outputStream << "Numerical";
this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
break;
default:
cmCTestLog(this->CTest, HANDLER_OUTPUT,
this->TestResult.ExceptionStatus);
this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
}
} else if ("Disabled" == this->TestResult.CompletionStatus) {
outputStream << "***Not Run (Disabled) ";
} else // cmProcess::State::Error
{
outputStream << "***Not Run ";
}
passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
char buf[1024];
snprintf(buf, sizeof(buf), "%6.2f sec",
this->TestProcess->GetTotalTime().count());
outputStream << buf << "\n";
bool passedOrSkipped = passed || skipped;
if (this->CTest->GetTestProgressOutput()) {
if (!passedOrSkipped) {
// If the test did not pass, reprint test name and error
std::string output = this->GetTestPrefix(completed, total);
std::string testName = this->TestProperties->Name;
int const maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
testName.resize(maxTestNameWidth + 4, '.');
output += testName;
output += outputStream.str();
outputStream.str("");
outputStream.clear();
outputStream << output;
cmCTestLog(this->CTest, HANDLER_TEST_PROGRESS_OUTPUT, "\n"); // flush
}
if (completed == total) {
std::string testName = this->GetTestPrefix(completed, total) +
this->TestProperties->Name + "\n";
cmCTestLog(this->CTest, HANDLER_TEST_PROGRESS_OUTPUT, testName);
}
}
if (!this->CTest->GetTestProgressOutput() || !passedOrSkipped) {
cmCTestLog(this->CTest, HANDLER_OUTPUT, outputStream.str());
}
if (outputTestErrorsToConsole) {
cmCTestLog(this->CTest, HANDLER_OUTPUT, this->ProcessOutput << std::endl);
}
if (!resourceSpecParseError.empty()) {
cmCTestLog(this->CTest, ERROR_MESSAGE,
resourceSpecParseError << std::endl);
} else if (!this->TestProperties->GeneratedResourceSpecFile.empty()) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Using generated resource spec file "
<< this->TestProperties->GeneratedResourceSpecFile
<< std::endl);
}
if (this->TestHandler->LogFile) {
*this->TestHandler->LogFile << "Test time = " << buf << std::endl;
}
this->ParseOutputForMeasurements();
// if this is doing MemCheck then all the output needs to be put into
// Output since that is what is parsed by cmCTestMemCheckHandler
if (!this->TestHandler->MemCheck && started) {
this->TestHandler->CleanTestOutput(
this->ProcessOutput,
static_cast<size_t>(this->TestResult.Status ==
cmCTestTestHandler::COMPLETED
? this->TestHandler->TestOptions.OutputSizePassed
: this->TestHandler->TestOptions.OutputSizeFailed),
this->TestHandler->TestOptions.OutputTruncation);
}
this->TestResult.Reason = reason;
if (this->TestHandler->LogFile) {
bool pass = true;
char const* reasonType = "Test Pass Reason";
if (this->TestResult.Status != cmCTestTestHandler::COMPLETED &&
this->TestResult.Status != cmCTestTestHandler::NOT_RUN) {
reasonType = "Test Fail Reason";
pass = false;
}
auto ttime = this->TestProcess->GetTotalTime();
auto hours = std::chrono::duration_cast<std::chrono::hours>(ttime);
ttime -= hours;
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(ttime);
ttime -= minutes;
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(ttime);
char buffer[100];
snprintf(buffer, sizeof(buffer), "%02u:%02u:%02u",
static_cast<unsigned>(hours.count()),
static_cast<unsigned>(minutes.count()),
static_cast<unsigned>(seconds.count()));
*this->TestHandler->LogFile
<< "----------------------------------------------------------"
<< std::endl;
if (!this->TestResult.Reason.empty()) {
*this->TestHandler->LogFile << reasonType << ":\n"
<< this->TestResult.Reason << "\n";
} else {
if (pass) {
*this->TestHandler->LogFile << "Test Passed.\n";
} else {
*this->TestHandler->LogFile << "Test Failed.\n";
}
}
*this->TestHandler->LogFile
<< "\"" << this->TestProperties->Name
<< "\" end time: " << this->CTest->CurrentTime() << std::endl
<< "\"" << this->TestProperties->Name << "\" time elapsed: " << buffer
<< std::endl
<< "----------------------------------------------------------"
<< std::endl
<< std::endl;
}
// if the test actually started and ran
// record the results in TestResult
if (started) {
std::string compressedOutput;
if (!this->TestHandler->MemCheck &&
this->CTest->ShouldCompressTestOutput()) {
std::string str = this->ProcessOutput;
if (this->CTest->CompressString(str)) {
compressedOutput = std::move(str);
}
}
bool compress = !compressedOutput.empty() &&
compressedOutput.length() < this->ProcessOutput.length();
this->TestResult.Output =
compress ? compressedOutput : this->ProcessOutput;
this->TestResult.CompressOutput = compress;
this->TestResult.ReturnValue = this->TestProcess->GetExitValue();
if (!skipped) {
this->TestResult.CompletionStatus = "Completed";
}
this->TestResult.ExecutionTime = this->TestProcess->GetTotalTime();
this->MemCheckPostProcess();
this->ComputeWeightedCost();
}
// If the test does not need to rerun push the current TestResult onto the
// TestHandler vector
if (!this->NeedsToRepeat()) {
this->TestHandler->TestResults.push_back(this->TestResult);
}
cmCTestRunTest::EndTestResult testResult;
testResult.Passed = passed || skipped;
if (res == cmProcess::State::Expired &&
this->TestProcess->GetTimeoutReason() ==
cmProcess::TimeoutReason::StopTime) {
testResult.StopTimePassed = true;
}
this->TestProcess.reset();
return testResult;
}
bool cmCTestRunTest::StartAgain(std::unique_ptr<cmCTestRunTest> runner,
size_t completed)
{
auto* testRun = runner.get();
if (!testRun->RunAgain) {
return false;
}
testRun->RunAgain = false; // reset
testRun->TestProcess = cm::make_unique<cmProcess>(std::move(runner));
// change to tests directory
cmWorkingDirectory workdir(testRun->TestProperties->Directory);
if (workdir.Failed()) {
testRun->StartFailure(testRun->TotalNumberOfTests, workdir.GetError(),
"Failed to change working directory");
return true;
}
testRun->StartTest(completed, testRun->TotalNumberOfTests);
return true;
}
bool cmCTestRunTest::NeedsToRepeat()
{
this->NumberOfRunsLeft--;
if (this->NumberOfRunsLeft == 0) {
return false;
}
// If a test is marked as NOT_RUN it will not be repeated
// no matter the repeat settings, so just record it as-is.
if (this->TestResult.Status == cmCTestTestHandler::NOT_RUN) {
return false;
}
// if number of runs left is not 0, and we are running until
// we find a failed (or passed) test, then return true so the test can be
// restarted
if ((this->RepeatMode == cmCTest::Repeat::UntilFail &&
this->TestResult.Status == cmCTestTestHandler::COMPLETED) ||
(this->RepeatMode == cmCTest::Repeat::UntilPass &&
this->TestResult.Status != cmCTestTestHandler::COMPLETED) ||
(this->RepeatMode == cmCTest::Repeat::AfterTimeout &&
this->TestResult.Status == cmCTestTestHandler::TIMEOUT)) {
this->RunAgain = true;
return true;
}
return false;
}
void cmCTestRunTest::ComputeWeightedCost()
{
double prev = static_cast<double>(this->TestProperties->PreviousRuns);
double avgcost = static_cast<double>(this->TestProperties->Cost);
double current = this->TestResult.ExecutionTime.count();
if (this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
this->TestProperties->Cost =
static_cast<float>(((prev * avgcost) + current) / (prev + 1.0));
this->TestProperties->PreviousRuns++;
}
}
void cmCTestRunTest::MemCheckPostProcess()
{
if (!this->TestHandler->MemCheck) {
return;
}
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": process test output now: "
<< this->TestProperties->Name << " "
<< this->TestResult.Name << std::endl,
this->TestHandler->GetQuiet());
cmCTestMemCheckHandler* handler =
static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
handler->PostProcessTest(this->TestResult, this->Index);
}
void cmCTestRunTest::StartFailure(std::unique_ptr<cmCTestRunTest> runner,
size_t total, std::string const& output,
std::string const& detail)
{
auto* testRun = runner.get();
testRun->TestProcess = cm::make_unique<cmProcess>(std::move(runner));
testRun->StartFailure(total, output, detail);
testRun->FinalizeTest(false);
}
void cmCTestRunTest::StartFailure(size_t total, std::string const& output,
std::string const& detail)
{
// Still need to log the Start message so the test summary records our
// attempt to start this test
if (!this->CTest->GetTestProgressOutput()) {
cmCTestLog(this->CTest, HANDLER_OUTPUT,
std::setw(2 * getNumWidth(total) + 8)
<< "Start "
<< std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
<< this->TestProperties->Index << ": "
<< this->TestProperties->Name << std::endl);
}
this->ProcessOutput.clear();
if (!output.empty()) {
*this->TestHandler->LogFile << output << std::endl;
cmCTestLog(this->CTest, ERROR_MESSAGE, output << std::endl);
}
this->TestResult.Properties = this->TestProperties;
this->TestResult.ExecutionTime = cmDuration::zero();
this->TestResult.CompressOutput = false;
this->TestResult.ReturnValue = -1;
this->TestResult.CompletionStatus = detail;
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
this->TestResult.TestCount = this->TestProperties->Index;
this->TestResult.Name = this->TestProperties->Name;
this->TestResult.Path = this->TestProperties->Directory;
this->TestResult.Output = output;
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
}
std::string cmCTestRunTest::GetTestPrefix(size_t completed, size_t total) const
{
std::ostringstream outputStream;
outputStream << std::setw(getNumWidth(total)) << completed << "/";
outputStream << std::setw(getNumWidth(total)) << total << " ";
if (this->TestHandler->MemCheck) {
outputStream << "MemCheck";
} else {
outputStream << "Test";
}
std::ostringstream indexStr;
indexStr << " #" << this->Index << ":";
outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
<< indexStr.str();
outputStream << " ";
return outputStream.str();
}
void cmCTestRunTest::StartTest(std::unique_ptr<cmCTestRunTest> runner,
size_t completed, size_t total)
{
auto* testRun = runner.get();
testRun->TestProcess = cm::make_unique<cmProcess>(std::move(runner));
if (!testRun->StartTest(completed, total)) {
testRun->FinalizeTest(false);
}
}
// Starts the execution of a test. Returns once it has started
bool cmCTestRunTest::StartTest(size_t completed, size_t total)
{
this->TotalNumberOfTests = total; // save for rerun case
if (!this->CTest->GetTestProgressOutput()) {
cmCTestLog(this->CTest, HANDLER_OUTPUT,
std::setw(2 * getNumWidth(total) + 8)
<< "Start "
<< std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
<< this->TestProperties->Index << ": "
<< this->TestProperties->Name << std::endl);
} else {
std::string testName = this->GetTestPrefix(completed, total) +
this->TestProperties->Name + "\n";
cmCTestLog(this->CTest, HANDLER_TEST_PROGRESS_OUTPUT, testName);
}
this->ProcessOutput.clear();
this->TestResult.Properties = this->TestProperties;
this->TestResult.ExecutionTime = cmDuration::zero();
this->TestResult.CompressOutput = false;
this->TestResult.ReturnValue = -1;
this->TestResult.TestCount = this->TestProperties->Index;
this->TestResult.Name = this->TestProperties->Name;
this->TestResult.Path = this->TestProperties->Directory;
// Reject invalid test properties.
if (this->TestProperties->Error) {
std::string const& msg = *this->TestProperties->Error;
*this->TestHandler->LogFile << msg << std::endl;
cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
this->TestResult.CompletionStatus = "Invalid Test Properties";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
this->TestResult.Output = msg;
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
return false;
}
// Return immediately if test is disabled
if (this->TestProperties->Disabled) {
this->TestResult.CompletionStatus = "Disabled";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
this->TestResult.Output = "Disabled";
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
return false;
}
this->TestResult.CompletionStatus = "Failed to start";
this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
// Check for failed fixture dependencies before we even look at the command
// arguments because if we are not going to run the test, the command and
// its arguments are irrelevant. This matters for the case where a fixture
// dependency might be creating the executable we want to run.
if (!this->FailedDependencies.empty()) {
std::string msg = "Failed test dependencies:";
for (std::string const& failedDep : this->FailedDependencies) {
msg += " " + failedDep;
}
*this->TestHandler->LogFile << msg << std::endl;
cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
this->TestResult.Output = msg;
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
this->TestResult.CompletionStatus = "Fixture dependency failed";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
return false;
}
this->ComputeArguments();
std::vector<std::string>& args = this->TestProperties->Args;
if (args.size() >= 2 && args[1] == "NOT_AVAILABLE") {
std::string msg;
if (this->CTest->GetConfigType().empty()) {
msg = "Test not available without configuration. (Missing \"-C "
"<config>\"?)";
} else {
msg = cmStrCat("Test not available in configuration \"",
this->CTest->GetConfigType(), "\".");
}
*this->TestHandler->LogFile << msg << std::endl;
cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
this->TestResult.Output = msg;
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
this->TestResult.CompletionStatus = "Missing Configuration";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
return false;
}
// Check if all required files exist
for (std::string const& file : this->TestProperties->RequiredFiles) {
if (!cmSystemTools::FileExists(file)) {
// Required file was not found
*this->TestHandler->LogFile << "Unable to find required file: " << file
<< std::endl;
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Unable to find required file: " << file << std::endl);
this->TestResult.Output = "Unable to find required file: " + file;
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
this->TestResult.CompletionStatus = "Required Files Missing";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
return false;
}
}
// log and return if we did not find the executable
if (this->ActualCommand.empty()) {
// if the command was not found create a TestResult object
// that has that information
*this->TestHandler->LogFile << "Unable to find executable: " << args[1]
<< std::endl;
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Unable to find executable: " << args[1] << std::endl);
this->TestResult.Output = "Unable to find executable: " + args[1];
this->TestResult.FullCommandLine.clear();
this->TestResult.Environment.clear();
this->TestResult.CompletionStatus = "Unable to find executable";
this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
return false;
}
this->StartTime = this->CTest->CurrentTime();
if (this->CTest->GetInstrumentation().HasQuery()) {
this->CTest->GetInstrumentation().GetPreTestStats();
}
return this->ForkProcess();
}
void cmCTestRunTest::ComputeArguments()
{
this->Arguments.clear(); // reset because this might be a rerun
auto j = this->TestProperties->Args.begin();
++j; // skip test name
// find the test executable
if (this->TestHandler->MemCheck) {
cmCTestMemCheckHandler* handler =
static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
this->ActualCommand = handler->MemoryTester;
this->TestProperties->Args[1] =
this->TestHandler->FindTheExecutable(this->TestProperties->Args[1]);
} else {
this->ActualCommand =
this->TestHandler->FindTheExecutable(this->TestProperties->Args[1]);
++j; // skip the executable (it will be actualCommand)
}
std::string testCommand =
cmSystemTools::ConvertToOutputPath(this->ActualCommand);
// Prepends memcheck args to our command string
this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
for (std::string const& arg : this->Arguments) {
testCommand += " \"";
testCommand += arg;
testCommand += "\"";
}
for (; j != this->TestProperties->Args.end(); ++j) {
testCommand += " \"";
testCommand += *j;
testCommand += "\"";
this->Arguments.push_back(*j);
}
this->TestResult.FullCommandLine = testCommand;
// Print the test command in verbose mode
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
std::endl
<< this->Index << ": "
<< (this->TestHandler->MemCheck ? "MemCheck" : "Test")
<< " command: " << testCommand << std::endl);
// Print any test-specific env vars in verbose mode
if (!this->TestProperties->Directory.empty()) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": "
<< "Working Directory: "
<< this->TestProperties->Directory << std::endl);
}
// Print any test-specific env vars in verbose mode
if (!this->TestProperties->Environment.empty()) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": "
<< "Environment variables: " << std::endl);
}
for (std::string const& env : this->TestProperties->Environment) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": " << env << std::endl);
}
if (!this->TestProperties->EnvironmentModification.empty()) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": "
<< "Environment variable modifications: "
<< std::endl);
}
for (std::string const& envmod :
this->TestProperties->EnvironmentModification) {
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": " << envmod << std::endl);
}
}
void cmCTestRunTest::ParseOutputForMeasurements()
{
if (!this->ProcessOutput.empty() &&
(this->ProcessOutput.find("<DartMeasurement") != std::string::npos ||
this->ProcessOutput.find("<CTestMeasurement") != std::string::npos)) {
if (this->TestHandler->AllTestMeasurementsRegex.find(
this->ProcessOutput)) {
this->TestResult.TestMeasurementsOutput =
this->TestHandler->AllTestMeasurementsRegex.match(1);
// keep searching and replacing until none are left
while (this->TestHandler->SingleTestMeasurementRegex.find(
this->ProcessOutput)) {
// replace the exact match for the string
cmSystemTools::ReplaceString(
this->ProcessOutput,
this->TestHandler->SingleTestMeasurementRegex.match(1).c_str(), "");
}
}
}
}
bool cmCTestRunTest::ForkProcess()
{
this->TestProcess->SetId(this->Index);
this->TestProcess->SetWorkingDirectory(this->TestProperties->Directory);
this->TestProcess->SetCommand(this->ActualCommand);
this->TestProcess->SetCommandArguments(this->Arguments);
cm::optional<cmDuration> timeout;
// Check TIMEOUT test property.
if (this->TestProperties->Timeout &&
*this->TestProperties->Timeout >= cmDuration::zero()) {
timeout = this->TestProperties->Timeout;
}
// An explicit TIMEOUT=0 test property means "no timeout".
if (timeout) {
if (*timeout == std::chrono::duration<double>::zero()) {
timeout = cm::nullopt;
}
} else {
// Check --timeout.
if (this->CTest->GetGlobalTimeout() > cmDuration::zero()) {
timeout = this->CTest->GetGlobalTimeout();
}
if (!timeout) {
// Check CTEST_TEST_TIMEOUT.
cmDuration ctestTestTimeout = this->CTest->GetTimeOut();
if (ctestTestTimeout > cmDuration::zero()) {
timeout = ctestTestTimeout;
}
}
}
// Check CTEST_TIME_LIMIT.
cmDuration timeRemaining = this->CTest->GetRemainingTimeAllowed();
if (timeRemaining != cmCTest::MaxDuration()) {
// This two minute buffer is historical.
timeRemaining -= std::chrono::minutes(2);
}
// Check --stop-time.
std::chrono::system_clock::time_point stop_time = this->CTest->GetStopTime();
if (stop_time != std::chrono::system_clock::time_point()) {
cmDuration timeUntilStop =
(stop_time - std::chrono::system_clock::now()) % std::chrono::hours(24);
this->TestProcess->SetStopTimeout(timeUntilStop);
}
// Enforce remaining time even over explicit TIMEOUT=0.
if (timeRemaining <= cmDuration::zero()) {
timeRemaining = cmDuration::zero();
}
if (!timeout || timeRemaining < *timeout) {
timeout = timeRemaining;
}
// Inform the test process of its normal timeout
if (timeout) {
this->TestProcess->SetTimeout(*timeout);
}
// Ask the test process which timeout is in effect.
if (auto ctimeout = this->TestProcess->GetComputedTimeout()) {
timeout = ctimeout->Duration;
}
if (timeout) {
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index << ": "
<< "Test timeout computed to be: "
<< cmDurationTo<unsigned int>(*timeout)
<< "\n",
this->TestHandler->GetQuiet());
this->TestProcess->SetTimeout(*timeout);
} else {
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
this->Index
<< ": "
<< "Test timeout suppressed by TIMEOUT property.\n",
this->TestHandler->GetQuiet());
}
cmSystemTools::SaveRestoreEnvironment sre;
std::ostringstream envMeasurement;
// We split processing ENVIRONMENT and ENVIRONMENT_MODIFICATION into two
// phases to ensure that MYVAR=reset: in the latter phase resets to the
// former phase's settings, rather than to the original environment.
if (!this->TestProperties->Environment.empty()) {
cmSystemTools::EnvDiff diff;
diff.AppendEnv(this->TestProperties->Environment);
diff.ApplyToCurrentEnv(&envMeasurement);
}
if (!this->TestProperties->EnvironmentModification.empty()) {
cmSystemTools::EnvDiff diff;
bool env_ok = true;
for (auto const& envmod : this->TestProperties->EnvironmentModification) {
env_ok &= diff.ParseOperation(envmod);
}
if (!env_ok) {
return false;
}
diff.ApplyToCurrentEnv(&envMeasurement);
}
if (this->UseAllocatedResources) {
std::vector<std::string> envLog;
this->SetupResourcesEnvironment(&envLog);
for (auto const& var : envLog) {
envMeasurement << var << std::endl;
}
} else {
cmSystemTools::UnsetEnv("CTEST_RESOURCE_GROUP_COUNT");
// Signify that this variable is being actively unset
envMeasurement << "#CTEST_RESOURCE_GROUP_COUNT=" << std::endl;
}
this->TestResult.Environment = envMeasurement.str();
// Remove last newline
this->TestResult.Environment.erase(this->TestResult.Environment.length() -
1);
return this->TestProcess->StartProcess(*this->MultiTestHandler.Loop,
&this->TestProperties->Affinity);
}
void cmCTestRunTest::SetupResourcesEnvironment(std::vector<std::string>* log)
{
std::string processCount =
cmStrCat("CTEST_RESOURCE_GROUP_COUNT=", this->AllocatedResources.size());
cmSystemTools::PutEnv(processCount);
if (log) {
log->emplace_back(std::move(processCount));
}
std::size_t i = 0;
for (auto const& process : this->AllocatedResources) {
std::string prefix = cmStrCat("CTEST_RESOURCE_GROUP_", i);
std::string resourceList = cmStrCat(prefix, '=');
prefix += '_';
bool firstType = true;
for (auto const& it : process) {
if (!firstType) {
resourceList += ',';
}
firstType = false;
auto resourceType = it.first;
resourceList += resourceType;
std::string var =
cmStrCat(prefix, cmSystemTools::UpperCase(resourceType), '=');
bool firstName = true;
for (auto const& it2 : it.second) {
if (!firstName) {
var += ';';
}
firstName = false;
var += cmStrCat("id:", it2.Id, ",slots:", it2.Slots);
}
cmSystemTools::PutEnv(var);
if (log) {
log->push_back(var);
}
}
cmSystemTools::PutEnv(resourceList);
if (log) {
log->push_back(resourceList);
}
++i;
}
}
void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
{
std::ostringstream outputStream;
// If this is the last or only run of this test, or progress output is
// requested, then print out completed / total.
// Only issue is if a test fails and we are running until fail
// then it will never print out the completed / total, same would
// got for run until pass. Trick is when this is called we don't
// yet know if we are passing or failing.
bool const progressOnLast =
(this->RepeatMode != cmCTest::Repeat::UntilPass &&
this->RepeatMode != cmCTest::Repeat::AfterTimeout);
if ((progressOnLast && this->NumberOfRunsLeft == 1) ||
(!progressOnLast && this->NumberOfRunsLeft == this->NumberOfRunsTotal) ||
this->CTest->GetTestProgressOutput()) {
outputStream << std::setw(getNumWidth(total)) << completed << "/";
outputStream << std::setw(getNumWidth(total)) << total << " ";
}
// if this is one of several runs of a test just print blank space
// to keep things neat
else {
outputStream << std::setw(getNumWidth(total)) << " ";
outputStream << std::setw(getNumWidth(total)) << " ";
}
if (this->TestHandler->MemCheck) {
outputStream << "MemCheck";
} else {
outputStream << "Test";
}
std::ostringstream indexStr;
indexStr << " #" << this->Index << ":";
outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
<< indexStr.str();
outputStream << " ";
int const maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
std::string outname = this->TestProperties->Name + " ";
outname.resize(maxTestNameWidth + 4, '.');
outputStream << outname;
*this->TestHandler->LogFile << this->TestProperties->Index << "/"
<< this->TestHandler->TotalNumberOfTests
<< " Testing: " << this->TestProperties->Name
<< std::endl;
*this->TestHandler->LogFile << this->TestProperties->Index << "/"
<< this->TestHandler->TotalNumberOfTests
<< " Test: " << this->TestProperties->Name
<< std::endl;
*this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
for (std::string const& arg : this->Arguments) {
*this->TestHandler->LogFile << " \"" << arg << "\"";
}
*this->TestHandler->LogFile
<< std::endl
<< "Directory: " << this->TestProperties->Directory << std::endl
<< "\"" << this->TestProperties->Name
<< "\" start time: " << this->StartTime << std::endl;
*this->TestHandler->LogFile
<< "Output:" << std::endl
<< "----------------------------------------------------------"
<< std::endl;
*this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
<< std::endl;
if (!this->CTest->GetTestProgressOutput()) {
cmCTestLog(this->CTest, HANDLER_OUTPUT, outputStream.str());
}
cmCTestLog(this->CTest, DEBUG,
"Testing " << this->TestProperties->Name << " ... ");
}
void cmCTestRunTest::FinalizeTest(bool started)
{
if (this->CTest->GetInstrumentation().HasQuery()) {
std::string data_file = this->CTest->GetInstrumentation().InstrumentTest(
this->TestProperties->Name, this->ActualCommand, this->Arguments,
this->TestProcess->GetExitValue(), this->TestProcess->GetStartTime(),
this->TestProcess->GetSystemStartTime(),
this->GetCTest()->GetConfigType());
this->TestResult.InstrumentationFile = data_file;
}
this->MultiTestHandler.FinishTestProcess(this->TestProcess->GetRunner(),
started);
}
|