Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036
00037 #include "gtest/gtest.h"
00038
00039 using ::testing::InitGoogleTest;
00040 using ::testing::Test;
00041 using ::testing::internal::posix::GetEnv;
00042 using ::testing::internal::posix::Stat;
00043 using ::testing::internal::posix::StatStruct;
00044
00045 namespace {
00046
00047
00048
00049 const bool kTestPrematureExitFileEnvVarShouldBeSet = false;
00050
00051 class PrematureExitTest : public Test {
00052 public:
00053
00054 static bool FileExists(const char* filepath) {
00055 StatStruct stat;
00056 return Stat(filepath, &stat) == 0;
00057 }
00058
00059 protected:
00060 PrematureExitTest() {
00061 premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
00062
00063
00064 if (premature_exit_file_path_ == NULL) {
00065 premature_exit_file_path_ = "";
00066 }
00067 }
00068
00069
00070 bool PrematureExitFileExists() const {
00071 return FileExists(premature_exit_file_path_);
00072 }
00073
00074 const char* premature_exit_file_path_;
00075 };
00076
00077 typedef PrematureExitTest PrematureExitDeathTest;
00078
00079
00080
00081
00082
00083
00084 TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
00085 if (*premature_exit_file_path_ == '\0') {
00086 return;
00087 }
00088
00089 EXPECT_DEATH_IF_SUPPORTED({
00090
00091
00092
00093
00094 if (PrematureExitFileExists()) {
00095 exit(1);
00096 }
00097 }, "");
00098 }
00099
00100
00101
00102 TEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
00103 GTEST_INTENTIONAL_CONST_COND_PUSH_()
00104 if (kTestPrematureExitFileEnvVarShouldBeSet) {
00105 GTEST_INTENTIONAL_CONST_COND_POP_()
00106 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00107 ASSERT_TRUE(filepath != NULL);
00108 ASSERT_NE(*filepath, '\0');
00109 }
00110 }
00111
00112
00113
00114 TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
00115 if (*premature_exit_file_path_ == '\0') {
00116 return;
00117 }
00118
00119 EXPECT_TRUE(PrematureExitFileExists())
00120 << " file " << premature_exit_file_path_
00121 << " should exist during test execution, but doesn't.";
00122 }
00123
00124 }
00125
00126 int main(int argc, char **argv) {
00127 InitGoogleTest(&argc, argv);
00128 const int exit_code = RUN_ALL_TESTS();
00129
00130
00131
00132 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00133 if (filepath != NULL && *filepath != '\0') {
00134 if (PrematureExitTest::FileExists(filepath)) {
00135 printf(
00136 "File %s shouldn't exist after the test program finishes, but does.",
00137 filepath);
00138 return 1;
00139 }
00140 }
00141
00142 return exit_code;
00143 }