scoped_temp_dir_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include "butil/file_util.h"
  6. #include "butil/files/file.h"
  7. #include "butil/files/scoped_temp_dir.h"
  8. #include <gtest/gtest.h>
  9. namespace butil {
  10. TEST(ScopedTempDir, FullPath) {
  11. FilePath test_path;
  12. butil::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"),
  13. &test_path);
  14. // Against an existing dir, it should get destroyed when leaving scope.
  15. EXPECT_TRUE(DirectoryExists(test_path));
  16. {
  17. ScopedTempDir dir;
  18. EXPECT_TRUE(dir.Set(test_path));
  19. EXPECT_TRUE(dir.IsValid());
  20. }
  21. EXPECT_FALSE(DirectoryExists(test_path));
  22. {
  23. ScopedTempDir dir;
  24. EXPECT_TRUE(dir.Set(test_path));
  25. // Now the dir doesn't exist, so ensure that it gets created.
  26. EXPECT_TRUE(DirectoryExists(test_path));
  27. // When we call Release(), it shouldn't get destroyed when leaving scope.
  28. FilePath path = dir.Take();
  29. EXPECT_EQ(path.value(), test_path.value());
  30. EXPECT_FALSE(dir.IsValid());
  31. }
  32. EXPECT_TRUE(DirectoryExists(test_path));
  33. // Clean up.
  34. {
  35. ScopedTempDir dir;
  36. EXPECT_TRUE(dir.Set(test_path));
  37. }
  38. EXPECT_FALSE(DirectoryExists(test_path));
  39. }
  40. TEST(ScopedTempDir, TempDir) {
  41. // In this case, just verify that a directory was created and that it's a
  42. // child of TempDir.
  43. FilePath test_path;
  44. {
  45. ScopedTempDir dir;
  46. EXPECT_TRUE(dir.CreateUniqueTempDir());
  47. test_path = dir.path();
  48. EXPECT_TRUE(DirectoryExists(test_path));
  49. FilePath tmp_dir;
  50. EXPECT_TRUE(butil::GetTempDir(&tmp_dir));
  51. EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
  52. }
  53. EXPECT_FALSE(DirectoryExists(test_path));
  54. }
  55. TEST(ScopedTempDir, UniqueTempDirUnderPath) {
  56. // Create a path which will contain a unique temp path.
  57. FilePath base_path;
  58. ASSERT_TRUE(butil::CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"),
  59. &base_path));
  60. FilePath test_path;
  61. {
  62. ScopedTempDir dir;
  63. EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
  64. test_path = dir.path();
  65. EXPECT_TRUE(DirectoryExists(test_path));
  66. EXPECT_TRUE(base_path.IsParent(test_path));
  67. EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
  68. }
  69. EXPECT_FALSE(DirectoryExists(test_path));
  70. butil::DeleteFile(base_path, true);
  71. }
  72. TEST(ScopedTempDir, MultipleInvocations) {
  73. ScopedTempDir dir;
  74. EXPECT_TRUE(dir.CreateUniqueTempDir());
  75. EXPECT_FALSE(dir.CreateUniqueTempDir());
  76. EXPECT_TRUE(dir.Delete());
  77. EXPECT_TRUE(dir.CreateUniqueTempDir());
  78. EXPECT_FALSE(dir.CreateUniqueTempDir());
  79. ScopedTempDir other_dir;
  80. EXPECT_TRUE(other_dir.Set(dir.Take()));
  81. EXPECT_TRUE(dir.CreateUniqueTempDir());
  82. EXPECT_FALSE(dir.CreateUniqueTempDir());
  83. EXPECT_FALSE(other_dir.CreateUniqueTempDir());
  84. }
  85. #if defined(OS_WIN)
  86. TEST(ScopedTempDir, LockedTempDir) {
  87. ScopedTempDir dir;
  88. EXPECT_TRUE(dir.CreateUniqueTempDir());
  89. butil::File file(dir.path().Append(FILE_PATH_LITERAL("temp")),
  90. butil::File::FLAG_CREATE_ALWAYS | butil::File::FLAG_WRITE);
  91. EXPECT_TRUE(file.IsValid());
  92. EXPECT_EQ(butil::File::FILE_OK, file.error_details());
  93. EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
  94. EXPECT_FALSE(dir.path().empty()); // We should still have a valid path.
  95. file.Close();
  96. // Now, we should be able to delete.
  97. EXPECT_TRUE(dir.Delete());
  98. }
  99. #endif // defined(OS_WIN)
  100. } // namespace butil