2023-08-18 08:07:25 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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 createFile(dir string, name string) error {
|
|
|
|
f, err := os.Create(filepath.Join(dir, name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupFiles(t *testing.T) string {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
err := createFile(dir, "a.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dir, "b.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dir, "ab.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dir, "abc.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dir, "c.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dir, "d.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dbDir := filepath.Join(dir, ".databricks")
|
|
|
|
err = os.Mkdir(dbDir, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(dbDir, "e.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-10 08:45:15 +00:00
|
|
|
testDir := filepath.Join(dir, "test")
|
|
|
|
err = os.Mkdir(testDir, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sub1 := filepath.Join(testDir, "sub1")
|
|
|
|
err = os.Mkdir(sub1, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(sub1, "f.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sub2 := filepath.Join(sub1, "sub2")
|
|
|
|
err = os.Mkdir(sub2, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = createFile(sub2, "g.go")
|
|
|
|
require.NoError(t, err)
|
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)
|
2023-10-10 08:45:15 +00:00
|
|
|
require.Equal(t, len(fileList), 9)
|
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)
|
|
|
|
require.Equal(t, len(fileList), 1)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
inc, err = fileset.NewGlobSet(root, []string{".databricks/*"})
|
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)
|
2023-10-10 08:45:15 +00:00
|
|
|
require.Equal(t, len(fileList), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|