Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix templates tests #2176

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions internal/pkg/utils/testhelper/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testhelper
import (
"context"
"fmt"
"regexp"
"strings"

"github.com/keboola/go-utils/pkg/wildcards"
Expand Down Expand Up @@ -54,9 +55,12 @@ func DirectoryContentsSame(ctx context.Context, expectedFs filesystem.Fs, expect
if err != nil {
return err
}
normalizeExpectedFile := normalizeString(expectedFile.Content)
normalizeActualFile := normalizeString(actualFile.Content)

err = wildcards.Compare(
expectedFile.Content,
actualFile.Content,
normalizeExpectedFile,
normalizeActualFile,
)
if err != nil {
return errors.PrefixErrorf(err, `different content of the file "%s"`, node.relPath)
Expand Down Expand Up @@ -116,8 +120,8 @@ func compareDirectories(ctx context.Context, expectedFs filesystem.Fs, expectedD
}

// Create node
hashMap[relPath] = &fileNodeState{
relPath: relPath,
hashMap[normalizeString(relPath)] = &fileNodeState{
relPath: normalizeString(relPath),
actual: &fileNode{info.IsDir(), path},
}

Expand Down Expand Up @@ -151,11 +155,11 @@ func compareDirectories(ctx context.Context, expectedFs filesystem.Fs, expectedD
}

// Create node if not exists
if _, ok := hashMap[relPath]; !ok {
hashMap[relPath] = &fileNodeState{}
if _, ok := hashMap[normalizeString(relPath)]; !ok {
hashMap[normalizeString(relPath)] = &fileNodeState{}
}
hashMap[relPath].relPath = relPath
hashMap[relPath].expected = &fileNode{info.IsDir(), path}
hashMap[normalizeString(relPath)].relPath = normalizeString(relPath)
hashMap[normalizeString(relPath)].expected = &fileNode{info.IsDir(), path}

return nil
})
Expand All @@ -165,3 +169,16 @@ func compareDirectories(ctx context.Context, expectedFs filesystem.Fs, expectedD

return hashMap
}

// normalizeString replaces numeric IDs in a string with "-%s".
func normalizeString(input string) string {
return regexp.MustCompile(`-\d+`).
ReplaceAllString(input, "-%s")
}

func Normalize(t assert.TestingT, input string) string {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return normalizeString(input)
}
41 changes: 41 additions & 0 deletions internal/pkg/utils/testhelper/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,44 @@ func TestAssertDirectorySameWildcards(t *testing.T) {
func newMockedT() *mockedT {
return &mockedT{buf: bytes.NewBuffer(nil)}
}

func TestNormalize(t *testing.T) {
t.Parallel()

type args struct {
input string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test with .json file",
args: args{
input: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake-10960305/task.json",
},
want: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake-%s/task.json",
},
{
name: "Test with .yaml file (numeric suffix at the end)",
args: args{
input: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake-98765432/task.yaml",
},
want: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake-%s/task.yaml",
},
{
name: "Test with path without digits before file extension",
args: args{
input: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake",
},
want: "/main/other/keboola.orchestrator/flow-mgmt-jira-instance/phases/003-load/001-keboola-wr-snowflake",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, Normalize(t, tt.args.input), "Normalize(%v, %v)", t, tt.args.input)
})
}
}
Loading