mirror of https://github.com/databricks/cli.git
Added test for negation pattern in sync include exclude section (#1637)
## Changes Added test for negation pattern in sync include exclude section
This commit is contained in:
parent
89c0af5bdc
commit
1fb8e324d5
|
@ -3,6 +3,7 @@ package validate
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
|
@ -49,7 +50,13 @@ func checkPatterns(patterns []string, path string, rb bundle.ReadOnlyBundle) (di
|
||||||
|
|
||||||
for i, pattern := range patterns {
|
for i, pattern := range patterns {
|
||||||
index := i
|
index := i
|
||||||
p := pattern
|
fullPattern := pattern
|
||||||
|
// If the pattern is negated, strip the negation prefix
|
||||||
|
// and check if the pattern matches any files.
|
||||||
|
// Negation in gitignore syntax means "don't look at this path'
|
||||||
|
// So if p matches nothing it's useless negation, but if there are matches,
|
||||||
|
// it means: do not include these files into result set
|
||||||
|
p := strings.TrimPrefix(fullPattern, "!")
|
||||||
errs.Go(func() error {
|
errs.Go(func() error {
|
||||||
fs, err := fileset.NewGlobSet(rb.BundleRoot(), []string{p})
|
fs, err := fileset.NewGlobSet(rb.BundleRoot(), []string{p})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -66,7 +73,7 @@ func checkPatterns(patterns []string, path string, rb bundle.ReadOnlyBundle) (di
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
diags = diags.Append(diag.Diagnostic{
|
diags = diags.Append(diag.Diagnostic{
|
||||||
Severity: diag.Warning,
|
Severity: diag.Warning,
|
||||||
Summary: fmt.Sprintf("Pattern %s does not match any files", p),
|
Summary: fmt.Sprintf("Pattern %s does not match any files", fullPattern),
|
||||||
Locations: []dyn.Location{loc.Location()},
|
Locations: []dyn.Location{loc.Location()},
|
||||||
Paths: []dyn.Path{loc.Path()},
|
Paths: []dyn.Path{loc.Path()},
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
bundle:
|
||||||
|
name: sync_negate
|
||||||
|
|
||||||
|
workspace:
|
||||||
|
host: https://acme.cloud.databricks.com/
|
||||||
|
|
||||||
|
sync:
|
||||||
|
exclude:
|
||||||
|
- ./*
|
||||||
|
- '!*.txt'
|
||||||
|
include:
|
||||||
|
- '*.txt'
|
||||||
|
|
||||||
|
targets:
|
||||||
|
default:
|
||||||
|
dev:
|
||||||
|
sync:
|
||||||
|
exclude:
|
||||||
|
- ./*
|
||||||
|
- '!*.txt2'
|
||||||
|
include:
|
||||||
|
- '*.txt'
|
|
@ -42,3 +42,22 @@ func TestSyncIncludeExcludeNoMatchesTest(t *testing.T) {
|
||||||
require.Equal(t, diags[2].Severity, diag.Warning)
|
require.Equal(t, diags[2].Severity, diag.Warning)
|
||||||
require.Contains(t, summaries, diags[2].Summary)
|
require.Contains(t, summaries, diags[2].Summary)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSyncIncludeWithNegate(t *testing.T) {
|
||||||
|
b := loadTarget(t, "./sync/negate", "default")
|
||||||
|
|
||||||
|
diags := bundle.ApplyReadOnly(context.Background(), bundle.ReadOnly(b), validate.ValidateSyncPatterns())
|
||||||
|
require.Len(t, diags, 0)
|
||||||
|
require.NoError(t, diags.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSyncIncludeWithNegateNoMatches(t *testing.T) {
|
||||||
|
b := loadTarget(t, "./sync/negate", "dev")
|
||||||
|
|
||||||
|
diags := bundle.ApplyReadOnly(context.Background(), bundle.ReadOnly(b), validate.ValidateSyncPatterns())
|
||||||
|
require.Len(t, diags, 1)
|
||||||
|
require.NoError(t, diags.Error())
|
||||||
|
|
||||||
|
require.Equal(t, diags[0].Severity, diag.Warning)
|
||||||
|
require.Equal(t, diags[0].Summary, "Pattern !*.txt2 does not match any files")
|
||||||
|
}
|
||||||
|
|
|
@ -2,70 +2,32 @@ package sync
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/internal/testutil"
|
||||||
"github.com/databricks/cli/libs/fileset"
|
"github.com/databricks/cli/libs/fileset"
|
||||||
"github.com/databricks/cli/libs/git"
|
"github.com/databricks/cli/libs/git"
|
||||||
"github.com/databricks/cli/libs/vfs"
|
"github.com/databricks/cli/libs/vfs"
|
||||||
"github.com/stretchr/testify/require"
|
"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 {
|
func setupFiles(t *testing.T) string {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
err := createFile(dir, "a.go")
|
for _, f := range []([]string){
|
||||||
require.NoError(t, err)
|
[]string{dir, "a.go"},
|
||||||
|
[]string{dir, "b.go"},
|
||||||
err = createFile(dir, "b.go")
|
[]string{dir, "ab.go"},
|
||||||
require.NoError(t, err)
|
[]string{dir, "abc.go"},
|
||||||
|
[]string{dir, "c.go"},
|
||||||
err = createFile(dir, "ab.go")
|
[]string{dir, "d.go"},
|
||||||
require.NoError(t, err)
|
[]string{dir, ".databricks", "e.go"},
|
||||||
|
[]string{dir, "test", "sub1", "f.go"},
|
||||||
err = createFile(dir, "abc.go")
|
[]string{dir, "test", "sub1", "sub2", "g.go"},
|
||||||
require.NoError(t, err)
|
[]string{dir, "test", "sub1", "sub2", "h.txt"},
|
||||||
|
} {
|
||||||
err = createFile(dir, "c.go")
|
testutil.Touch(t, f...)
|
||||||
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)
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
return dir
|
return dir
|
||||||
}
|
}
|
||||||
|
@ -97,7 +59,7 @@ func TestGetFileSet(t *testing.T) {
|
||||||
|
|
||||||
fileList, err := s.GetFileList(ctx)
|
fileList, err := s.GetFileList(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(fileList), 9)
|
require.Equal(t, len(fileList), 10)
|
||||||
|
|
||||||
inc, err = fileset.NewGlobSet(root, []string{})
|
inc, err = fileset.NewGlobSet(root, []string{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -115,9 +77,9 @@ func TestGetFileSet(t *testing.T) {
|
||||||
|
|
||||||
fileList, err = s.GetFileList(ctx)
|
fileList, err = s.GetFileList(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(fileList), 1)
|
require.Equal(t, len(fileList), 2)
|
||||||
|
|
||||||
inc, err = fileset.NewGlobSet(root, []string{".databricks/*"})
|
inc, err = fileset.NewGlobSet(root, []string{"./.databricks/*.go"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
excl, err = fileset.NewGlobSet(root, []string{})
|
excl, err = fileset.NewGlobSet(root, []string{})
|
||||||
|
@ -133,7 +95,7 @@ func TestGetFileSet(t *testing.T) {
|
||||||
|
|
||||||
fileList, err = s.GetFileList(ctx)
|
fileList, err = s.GetFileList(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(fileList), 10)
|
require.Equal(t, len(fileList), 11)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRecursiveExclude(t *testing.T) {
|
func TestRecursiveExclude(t *testing.T) {
|
||||||
|
@ -165,3 +127,34 @@ func TestRecursiveExclude(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(fileList), 7)
|
require.Equal(t, len(fileList), 7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue