aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/cpp
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-20 10:05:18 +0000
committerGravatar Vladimir Moskva <vladmos@google.com>2017-01-20 12:21:39 +0000
commitd5527469ca629f806a7576783289cc0613bf418b (patch)
treecc2716786b0e2f6092ba5515f9af997ef7a98705 /src/test/cpp
parent9a338502f625756a046301a98f209cae8ec675aa (diff)
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
Diffstat (limited to 'src/test/cpp')
-rw-r--r--src/test/cpp/util/file_test.cc54
1 files changed, 54 insertions, 0 deletions
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 <stdio.h>
#include <string.h>
+#include <algorithm>
+#include <map>
#include <memory> // unique_ptr
#include <thread> // 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<IPipe> 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<string, bool> 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<string, bool> expected;
+ expected["file1.txt"] = false;
+ expected["file2.txt"] = false;
+ expected["dir1"] = true;
+
+ CollectingDirectoryEntryConsumer consumer;
+ ForEachDirectoryEntry(rootdir, &consumer);
+ ASSERT_EQ(consumer.entries, expected);
+
+ vector<string> actual2;
+ GetAllFilesUnder(rootdir, &actual2);
+ std::sort(actual2.begin(), actual2.end());
+
+ vector<string> expected2;
+ vector<string> 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