mirror of https://github.com/databricks/cli.git
Simplify replacements logic for golden files (#2132)
## Changes - Do not sort, use fixed order of replacements. ## Tests Existing tests.
This commit is contained in:
parent
2b452973f3
commit
98a1e73a0f
|
@ -6,7 +6,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -17,6 +16,10 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
testerName = "$USERNAME"
|
||||||
|
)
|
||||||
|
|
||||||
var OverwriteMode = false
|
var OverwriteMode = false
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -165,7 +168,7 @@ func PrepareReplacements(t testutil.TestingT, r *ReplacementsContext, w *databri
|
||||||
r.Set(w.Config.AzureResourceID, "$DATABRICKS_AZURE_RESOURCE_ID")
|
r.Set(w.Config.AzureResourceID, "$DATABRICKS_AZURE_RESOURCE_ID")
|
||||||
r.Set(w.Config.AzureClientSecret, "$ARM_CLIENT_SECRET")
|
r.Set(w.Config.AzureClientSecret, "$ARM_CLIENT_SECRET")
|
||||||
// r.Set(w.Config.AzureClientID, "$ARM_CLIENT_ID")
|
// r.Set(w.Config.AzureClientID, "$ARM_CLIENT_ID")
|
||||||
r.Set(w.Config.AzureClientID, "$USERNAME")
|
r.Set(w.Config.AzureClientID, testerName)
|
||||||
r.Set(w.Config.AzureTenantID, "$ARM_TENANT_ID")
|
r.Set(w.Config.AzureTenantID, "$ARM_TENANT_ID")
|
||||||
r.Set(w.Config.ActionsIDTokenRequestURL, "$ACTIONS_ID_TOKEN_REQUEST_URL")
|
r.Set(w.Config.ActionsIDTokenRequestURL, "$ACTIONS_ID_TOKEN_REQUEST_URL")
|
||||||
r.Set(w.Config.ActionsIDTokenRequestToken, "$ACTIONS_ID_TOKEN_REQUEST_TOKEN")
|
r.Set(w.Config.ActionsIDTokenRequestToken, "$ACTIONS_ID_TOKEN_REQUEST_TOKEN")
|
||||||
|
@ -181,24 +184,20 @@ func PrepareReplacementsUser(t testutil.TestingT, r *ReplacementsContext, u iam.
|
||||||
t.Helper()
|
t.Helper()
|
||||||
// There could be exact matches or overlap between different name fields, so sort them by length
|
// There could be exact matches or overlap between different name fields, so sort them by length
|
||||||
// to ensure we match the largest one first and map them all to the same token
|
// to ensure we match the largest one first and map them all to the same token
|
||||||
names := []string{
|
|
||||||
u.DisplayName,
|
|
||||||
u.UserName,
|
|
||||||
iamutil.GetShortUserName(&u),
|
|
||||||
}
|
|
||||||
if u.Name != nil {
|
|
||||||
names = append(names, u.Name.FamilyName)
|
|
||||||
names = append(names, u.Name.GivenName)
|
|
||||||
}
|
|
||||||
for _, val := range u.Emails {
|
|
||||||
names = append(names, val.Value)
|
|
||||||
}
|
|
||||||
stableSortReverseLength(names)
|
|
||||||
|
|
||||||
for _, name := range names {
|
r.Set(u.UserName, testerName)
|
||||||
r.Set(name, "$USERNAME")
|
r.Set(u.DisplayName, testerName)
|
||||||
|
if u.Name != nil {
|
||||||
|
r.Set(u.Name.FamilyName, testerName)
|
||||||
|
r.Set(u.Name.GivenName, testerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, val := range u.Emails {
|
||||||
|
r.Set(val.Value, testerName)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Set(iamutil.GetShortUserName(&u), testerName)
|
||||||
|
|
||||||
for ind, val := range u.Groups {
|
for ind, val := range u.Groups {
|
||||||
r.Set(val.Value, fmt.Sprintf("$USER.Groups[%d]", ind))
|
r.Set(val.Value, fmt.Sprintf("$USER.Groups[%d]", ind))
|
||||||
}
|
}
|
||||||
|
@ -210,12 +209,6 @@ func PrepareReplacementsUser(t testutil.TestingT, r *ReplacementsContext, u iam.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stableSortReverseLength(strs []string) {
|
|
||||||
slices.SortStableFunc(strs, func(a, b string) int {
|
|
||||||
return len(b) - len(a)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NormalizeNewlines(input string) string {
|
func NormalizeNewlines(input string) string {
|
||||||
output := strings.ReplaceAll(input, "\r\n", "\n")
|
output := strings.ReplaceAll(input, "\r\n", "\n")
|
||||||
return strings.ReplaceAll(output, "\r", "\n")
|
return strings.ReplaceAll(output, "\r", "\n")
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package testdiff
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSort(t *testing.T) {
|
|
||||||
input := []string{"a", "bc", "cd"}
|
|
||||||
stableSortReverseLength(input)
|
|
||||||
assert.Equal(t, []string{"bc", "cd", "a"}, input)
|
|
||||||
}
|
|
Loading…
Reference in New Issue