Skip to content

Commit

Permalink
add EXPECT_NO_COREFILE_DEATH
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouronghua committed Oct 21, 2024
1 parent 35646c5 commit 4818b9a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions googletest/include/gtest/gtest-death-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ GTEST_API_ bool InDeathTestChild();
#define EXPECT_DEATH(statement, matcher) \
EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)

// check if it is a child of ExceptionTest
#define EXPECT_NO_COREFILE_DEATH(statement, matcher) \
do { \
if (dynamic_cast<::testing::ExceptionTest *>(this)) { \
EXPECT_DEATH(statement, matcher); \
} else { \
EXPECT_TRUE(false) << "EXPECT_NO_COREFILE_DEATH can only be used in " \
"::testing::ExceptionTest and its subclass"; \
} \
} while (0)

// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:

// Tests that an exit code describes a normal exit with a given exit code.
Expand Down
41 changes: 41 additions & 0 deletions googletest/include/gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_

#include <sys/resource.h>

#include <cstddef>
#include <cstdint>
#include <limits>
Expand Down Expand Up @@ -360,6 +362,45 @@ class GTEST_API_ Test {
Test& operator=(const Test&) = delete;
};

// The class is a helpler to implements a test suite without core file
// generated.
class ExceptionTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
struct rlimit rl;
rl.rlim_cur = 0;
rl.rlim_max = 0;
// store old value of core dump file size
if (getrlimit(RLIMIT_CORE, &rl_old_) == -1) {
exit(EXIT_FAILURE);
}

// do not generate core dump file, we know it will fail
if (setrlimit(RLIMIT_CORE, &rl) == -1) {
exit(EXIT_FAILURE);
} else {
GTEST_LOG_(INFO) << "set RLIMIT_CORE success";
GTEST_LOG_(INFO) << "rl_old_ rlim_cur: %d" << rl_old_.rlim_cur;
GTEST_LOG_(INFO) << "rl_old_ rlim_max: %d" << rl_old_.rlim_max;
}
}
static void TearDownTestSuite() {
// do not generate core dump file, we know it will fail
if (setrlimit(RLIMIT_CORE, &rl_old_) == -1) {
exit(EXIT_FAILURE);
} else {
GTEST_LOG_(INFO) << "restore RLIMIT_CORE success";
}
}
void SetUp() override {}

void TearDown() override {}

private:
static struct rlimit rl_old_;
};
struct rlimit ExceptionTest::rl_old_ = {0, 0};

typedef internal::TimeInMillis TimeInMillis;

// A copyable object representing a user specified test property which can be
Expand Down

0 comments on commit 4818b9a

Please sign in to comment.