Override TemporaryFile's default move constructor
[folly.git] / folly / experimental / test / TestUtilTest.cpp
index 79046e0736ea126f8b91f4339a4a45ee192ea61e..27fc13467811d48ed0382b035b8d3fd6b3bfa9ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <folly/experimental/TestUtil.h>
 
-#include <sys/stat.h>
-#include <sys/types.h>
-
 #include <system_error>
 
 #include <boost/algorithm/string.hpp>
 #include <glog/logging.h>
 
 #include <folly/Memory.h>
-#include <folly/portability/Environment.h>
 #include <folly/portability/Fcntl.h>
 #include <folly/portability/GTest.h>
+#include <folly/portability/Stdlib.h>
 
 using namespace folly;
 using namespace folly::test;
@@ -56,6 +53,19 @@ TEST(TemporaryFile, Simple) {
   });
 }
 
+TEST(TemporaryFile, EarlyClose) {
+  fs::path p;
+  {
+    TemporaryFile f;
+    p = f.path();
+    EXPECT_TRUE(fs::exists(p));
+    f.close();
+    EXPECT_EQ(-1, f.fd());
+    EXPECT_TRUE(fs::exists(p));
+  }
+  EXPECT_FALSE(fs::exists(p));
+}
+
 TEST(TemporaryFile, Prefix) {
   TemporaryFile f("Foo");
   EXPECT_TRUE(f.path().is_absolute());
@@ -75,6 +85,35 @@ TEST(TemporaryFile, NoSuchPath) {
                std::system_error);
 }
 
+TEST(TemporaryFile, moveAssignment) {
+  TemporaryFile f;
+  int fd;
+
+  EXPECT_TRUE(f.path().is_absolute());
+  {
+    TemporaryFile g("Foo", ".");
+    EXPECT_NE(g.fd(), -1);
+    fd = g.fd();
+    f = std::move(g);
+  }
+  EXPECT_EQ(fs::path("."), f.path().parent_path());
+  EXPECT_EQ(f.fd(), fd);
+
+  TemporaryFile h = TemporaryFile("FooBar", ".");
+  EXPECT_NE(h.fd(), -1);
+}
+
+TEST(TemporaryFile, moveCtor) {
+  struct FooBar {
+    TemporaryFile f_;
+    explicit FooBar(TemporaryFile&& f) : f_(std::move(f)) {}
+  };
+  TemporaryFile g("Foo");
+  FooBar fb(std::move(g));
+  EXPECT_EQ(g.fd(), -1);
+  EXPECT_NE(fb.f_.fd(), -1);
+}
+
 void testTemporaryDirectory(TemporaryDirectory::Scope scope) {
   fs::path path;
   {
@@ -121,7 +160,7 @@ TEST(TemporaryDirectory, SafelyMove) {
     expectTempdirExists(d);
     expectTempdirExists(d2);
 
-    dir = folly::make_unique<TemporaryDirectory>(std::move(d));
+    dir = std::make_unique<TemporaryDirectory>(std::move(d));
     dir2 = std::move(d2);
   }
 
@@ -190,52 +229,3 @@ TEST(CaptureFD, ChunkCob) {
   }
   EXPECT_EQ(2, chunks.size());
 }
-
-
-class EnvVarSaverTest : public testing::Test {};
-
-TEST_F(EnvVarSaverTest, ExampleNew) {
-  auto key = "hahahahaha";
-  EXPECT_EQ(nullptr, getenv(key));
-
-  PCHECK(0 == setenv(key, "", true));
-  EXPECT_STREQ("", getenv(key));
-  PCHECK(0 == unsetenv(key));
-  EXPECT_EQ(nullptr, getenv(key));
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == setenv(key, "blah", true));
-  EXPECT_EQ("blah", std::string{getenv(key)});
-  saver = nullptr;
-  EXPECT_EQ(nullptr, getenv(key));
-}
-
-TEST_F(EnvVarSaverTest, ExampleExisting) {
-  auto key = "PATH";
-  EXPECT_NE(nullptr, getenv(key));
-  auto value = std::string{getenv(key)};
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == setenv(key, "blah", true));
-  EXPECT_EQ("blah", std::string{getenv(key)});
-  saver = nullptr;
-  EXPECT_TRUE(value == getenv(key));
-}
-
-TEST_F(EnvVarSaverTest, ExampleDeleting) {
-  auto key = "PATH";
-  EXPECT_NE(nullptr, getenv(key));
-  auto value = std::string{getenv(key)};
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == unsetenv(key));
-  EXPECT_EQ(nullptr, getenv(key));
-  saver = nullptr;
-  EXPECT_TRUE(value == getenv(key));
-}
-
-int main(int argc, char *argv[]) {
-  testing::InitGoogleTest(&argc, argv);
-  gflags::ParseCommandLineFlags(&argc, &argv, true);
-  return RUN_ALL_TESTS();
-}