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