From bea1e20cb9760563c3dc6f30761d2aa7d233772a Mon Sep 17 00:00:00 2001 From: Juergen Ributzka Date: Mon, 13 Mar 2017 21:34:07 +0000 Subject: [PATCH] [Support] Test directory iterators and recursive directory iterators with broken symlinks. This commit adds a unit test to the file system tests to verify the behavior of the directory iterator and recursive directory iterator with broken symlinks. This test is Unix only. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297669 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/Path.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index c9c98f5d70b..ad5fbffaa66 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -778,6 +778,84 @@ TEST_F(FileSystemTest, DirectoryIteration) { ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel")); } +#ifdef LLVM_ON_UNIX +TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) { + // Create a known hierarchy to recurse over. + ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + "/symlink")); + ASSERT_NO_ERROR( + fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/a")); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/symlink/b/bb")); + ASSERT_NO_ERROR( + fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/ba")); + ASSERT_NO_ERROR( + fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/bc")); + ASSERT_NO_ERROR( + fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/c")); + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/symlink/d/dd/ddd")); + ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory) + "/symlink/d/dd", + Twine(TestDirectory) + "/symlink/d/da")); + ASSERT_NO_ERROR( + fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/e")); + + typedef std::vector v_t; + v_t visited; + + // The directory iterator doesn't stat the file, so we should be able to + // iterate over the whole directory. + std::error_code ec; + for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e; + i != e; i.increment(ec)) { + ASSERT_NO_ERROR(ec); + visited.push_back(path::filename(i->path())); + } + std::sort(visited.begin(), visited.end()); + v_t expected = {"a", "b", "c", "d", "e"}; + ASSERT_TRUE(visited.size() == expected.size()); + ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); + visited.clear(); + + // The recursive directory iterator has to stat the file, so we need to skip + // the broken symlinks. + for (fs::recursive_directory_iterator + i(Twine(TestDirectory) + "/symlink", ec), + e; + i != e; i.increment(ec)) { + ASSERT_NO_ERROR(ec); + + fs::file_status status; + if (i->status(status) == std::errc::no_such_file_or_directory) { + i.no_push(); + continue; + } + + visited.push_back(path::filename(i->path())); + } + std::sort(visited.begin(), visited.end()); + expected = {"b", "bb", "d", "da", "dd", "ddd", "ddd"}; + ASSERT_TRUE(visited.size() == expected.size()); + ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); + visited.clear(); + + // This recursive directory iterator doesn't follow symlinks, so we don't need + // to skip them. + for (fs::recursive_directory_iterator + i(Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false), + e; + i != e; i.increment(ec)) { + ASSERT_NO_ERROR(ec); + visited.push_back(path::filename(i->path())); + } + + expected = {"a", "b", "ba", "bb", "bc", "c", "d", "da", "dd", "ddd", "e"}; + ASSERT_TRUE(visited.size() == expected.size()); + ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); + + ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/symlink")); +} +#endif + TEST_F(FileSystemTest, Remove) { SmallString<64> BaseDir; SmallString<64> Paths[4];