2023-08-18 08:07:25 +00:00
|
|
|
package fileset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
2024-05-30 07:41:50 +00:00
|
|
|
"path"
|
2023-08-18 08:07:25 +00:00
|
|
|
"slices"
|
2023-10-10 08:45:15 +00:00
|
|
|
"strings"
|
2023-08-18 08:07:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2023-08-18 08:07:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func collectRelativePaths(files []File) []string {
|
|
|
|
relativePaths := make([]string, 0)
|
|
|
|
for _, f := range files {
|
|
|
|
relativePaths = append(relativePaths, f.Relative)
|
|
|
|
}
|
|
|
|
return relativePaths
|
|
|
|
}
|
2023-08-18 08:07:25 +00:00
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func TestGlobFileset(t *testing.T) {
|
|
|
|
root := vfs.MustNew("../filer")
|
|
|
|
entries, err := root.ReadDir(".")
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
g, err := NewGlobSet(root, []string{
|
|
|
|
"./*.go",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := g.All()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(files), len(entries))
|
|
|
|
for _, f := range files {
|
|
|
|
exists := slices.ContainsFunc(entries, func(de fs.DirEntry) bool {
|
2024-05-30 07:41:50 +00:00
|
|
|
return de.Name() == path.Base(f.Relative)
|
2023-08-18 08:07:25 +00:00
|
|
|
})
|
|
|
|
require.True(t, exists)
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err = NewGlobSet(root, []string{
|
|
|
|
"./*.js",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err = g.All()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(files), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobFilesetWithRelativeRoot(t *testing.T) {
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew("../filer")
|
|
|
|
entries, err := root.ReadDir(".")
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
g, err := NewGlobSet(root, []string{
|
|
|
|
"./*.go",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := g.All()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(files), len(entries))
|
|
|
|
}
|
2023-10-10 08:45:15 +00:00
|
|
|
|
|
|
|
func TestGlobFilesetRecursively(t *testing.T) {
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew("../git")
|
2023-10-10 08:45:15 +00:00
|
|
|
entries := make([]string, 0)
|
2024-05-30 07:41:50 +00:00
|
|
|
err := fs.WalkDir(root, "testdata", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if !d.IsDir() {
|
2023-10-10 08:45:15 +00:00
|
|
|
entries = append(entries, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
g, err := NewGlobSet(root, []string{
|
|
|
|
"testdata/*",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := g.All()
|
|
|
|
require.NoError(t, err)
|
2024-05-30 07:41:50 +00:00
|
|
|
require.ElementsMatch(t, entries, collectRelativePaths(files))
|
2023-10-10 08:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobFilesetDir(t *testing.T) {
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew("../git")
|
2023-10-10 08:45:15 +00:00
|
|
|
entries := make([]string, 0)
|
2024-05-30 07:41:50 +00:00
|
|
|
err := fs.WalkDir(root, "testdata/a", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if !d.IsDir() {
|
2023-10-10 08:45:15 +00:00
|
|
|
entries = append(entries, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
g, err := NewGlobSet(root, []string{
|
|
|
|
"testdata/a",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := g.All()
|
|
|
|
require.NoError(t, err)
|
2024-05-30 07:41:50 +00:00
|
|
|
require.ElementsMatch(t, entries, collectRelativePaths(files))
|
2023-10-10 08:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobFilesetDoubleQuotesWithFilePatterns(t *testing.T) {
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew("../git")
|
2023-10-10 08:45:15 +00:00
|
|
|
entries := make([]string, 0)
|
2024-05-30 07:41:50 +00:00
|
|
|
err := fs.WalkDir(root, "testdata", func(path string, d fs.DirEntry, err error) error {
|
2023-10-10 08:45:15 +00:00
|
|
|
if strings.HasSuffix(path, ".txt") {
|
|
|
|
entries = append(entries, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
g, err := NewGlobSet(root, []string{
|
|
|
|
"testdata/**/*.txt",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
files, err := g.All()
|
|
|
|
require.NoError(t, err)
|
2024-05-30 07:41:50 +00:00
|
|
|
require.ElementsMatch(t, entries, collectRelativePaths(files))
|
2023-10-10 08:45:15 +00:00
|
|
|
}
|