llvm-project/libc/utils/testutils/ExecuteFunctionUnix.cpp
Alex Brachet 0368997402 [libc] [UnitTest] Create death tests
Summary: This patch adds `EXPECT_EXITS` and `EXPECT_DEATH` macros for testing exit codes and deadly signals. They are less convoluted than their analogs in GTEST and don't have matchers but just take an int for either the exit code or the signal respectively. Nor do they have any regex match against the stdout/stderr of the child process.

Reviewers: sivachandra, gchatelet

Reviewed By: sivachandra

Subscribers: mgorny, MaskRay, tschuett, libc-commits

Differential Revision: https://reviews.llvm.org/D74665
2020-02-24 17:53:43 -05:00

53 lines
1.3 KiB
C++

//===------- ExecuteFunction implementation for Unix-like Systems ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ExecuteFunction.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdlib>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
namespace __llvm_libc {
namespace testutils {
bool ProcessStatus::exitedNormally() { return WIFEXITED(PlatformDefined); }
int ProcessStatus::getExitCode() {
assert(exitedNormally() && "Abnormal termination, no exit code");
return WEXITSTATUS(PlatformDefined);
}
int ProcessStatus::getFatalSignal() {
if (exitedNormally())
return 0;
return WTERMSIG(PlatformDefined);
}
ProcessStatus invokeInSubprocess(FunctionCaller *Func) {
// Don't copy the buffers into the child process and print twice.
llvm::outs().flush();
llvm::errs().flush();
pid_t Pid = ::fork();
if (!Pid) {
(*Func)();
std::exit(0);
}
int WStatus;
::waitpid(Pid, &WStatus, 0);
delete Func;
return {WStatus};
}
const char *signalAsString(int Signum) { return ::strsignal(Signum); }
} // namespace testutils
} // namespace __llvm_libc