//===----------------------------------------------------------------------===//
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
SmallString<64> TestPath;
sys::fs::createTemporaryFile("MemoryBufferTest_NullTerminator4K", "temp",
TestFD, TestPath);
+ FileRemover Cleanup(TestPath);
raw_fd_ostream OF(TestFD, true, /*unbuffered=*/true);
for (unsigned i = 0; i < 4096 / 16; ++i) {
OF << "0123456789abcdef";
SmallString<64> TestPath;
// Create a temporary file and write data into it.
sys::fs::createTemporaryFile("prefix", "temp", TestFD, TestPath);
+ FileRemover Cleanup(TestPath);
// OF is responsible for closing the file; If the file is not
// reopened, it will be unbuffered so that the results are
// immediately visible through the fd.
int FD;
SmallString<64> TestPath;
sys::fs::createTemporaryFile("MemoryBufferTest_Slice", "temp", FD, TestPath);
+ FileRemover Cleanup(TestPath);
raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
for (unsigned i = 0; i < 0x2000 / 8; ++i) {
OF << "12345678";
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
fs::createUniqueDirectory("dir2", Dir2));
ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
ASSERT_NE(F1, F2);
+ ASSERT_NO_ERROR(fs::remove(TempPath2));
+ ASSERT_NO_ERROR(fs::remove(TempPath));
}
TEST_F(FileSystemTest, TempFiles) {
SmallString<64> TempPath3;
ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
ASSERT_FALSE(TempPath3.endswith("."));
+ FileRemover Cleanup3(TempPath3);
// Create a hard link to Temp1.
ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
fs::file_status Status;
ASSERT_NO_ERROR(fs::status(FD, Status));
ASSERT_EQ(Status.getSize(), 123U);
+ ::close(FD);
+ ASSERT_NO_ERROR(fs::remove(TempPath));
}
TEST_F(FileSystemTest, FileMapping) {
mfr.data()[Val.size()] = 0;
// Unmap temp file
}
+ ASSERT_EQ(close(FileDescriptor), 0);
// Map it back in read-only
- int FD;
- EC = fs::openFileForRead(Twine(TempPath), FD);
- ASSERT_NO_ERROR(EC);
- fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
- ASSERT_NO_ERROR(EC);
-
- // Verify content
- EXPECT_EQ(StringRef(mfr.const_data()), Val);
-
- // Unmap temp file
- fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
- ASSERT_NO_ERROR(EC);
- ASSERT_EQ(close(FD), 0);
+ {
+ int FD;
+ EC = fs::openFileForRead(Twine(TempPath), FD);
+ ASSERT_NO_ERROR(EC);
+ fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
+ ASSERT_NO_ERROR(EC);
+
+ // Verify content
+ EXPECT_EQ(StringRef(mfr.const_data()), Val);
+
+ // Unmap temp file
+ fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
+ ASSERT_NO_ERROR(EC);
+ ASSERT_EQ(close(FD), 0);
+ }
+ ASSERT_NO_ERROR(fs::remove(TempPath));
}
TEST(Support, NormalizePath) {
SmallString<64> TempPath;
ASSERT_NO_ERROR(
fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+ FileRemover Cleanup(TempPath);
// Make sure it exists.
ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
SmallString<64> TempPath;
ASSERT_NO_ERROR(
fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+ FileRemover Cleanup(TempPath);
// Make sure it exists.
ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
ASSERT_NO_ERROR(
fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
"\xE2\x84\xB5.0", FileDescriptor, TempPath));
+ FileRemover Cleanup(TempPath);
// Make sure it exists.
ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
SmallString<64> TempPath;
ASSERT_NO_ERROR(
fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+ FileRemover Cleanup(TempPath);
// Make sure it exists.
ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
#include "gtest/gtest.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
+#define ASSERT_NO_ERROR(x) \
+ if (std::error_code ASSERT_NO_ERROR_ec = x) { \
+ errs() << #x ": did not return errc::success.\n" \
+ << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
+ << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
+ } else { \
+ }
+
+
namespace {
TEST(raw_pwrite_ostreamTest, TestSVector) {
#endif
}
+#ifdef _MSC_VER
+#define setenv(name, var, ignore) _putenv_s(name, var)
+#endif
+
TEST(raw_pwrite_ostreamTest, TestFD) {
SmallString<64> Path;
int FD;
- sys::fs::createTemporaryFile("foo", "bar", FD, Path);
+
+ // If we want to clean up from a death test, we have to remove the file from
+ // the parent process. Have the parent create the file, pass it via
+ // environment variable to the child, let the child crash, and then remove it
+ // in the parent.
+ const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
+ if (ParentPath) {
+ Path = ParentPath;
+ ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
+ } else {
+ ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));
+ setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);
+ }
+ FileRemover Cleanup(Path);
+
raw_fd_ostream OS(FD, true);
OS << "abcd";
StringRef Test = "test";