googletest-options-test.cc 7.5 KB

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