• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Authors: keith.ray@gmail.com (Keith Ray)
31 //
32 // Google Test UnitTestOptions tests
33 //
34 // This file tests classes and functions used internally by
35 // Google Test.  They are subject to change without notice.
36 //
37 // This file is #included from gtest.cc, to avoid changing build or
38 // make-files on Windows and other platforms. Do not #include this file
39 // anywhere else!
40 
41 #include <gtest/gtest.h>
42 
43 #if GTEST_OS_WINDOWS_MOBILE
44 #include <windows.h>
45 #elif GTEST_OS_WINDOWS
46 #include <direct.h>
47 #endif  // GTEST_OS_WINDOWS_MOBILE
48 
49 // Indicates that this translation unit is part of Google Test's
50 // implementation.  It must come before gtest-internal-inl.h is
51 // included, or there will be a compiler error.  This trick is to
52 // prevent a user from accidentally including gtest-internal-inl.h in
53 // his code.
54 #define GTEST_IMPLEMENTATION_ 1
55 #include "src/gtest-internal-inl.h"
56 #undef GTEST_IMPLEMENTATION_
57 
58 namespace testing {
59 namespace internal {
60 namespace {
61 
62 // Turns the given relative path into an absolute path.
GetAbsolutePathOf(const FilePath & relative_path)63 FilePath GetAbsolutePathOf(const FilePath& relative_path) {
64   return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
65 }
66 
67 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
68 
TEST(XmlOutputTest,GetOutputFormatDefault)69 TEST(XmlOutputTest, GetOutputFormatDefault) {
70   GTEST_FLAG(output) = "";
71   EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
72 }
73 
TEST(XmlOutputTest,GetOutputFormat)74 TEST(XmlOutputTest, GetOutputFormat) {
75   GTEST_FLAG(output) = "xml:filename";
76   EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
77 }
78 
TEST(XmlOutputTest,GetOutputFileDefault)79 TEST(XmlOutputTest, GetOutputFileDefault) {
80   GTEST_FLAG(output) = "";
81   EXPECT_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(),
82                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
83 }
84 
TEST(XmlOutputTest,GetOutputFileSingleFile)85 TEST(XmlOutputTest, GetOutputFileSingleFile) {
86   GTEST_FLAG(output) = "xml:filename.abc";
87   EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(),
88                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
89 }
90 
TEST(XmlOutputTest,GetOutputFileFromDirectoryPath)91 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
92 #if GTEST_OS_WINDOWS
93   GTEST_FLAG(output) = "xml:path\\";
94   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
95   EXPECT_TRUE(
96       _strcmpi(output_file.c_str(),
97                GetAbsolutePathOf(
98                    FilePath("path\\gtest-options_test.xml")).c_str()) == 0 ||
99       _strcmpi(output_file.c_str(),
100                GetAbsolutePathOf(
101                    FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 ||
102       _strcmpi(output_file.c_str(),
103                GetAbsolutePathOf(
104                    FilePath("path\\gtest_all_test.xml")).c_str()) == 0)
105                        << " output_file = " << output_file;
106 #else
107   GTEST_FLAG(output) = "xml:path/";
108   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
109   // TODO(wan@google.com): libtool causes the test binary file to be
110   //   named lt-gtest-options_test.  Therefore the output file may be
111   //   named .../lt-gtest-options_test.xml.  We should remove this
112   //   hard-coded logic when Chandler Carruth's libtool replacement is
113   //   ready.
114   EXPECT_TRUE(output_file ==
115               GetAbsolutePathOf(
116                   FilePath("path/gtest-options_test.xml")).c_str() ||
117               output_file ==
118               GetAbsolutePathOf(
119                   FilePath("path/lt-gtest-options_test.xml")).c_str() ||
120               output_file ==
121               GetAbsolutePathOf(
122                   FilePath("path/gtest_all_test.xml")).c_str() ||
123               output_file ==
124               GetAbsolutePathOf(
125                   FilePath("path/lt-gtest_all_test.xml")).c_str())
126                       << " output_file = " << output_file;
127 #endif
128 }
129 
TEST(OutputFileHelpersTest,GetCurrentExecutableName)130 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
131   const FilePath executable = GetCurrentExecutableName();
132   const char* const exe_str = executable.c_str();
133 #if GTEST_OS_WINDOWS
134   ASSERT_TRUE(_strcmpi("gtest-options_test", exe_str) == 0 ||
135               _strcmpi("gtest-options-ex_test", exe_str) == 0 ||
136               _strcmpi("gtest_all_test", exe_str) == 0)
137               << "GetCurrentExecutableName() returns " << exe_str;
138 #else
139   // TODO(wan@google.com): remove the hard-coded "lt-" prefix when
140   //   Chandler Carruth's libtool replacement is ready.
141   EXPECT_TRUE(String(exe_str) == "gtest-options_test" ||
142               String(exe_str) == "lt-gtest-options_test" ||
143               String(exe_str) == "gtest_all_test" ||
144               String(exe_str) == "lt-gtest_all_test")
145                   << "GetCurrentExecutableName() returns " << exe_str;
146 #endif  // GTEST_OS_WINDOWS
147 }
148 
149 class XmlOutputChangeDirTest : public Test {
150  protected:
SetUp()151   virtual void SetUp() {
152     original_working_dir_ = FilePath::GetCurrentDir();
153     posix::ChDir("..");
154     // This will make the test fail if run from the root directory.
155     EXPECT_STRNE(original_working_dir_.c_str(),
156                  FilePath::GetCurrentDir().c_str());
157   }
158 
TearDown()159   virtual void TearDown() {
160     posix::ChDir(original_working_dir_.c_str());
161   }
162 
163   FilePath original_working_dir_;
164 };
165 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithDefault)166 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
167   GTEST_FLAG(output) = "";
168   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
169                                      FilePath("test_detail.xml")).c_str(),
170                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
171 }
172 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithDefaultXML)173 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
174   GTEST_FLAG(output) = "xml";
175   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
176                                      FilePath("test_detail.xml")).c_str(),
177                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
178 }
179 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithRelativeFile)180 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
181   GTEST_FLAG(output) = "xml:filename.abc";
182   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
183                                      FilePath("filename.abc")).c_str(),
184                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
185 }
186 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithRelativePath)187 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
188 #if GTEST_OS_WINDOWS
189   GTEST_FLAG(output) = "xml:path\\";
190   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
191   EXPECT_TRUE(
192       _strcmpi(output_file.c_str(),
193                FilePath::ConcatPaths(
194                    original_working_dir_,
195                    FilePath("path\\gtest-options_test.xml")).c_str()) == 0 ||
196       _strcmpi(output_file.c_str(),
197                FilePath::ConcatPaths(
198                    original_working_dir_,
199                    FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 ||
200       _strcmpi(output_file.c_str(),
201                FilePath::ConcatPaths(
202                    original_working_dir_,
203                    FilePath("path\\gtest_all_test.xml")).c_str()) == 0)
204                        << " output_file = " << output_file;
205 #else
206   GTEST_FLAG(output) = "xml:path/";
207   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
208   // TODO(wan@google.com): libtool causes the test binary file to be
209   //   named lt-gtest-options_test.  Therefore the output file may be
210   //   named .../lt-gtest-options_test.xml.  We should remove this
211   //   hard-coded logic when Chandler Carruth's libtool replacement is
212   //   ready.
213   EXPECT_TRUE(output_file == FilePath::ConcatPaths(original_working_dir_,
214                       FilePath("path/gtest-options_test.xml")).c_str() ||
215               output_file == FilePath::ConcatPaths(original_working_dir_,
216                       FilePath("path/lt-gtest-options_test.xml")).c_str() ||
217               output_file == FilePath::ConcatPaths(original_working_dir_,
218                       FilePath("path/gtest_all_test.xml")).c_str() ||
219               output_file == FilePath::ConcatPaths(original_working_dir_,
220                       FilePath("path/lt-gtest_all_test.xml")).c_str())
221                   << " output_file = " << output_file;
222 #endif
223 }
224 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithAbsoluteFile)225 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
226 #if GTEST_OS_WINDOWS
227   GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
228   EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(),
229                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
230 #else
231   GTEST_FLAG(output) ="xml:/tmp/filename.abc";
232   EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(),
233                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
234 #endif
235 }
236 
TEST_F(XmlOutputChangeDirTest,PreserveOriginalWorkingDirWithAbsolutePath)237 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
238 #if GTEST_OS_WINDOWS
239   GTEST_FLAG(output) = "xml:c:\\tmp\\";
240   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
241   EXPECT_TRUE(
242       _strcmpi(output_file.c_str(),
243                FilePath("c:\\tmp\\gtest-options_test.xml").c_str()) == 0 ||
244       _strcmpi(output_file.c_str(),
245                FilePath("c:\\tmp\\gtest-options-ex_test.xml").c_str()) == 0 ||
246       _strcmpi(output_file.c_str(),
247                FilePath("c:\\tmp\\gtest_all_test.xml").c_str()) == 0)
248                    << " output_file = " << output_file;
249 #else
250   GTEST_FLAG(output) = "xml:/tmp/";
251   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
252   // TODO(wan@google.com): libtool causes the test binary file to be
253   //   named lt-gtest-options_test.  Therefore the output file may be
254   //   named .../lt-gtest-options_test.xml.  We should remove this
255   //   hard-coded logic when Chandler Carruth's libtool replacement is
256   //   ready.
257   EXPECT_TRUE(output_file == "/tmp/gtest-options_test.xml" ||
258               output_file == "/tmp/lt-gtest-options_test.xml" ||
259               output_file == "/tmp/gtest_all_test.xml" ||
260               output_file == "/tmp/lt-gtest_all_test.xml")
261                   << " output_file = " << output_file;
262 #endif
263 }
264 
265 }  // namespace
266 }  // namespace internal
267 }  // namespace testing
268