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 #else
119 const bool success =
120 exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
121 exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
122 #endif // GTEST_OS_WINDOWS
123 if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
124 }
125
126 #ifndef GTEST_OS_FUCHSIA
127
128 class XmlOutputChangeDirTest : public Test {
129 protected:
SetUp()130 void SetUp() override {
131 original_working_dir_ = FilePath::GetCurrentDir();
132 posix::ChDir("..");
133 // This will make the test fail if run from the root directory.
134 EXPECT_NE(original_working_dir_.string(),
135 FilePath::GetCurrentDir().string());
136 }
137
TearDown()138 void TearDown() override {
139 posix::ChDir(original_working_dir_.string().c_str());
140 }
141
142 FilePath original_working_dir_;
143 };
144
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithDefault)145 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
146 GTEST_FLAG_SET(output, "");
147 EXPECT_EQ(
148 FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
149 .string(),
150 UnitTestOptions::GetAbsolutePathToOutputFile());
151 }
152
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithDefaultXML)153 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
154 GTEST_FLAG_SET(output, "xml");
155 EXPECT_EQ(
156 FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
157 .string(),
158 UnitTestOptions::GetAbsolutePathToOutputFile());
159 }
160
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithRelativeFile)161 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
162 GTEST_FLAG_SET(output, "xml:filename.abc");
163 EXPECT_EQ(
164 FilePath::ConcatPaths(original_working_dir_, FilePath("filename.abc"))
165 .string(),
166 UnitTestOptions::GetAbsolutePathToOutputFile());
167 }
168
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithRelativePath)169 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
170 GTEST_FLAG_SET(output, "xml:path" GTEST_PATH_SEP_);
171 const std::string expected_output_file =
172 FilePath::ConcatPaths(
173 original_working_dir_,
174 FilePath(std::string("path") + GTEST_PATH_SEP_ +
175 GetCurrentExecutableName().string() + ".xml"))
176 .string();
177 const std::string& output_file =
178 UnitTestOptions::GetAbsolutePathToOutputFile();
179 #ifdef GTEST_OS_WINDOWS
180 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
181 #else
182 EXPECT_EQ(expected_output_file, output_file.c_str());
183 #endif
184 }
185
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithAbsoluteFile)186 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
187 #ifdef GTEST_OS_WINDOWS
188 GTEST_FLAG_SET(output, "xml:c:\\tmp\\filename.abc");
189 EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
190 UnitTestOptions::GetAbsolutePathToOutputFile());
191 #else
192 GTEST_FLAG_SET(output, "xml:/tmp/filename.abc");
193 EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
194 UnitTestOptions::GetAbsolutePathToOutputFile());
195 #endif
196 }
197
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithAbsolutePath)198 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
199 #ifdef GTEST_OS_WINDOWS
200 const std::string path = "c:\\tmp\\";
201 #else
202 const std::string path = "/tmp/";
203 #endif
204
205 GTEST_FLAG_SET(output, "xml:" + path);
206 const std::string expected_output_file =
207 path + GetCurrentExecutableName().string() + ".xml";
208 const std::string& output_file =
209 UnitTestOptions::GetAbsolutePathToOutputFile();
210
211 #ifdef GTEST_OS_WINDOWS
212 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
213 #else
214 EXPECT_EQ(expected_output_file, output_file.c_str());
215 #endif
216 }
217
218 #endif // !GTEST_OS_FUCHSIA
219
220 } // namespace
221 } // namespace internal
222 } // namespace testing
223