Simplify replacements logic for golden files (#2132)

## Changes
- Do not sort, use fixed order of replacements.

## Tests
Existing tests.
This commit is contained in:
Denis Bilenko 2025-01-14 12:00:38 +01:00 committed by GitHub
parent 2b452973f3
commit 98a1e73a0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 36 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strings"
"testing"
@ -17,6 +16,10 @@ import (
"github.com/stretchr/testify/assert"
)
const (
testerName = "$USERNAME"
)
var OverwriteMode = false
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.AzureClientSecret, "$ARM_CLIENT_SECRET")
// 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.ActionsIDTokenRequestURL, "$ACTIONS_ID_TOKEN_REQUEST_URL")
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()
// 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
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(name, "$USERNAME")
r.Set(u.UserName, testerName)
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 {
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 {
output := strings.ReplaceAll(input, "\r\n", "\n")
return strings.ReplaceAll(output, "\r", "\n")

View File

@ -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)
}