2023-08-18 08:07:25 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-31 13:42:23 +00:00
|
|
|
"github.com/databricks/cli/internal/testutil"
|
2023-08-18 08:07:25 +00:00
|
|
|
"github.com/databricks/cli/libs/fileset"
|
|
|
|
"github.com/databricks/cli/libs/git"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupFiles(t *testing.T) string {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
2024-07-31 13:42:23 +00:00
|
|
|
for _, f := range []([]string){
|
|
|
|
[]string{dir, "a.go"},
|
|
|
|
[]string{dir, "b.go"},
|
|
|
|
[]string{dir, "ab.go"},
|
|
|
|
[]string{dir, "abc.go"},
|
|
|
|
[]string{dir, "c.go"},
|
|
|
|
[]string{dir, "d.go"},
|
|
|
|
[]string{dir, ".databricks", "e.go"},
|
|
|
|
[]string{dir, "test", "sub1", "f.go"},
|
|
|
|
[]string{dir, "test", "sub1", "sub2", "g.go"},
|
|
|
|
[]string{dir, "test", "sub1", "sub2", "h.txt"},
|
|
|
|
} {
|
|
|
|
testutil.Touch(t, f...)
|
|
|
|
}
|
2023-08-18 08:07:25 +00:00
|
|
|
|
2023-10-10 08:45:15 +00:00
|
|
|
return dir
|
2023-08-18 08:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFileSet(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dir := setupFiles(t)
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew(dir)
|
|
|
|
fileSet, err := git.NewFileSet(root)
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fileSet.EnsureValidGitIgnoreExists()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
inc, err := fileset.NewGlobSet(root, []string{})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
excl, err := fileset.NewGlobSet(root, []string{})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := &Sync{
|
|
|
|
SyncOptions: &SyncOptions{},
|
|
|
|
|
|
|
|
fileSet: fileSet,
|
|
|
|
includeFileSet: inc,
|
|
|
|
excludeFileSet: excl,
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
fileList, err := s.GetFileList(ctx)
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
2024-07-31 13:42:23 +00:00
|
|
|
require.Equal(t, len(fileList), 10)
|
2023-08-18 08:07:25 +00:00
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
inc, err = fileset.NewGlobSet(root, []string{})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
excl, err = fileset.NewGlobSet(root, []string{"*.go"})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s = &Sync{
|
|
|
|
SyncOptions: &SyncOptions{},
|
|
|
|
|
|
|
|
fileSet: fileSet,
|
|
|
|
includeFileSet: inc,
|
|
|
|
excludeFileSet: excl,
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
fileList, err = s.GetFileList(ctx)
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
2024-07-31 13:42:23 +00:00
|
|
|
require.Equal(t, len(fileList), 2)
|
2023-08-18 08:07:25 +00:00
|
|
|
|
2024-07-31 13:42:23 +00:00
|
|
|
inc, err = fileset.NewGlobSet(root, []string{"./.databricks/*.go"})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
excl, err = fileset.NewGlobSet(root, []string{})
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s = &Sync{
|
|
|
|
SyncOptions: &SyncOptions{},
|
|
|
|
|
|
|
|
fileSet: fileSet,
|
|
|
|
includeFileSet: inc,
|
|
|
|
excludeFileSet: excl,
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
fileList, err = s.GetFileList(ctx)
|
2023-08-18 08:07:25 +00:00
|
|
|
require.NoError(t, err)
|
2024-07-31 13:42:23 +00:00
|
|
|
require.Equal(t, len(fileList), 11)
|
2023-10-10 08:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecursiveExclude(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dir := setupFiles(t)
|
2024-05-30 07:41:50 +00:00
|
|
|
root := vfs.MustNew(dir)
|
|
|
|
fileSet, err := git.NewFileSet(root)
|
2023-10-10 08:45:15 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-18 08:07:25 +00:00
|
|
|
|
2023-10-10 08:45:15 +00:00
|
|
|
err = fileSet.EnsureValidGitIgnoreExists()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
inc, err := fileset.NewGlobSet(root, []string{})
|
2023-10-10 08:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
excl, err := fileset.NewGlobSet(root, []string{"test/**"})
|
2023-10-10 08:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := &Sync{
|
|
|
|
SyncOptions: &SyncOptions{},
|
|
|
|
|
|
|
|
fileSet: fileSet,
|
|
|
|
includeFileSet: inc,
|
|
|
|
excludeFileSet: excl,
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
fileList, err := s.GetFileList(ctx)
|
2023-10-10 08:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(fileList), 7)
|
2023-08-18 08:07:25 +00:00
|
|
|
}
|
2024-07-31 13:42:23 +00:00
|
|
|
|
|
|
|
func TestNegateExclude(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dir := setupFiles(t)
|
|
|
|
root := vfs.MustNew(dir)
|
|
|
|
fileSet, err := git.NewFileSet(root)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = fileSet.EnsureValidGitIgnoreExists()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
inc, err := fileset.NewGlobSet(root, []string{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
excl, err := fileset.NewGlobSet(root, []string{"./*", "!*.txt"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := &Sync{
|
|
|
|
SyncOptions: &SyncOptions{},
|
|
|
|
|
|
|
|
fileSet: fileSet,
|
|
|
|
includeFileSet: inc,
|
|
|
|
excludeFileSet: excl,
|
|
|
|
}
|
|
|
|
|
|
|
|
fileList, err := s.GetFileList(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(fileList), 1)
|
|
|
|
require.Equal(t, fileList[0].Relative, "test/sub1/sub2/h.txt")
|
|
|
|
}
|