Include root path in directory resolve index (#869)

This commit is contained in:
Alex Goodman 2022-03-07 11:34:16 -05:00 committed by GitHub
parent a86dd3704e
commit 991af0d857
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -147,7 +147,7 @@ func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error)
}
// link cycles could cause a revisit --we should not allow this
if r.fileTree.HasPath(file.Path(path)) {
if r.hasBeenIndexed(path) {
return "", nil
}
@ -193,6 +193,23 @@ func (r directoryResolver) addPathToIndex(p string, info os.FileInfo) (string, e
}
}
func (r directoryResolver) hasBeenIndexed(p string) bool {
filePath := file.Path(p)
if !r.fileTree.HasPath(filePath) {
return false
}
exists, ref, err := r.fileTree.File(filePath)
if err != nil || !exists || ref == nil {
return false
}
// cases like "/" will be in the tree, but not been indexed yet (a special case). We want to capture
// these cases as new paths to index.
_, exists = r.metadata[ref.ID()]
return exists
}
func (r directoryResolver) addDirectoryToIndex(p string, info os.FileInfo) error {
ref, err := r.fileTree.AddDir(file.Path(p))
if err != nil {

View File

@ -848,3 +848,20 @@ func testWithTimeout(t *testing.T, timeout time.Duration, test func(*testing.T))
case <-done:
}
}
func Test_IncludeRootPathInIndex(t *testing.T) {
filterFn := func(path string, _ os.FileInfo) bool {
return path != "/"
}
resolver, err := newDirectoryResolver("/", filterFn)
require.NoError(t, err)
exists, ref, err := resolver.fileTree.File(file.Path("/"))
require.NoError(t, err)
require.NotNil(t, ref)
assert.True(t, exists)
_, exists = resolver.metadata[ref.ID()]
require.True(t, exists)
}