You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
24 KiB

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