2022-11-18 09:57:31 +00:00
|
|
|
package mutator_test
|
|
|
|
|
|
|
|
import (
|
2022-11-28 09:59:43 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-11-28 10:34:25 +00:00
|
|
|
"runtime"
|
2022-11-18 09:57:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func touch(t *testing.T, path, file string) {
|
|
|
|
f, err := os.Create(filepath.Join(path, file))
|
|
|
|
require.NoError(t, err)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesEmpty(t *testing.T) {
|
2022-11-28 09:59:43 +00:00
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: ".",
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesAbs(t *testing.T) {
|
2022-11-28 10:34:25 +00:00
|
|
|
// remove this once equivalent tests for windows have been set up
|
|
|
|
// or this test has been fixed for windows
|
|
|
|
// date: 28 Nov 2022
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("skipping temperorilty to make windows unit tests green")
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: ".",
|
|
|
|
Include: []string{
|
|
|
|
"/tmp/*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "must be relative paths")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesSingleGlob(t *testing.T) {
|
2022-11-28 09:59:43 +00:00
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-18 10:16:34 +00:00
|
|
|
touch(t, bundle.Config.Path, "databricks.yml")
|
2022-11-28 09:59:43 +00:00
|
|
|
touch(t, bundle.Config.Path, "a.yml")
|
|
|
|
touch(t, bundle.Config.Path, "b.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-20 20:01:28 +00:00
|
|
|
assert.Equal(t, []string{"a.yml", "b.yml"}, bundle.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesMultiGlob(t *testing.T) {
|
2022-11-28 09:59:43 +00:00
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"a*.yml",
|
|
|
|
"b*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
touch(t, bundle.Config.Path, "a1.yml")
|
|
|
|
touch(t, bundle.Config.Path, "b1.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-20 20:01:28 +00:00
|
|
|
assert.Equal(t, []string{"a1.yml", "b1.yml"}, bundle.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesRemoveDups(t *testing.T) {
|
2022-11-28 09:59:43 +00:00
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"*.yml",
|
|
|
|
"*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
touch(t, bundle.Config.Path, "a.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
|
2022-11-18 09:57:31 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-20 20:01:28 +00:00
|
|
|
assert.Equal(t, []string{"a.yml"}, bundle.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-07-07 10:22:58 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|