Repository: kythe/kythe
Pull request #4353 (2020-02-06 21:15:05)

fix(kzip_merge): add a test that globwalker behaves like filepath.Walk
Adds a fairly simple test of the glob-based Walk implementation that compares its result to that of using LocalFS directly.


Patch:
diff --git a/kythe/go/platform/vfs/BUILD b/kythe/go/platform/vfs/BUILD
index efcf1c110..a8cdfec00 100644
--- a/kythe/go/platform/vfs/BUILD
+++ b/kythe/go/platform/vfs/BUILD
@@ -1,4 +1,4 @@
-load("//tools:build_rules/shims.bzl", "go_library")
+load("//tools:build_rules/shims.bzl", "go_library", "go_test")
 
 package(default_visibility = ["//kythe:default_visibility"])
 
@@ -6,3 +6,16 @@ go_library(
     name = "vfs",
     srcs = ["vfs.go"],
 )
+
+go_test(
+    name = "vfs_test",
+    size = "small",
+    srcs = ["vfs_test.go"],
+    data = glob([
+        "testdata/**",
+    ]),
+    library = "vfs",
+    deps = [
+        "//kythe/go/test/testutil",
+    ],
+)
diff --git a/kythe/go/platform/vfs/testdata/a/b/*/c/empty.txt b/kythe/go/platform/vfs/testdata/a/b/*/c/empty.txt
new file mode 100644
index 000000000..e69de29bb
diff --git a/kythe/go/platform/vfs/testdata/a/b/c/d/empty.txt b/kythe/go/platform/vfs/testdata/a/b/c/d/empty.txt
new file mode 100644
index 000000000..e69de29bb
diff --git a/kythe/go/platform/vfs/vfs.go b/kythe/go/platform/vfs/vfs.go
index 4f1a49e1e..201096018 100644
--- a/kythe/go/platform/vfs/vfs.go
+++ b/kythe/go/platform/vfs/vfs.go
@@ -318,13 +318,12 @@ func (gw *globWalker) walk(ctx context.Context, path string, info os.FileInfo, w
 		return userErr
 	}
 	for _, name := range names {
-		filename := filepath.Join(path, name)
-		fileInfo, err := gw.r.Stat(ctx, filename)
+		fileInfo, err := gw.r.Stat(ctx, name)
 		if err != nil {
-			if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
+			if err := walkFn(name, fileInfo, err); err != nil && err != filepath.SkipDir {
 				return err
 			}
-		} else if err := gw.walk(ctx, path, info, walkFn); err != nil {
+		} else if err := gw.walk(ctx, name, info, walkFn); err != nil {
 			if !fileInfo.IsDir() || err != filepath.SkipDir {
 				return err
 			}


Test patch:
diff --git a/kythe/go/platform/vfs/vfs_test.go b/kythe/go/platform/vfs/vfs_test.go
new file mode 100644
index 000000000..a714c1160
--- /dev/null
+++ b/kythe/go/platform/vfs/vfs_test.go
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2020 The Kythe Authors. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package vfs
+
+import (
+	"context"
+	"os"
+	"testing"
+
+	"kythe.io/kythe/go/test/testutil"
+)
+
+const testDataDir = "testdata"
+
+func TestGlobWalker(t *testing.T) {
+	byglob, err := collectFiles(&globWalker{LocalFS{}}, testutil.TestFilePath(t, testDataDir))
+	testutil.FatalOnErrT(t, "unable to collect files from glob", err)
+	bylocal, err := collectFiles(LocalFS{}, testutil.TestFilePath(t, testDataDir))
+	testutil.FatalOnErrT(t, "unable to collect local files", err)
+	testutil.FatalOnErrT(t, "local and glob walks differ", testutil.DeepEqual(bylocal, byglob))
+}
+
+func collectFiles(walk Walker, root string) ([]string, error) {
+	var files []string
+	return files, walk.Walk(context.Background(), root, func(path string, info os.FileInfo, err error) error {
+		if err != nil {
+			return err
+		}
+		files = append(files, path)
+		return nil
+	})
+}