Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions bundle/config/mutator/process_root_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path/filepath"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
Expand Down Expand Up @@ -49,6 +50,12 @@ func (m *processRootIncludes) Apply(ctx context.Context, b *bundle.Bundle) error
return err
}

// If the entry is not a glob pattern and no matches found,
// return an error because the file defined is not found
if len(matches) == 0 && !strings.ContainsAny(entry, "*?[") {
return fmt.Errorf("%s defined in 'include' section does not match any files", entry)
}

// Filter matches to ones we haven't seen yet.
var includes []string
for _, match := range matches {
Expand Down
14 changes: 14 additions & 0 deletions bundle/config/mutator/process_root_includes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,17 @@ func TestProcessRootIncludesRemoveDups(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []string{"a.yml"}, bundle.Config.Include)
}

func TestProcessRootIncludesNotExists(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Path: t.TempDir(),
Include: []string{
"notexist.yml",
},
},
}
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
require.Error(t, err)
assert.Contains(t, err.Error(), "notexist.yml defined in 'include' section does not match any files")
}
2 changes: 1 addition & 1 deletion bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *Root) Load(path string) error {
}
err = yaml.Unmarshal(raw, r)
if err != nil {
return err
return fmt.Errorf("failed to load %s: %w", path, err)
}

r.Path = filepath.Dir(path)
Expand Down
5 changes: 5 additions & 0 deletions bundle/tests/include_invalid/bundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bundle:
name: include_invalid

include:
- notexists.yml
34 changes: 34 additions & 0 deletions bundle/tests/include_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config_tests

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

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestIncludeInvalid(t *testing.T) {
b, err := bundle.Load("./include_invalid")
require.NoError(t, err)
err = bundle.Apply(context.Background(), b, bundle.Seq(mutator.DefaultMutators()...))
require.Error(t, err)
assert.Contains(t, err.Error(), "notexists.yml defined in 'include' section does not match any files")
}

func TestIncludeWithGlob(t *testing.T) {
b := load(t, "./include_with_glob")

keys := maps.Keys(b.Config.Resources.Jobs)
sort.Strings(keys)
assert.Equal(t, []string{"my_job"}, keys)

job := b.Config.Resources.Jobs["my_job"]
assert.Equal(t, "1", job.ID)
assert.Equal(t, "include_with_glob/job.yml", filepath.ToSlash(job.ConfigFilePath))
}
7 changes: 7 additions & 0 deletions bundle/tests/include_with_glob/bundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bundle:
name: include_with_glob

include:
- "*.yml"
- "?.yml"
- "[a-z].yml"
4 changes: 4 additions & 0 deletions bundle/tests/include_with_glob/job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources:
jobs:
my_job:
id: 1