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"
|
2023-08-02 10:18:19 +00:00
|
|
|
"path"
|
2022-11-28 10:34:25 +00:00
|
|
|
"runtime"
|
2023-09-11 08:18:43 +00:00
|
|
|
"strings"
|
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"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/bundle/env"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/cli/internal/testutil"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessRootIncludesEmpty(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2022-11-28 09:59:43 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: ".",
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2022-11-28 09:59:43 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: ".",
|
|
|
|
Include: []string{
|
|
|
|
"/tmp/*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.True(t, diags.HasError())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "must be relative paths")
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesSingleGlob(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2022-11-28 09:59:43 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
testutil.Touch(t, b.Config.Path, "databricks.yml")
|
|
|
|
testutil.Touch(t, b.Config.Path, "a.yml")
|
|
|
|
testutil.Touch(t, b.Config.Path, "b.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, []string{"a.yml", "b.yml"}, b.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesMultiGlob(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2022-11-28 09:59:43 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"a*.yml",
|
|
|
|
"b*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
testutil.Touch(t, b.Config.Path, "a1.yml")
|
|
|
|
testutil.Touch(t, b.Config.Path, "b1.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, []string{"a1.yml", "b1.yml"}, b.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesRemoveDups(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2022-11-28 09:59:43 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"*.yml",
|
|
|
|
"*.yml",
|
|
|
|
},
|
2022-11-18 09:57:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:41:58 +00:00
|
|
|
testutil.Touch(t, b.Config.Path, "a.yml")
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, []string{"a.yml"}, b.Config.Include)
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-07-07 10:22:58 +00:00
|
|
|
|
|
|
|
func TestProcessRootIncludesNotExists(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-07-07 10:22:58 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: t.TempDir(),
|
|
|
|
Include: []string{
|
|
|
|
"notexist.yml",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.True(t, diags.HasError())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "notexist.yml defined in 'include' section does not match any files")
|
2023-07-07 10:22:58 +00:00
|
|
|
}
|
2023-08-02 10:18:19 +00:00
|
|
|
|
|
|
|
func TestProcessRootIncludesExtrasFromEnvVar(t *testing.T) {
|
|
|
|
rootPath := t.TempDir()
|
|
|
|
testYamlName := "extra_include_path.yml"
|
2024-03-18 14:41:58 +00:00
|
|
|
testutil.Touch(t, rootPath, testYamlName)
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.IncludesVariable, path.Join(rootPath, testYamlName))
|
2023-08-02 10:18:19 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-02 10:18:19 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: rootPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Contains(t, b.Config.Include, testYamlName)
|
2023-08-02 10:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessRootIncludesDedupExtrasFromEnvVar(t *testing.T) {
|
|
|
|
rootPath := t.TempDir()
|
|
|
|
testYamlName := "extra_include_path.yml"
|
2024-03-18 14:41:58 +00:00
|
|
|
testutil.Touch(t, rootPath, testYamlName)
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.IncludesVariable, strings.Join(
|
|
|
|
[]string{
|
|
|
|
path.Join(rootPath, testYamlName),
|
|
|
|
path.Join(rootPath, testYamlName),
|
|
|
|
},
|
|
|
|
string(os.PathListSeparator),
|
|
|
|
))
|
2023-08-02 10:18:19 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-02 10:18:19 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Path: rootPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.ProcessRootIncludes())
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, []string{testYamlName}, b.Config.Include)
|
2023-08-02 10:18:19 +00:00
|
|
|
}
|