googletest-options-test.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #ifdef GTEST_OS_WINDOWS_MOBILE
  41. #include <windows.h>
  42. #elif defined(GTEST_OS_WINDOWS)
  43. #include <direct.h>
  44. #elif defined(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. #ifdef 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. #ifdef 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 defined(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 defined(GTEST_OS_FUCHSIA)
  104. const bool success = exe_str == "app";
  105. #elif defined(__EMSCRIPTEN__)
  106. const bool success = exe_str == "patched_googletest-options-test.js";
  107. #else
  108. const bool success =
  109. exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
  110. exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
  111. #endif // platform ifdefs
  112. if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
  113. }
  114. #ifndef 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_SET(output, "");
  131. EXPECT_EQ(
  132. FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
  133. .string(),
  134. UnitTestOptions::GetAbsolutePathToOutputFile());
  135. }
  136. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
  137. GTEST_FLAG_SET(output, "xml");
  138. EXPECT_EQ(
  139. FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
  140. .string(),
  141. UnitTestOptions::GetAbsolutePathToOutputFile());
  142. }
  143. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
  144. GTEST_FLAG_SET(output, "xml:filename.abc");
  145. EXPECT_EQ(
  146. FilePath::ConcatPaths(original_working_dir_, FilePath("filename.abc"))
  147. .string(),
  148. UnitTestOptions::GetAbsolutePathToOutputFile());
  149. }
  150. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
  151. GTEST_FLAG_SET(output, "xml:path" GTEST_PATH_SEP_);
  152. const std::string expected_output_file =
  153. FilePath::ConcatPaths(
  154. original_working_dir_,
  155. FilePath(std::string("path") + GTEST_PATH_SEP_ +
  156. GetCurrentExecutableName().string() + ".xml"))
  157. .string();
  158. const std::string& output_file =
  159. UnitTestOptions::GetAbsolutePathToOutputFile();
  160. #ifdef GTEST_OS_WINDOWS
  161. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  162. #else
  163. EXPECT_EQ(expected_output_file, output_file.c_str());
  164. #endif
  165. }
  166. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
  167. #ifdef GTEST_OS_WINDOWS
  168. GTEST_FLAG_SET(output, "xml:c:\\tmp\\filename.abc");
  169. EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
  170. UnitTestOptions::GetAbsolutePathToOutputFile());
  171. #else
  172. GTEST_FLAG_SET(output, "xml:/tmp/filename.abc");
  173. EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
  174. UnitTestOptions::GetAbsolutePathToOutputFile());
  175. #endif
  176. }
  177. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
  178. #ifdef GTEST_OS_WINDOWS
  179. const std::string path = "c:\\tmp\\";
  180. #else
  181. const std::string path = "/tmp/";
  182. #endif
  183. GTEST_FLAG_SET(output, "xml:" + path);
  184. const std::string expected_output_file =
  185. path + GetCurrentExecutableName().string() + ".xml";
  186. const std::string& output_file =
  187. UnitTestOptions::GetAbsolutePathToOutputFile();
  188. #ifdef GTEST_OS_WINDOWS
  189. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  190. #else
  191. EXPECT_EQ(expected_output_file, output_file.c_str());
  192. #endif
  193. }
  194. #endif // !GTEST_OS_FUCHSIA
  195. } // namespace
  196. } // namespace internal
  197. } // namespace testing