1 // Copyright 2008, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Authors: keith.ray (at) gmail.com (Keith Ray) 31 // 32 // Google Test UnitTestOptions tests 33 // 34 // This file tests classes and functions used internally by 35 // Google Test. They are subject to change without notice. 36 // 37 // This file is #included from gtest.cc, to avoid changing build or 38 // make-files on Windows and other platforms. Do not #include this file 39 // anywhere else! 40 41 #include <gtest/gtest.h> 42 43 #ifdef _WIN32_WCE 44 #include <windows.h> 45 #elif GTEST_OS_WINDOWS 46 #include <direct.h> 47 #endif // _WIN32_WCE 48 49 // Indicates that this translation unit is part of Google Test's 50 // implementation. It must come before gtest-internal-inl.h is 51 // included, or there will be a compiler error. This trick is to 52 // prevent a user from accidentally including gtest-internal-inl.h in 53 // his code. 54 #define GTEST_IMPLEMENTATION_ 1 55 #include "src/gtest-internal-inl.h" 56 #undef GTEST_IMPLEMENTATION_ 57 58 namespace testing { 59 namespace internal { 60 namespace { 61 62 // Turns the given relative path into an absolute path. 63 FilePath GetAbsolutePathOf(const FilePath& relative_path) { 64 return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path); 65 } 66 67 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile. 68 69 TEST(XmlOutputTest, GetOutputFormatDefault) { 70 GTEST_FLAG(output) = ""; 71 EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str()); 72 } 73 74 TEST(XmlOutputTest, GetOutputFormat) { 75 GTEST_FLAG(output) = "xml:filename"; 76 EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str()); 77 } 78 79 TEST(XmlOutputTest, GetOutputFileDefault) { 80 GTEST_FLAG(output) = ""; 81 EXPECT_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(), 82 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 83 } 84 85 TEST(XmlOutputTest, GetOutputFileSingleFile) { 86 GTEST_FLAG(output) = "xml:filename.abc"; 87 EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(), 88 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 89 } 90 91 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { 92 #if GTEST_OS_WINDOWS 93 GTEST_FLAG(output) = "xml:path\\"; 94 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 95 EXPECT_TRUE( 96 _strcmpi(output_file.c_str(), 97 GetAbsolutePathOf( 98 FilePath("path\\gtest-options_test.xml")).c_str()) == 0 || 99 _strcmpi(output_file.c_str(), 100 GetAbsolutePathOf( 101 FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 || 102 _strcmpi(output_file.c_str(), 103 GetAbsolutePathOf( 104 FilePath("path\\gtest_all_test.xml")).c_str()) == 0) 105 << " output_file = " << output_file; 106 #else 107 GTEST_FLAG(output) = "xml:path/"; 108 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 109 // TODO(wan (at) google.com): libtool causes the test binary file to be 110 // named lt-gtest-options_test. Therefore the output file may be 111 // named .../lt-gtest-options_test.xml. We should remove this 112 // hard-coded logic when Chandler Carruth's libtool replacement is 113 // ready. 114 EXPECT_TRUE(output_file == 115 GetAbsolutePathOf( 116 FilePath("path/gtest-options_test.xml")).c_str() || 117 output_file == 118 GetAbsolutePathOf( 119 FilePath("path/lt-gtest-options_test.xml")).c_str() || 120 output_file == 121 GetAbsolutePathOf( 122 FilePath("path/gtest_all_test.xml")).c_str() || 123 output_file == 124 GetAbsolutePathOf( 125 FilePath("path/lt-gtest_all_test.xml")).c_str()) 126 << " output_file = " << output_file; 127 #endif 128 } 129 130 TEST(OutputFileHelpersTest, GetCurrentExecutableName) { 131 const FilePath executable = GetCurrentExecutableName(); 132 const char* const exe_str = executable.c_str(); 133 #if defined(_WIN32_WCE) || GTEST_OS_WINDOWS 134 ASSERT_TRUE(_strcmpi("gtest-options_test", exe_str) == 0 || 135 _strcmpi("gtest-options-ex_test", exe_str) == 0 || 136 _strcmpi("gtest_all_test", exe_str) == 0) 137 << "GetCurrentExecutableName() returns " << exe_str; 138 #else 139 // TODO(wan (at) google.com): remove the hard-coded "lt-" prefix when 140 // Chandler Carruth's libtool replacement is ready. 141 EXPECT_TRUE(String(exe_str) == "gtest-options_test" || 142 String(exe_str) == "lt-gtest-options_test" || 143 String(exe_str) == "gtest_all_test" || 144 String(exe_str) == "lt-gtest_all_test") 145 << "GetCurrentExecutableName() returns " << exe_str; 146 #endif 147 } 148 149 class XmlOutputChangeDirTest : public Test { 150 protected: 151 virtual void SetUp() { 152 original_working_dir_ = FilePath::GetCurrentDir(); 153 ChDir(".."); 154 // This will make the test fail if run from the root directory. 155 EXPECT_STRNE(original_working_dir_.c_str(), 156 FilePath::GetCurrentDir().c_str()); 157 } 158 159 virtual void TearDown() { 160 ChDir(original_working_dir_.c_str()); 161 } 162 163 void ChDir(const char* dir) { 164 #if GTEST_OS_WINDOWS 165 _chdir(dir); 166 #else 167 chdir(dir); 168 #endif 169 } 170 171 FilePath original_working_dir_; 172 }; 173 174 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) { 175 GTEST_FLAG(output) = ""; 176 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, 177 FilePath("test_detail.xml")).c_str(), 178 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 179 } 180 181 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) { 182 GTEST_FLAG(output) = "xml"; 183 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, 184 FilePath("test_detail.xml")).c_str(), 185 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 186 } 187 188 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) { 189 GTEST_FLAG(output) = "xml:filename.abc"; 190 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, 191 FilePath("filename.abc")).c_str(), 192 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 193 } 194 195 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { 196 #if GTEST_OS_WINDOWS 197 GTEST_FLAG(output) = "xml:path\\"; 198 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 199 EXPECT_TRUE( 200 _strcmpi(output_file.c_str(), 201 FilePath::ConcatPaths( 202 original_working_dir_, 203 FilePath("path\\gtest-options_test.xml")).c_str()) == 0 || 204 _strcmpi(output_file.c_str(), 205 FilePath::ConcatPaths( 206 original_working_dir_, 207 FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 || 208 _strcmpi(output_file.c_str(), 209 FilePath::ConcatPaths( 210 original_working_dir_, 211 FilePath("path\\gtest_all_test.xml")).c_str()) == 0) 212 << " output_file = " << output_file; 213 #else 214 GTEST_FLAG(output) = "xml:path/"; 215 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 216 // TODO(wan (at) google.com): libtool causes the test binary file to be 217 // named lt-gtest-options_test. Therefore the output file may be 218 // named .../lt-gtest-options_test.xml. We should remove this 219 // hard-coded logic when Chandler Carruth's libtool replacement is 220 // ready. 221 EXPECT_TRUE(output_file == FilePath::ConcatPaths(original_working_dir_, 222 FilePath("path/gtest-options_test.xml")).c_str() || 223 output_file == FilePath::ConcatPaths(original_working_dir_, 224 FilePath("path/lt-gtest-options_test.xml")).c_str() || 225 output_file == FilePath::ConcatPaths(original_working_dir_, 226 FilePath("path/gtest_all_test.xml")).c_str() || 227 output_file == FilePath::ConcatPaths(original_working_dir_, 228 FilePath("path/lt-gtest_all_test.xml")).c_str()) 229 << " output_file = " << output_file; 230 #endif 231 } 232 233 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) { 234 #if GTEST_OS_WINDOWS 235 GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc"; 236 EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(), 237 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 238 #else 239 GTEST_FLAG(output) ="xml:/tmp/filename.abc"; 240 EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(), 241 UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); 242 #endif 243 } 244 245 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) { 246 #if GTEST_OS_WINDOWS 247 GTEST_FLAG(output) = "xml:c:\\tmp\\"; 248 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 249 EXPECT_TRUE( 250 _strcmpi(output_file.c_str(), 251 FilePath("c:\\tmp\\gtest-options_test.xml").c_str()) == 0 || 252 _strcmpi(output_file.c_str(), 253 FilePath("c:\\tmp\\gtest-options-ex_test.xml").c_str()) == 0 || 254 _strcmpi(output_file.c_str(), 255 FilePath("c:\\tmp\\gtest_all_test.xml").c_str()) == 0) 256 << " output_file = " << output_file; 257 #else 258 GTEST_FLAG(output) = "xml:/tmp/"; 259 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); 260 // TODO(wan (at) google.com): libtool causes the test binary file to be 261 // named lt-gtest-options_test. Therefore the output file may be 262 // named .../lt-gtest-options_test.xml. We should remove this 263 // hard-coded logic when Chandler Carruth's libtool replacement is 264 // ready. 265 EXPECT_TRUE(output_file == "/tmp/gtest-options_test.xml" || 266 output_file == "/tmp/lt-gtest-options_test.xml" || 267 output_file == "/tmp/gtest_all_test.xml" || 268 output_file == "/tmp/lt-gtest_all_test.xml") 269 << " output_file = " << output_file; 270 #endif 271 } 272 273 } // namespace 274 } // namespace internal 275 } // namespace testing 276