Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for negation pattern in sync include exclude section #1637

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 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,11 @@ 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.
// If it doesn't, it means that negation pattern is not matching any files.
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
p := strings.TrimPrefix(fullPattern, "!")
errs.Go(func() error {
fs, err := fileset.NewGlobSet(rb.BundleRoot(), []string{p})
if err != nil {
Expand All @@ -66,7 +71,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) {
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
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")
}
42 changes: 38 additions & 4 deletions libs/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func setupFiles(t *testing.T) string {
err = createFile(sub2, "g.go")
require.NoError(t, err)

err = createFile(sub2, "h.txt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend creating these files and directories using for loops, to make the code terser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shreyas-goenka we can't really because we need predictable structure and the files are located in different subdirectories so using loops will make it even less readable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about simplifying it like this? The file tree structure is a lot more evident in this case.

for p in {
  []string{dir, "a.go"},
  []string{dir, "b.go"},
  ...
  []string{dir, ".databricks", "e.go"}
  ...
  []string{dir, "test", "sub1", "f.go"}
} {
  testutil.Touch(t, p...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a good one, thanks!

require.NoError(t, err)

return dir
}

Expand Down Expand Up @@ -97,7 +100,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 +118,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 +136,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 +168,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")
}
Loading