/Unittests/googletest/test/gtest-filepath_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 588 lines · 430 code · 82 blank · 76 comment · 10 complexity · 6964b7d15c1ea73bd9b2f02b4cd0dc83 MD5 · raw file

  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. // RemoveFileName "" -> "./"
  135. TEST(RemoveFileNameTest, EmptyName) {
  136. #if GTEST_OS_WINDOWS_MOBILE
  137. // On Windows CE, we use the root as the current directory.
  138. EXPECT_STREQ(GTEST_PATH_SEP_,
  139. FilePath("").RemoveFileName().c_str());
  140. #else
  141. EXPECT_STREQ("." GTEST_PATH_SEP_,
  142. FilePath("").RemoveFileName().c_str());
  143. #endif
  144. }
  145. // RemoveFileName "adir/" -> "adir/"
  146. TEST(RemoveFileNameTest, ButNoFile) {
  147. EXPECT_STREQ("adir" GTEST_PATH_SEP_,
  148. FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str());
  149. }
  150. // RemoveFileName "adir/afile" -> "adir/"
  151. TEST(RemoveFileNameTest, GivesDirName) {
  152. EXPECT_STREQ("adir" GTEST_PATH_SEP_,
  153. FilePath("adir" GTEST_PATH_SEP_ "afile")
  154. .RemoveFileName().c_str());
  155. }
  156. // RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
  157. TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
  158. EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
  159. FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
  160. .RemoveFileName().c_str());
  161. }
  162. // RemoveFileName "/afile" -> "/"
  163. TEST(RemoveFileNameTest, GivesRootDir) {
  164. EXPECT_STREQ(GTEST_PATH_SEP_,
  165. FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str());
  166. }
  167. TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
  168. FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
  169. 0, "xml");
  170. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
  171. }
  172. TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
  173. FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
  174. 12, "xml");
  175. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
  176. }
  177. TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
  178. FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
  179. FilePath("bar"), 0, "xml");
  180. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
  181. }
  182. TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
  183. FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
  184. FilePath("bar"), 12, "xml");
  185. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
  186. }
  187. TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
  188. FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
  189. 0, "xml");
  190. EXPECT_STREQ("bar.xml", actual.c_str());
  191. }
  192. TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
  193. FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
  194. 14, "xml");
  195. EXPECT_STREQ("bar_14.xml", actual.c_str());
  196. }
  197. TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
  198. FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
  199. FilePath("bar.xml"));
  200. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
  201. }
  202. TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
  203. FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
  204. FilePath("bar.xml"));
  205. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
  206. }
  207. TEST(ConcatPathsTest, Path1BeingEmpty) {
  208. FilePath actual = FilePath::ConcatPaths(FilePath(""),
  209. FilePath("bar.xml"));
  210. EXPECT_STREQ("bar.xml", actual.c_str());
  211. }
  212. TEST(ConcatPathsTest, Path2BeingEmpty) {
  213. FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
  214. FilePath(""));
  215. EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str());
  216. }
  217. TEST(ConcatPathsTest, BothPathBeingEmpty) {
  218. FilePath actual = FilePath::ConcatPaths(FilePath(""),
  219. FilePath(""));
  220. EXPECT_STREQ("", actual.c_str());
  221. }
  222. TEST(ConcatPathsTest, Path1ContainsPathSep) {
  223. FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
  224. FilePath("foobar.xml"));
  225. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
  226. actual.c_str());
  227. }
  228. TEST(ConcatPathsTest, Path2ContainsPathSep) {
  229. FilePath actual = FilePath::ConcatPaths(
  230. FilePath("foo" GTEST_PATH_SEP_),
  231. FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
  232. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
  233. actual.c_str());
  234. }
  235. TEST(ConcatPathsTest, Path2EndsWithPathSep) {
  236. FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
  237. FilePath("bar" GTEST_PATH_SEP_));
  238. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str());
  239. }
  240. // RemoveTrailingPathSeparator "" -> ""
  241. TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
  242. EXPECT_STREQ("",
  243. FilePath("").RemoveTrailingPathSeparator().c_str());
  244. }
  245. // RemoveTrailingPathSeparator "foo" -> "foo"
  246. TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
  247. EXPECT_STREQ("foo",
  248. FilePath("foo").RemoveTrailingPathSeparator().c_str());
  249. }
  250. // RemoveTrailingPathSeparator "foo/" -> "foo"
  251. TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
  252. EXPECT_STREQ(
  253. "foo",
  254. FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str());
  255. }
  256. // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
  257. TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
  258. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
  259. FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
  260. .RemoveTrailingPathSeparator().c_str());
  261. }
  262. // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
  263. TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
  264. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
  265. FilePath("foo" GTEST_PATH_SEP_ "bar")
  266. .RemoveTrailingPathSeparator().c_str());
  267. }
  268. TEST(DirectoryTest, RootDirectoryExists) {
  269. #if GTEST_OS_WINDOWS // We are on Windows.
  270. char current_drive[_MAX_PATH]; // NOLINT
  271. current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
  272. current_drive[1] = ':';
  273. current_drive[2] = '\\';
  274. current_drive[3] = '\0';
  275. EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
  276. #else
  277. EXPECT_TRUE(FilePath("/").DirectoryExists());
  278. #endif // GTEST_OS_WINDOWS
  279. }
  280. #if GTEST_OS_WINDOWS
  281. TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
  282. const int saved_drive_ = _getdrive();
  283. // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
  284. for (char drive = 'Z'; drive >= 'A'; drive--)
  285. if (_chdrive(drive - 'A' + 1) == -1) {
  286. char non_drive[_MAX_PATH]; // NOLINT
  287. non_drive[0] = drive;
  288. non_drive[1] = ':';
  289. non_drive[2] = '\\';
  290. non_drive[3] = '\0';
  291. EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
  292. break;
  293. }
  294. _chdrive(saved_drive_);
  295. }
  296. #endif // GTEST_OS_WINDOWS
  297. #if !GTEST_OS_WINDOWS_MOBILE
  298. // Windows CE _does_ consider an empty directory to exist.
  299. TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
  300. EXPECT_FALSE(FilePath("").DirectoryExists());
  301. }
  302. #endif // !GTEST_OS_WINDOWS_MOBILE
  303. TEST(DirectoryTest, CurrentDirectoryExists) {
  304. #if GTEST_OS_WINDOWS // We are on Windows.
  305. #ifndef _WIN32_CE // Windows CE doesn't have a current directory.
  306. EXPECT_TRUE(FilePath(".").DirectoryExists());
  307. EXPECT_TRUE(FilePath(".\\").DirectoryExists());
  308. #endif // _WIN32_CE
  309. #else
  310. EXPECT_TRUE(FilePath(".").DirectoryExists());
  311. EXPECT_TRUE(FilePath("./").DirectoryExists());
  312. #endif // GTEST_OS_WINDOWS
  313. }
  314. TEST(NormalizeTest, NullStringsEqualEmptyDirectory) {
  315. EXPECT_STREQ("", FilePath(NULL).c_str());
  316. EXPECT_STREQ("", FilePath(String(NULL)).c_str());
  317. }
  318. // "foo/bar" == foo//bar" == "foo///bar"
  319. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
  320. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
  321. FilePath("foo" GTEST_PATH_SEP_ "bar").c_str());
  322. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
  323. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
  324. EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
  325. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
  326. GTEST_PATH_SEP_ "bar").c_str());
  327. }
  328. // "/bar" == //bar" == "///bar"
  329. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
  330. EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
  331. FilePath(GTEST_PATH_SEP_ "bar").c_str());
  332. EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
  333. FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
  334. EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
  335. FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
  336. }
  337. // "foo/" == foo//" == "foo///"
  338. TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
  339. EXPECT_STREQ("foo" GTEST_PATH_SEP_,
  340. FilePath("foo" GTEST_PATH_SEP_).c_str());
  341. EXPECT_STREQ("foo" GTEST_PATH_SEP_,
  342. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
  343. EXPECT_STREQ("foo" GTEST_PATH_SEP_,
  344. FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
  345. }
  346. TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
  347. FilePath default_path;
  348. FilePath non_default_path("path");
  349. non_default_path = default_path;
  350. EXPECT_STREQ("", non_default_path.c_str());
  351. EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged.
  352. }
  353. TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
  354. FilePath non_default_path("path");
  355. FilePath default_path;
  356. default_path = non_default_path;
  357. EXPECT_STREQ("path", default_path.c_str());
  358. EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged.
  359. }
  360. TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
  361. const FilePath const_default_path("const_path");
  362. FilePath non_default_path("path");
  363. non_default_path = const_default_path;
  364. EXPECT_STREQ("const_path", non_default_path.c_str());
  365. }
  366. class DirectoryCreationTest : public Test {
  367. protected:
  368. virtual void SetUp() {
  369. testdata_path_.Set(FilePath(String::Format("%s%s%s",
  370. TempDir().c_str(), GetCurrentExecutableName().c_str(),
  371. "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_)));
  372. testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
  373. unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
  374. 0, "txt"));
  375. unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
  376. 1, "txt"));
  377. remove(testdata_file_.c_str());
  378. remove(unique_file0_.c_str());
  379. remove(unique_file1_.c_str());
  380. posix::RmDir(testdata_path_.c_str());
  381. }
  382. virtual void TearDown() {
  383. remove(testdata_file_.c_str());
  384. remove(unique_file0_.c_str());
  385. remove(unique_file1_.c_str());
  386. posix::RmDir(testdata_path_.c_str());
  387. }
  388. String TempDir() const {
  389. #if GTEST_OS_WINDOWS_MOBILE
  390. return String("\\temp\\");
  391. #elif GTEST_OS_WINDOWS
  392. const char* temp_dir = posix::GetEnv("TEMP");
  393. if (temp_dir == NULL || temp_dir[0] == '\0')
  394. return String("\\temp\\");
  395. else if (String(temp_dir).EndsWith("\\"))
  396. return String(temp_dir);
  397. else
  398. return String::Format("%s\\", temp_dir);
  399. #else
  400. return String("/tmp/");
  401. #endif // GTEST_OS_WINDOWS_MOBILE
  402. }
  403. void CreateTextFile(const char* filename) {
  404. FILE* f = posix::FOpen(filename, "w");
  405. fprintf(f, "text\n");
  406. fclose(f);
  407. }
  408. // Strings representing a directory and a file, with identical paths
  409. // except for the trailing separator character that distinquishes
  410. // a directory named 'test' from a file named 'test'. Example names:
  411. FilePath testdata_path_; // "/tmp/directory_creation/test/"
  412. FilePath testdata_file_; // "/tmp/directory_creation/test"
  413. FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt"
  414. FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt"
  415. };
  416. TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
  417. EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
  418. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  419. EXPECT_TRUE(testdata_path_.DirectoryExists());
  420. }
  421. TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
  422. EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
  423. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  424. // Call 'create' again... should still succeed.
  425. EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  426. }
  427. TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
  428. FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_,
  429. FilePath("unique"), "txt"));
  430. EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str());
  431. EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there
  432. testdata_path_.CreateDirectoriesRecursively();
  433. EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there
  434. CreateTextFile(file_path.c_str());
  435. EXPECT_TRUE(file_path.FileOrDirectoryExists());
  436. FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_,
  437. FilePath("unique"), "txt"));
  438. EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str());
  439. EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there
  440. CreateTextFile(file_path2.c_str());
  441. EXPECT_TRUE(file_path2.FileOrDirectoryExists());
  442. }
  443. TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
  444. // force a failure by putting a file where we will try to create a directory.
  445. CreateTextFile(testdata_file_.c_str());
  446. EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
  447. EXPECT_FALSE(testdata_file_.DirectoryExists());
  448. EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
  449. }
  450. TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
  451. const FilePath test_detail_xml("test_detail.xml");
  452. EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
  453. }
  454. TEST(FilePathTest, DefaultConstructor) {
  455. FilePath fp;
  456. EXPECT_STREQ("", fp.c_str());
  457. }
  458. TEST(FilePathTest, CharAndCopyConstructors) {
  459. const FilePath fp("spicy");
  460. EXPECT_STREQ("spicy", fp.c_str());
  461. const FilePath fp_copy(fp);
  462. EXPECT_STREQ("spicy", fp_copy.c_str());
  463. }
  464. TEST(FilePathTest, StringConstructor) {
  465. const FilePath fp(String("cider"));
  466. EXPECT_STREQ("cider", fp.c_str());
  467. }
  468. TEST(FilePathTest, Set) {
  469. const FilePath apple("apple");
  470. FilePath mac("mac");
  471. mac.Set(apple); // Implement Set() since overloading operator= is forbidden.
  472. EXPECT_STREQ("apple", mac.c_str());
  473. EXPECT_STREQ("apple", apple.c_str());
  474. }
  475. TEST(FilePathTest, ToString) {
  476. const FilePath file("drink");
  477. String str(file.ToString());
  478. EXPECT_STREQ("drink", str.c_str());
  479. }
  480. TEST(FilePathTest, RemoveExtension) {
  481. EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str());
  482. EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str());
  483. }
  484. TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
  485. EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str());
  486. }
  487. TEST(FilePathTest, IsDirectory) {
  488. EXPECT_FALSE(FilePath("cola").IsDirectory());
  489. EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
  490. }
  491. TEST(FilePathTest, IsAbsolutePath) {
  492. EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  493. EXPECT_FALSE(FilePath("").IsAbsolutePath());
  494. #if GTEST_OS_WINDOWS
  495. EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
  496. GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  497. EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
  498. #else
  499. EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
  500. .IsAbsolutePath());
  501. #endif // GTEST_OS_WINDOWS
  502. }
  503. } // namespace
  504. } // namespace internal
  505. } // namespace testing
  506. #undef GTEST_PATH_SEP_