123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include <stdio.h>
- #include "gtest/gtest.h"
- using ::testing::InitGoogleTest;
- using ::testing::Test;
- using ::testing::internal::posix::GetEnv;
- using ::testing::internal::posix::Stat;
- using ::testing::internal::posix::StatStruct;
- namespace {
- const bool kTestPrematureExitFileEnvVarShouldBeSet = false;
- class PrematureExitTest : public Test {
- public:
-
- static bool FileExists(const char* filepath) {
- StatStruct stat;
- return Stat(filepath, &stat) == 0;
- }
- protected:
- PrematureExitTest() {
- premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
-
- if (premature_exit_file_path_ == NULL) {
- premature_exit_file_path_ = "";
- }
- }
-
- bool PrematureExitFileExists() const {
- return FileExists(premature_exit_file_path_);
- }
- const char* premature_exit_file_path_;
- };
- typedef PrematureExitTest PrematureExitDeathTest;
- TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
- if (*premature_exit_file_path_ == '\0') {
- return;
- }
- EXPECT_DEATH_IF_SUPPORTED({
-
-
-
-
- if (PrematureExitFileExists()) {
- exit(1);
- }
- }, "");
- }
- TEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
- if (kTestPrematureExitFileEnvVarShouldBeSet) {
- const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
- ASSERT_TRUE(filepath != NULL);
- ASSERT_NE(*filepath, '\0');
- }
- }
- TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
- if (*premature_exit_file_path_ == '\0') {
- return;
- }
- EXPECT_TRUE(PrematureExitFileExists())
- << " file " << premature_exit_file_path_
- << " should exist during test execution, but doesn't.";
- }
- }
- int main(int argc, char **argv) {
- InitGoogleTest(&argc, argv);
- const int exit_code = RUN_ALL_TESTS();
-
-
- const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
- if (filepath != NULL && *filepath != '\0') {
- if (PrematureExitTest::FileExists(filepath)) {
- printf(
- "File %s shouldn't exist after the test program finishes, but does.",
- filepath);
- return 1;
- }
- }
- return exit_code;
- }
|