Skip to content

Commit

Permalink
fix(slugify): limited slug is not idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-igrychev committed Feb 3, 2022
1 parent 70b3f7a commit 4b06e8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/slug/slug.go
Expand Up @@ -30,7 +30,7 @@ func Slug(data string) string {
}

func LimitedSlug(data string, slugMaxSize int) string {
if len(data) == 0 || slugify(data) == data && len(data) < slugMaxSize {
if len(data) == 0 || slugify(data) == data && len(data) <= slugMaxSize {
return data
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/slug/slug_test.go
Expand Up @@ -47,6 +47,10 @@ func TestSlug(t *testing.T) {
if len(result) > DefaultSlugMaxSize {
t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", DefaultSlugMaxSize, len(result))
}

tRunIdempotence(t, test.name, test.data, func(s string) string {
return LimitedSlug(s, DefaultSlugMaxSize)
})
})
}
}
Expand Down Expand Up @@ -90,6 +94,8 @@ func TestDockerTag(t *testing.T) {
t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", dockerTagMaxSize, len(result))
}
})

tRunIdempotence(t, test.name, test.data, DockerTag)
}
}

Expand Down Expand Up @@ -137,6 +143,8 @@ func TestHelmRelease(t *testing.T) {
t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", helmReleaseMaxSize, len(result))
}
})

tRunIdempotence(t, test.name, test.data, HelmRelease)
}
}

Expand Down Expand Up @@ -179,5 +187,17 @@ func TestKubernetesNamespace(t *testing.T) {
t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", kubernetesNamespaceMaxSize, len(result))
}
})

tRunIdempotence(t, test.name, test.data, KubernetesNamespace)
}
}

func tRunIdempotence(t *testing.T, testName, testData string, slugger func(string) string) {
t.Run(testName+"-idempotence", func(t *testing.T) {
firstResult := slugger(testData)
secondResult := slugger(firstResult)
if firstResult != secondResult {
t.Errorf("\n[EXPECTED]: %s (%d)\n[GOT]: %s (%d)", firstResult, len(firstResult), secondResult, len(secondResult))
}
})
}

0 comments on commit 4b06e8a

Please sign in to comment.