2020-05-03 14:40:59 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/karrick/godirwalk"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSkipWalk(t *testing.T) {
|
|
|
|
t.Run("done", func(t *testing.T) {
|
2020-07-18 20:58:35 +02:00
|
|
|
done := make(Done)
|
2020-05-03 14:40:59 +02:00
|
|
|
ignore := NewIgnoreList(".ppignore", true, false)
|
|
|
|
|
2020-07-18 20:58:35 +02:00
|
|
|
done["foo.jpg"] = Found
|
2020-05-03 14:40:59 +02:00
|
|
|
|
|
|
|
if skip, result := SkipWalk("testdata/directory", true, false, done, ignore); skip {
|
|
|
|
assert.Nil(t, result)
|
|
|
|
} else {
|
|
|
|
t.Fatal("should be skipped")
|
|
|
|
}
|
|
|
|
|
2020-07-18 20:58:35 +02:00
|
|
|
assert.True(t, done["foo.jpg"].Exists())
|
|
|
|
assert.True(t, done["testdata/directory"].Exists())
|
2020-05-03 14:40:59 +02:00
|
|
|
assert.Equal(t, 2, len(done))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("symlink", func(t *testing.T) {
|
2020-07-18 20:58:35 +02:00
|
|
|
done := make(Done)
|
2020-05-03 14:40:59 +02:00
|
|
|
ignore := NewIgnoreList(".ppignore", true, false)
|
|
|
|
|
|
|
|
if skip, result := SkipWalk("testdata/directory/subdirectory/symlink", false, true, done, ignore); skip {
|
|
|
|
assert.Nil(t, result)
|
|
|
|
} else {
|
|
|
|
t.Fatal("should be skipped")
|
|
|
|
}
|
|
|
|
|
|
|
|
if skip, result := SkipWalk("testdata/directory/subdirectory/symlink/self", false, true, done, ignore); skip {
|
|
|
|
assert.Error(t, result)
|
|
|
|
} else {
|
|
|
|
t.Fatal("should be skipped")
|
|
|
|
}
|
|
|
|
|
|
|
|
if skip, result := SkipWalk("testdata/directory/subdirectory/symlink/self/self", false, true, done, ignore); skip {
|
|
|
|
assert.Error(t, result)
|
|
|
|
} else {
|
|
|
|
t.Fatal("should be skipped")
|
|
|
|
}
|
|
|
|
|
2020-07-18 20:58:35 +02:00
|
|
|
assert.True(t, done["testdata/linked"].Exists())
|
|
|
|
assert.True(t, done["testdata/directory/subdirectory/symlink"].Exists())
|
|
|
|
assert.True(t, done["testdata/directory/subdirectory/symlink/self"].Exists())
|
|
|
|
assert.True(t, done["testdata/directory/subdirectory/symlink/self/self"].Exists())
|
2020-05-03 14:40:59 +02:00
|
|
|
assert.Equal(t, 4, len(done))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("godirwalk", func(t *testing.T) {
|
2020-07-18 20:58:35 +02:00
|
|
|
done := make(Done)
|
2020-05-03 14:40:59 +02:00
|
|
|
var skipped []string
|
|
|
|
var skippedDirs []string
|
|
|
|
testPath := "testdata"
|
|
|
|
ignore := NewIgnoreList(".ppignore", true, false)
|
|
|
|
|
|
|
|
err := godirwalk.Walk(testPath, &godirwalk.Options{
|
|
|
|
Callback: func(fileName string, info *godirwalk.Dirent) error {
|
|
|
|
isDir := info.IsDir()
|
|
|
|
isSymlink := info.IsSymlink()
|
|
|
|
|
|
|
|
if skip, result := SkipWalk(fileName, isDir, isSymlink, done, ignore); skip {
|
|
|
|
if result != nil {
|
|
|
|
skippedDirs = append(skippedDirs, fileName)
|
|
|
|
} else {
|
|
|
|
skipped = append(skipped, fileName)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-18 20:58:35 +02:00
|
|
|
done[fileName] = Found
|
2020-05-03 14:40:59 +02:00
|
|
|
|
|
|
|
if textName := TypeText.Find(fileName, false); textName != "" {
|
2020-07-18 20:58:35 +02:00
|
|
|
done[textName] = Found
|
2020-05-03 14:40:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Unsorted: false,
|
|
|
|
FollowSymbolicLinks: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, skippedDirs, "testdata/directory/subdirectory/.hiddendir")
|
|
|
|
|
|
|
|
expectSkipped := []string{
|
|
|
|
"testdata", "testdata/directory",
|
|
|
|
"testdata/directory/.ppignore",
|
|
|
|
"testdata/directory/bar.txt",
|
|
|
|
"testdata/directory/baz.xml",
|
|
|
|
"testdata/directory/subdirectory",
|
|
|
|
"testdata/directory/subdirectory/.hiddenfile",
|
2020-10-19 09:23:09 +02:00
|
|
|
"testdata/directory/subdirectory/.ppignore",
|
|
|
|
"testdata/directory/subdirectory/animals",
|
|
|
|
"testdata/directory/subdirectory/animals/.ppignore",
|
|
|
|
"testdata/directory/subdirectory/animals/dog.json",
|
|
|
|
"testdata/directory/subdirectory/animals/gopher.json",
|
|
|
|
"testdata/directory/subdirectory/animals/gopher.md",
|
2020-05-03 14:40:59 +02:00
|
|
|
"testdata/directory/subdirectory/example.txt",
|
|
|
|
"testdata/directory/subdirectory/foo.txt",
|
|
|
|
"testdata/directory/subdirectory/symlink",
|
|
|
|
"testdata/directory/subdirectory/symlink/somefile.txt",
|
|
|
|
"testdata/directory/subdirectory/symlink/test.md",
|
|
|
|
"testdata/directory/subdirectory/symlink/test.txt"}
|
|
|
|
|
|
|
|
assert.Equal(t, expectSkipped, skipped)
|
|
|
|
})
|
|
|
|
}
|