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 if (kTestPrematureExitFileEnvVarShouldBeSet) {
00104 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00105 ASSERT_TRUE(filepath != NULL);
00106 ASSERT_NE(*filepath, '\0');
00107 }
00108 }
00109
00110
00111
00112 TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
00113 if (*premature_exit_file_path_ == '\0') {
00114 return;
00115 }
00116
00117 EXPECT_TRUE(PrematureExitFileExists())
00118 << " file " << premature_exit_file_path_
00119 << " should exist during test execution, but doesn't.";
00120 }
00121
00122 }
00123
00124 int main(int argc, char **argv) {
00125 InitGoogleTest(&argc, argv);
00126 const int exit_code = RUN_ALL_TESTS();
00127
00128
00129
00130 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00131 if (filepath != NULL && *filepath != '\0') {
00132 if (PrematureExitTest::FileExists(filepath)) {
00133 printf(
00134 "File %s shouldn't exist after the test program finishes, but does.",
00135 filepath);
00136 return 1;
00137 }
00138 }
00139
00140 return exit_code;
00141 }