Skip to content

Commit

Permalink
Added test for negation pattern in sync include exclude section (#1637)
Browse files Browse the repository at this point in the history
## Changes
Added test for negation pattern in sync include exclude section
  • Loading branch information
andrewnester authored Jul 31, 2024
1 parent 89c0af5 commit 1fb8e32
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 59 deletions.
11 changes: 9 additions & 2 deletions bundle/config/validate/validate_sync_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validate
import (
"context"
"fmt"
"strings"
"sync"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -49,7 +50,13 @@ func checkPatterns(patterns []string, path string, rb bundle.ReadOnlyBundle) (di

for i, pattern := range patterns {
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 {
fs, err := fileset.NewGlobSet(rb.BundleRoot(), []string{p})
if err != nil {
Expand All @@ -66,7 +73,7 @@ func checkPatterns(patterns []string, path string, rb bundle.ReadOnlyBundle) (di
mu.Lock()
diags = diags.Append(diag.Diagnostic{
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()},
Paths: []dyn.Path{loc.Path()},
})
Expand Down
22 changes: 22 additions & 0 deletions bundle/tests/sync/negate/databricks.yml
Original file line number Diff line number Diff line change
@@ -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'
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions bundle/tests/sync_include_exclude_no_matches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ func TestSyncIncludeExcludeNoMatchesTest(t *testing.T) {
require.Equal(t, diags[2].Severity, diag.Warning)
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")
}
107 changes: 50 additions & 57 deletions libs/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,32 @@ package sync

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/fileset"
"github.com/databricks/cli/libs/git"
"github.com/databricks/cli/libs/vfs"
"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)

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)
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...)
}

return dir
}
Expand Down Expand Up @@ -97,7 +59,7 @@ func TestGetFileSet(t *testing.T) {

fileList, err := s.GetFileList(ctx)
require.NoError(t, err)
require.Equal(t, len(fileList), 9)
require.Equal(t, len(fileList), 10)

inc, err = fileset.NewGlobSet(root, []string{})
require.NoError(t, err)
Expand All @@ -115,9 +77,9 @@ func TestGetFileSet(t *testing.T) {

fileList, err = s.GetFileList(ctx)
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)

excl, err = fileset.NewGlobSet(root, []string{})
Expand All @@ -133,7 +95,7 @@ func TestGetFileSet(t *testing.T) {

fileList, err = s.GetFileList(ctx)
require.NoError(t, err)
require.Equal(t, len(fileList), 10)
require.Equal(t, len(fileList), 11)
}

func TestRecursiveExclude(t *testing.T) {
Expand Down Expand Up @@ -165,3 +127,34 @@ func TestRecursiveExclude(t *testing.T) {
require.NoError(t, err)
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")
}

0 comments on commit 1fb8e32

Please sign in to comment.