-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share test initializer in common helper function (#1695)
## 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
Showing
3 changed files
with
33 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
@@ -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] | ||
|