gtest-filepath_test.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 filepath utilities
  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_unittest.cc, to avoid changing
  38. // build or make-files for some existing Google Test clients. Do not
  39. // #include this file anywhere else!
  40. #include "gtest/internal/gtest-filepath.h"
  41. #include "gtest/gtest.h"
  42. // Indicates that this translation unit is part of Google Test's
  43. // implementation. It must come before gtest-internal-inl.h is
  44. // included, or there will be a compiler error. This trick is to
  45. // prevent a user from accidentally including gtest-internal-inl.h in
  46. // his code.
  47. #define GTEST_IMPLEMENTATION_ 1
  48. #include "src/gtest-internal-inl.h"
  49. #undef GTEST_IMPLEMENTATION_
  50. #if GTEST_OS_WINDOWS_MOBILE
  51. # include <windows.h> // NOLINT
  52. #elif GTEST_OS_WINDOWS
  53. # include <direct.h> // NOLINT
  54. #endif // GTEST_OS_WINDOWS_MOBILE
  55. namespace testing {
  56. namespace internal {
  57. namespace {
  58. #if GTEST_OS_WINDOWS_MOBILE
  59. // TODO(wan@google.com): Move these to the POSIX adapter section in
  60. // gtest-port.h.
  61. // Windows CE doesn't have the remove C function.
  62. int remove(const char* path) {
  63. LPCWSTR wpath = String::AnsiToUtf16(path);
  64. int ret = DeleteFile(wpath) ? 0 : -1;
  65. delete [] wpath;
  66. return ret;
  67. }
  68. // Windows CE doesn't have the _rmdir C function.
  69. int _rmdir(const char* path) {
  70. FilePath filepath(path);
  71. LPCWSTR wpath = String::AnsiToUtf16(
  72. filepath.RemoveTrailingPathSeparator().c_str());
  73. int ret = RemoveDirectory(wpath) ? 0 : -1;
  74. delete [] wpath;
  75. return ret;
  76. }
  77. #else
  78. TEST(GetCurrentDirTest, ReturnsCurrentDir) {
  79. const FilePath original_dir = FilePath::GetCurrentDir();
  80. EXPECT_FALSE(original_dir.IsEmpty());
  81. posix::ChDir(GTEST_PATH_SEP_);
  82. const FilePath cwd = FilePath::GetCurrentDir();
  83. posix::ChDir(original_dir.c_str());
  84. # if GTEST_OS_WINDOWS
  85. // Skips the ":".
  86. const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
  87. ASSERT_TRUE(cwd_without_drive != NULL);
  88. EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
  89. # else
  90. EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
  91. # endif
  92. }
  93. #endif // GTEST_OS_WINDOWS_MOBILE
  94. TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
  95. EXPECT_TRUE(FilePath("").IsEmpty());
  96. }
  97. TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
  98. EXPECT_FALSE(FilePath("a").IsEmpty());
  99. EXPECT_FALSE(FilePath(".").IsEmpty());
  100. EXPECT_FALSE(FilePath("a/b").IsEmpty());
  101. EXPECT_FALSE(FilePath("a\\b\\").IsEmpty());
  102. }
  103. // RemoveDirectoryName "" -> ""
  104. TEST(RemoveDirectoryNameTest, WhenEmptyName) {
  105. EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
  106. }
  107. // RemoveDirectoryName "afile" -> "afile"
  108. TEST(RemoveDirectoryNameTest, ButNoDirectory) {
  109. EXPECT_EQ("afile",
  110. FilePath("afile").RemoveDirectoryName().string());
  111. }
  112. // RemoveDirectoryName "/afile" -> "afile"
  113. TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
  114. EXPECT_EQ("afile",
  115. FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
  116. }
  117. // RemoveDirectoryName "adir/" -> ""
  118. TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
  119. EXPECT_EQ("",
  120. FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
  121. }
  122. // RemoveDirectoryName "adir/afile" -> "afile"
  123. TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
  124. EXPECT_EQ("afile",
  125. FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
  126. }
  127. // RemoveDirectoryName "adir/subdir/afile" -> "afile"
  128. TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
  129. EXPECT_EQ("afile",
  130. FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
  131. .RemoveDirectoryName().string());
  132. }
  133. #if GTEST_HAS_ALT_PATH_SEP_
  134. // Tests that RemoveDirectoryName() works with the alternate separator
  135. // on Windows.
  136. // RemoveDirectoryName("/afile") -> "afile"
  137. TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
  138. EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
  139. }
  140. // RemoveDirectoryName("adir/") -> ""
  141. TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
  142. EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
  143. }
  144. // RemoveDirectoryName("adir/afile") -> "afile"
  145. TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
  146. EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
  147. }
  148. // RemoveDirectoryName("adir/subdir/afile") -> "afile"
  149. TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
  150. EXPECT_EQ("afile",
  151. FilePath("adir/subdir/afile").RemoveDirectoryName().string());
  152. }
  153. #endif
  154. // RemoveFileName "" -> "./"
  155. TEST(RemoveFileNameTest, EmptyName) {
  156. #if GTEST_OS_WINDOWS_MOBILE
  157. // On Windows CE, we use the root as the current directory.
  158. EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
  159. #else
  160. EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
  161. #endif
  162. }
  163. // RemoveFileName "adir/" -> "adir/"
  164. TEST(RemoveFileNameTest, ButNoFile) {
  165. EXPECT_EQ("adir" GTEST_PATH_SEP_,
  166. FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().string());
  167. }
  168. // RemoveFileName "adir/afile" -> "adir/"
  169. TEST(RemoveFileNameTest, GivesDirName) {
  170. EXPECT_EQ("adir" GTEST_PATH_SEP_,
  171. FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveFileName().string());
  172. }
  173. // RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
  174. TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
  175. EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
  176. FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
  177. .RemoveFileName().string());
  178. }
  179. // RemoveFileName "/afile" -> "/"
  180. TEST(RemoveFileNameTest, GivesRootDir) {
  181. EXPECT_EQ(GTEST_PATH_SEP_,
  182. FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
  183. }
  184. #if GTEST_HAS_ALT_PATH_SEP_
  185. // Tests that RemoveFileName() works with the alternate separator on
  186. // Windows.
  187. // RemoveFileName("adir/") -> "adir/"
  188. TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
  189. EXPECT_EQ("adir" GTEST_PATH_SEP_,
  190. FilePath("adir/").RemoveFileName().string());
  191. }
  192. // RemoveFileName("adir/afile") -> "adir/"
  193. TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
  194. EXPECT_EQ("adir" GTEST_PATH_SEP_,
  195. FilePath("adir/afile").RemoveFileName().string());
  196. }
  197. // RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
  198. TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
  199. EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
  200. FilePath("adir/subdir/afile").RemoveFileName().string());
  201. }
  202. // RemoveFileName("/afile") -> "\"
  203. TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
  204. EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
  205. }
  206. #endif
  207. TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
  208. FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
  209. 0, "xml");
  210. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
  211. }
  212. TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
  213. FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
  214. 12, "xml");
  215. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
  216. }
  217. TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
  218. FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
  219. FilePath("bar"), 0, "xml");
  220. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
  221. }
  222. TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
  223. FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
  224. FilePath("bar"), 12, "xml");
  225. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
  226. }
  227. TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
  228. FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
  229. 0, "xml");
  230. EXPECT_EQ("bar.xml", actual.string());
  231. }
  232. TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
  233. FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
  234. 14, "xml");
  235. EXPECT_EQ("bar_14.xml", actual.string());
  236. }
  237. TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
  238. FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
  239. FilePath("bar.xml"));
  240. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
  241. }
  242. TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
  243. FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
  244. FilePath("bar.xml"));
  245. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
  246. }
  247. TEST(ConcatPathsTest, Path1BeingEmpty) {
  248. FilePath actual = FilePath::ConcatPaths(FilePath(""),
  249. FilePath("bar.xml"));
  250. EXPECT_EQ("bar.xml", actual.string());
  251. }
  252. TEST(ConcatPathsTest, Path2BeingEmpty) {
  253. FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
  254. EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
  255. }
  256. TEST(ConcatPathsTest, BothPathBeingEmpty) {
  257. FilePath actual = FilePath::ConcatPaths(FilePath(""),
  258. FilePath(""));
  259. EXPECT_EQ("", actual.string());
  260. }
  261. TEST(ConcatPathsTest, Path1ContainsPathSep) {
  262. FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
  263. FilePath("foobar.xml"));
  264. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
  265. actual.string());
  266. }
  267. TEST(ConcatPathsTest, Path2ContainsPathSep) {
  268. FilePath actual = FilePath::ConcatPaths(
  269. FilePath("foo" GTEST_PATH_SEP_),
  270. FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
  271. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
  272. actual.string());
  273. }
  274. TEST(ConcatPathsTest, Path2EndsWithPathSep) {
  275. FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
  276. FilePath("bar" GTEST_PATH_SEP_));
  277. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
  278. }
  279. // RemoveTrailingPathSeparator "" -> ""
  280. TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
  281. EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
  282. }
  283. // RemoveTrailingPathSeparator "foo" -> "foo"
  284. TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
  285. EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
  286. }
  287. // RemoveTrailingPathSeparator "foo/" -> "foo"
  288. TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
  289. EXPECT_EQ("foo",
  290. FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
  291. #if GTEST_HAS_ALT_PATH_SEP_
  292. EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
  293. #endif
  294. }
  295. // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
  296. TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
  297. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
  298. FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
  299. .RemoveTrailingPathSeparator().string());
  300. }
  301. // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
  302. TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
  303. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
  304. FilePath("foo" GTEST_PATH_SEP_ "bar")
  305. .RemoveTrailingPathSeparator().string());
  306. }
  307. TEST(DirectoryTest, RootDirectoryExists) {
  308. #if GTEST_OS_WINDOWS // We are on Windows.
  309. char current_drive[_MAX_PATH]; // NOLINT
  310. current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
  311. current_drive[1] = ':';
  312. current_drive[2] = '\\';
  313. current_drive[3] = '\0';
  314. EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
  315. #else
  316. EXPECT_TRUE(FilePath("/").DirectoryExists());
  317. #endif // GTEST_OS_WINDOWS
  318. }
  319. #if GTEST_OS_WINDOWS
  320. TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
  321. const int saved_drive_ = _getdrive();
  322. // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
  323. for (char drive = 'Z'; drive >= 'A'; drive--)
  324. if (_chdrive(drive - 'A' + 1) == -1) {
  325. char non_drive[_MAX_PATH]; // NOLINT
  326. non_drive[0] = drive;
  327. non_drive[1] = ':';
  328. non_drive[2] = '\\';
  329. non_drive[3] = '\0';
  330. EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
  331. break;
  332. }
  333. _chdrive(saved_drive_);
  334. }
  335. #endif // GTEST_OS_WINDOWS
  336. #if !GTEST_OS_WINDOWS_MOBILE
  337. // Windows CE _does_ consider an empty directory to exist.
  338. TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
  339. EXPECT_FALSE(FilePath("").DirectoryExists());
  340. }
  341. #endif // !GTEST_OS_WINDOWS_MOBILE
  342. TEST(DirectoryTest, CurrentDirectoryExists) {
  343. #if GTEST_OS_WINDOWS // We are on Windows.
  344. # ifndef _WIN32_CE // Windows CE doesn't have a current directory.
  345. EXPECT_TRUE(FilePath(".").DirectoryExists());
  346. EXPECT_TRUE(FilePath(".\\").DirectoryExists());
  347. # endif // _WIN32_CE
  348. #else
  349. EXPECT_TRUE(FilePath(".").DirectoryExists());
  350. EXPECT_TRUE(FilePath("./").DirectoryExists());
  351. #endif // GTEST_OS_WINDOWS
  352. }
  353. // "foo/bar" == foo//bar" == "foo///bar"
  354. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
  355. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
  356. FilePath("foo" GTEST_PATH_SEP_ "bar").string());
  357. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
  358. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
  359. EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
  360. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
  361. GTEST_PATH_SEP_ "bar").string());
  362. }
  363. // "/bar" == //bar" == "///bar"
  364. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
  365. EXPECT_EQ(GTEST_PATH_SEP_ "bar",
  366. FilePath(GTEST_PATH_SEP_ "bar").string());
  367. EXPECT_EQ(GTEST_PATH_SEP_ "bar",
  368. FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
  369. EXPECT_EQ(GTEST_PATH_SEP_ "bar",
  370. FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
  371. }
  372. // "foo/" == foo//" == "foo///"
  373. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
  374. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  375. FilePath("foo" GTEST_PATH_SEP_).string());
  376. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  377. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
  378. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  379. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
  380. }
  381. #if GTEST_HAS_ALT_PATH_SEP_
  382. // Tests that separators at the end of the string are normalized
  383. // regardless of their combination (e.g. "foo\" =="foo/\" ==
  384. // "foo\\/").
  385. TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
  386. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  387. FilePath("foo/").string());
  388. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  389. FilePath("foo" GTEST_PATH_SEP_ "/").string());
  390. EXPECT_EQ("foo" GTEST_PATH_SEP_,
  391. FilePath("foo//" GTEST_PATH_SEP_).string());
  392. }
  393. #endif
  394. TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
  395. FilePath default_path;
  396. FilePath non_default_path("path");
  397. non_default_path = default_path;
  398. EXPECT_EQ("", non_default_path.string());
  399. EXPECT_EQ("", default_path.string()); // RHS var is unchanged.
  400. }
  401. TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
  402. FilePath non_default_path("path");
  403. FilePath default_path;
  404. default_path = non_default_path;
  405. EXPECT_EQ("path", default_path.string());
  406. EXPECT_EQ("path", non_default_path.string()); // RHS var is unchanged.
  407. }
  408. TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
  409. const FilePath const_default_path("const_path");
  410. FilePath non_default_path("path");
  411. non_default_path = const_default_path;
  412. EXPECT_EQ("const_path", non_default_path.string());
  413. }
  414. class DirectoryCreationTest : public Test {
  415. protected:
  416. virtual void SetUp() {
  417. testdata_path_.Set(FilePath(
  418. TempDir() + GetCurrentExecutableName().string() +
  419. "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
  420. testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
  421. unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
  422. 0, "txt"));
  423. unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
  424. 1, "txt"));
  425. remove(testdata_file_.c_str());
  426. remove(unique_file0_.c_str());
  427. remove(unique_file1_.c_str());
  428. posix::RmDir(testdata_path_.c_str());
  429. }
  430. virtual void TearDown() {
  431. remove(testdata_file_.c_str());
  432. remove(unique_file0_.c_str());
  433. remove(unique_file1_.c_str());
  434. posix::RmDir(testdata_path_.c_str());
  435. }
  436. std::string TempDir() const {
  437. #if GTEST_OS_WINDOWS_MOBILE
  438. return "\\temp\\";
  439. #elif GTEST_OS_WINDOWS
  440. const char* temp_dir = posix::GetEnv("TEMP");
  441. if (temp_dir == NULL || temp_dir[0] == '\0')
  442. return "\\temp\\";
  443. else if (temp_dir[strlen(temp_dir) - 1] == '\\')
  444. return temp_dir;
  445. else
  446. return std::string(temp_dir) + "\\";
  447. #elif GTEST_OS_LINUX_ANDROID
  448. return "/sdcard/";
  449. #else
  450. return "/tmp/";
  451. #endif // GTEST_OS_WINDOWS_MOBILE
  452. }
  453. void CreateTextFile(const char* filename) {
  454. FILE* f = posix::FOpen(filename, "w");
  455. fprintf(f, "text\n");
  456. fclose(f);
  457. }
  458. // Strings representing a directory and a file, with identical paths
  459. // except for the trailing separator character that distinquishes
  460. // a directory named 'test' from a file named 'test'. Example names:
  461. FilePath testdata_path_; // "/tmp/directory_creation/test/"
  462. FilePath testdata_file_; // "/tmp/directory_creation/test"
  463. FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt"
  464. FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt"
  465. };
  466. TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
  467. EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
  468. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  469. EXPECT_TRUE(testdata_path_.DirectoryExists());
  470. }
  471. TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
  472. EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
  473. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  474. // Call 'create' again... should still succeed.
  475. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  476. }
  477. TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
  478. FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_,
  479. FilePath("unique"), "txt"));
  480. EXPECT_EQ(unique_file0_.string(), file_path.string());
  481. EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there
  482. testdata_path_.CreateDirectoriesRecursively();
  483. EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there
  484. CreateTextFile(file_path.c_str());
  485. EXPECT_TRUE(file_path.FileOrDirectoryExists());
  486. FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_,
  487. FilePath("unique"), "txt"));
  488. EXPECT_EQ(unique_file1_.string(), file_path2.string());
  489. EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there
  490. CreateTextFile(file_path2.c_str());
  491. EXPECT_TRUE(file_path2.FileOrDirectoryExists());
  492. }
  493. TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
  494. // force a failure by putting a file where we will try to create a directory.
  495. CreateTextFile(testdata_file_.c_str());
  496. EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
  497. EXPECT_FALSE(testdata_file_.DirectoryExists());
  498. EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
  499. }
  500. TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
  501. const FilePath test_detail_xml("test_detail.xml");
  502. EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
  503. }
  504. TEST(FilePathTest, DefaultConstructor) {
  505. FilePath fp;
  506. EXPECT_EQ("", fp.string());
  507. }
  508. TEST(FilePathTest, CharAndCopyConstructors) {
  509. const FilePath fp("spicy");
  510. EXPECT_EQ("spicy", fp.string());
  511. const FilePath fp_copy(fp);
  512. EXPECT_EQ("spicy", fp_copy.string());
  513. }
  514. TEST(FilePathTest, StringConstructor) {
  515. const FilePath fp(std::string("cider"));
  516. EXPECT_EQ("cider", fp.string());
  517. }
  518. TEST(FilePathTest, Set) {
  519. const FilePath apple("apple");
  520. FilePath mac("mac");
  521. mac.Set(apple); // Implement Set() since overloading operator= is forbidden.
  522. EXPECT_EQ("apple", mac.string());
  523. EXPECT_EQ("apple", apple.string());
  524. }
  525. TEST(FilePathTest, ToString) {
  526. const FilePath file("drink");
  527. EXPECT_EQ("drink", file.string());
  528. }
  529. TEST(FilePathTest, RemoveExtension) {
  530. EXPECT_EQ("app", FilePath("app.cc").RemoveExtension("cc").string());
  531. EXPECT_EQ("app", FilePath("app.exe").RemoveExtension("exe").string());
  532. EXPECT_EQ("APP", FilePath("APP.EXE").RemoveExtension("exe").string());
  533. }
  534. TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
  535. EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
  536. }
  537. TEST(FilePathTest, IsDirectory) {
  538. EXPECT_FALSE(FilePath("cola").IsDirectory());
  539. EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
  540. #if GTEST_HAS_ALT_PATH_SEP_
  541. EXPECT_TRUE(FilePath("koala/").IsDirectory());
  542. #endif
  543. }
  544. TEST(FilePathTest, IsAbsolutePath) {
  545. EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  546. EXPECT_FALSE(FilePath("").IsAbsolutePath());
  547. #if GTEST_OS_WINDOWS
  548. EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
  549. GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  550. EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
  551. EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
  552. GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  553. #else
  554. EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
  555. .IsAbsolutePath());
  556. #endif // GTEST_OS_WINDOWS
  557. }
  558. TEST(FilePathTest, IsRootDirectory) {
  559. #if GTEST_OS_WINDOWS
  560. EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
  561. EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
  562. EXPECT_TRUE(FilePath("e://").IsRootDirectory());
  563. EXPECT_FALSE(FilePath("").IsRootDirectory());
  564. EXPECT_FALSE(FilePath("b:").IsRootDirectory());
  565. EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
  566. EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
  567. EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
  568. #else
  569. EXPECT_TRUE(FilePath("/").IsRootDirectory());
  570. EXPECT_TRUE(FilePath("//").IsRootDirectory());
  571. EXPECT_FALSE(FilePath("").IsRootDirectory());
  572. EXPECT_FALSE(FilePath("\\").IsRootDirectory());
  573. EXPECT_FALSE(FilePath("/x").IsRootDirectory());
  574. #endif
  575. }
  576. } // namespace
  577. } // namespace internal
  578. } // namespace testing