googletest-filepath-test.cc 22 KB

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