googletest-options-test.cc 7.7 KB

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