From d5527469ca629f806a7576783289cc0613bf418b Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Fri, 20 Jan 2017 10:05:18 +0000 Subject: Bazel client, Windows: impl. ForEachDirectoryEntry Implement ForEachDirectoryEntry on Windows using FindFirstFileW / FindNextFileW. Supports long paths and traversing junctions. See https://github.com/bazelbuild/bazel/issues/2107 See https://github.com/bazelbuild/bazel/issues/2181 -- PiperOrigin-RevId: 145062749 MOS_MIGRATED_REVID=145062749 --- src/test/cpp/util/file_test.cc | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/test/cpp/util/file_test.cc') diff --git a/src/test/cpp/util/file_test.cc b/src/test/cpp/util/file_test.cc index 83bf3a9845..093841e20a 100644 --- a/src/test/cpp/util/file_test.cc +++ b/src/test/cpp/util/file_test.cc @@ -14,6 +14,8 @@ #include #include +#include +#include #include // unique_ptr #include // NOLINT (to silence Google-internal linter) @@ -23,7 +25,9 @@ namespace blaze_util { +using std::map; using std::string; +using std::vector; TEST(FileTest, TestSingleThreadedPipe) { std::unique_ptr pipe(CreatePipe()); @@ -144,4 +148,54 @@ TEST(FileTest, TestMtimeHandling) { ASSERT_FALSE(mtime.get()->GetIfInDistantFuture(file, &actual)); } +class CollectingDirectoryEntryConsumer : public DirectoryEntryConsumer { + public: + void Consume(const std::string& name, bool is_directory) override { + // use just base name for easy comparison and no hassle with path separators + // for Windows' sake (test runs on every platform) + entries[Basename(name)] = is_directory; + } + + map entries; +}; + +TEST(FileTest, ForEachDirectoryEntryTest) { + string rootdir(JoinPath(getenv("TEST_TMPDIR"), "foo")); + string file1(JoinPath(rootdir, "file1.txt")); + string file2(JoinPath(rootdir, "file2.txt")); + string subdir(JoinPath(rootdir, "dir1")); + string file3(JoinPath(subdir, "file3.txt")); + + ASSERT_TRUE(MakeDirectories(subdir, 0700)); + ASSERT_TRUE(WriteFile("hello", 5, file1)); + ASSERT_TRUE(WriteFile("hello", 5, file2)); + ASSERT_TRUE(WriteFile("hello", 5, file3)); + + map expected; + expected["file1.txt"] = false; + expected["file2.txt"] = false; + expected["dir1"] = true; + + CollectingDirectoryEntryConsumer consumer; + ForEachDirectoryEntry(rootdir, &consumer); + ASSERT_EQ(consumer.entries, expected); + + vector actual2; + GetAllFilesUnder(rootdir, &actual2); + std::sort(actual2.begin(), actual2.end()); + + vector expected2; + vector unixstyle_actual; + // normalize path separators for Windows' sake (test runs on every platform) + for (auto i : actual2) { + std::replace(i.begin(), i.end(), '\\', '/'); + unixstyle_actual.push_back(i); + } + for (auto i : {file3, file1, file2}) { + std::replace(i.begin(), i.end(), '\\', '/'); + expected2.push_back(i); + } + ASSERT_EQ(unixstyle_actual, expected2); +} + } // namespace blaze_util -- cgit v1.2.3