Skip to content

Commit

Permalink
Share test initializer in common helper function (#1695)
Browse files Browse the repository at this point in the history
## Changes

These tests inadvertently re-ran mutators, the first time through
`loadTarget` and the second time by running `phases.Initialize()`
themselves. Some of the mutators that are executed in
`phases.Initialize()` are also run as part of `loadTarget`. This is
overdue a refactor to make it unambiguous what runs when. Until then,
this removes the duplicated execution.

## Tests

Unit tests pass.
  • Loading branch information
pietern authored Aug 20, 2024
1 parent 6771ba0 commit af5048e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 62 deletions.
29 changes: 29 additions & 0 deletions bundle/tests/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -43,3 +47,28 @@ func loadTargetWithDiags(path, env string) (*bundle.Bundle, diag.Diagnostics) {
))
return b, diags
}

func configureMock(t *testing.T, b *bundle.Bundle) {
// Configure mock workspace client
m := mocks.NewMockWorkspaceClient(t)
m.WorkspaceClient.Config = &config.Config{
Host: "https://mock.databricks.workspace.com",
}
m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything).Return(&iam.User{
UserName: "[email protected]",
}, nil)
b.SetWorkpaceClient(m.WorkspaceClient)
}

func initializeTarget(t *testing.T, path, env string) (*bundle.Bundle, diag.Diagnostics) {
b := load(t, path)
configureMock(t, b)

ctx := context.Background()
diags := bundle.Apply(ctx, b, bundle.Seq(
mutator.SelectTarget(env),
phases.Initialize(),
))

return b, diags
}
37 changes: 2 additions & 35 deletions bundle/tests/pipeline_glob_paths_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
package config_tests

import (
"context"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestExpandPipelineGlobPaths(t *testing.T) {
b := loadTarget(t, "./pipeline_glob_paths", "default")

// Configure mock workspace client
m := mocks.NewMockWorkspaceClient(t)
m.WorkspaceClient.Config = &config.Config{
Host: "https://mock.databricks.workspace.com",
}
m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything).Return(&iam.User{
UserName: "[email protected]",
}, nil)
b.SetWorkpaceClient(m.WorkspaceClient)

ctx := context.Background()
diags := bundle.Apply(ctx, b, phases.Initialize())
b, diags := initializeTarget(t, "./pipeline_glob_paths", "default")
require.NoError(t, diags.Error())
require.Equal(
t,
Expand All @@ -37,19 +17,6 @@ func TestExpandPipelineGlobPaths(t *testing.T) {
}

func TestExpandPipelineGlobPathsWithNonExistent(t *testing.T) {
b := loadTarget(t, "./pipeline_glob_paths", "error")

// Configure mock workspace client
m := mocks.NewMockWorkspaceClient(t)
m.WorkspaceClient.Config = &config.Config{
Host: "https://mock.databricks.workspace.com",
}
m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything).Return(&iam.User{
UserName: "[email protected]",
}, nil)
b.SetWorkpaceClient(m.WorkspaceClient)

ctx := context.Background()
diags := bundle.Apply(ctx, b, phases.Initialize())
_, diags := initializeTarget(t, "./pipeline_glob_paths", "error")
require.ErrorContains(t, diags.Error(), "notebook ./non-existent not found")
}
29 changes: 2 additions & 27 deletions bundle/tests/relative_path_translation_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
package config_tests

import (
"context"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func configureMock(t *testing.T, b *bundle.Bundle) {
// Configure mock workspace client
m := mocks.NewMockWorkspaceClient(t)
m.WorkspaceClient.Config = &config.Config{
Host: "https://mock.databricks.workspace.com",
}
m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything).Return(&iam.User{
UserName: "[email protected]",
}, nil)
b.SetWorkpaceClient(m.WorkspaceClient)
}

func TestRelativePathTranslationDefault(t *testing.T) {
b := loadTarget(t, "./relative_path_translation", "default")
configureMock(t, b)

diags := bundle.Apply(context.Background(), b, phases.Initialize())
b, diags := initializeTarget(t, "./relative_path_translation", "default")
require.NoError(t, diags.Error())

t0 := b.Config.Resources.Jobs["job"].Tasks[0]
Expand All @@ -40,10 +18,7 @@ func TestRelativePathTranslationDefault(t *testing.T) {
}

func TestRelativePathTranslationOverride(t *testing.T) {
b := loadTarget(t, "./relative_path_translation", "override")
configureMock(t, b)

diags := bundle.Apply(context.Background(), b, phases.Initialize())
b, diags := initializeTarget(t, "./relative_path_translation", "override")
require.NoError(t, diags.Error())

t0 := b.Config.Resources.Jobs["job"].Tasks[0]
Expand Down

0 comments on commit af5048e

Please sign in to comment.