googletest-options-test.cc 7.7 KB

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