Skip to content

Commit

Permalink
Merge pull request #2176 from keboola/petr-hosek-PSGO-898
Browse files Browse the repository at this point in the history
fix: Fix templates tests
  • Loading branch information
hosekpeter authored Dec 13, 2024
2 parents f351423 + da740fa commit ff18572
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
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)
})
}
}

0 comments on commit ff18572

Please sign in to comment.