[llvm] 15ab7bc - Testing: Make TempFile safe to move; test Temp{Dir,File,Link}

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 1 13:48:14 PST 2022


Author: Duncan P. N. Exon Smith
Date: 2022-03-01T13:45:51-08:00
New Revision: 15ab7bc3af3c9f38b3404599d76e4ec3727a0f76

URL: https://github.com/llvm/llvm-project/commit/15ab7bc3af3c9f38b3404599d76e4ec3727a0f76
DIFF: https://github.com/llvm/llvm-project/commit/15ab7bc3af3c9f38b3404599d76e4ec3727a0f76.diff

LOG: Testing: Make TempFile safe to move; test Temp{Dir,File,Link}

Default the moves and delete the copies for TempFile, matching TempDir
and TempLink, and add tests for all of them to confirm that the
destructor is not harmful after it has been moved from.

Differential Revision: https://reviews.llvm.org/D120691

Added: 
    llvm/unittests/Testing/CMakeLists.txt
    llvm/unittests/Testing/Support/CMakeLists.txt
    llvm/unittests/Testing/Support/TempPathTest.cpp

Modified: 
    llvm/include/llvm/Testing/Support/SupportHelpers.h
    llvm/unittests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Testing/Support/SupportHelpers.h b/llvm/include/llvm/Testing/Support/SupportHelpers.h
index 2419fc95d8178..ec1485bf0f16b 100644
--- a/llvm/include/llvm/Testing/Support/SupportHelpers.h
+++ b/llvm/include/llvm/Testing/Support/SupportHelpers.h
@@ -238,6 +238,12 @@ class TempFile {
     }
   }
 
+  TempFile(const TempFile &) = delete;
+  TempFile &operator=(const TempFile &) = delete;
+
+  TempFile(TempFile &&) = default;
+  TempFile &operator=(TempFile &&) = default;
+
   /// The path to the file.
   StringRef path() const { return Path; }
 };

diff  --git a/llvm/unittests/CMakeLists.txt b/llvm/unittests/CMakeLists.txt
index 99782949b2559..c372aa7a91734 100644
--- a/llvm/unittests/CMakeLists.txt
+++ b/llvm/unittests/CMakeLists.txt
@@ -45,6 +45,7 @@ add_subdirectory(ProfileData)
 add_subdirectory(Support)
 add_subdirectory(TableGen)
 add_subdirectory(Target)
+add_subdirectory(Testing)
 add_subdirectory(TextAPI)
 add_subdirectory(Transforms)
 add_subdirectory(XRay)

diff  --git a/llvm/unittests/Testing/CMakeLists.txt b/llvm/unittests/Testing/CMakeLists.txt
new file mode 100644
index 0000000000000..fc23e64eeb7a4
--- /dev/null
+++ b/llvm/unittests/Testing/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(Support)

diff  --git a/llvm/unittests/Testing/Support/CMakeLists.txt b/llvm/unittests/Testing/Support/CMakeLists.txt
new file mode 100644
index 0000000000000..b2d2b1bda82ff
--- /dev/null
+++ b/llvm/unittests/Testing/Support/CMakeLists.txt
@@ -0,0 +1,10 @@
+set(LLVM_LINK_COMPONENTS
+  Support
+  TestingSupport
+  )
+
+add_llvm_unittest(TestingSupportTests
+  TempPathTest.cpp
+  )
+
+target_link_libraries(TestingSupportTests PRIVATE LLVMTestingSupport)

diff  --git a/llvm/unittests/Testing/Support/TempPathTest.cpp b/llvm/unittests/Testing/Support/TempPathTest.cpp
new file mode 100644
index 0000000000000..830221c30d7c2
--- /dev/null
+++ b/llvm/unittests/Testing/Support/TempPathTest.cpp
@@ -0,0 +1,100 @@
+//===- TempPathTest.cpp ---------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using llvm::unittest::TempDir;
+using llvm::unittest::TempFile;
+using llvm::unittest::TempLink;
+
+namespace {
+
+TEST(TempPathTest, TempDir) {
+  Optional<TempDir> Dir1, Dir2;
+  StringRef Prefix = "temp-path-test";
+  Dir1.emplace(Prefix, /*Unique=*/true);
+  EXPECT_EQ(Prefix,
+            sys::path::filename(Dir1->path()).take_front(Prefix.size()));
+  EXPECT_NE(Prefix, sys::path::filename(Dir1->path()));
+
+  std::string Path = Dir1->path().str();
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Dir2 = std::move(*Dir1);
+  ASSERT_EQ(Path, Dir2->path());
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Dir1 = None;
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Dir2 = None;
+  ASSERT_FALSE(sys::fs::exists(Path));
+}
+
+TEST(TempPathTest, TempFile) {
+  TempDir D("temp-path-test", /*Unique=*/true);
+  ASSERT_TRUE(sys::fs::exists(D.path()));
+
+  Optional<TempFile> File1, File2;
+  File1.emplace(D.path("file"), "suffix", "content");
+  EXPECT_EQ("file.suffix", sys::path::filename(File1->path()));
+  {
+    ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
+        MemoryBuffer::getFile(File1->path());
+    ASSERT_TRUE(Buffer);
+    ASSERT_EQ("content", (*Buffer)->getBuffer());
+  }
+
+  std::string Path = File1->path().str();
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  File2 = std::move(*File1);
+  ASSERT_EQ(Path, File2->path());
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  File1 = None;
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  File2 = None;
+  ASSERT_FALSE(sys::fs::exists(Path));
+}
+
+TEST(TempPathTest, TempLink) {
+  TempDir D("temp-path-test", /*Unique=*/true);
+  ASSERT_TRUE(sys::fs::exists(D.path()));
+  TempFile File(D.path("file"), "suffix", "content");
+
+  Optional<TempLink> Link1, Link2;
+  Link1.emplace(File.path(), D.path("link"));
+  EXPECT_EQ("link", sys::path::filename(Link1->path()));
+  {
+    ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
+        MemoryBuffer::getFile(Link1->path());
+    ASSERT_TRUE(Buffer);
+    ASSERT_EQ("content", (*Buffer)->getBuffer());
+  }
+
+  std::string Path = Link1->path().str();
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Link2 = std::move(*Link1);
+  ASSERT_EQ(Path, Link2->path());
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Link1 = None;
+  ASSERT_TRUE(sys::fs::exists(Path));
+
+  Link2 = None;
+  ASSERT_FALSE(sys::fs::exists(Path));
+}
+
+} // namespace


        


More information about the llvm-commits mailing list