diff options
Diffstat (limited to 'unittests/Support/Path.cpp')
| -rw-r--r-- | unittests/Support/Path.cpp | 130 |
1 files changed, 38 insertions, 92 deletions
diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 3e474f33ca6d..f624626f5e53 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -564,6 +564,27 @@ TEST_F(FileSystemTest, RealPath) { ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1")); } +TEST_F(FileSystemTest, TempFileKeepDiscard) { + // We can keep then discard. + auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%"); + ASSERT_TRUE((bool)TempFileOrError); + fs::TempFile File = std::move(*TempFileOrError); + ASSERT_FALSE((bool)File.keep(TestDirectory + "/keep")); + ASSERT_FALSE((bool)File.discard()); + ASSERT_TRUE(fs::exists(TestDirectory + "/keep")); + ASSERT_NO_ERROR(fs::remove(TestDirectory + "/keep")); +} + +TEST_F(FileSystemTest, TempFileDiscardDiscard) { + // We can discard twice. + auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%"); + ASSERT_TRUE((bool)TempFileOrError); + fs::TempFile File = std::move(*TempFileOrError); + ASSERT_FALSE((bool)File.discard()); + ASSERT_FALSE((bool)File.discard()); + ASSERT_FALSE(fs::exists(TestDirectory + "/keep")); +} + TEST_F(FileSystemTest, TempFiles) { // Create a temp file. int FileDescriptor; @@ -699,6 +720,21 @@ TEST_F(FileSystemTest, CreateDir) { ThisDir = path::parent_path(ThisDir); } + // Also verify that paths with Unix separators are handled correctly. + std::string LongPathWithUnixSeparators(TestDirectory.str()); + // Add at least one subdirectory to TestDirectory, and replace slashes with + // backslashes + do { + LongPathWithUnixSeparators.append("/DirNameWith19Charss"); + } while (LongPathWithUnixSeparators.size() < 260); + std::replace(LongPathWithUnixSeparators.begin(), + LongPathWithUnixSeparators.end(), + '\\', '/'); + ASSERT_NO_ERROR(fs::create_directories(Twine(LongPathWithUnixSeparators))); + // cleanup + ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + + "/DirNameWith19Charss")); + // Similarly for a relative pathname. Need to set the current directory to // TestDirectory so that the one we create ends up in the right place. char PreviousDir[260]; @@ -854,8 +890,8 @@ TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) { i != e; i.increment(ec)) { ASSERT_NO_ERROR(ec); - fs::file_status status; - if (i->status(status) == + ErrorOr<fs::basic_file_status> status = i->status(); + if (status.getError() == std::make_error_code(std::errc::no_such_file_or_directory)) { i.no_push(); continue; @@ -1145,96 +1181,6 @@ TEST(Support, ReplacePathPrefix) { EXPECT_EQ(Path, "/foo"); } -TEST_F(FileSystemTest, PathFromFD) { - // Create a temp file. - int FileDescriptor; - 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))); - - // Try to get the path from the file descriptor - SmallString<64> ResultPath; - std::error_code ErrorCode = - fs::getPathFromOpenFD(FileDescriptor, ResultPath); - - // If we succeeded, check that the paths are the same (modulo case): - if (!ErrorCode) { - // The paths returned by createTemporaryFile and getPathFromOpenFD - // should reference the same file on disk. - fs::UniqueID D1, D2; - ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); - ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); - ASSERT_EQ(D1, D2); - } - - ::close(FileDescriptor); -} - -TEST_F(FileSystemTest, PathFromFDWin32) { - // Create a temp file. - int FileDescriptor; - 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))); - - SmallVector<char, 8> ResultPath; - std::error_code ErrorCode = - fs::getPathFromOpenFD(FileDescriptor, ResultPath); - - if (!ErrorCode) { - // Now that we know how much space is required for the path, create a path - // buffer with exactly enough space (sans null terminator, which should not - // be present), and call getPathFromOpenFD again to ensure that the API - // properly handles exactly-sized buffers. - SmallVector<char, 8> ExactSizedPath(ResultPath.size()); - ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath); - ResultPath = ExactSizedPath; - } - - if (!ErrorCode) { - fs::UniqueID D1, D2; - ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); - ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); - ASSERT_EQ(D1, D2); - } - ::close(FileDescriptor); -} - -TEST_F(FileSystemTest, PathFromFDUnicode) { - // Create a temp file. - int FileDescriptor; - SmallString<64> TempPath; - - // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0" - 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))); - - SmallVector<char, 8> ResultPath; - std::error_code ErrorCode = - fs::getPathFromOpenFD(FileDescriptor, ResultPath); - - if (!ErrorCode) { - fs::UniqueID D1, D2; - ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); - ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); - ASSERT_EQ(D1, D2); - } - ::close(FileDescriptor); -} - TEST_F(FileSystemTest, OpenFileForRead) { // Create a temp file. int FileDescriptor; |
