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 // Google Test UnitTestOptions tests 31 // 32 // This file tests classes and functions used internally by 33 // Google Test. They are subject to change without notice. 34 // 35 // This file is #included from gtest.cc, to avoid changing build or 36 // make-files on Windows and other platforms. Do not #include this file 37 // anywhere else! 38 39 #include "gtest/gtest.h" 40 41 #if GTEST_OS_WINDOWS_MOBILE 42 # include <windows.h> 43 #elif GTEST_OS_WINDOWS 44 # include <direct.h> 45 #endif // GTEST_OS_WINDOWS_MOBILE 46 47 #include "src/gtest-internal-inl.h" 48 49 namespace testing { 50 namespace internal { 51 namespace { 52 53 // Turns the given relative path into an absolute path. 54 FilePath GetAbsolutePathOf(const FilePath& relative_path) { 55 return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path); 56 } 57 58 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile. 59 60 TEST(XmlOutputTest, GetOutputFormatDefault) { 61 GTEST_FLAG(output) = ""; 62 EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str()); 63 } 64 65 TEST(XmlOutputTest, GetOutputFormat) { 66 GTEST_FLAG(output) = "xml:filename"; 67 EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str()); 68 } 69 70 TEST(XmlOutputTest, GetOutputFileDefault) { 71 GTEST_FLAG(output) = ""; 72 EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(), 73 UnitTestOptions::GetAbsolutePathToOutputFile()); 74 } 75 76 TEST(XmlOutputTest, GetOutputFileSingleFile) { 77 GTEST_FLAG(output) = "xml:filename.abc"; 78 EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(), 79 UnitTestOptions::GetAbsolutePathToOutputFile()); 80 } 81 82 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { 83 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_; 84 const std::string expected_output_file = 85 GetAbsolutePathOf( 86 FilePath(std::string("path") + GTEST_PATH_SEP_ + 87 GetCurrentExecutableName().string() + ".xml")).string(); 88 const std::string& output_file = 89 UnitTestOptions::GetAbsolutePathToOutputFile(); 90 #if GTEST_OS_WINDOWS 91 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); 92 #else 93 EXPECT_EQ(expected_output_file, output_file.c_str()); 94 #endif 95 } 96 97 TEST(OutputFileHelpersTest, GetCurrentExecutableName) { 98 const std::string exe_str = GetCurrentExecutableName().string(); 99 #if GTEST_OS_WINDOWS 100 const bool success = 101 _strcmpi("googletest-options-test", exe_str.c_str()) == 0 || 102 _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 || 103 _strcmpi("gtest_all_test", exe_str.c_str()) == 0 || 104 _strcmpi("gtest_dll_test", exe_str.c_str()) == 0; 105 #elif GTEST_OS_FUCHSIA 106 const bool success = exe_str == "app"; 107 #else 108 // FIXME: remove the hard-coded "lt-" prefix when libtool replacement is ready 109 const bool success = 110 exe_str == "googletest-options-test" || 111 exe_str == "gtest_all_test" || 112 exe_str == "lt-gtest_all_test" || 113 exe_str == "gtest_dll_test"; 114 #endif // GTEST_OS_WINDOWS 115 if (!success) 116 FAIL() << "GetCurrentExecutableName() returns " << exe_str; 117 } 118 119 #if !GTEST_OS_FUCHSIA 120 121 class XmlOutputChangeDirTest : public Test { 122 protected: 123 virtual void SetUp() { 124 original_working_dir_ = FilePath::GetCurrentDir(); 125 posix::ChDir(".."); 126 // This will make the test fail if run from the root directory. 127 EXPECT_NE(original_working_dir_.string(), 128 FilePath::GetCurrentDir().string()); 129 } 130 131 virtual void TearDown() { 132 posix::ChDir(original_working_dir_.string().c_str()); 133 } 134 135 FilePath original_working_dir_; 136 }; 137 138 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) { 139 GTEST_FLAG(output) = ""; 140 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_, 141 FilePath("test_detail.xml")).string(), 142 UnitTestOptions::GetAbsolutePathToOutputFile()); 143 } 144 145 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) { 146 GTEST_FLAG(output) = "xml"; 147 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_, 148 FilePath("test_detail.xml")).string(), 149 UnitTestOptions::GetAbsolutePathToOutputFile()); 150 } 151 152 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) { 153 GTEST_FLAG(output) = "xml:filename.abc"; 154 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_, 155 FilePath("filename.abc")).string(), 156 UnitTestOptions::GetAbsolutePathToOutputFile()); 157 } 158 159 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { 160 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_; 161 const std::string expected_output_file = 162 FilePath::ConcatPaths( 163 original_working_dir_, 164 FilePath(std::string("path") + GTEST_PATH_SEP_ + 165 GetCurrentExecutableName().string() + ".xml")).string(); 166 const std::string& output_file = 167 UnitTestOptions::GetAbsolutePathToOutputFile(); 168 #if GTEST_OS_WINDOWS 169 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); 170 #else 171 EXPECT_EQ(expected_output_file, output_file.c_str()); 172 #endif 173 } 174 175 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) { 176 #if GTEST_OS_WINDOWS 177 GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc"; 178 EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(), 179 UnitTestOptions::GetAbsolutePathToOutputFile()); 180 #else 181 GTEST_FLAG(output) ="xml:/tmp/filename.abc"; 182 EXPECT_EQ(FilePath("/tmp/filename.abc").string(), 183 UnitTestOptions::GetAbsolutePathToOutputFile()); 184 #endif 185 } 186 187 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) { 188 #if GTEST_OS_WINDOWS 189 const std::string path = "c:\\tmp\\"; 190 #else 191 const std::string path = "/tmp/"; 192 #endif 193 194 GTEST_FLAG(output) = "xml:" + path; 195 const std::string expected_output_file = 196 path + GetCurrentExecutableName().string() + ".xml"; 197 const std::string& output_file = 198 UnitTestOptions::GetAbsolutePathToOutputFile(); 199 200 #if GTEST_OS_WINDOWS 201 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); 202 #else 203 EXPECT_EQ(expected_output_file, output_file.c_str()); 204 #endif 205 } 206 207 #endif // !GTEST_OS_FUCHSIA 208 209 } // namespace 210 } // namespace internal 211 } // namespace testing 212