Skip to content
Permalink
Browse files
fix(slugify): limited slug is not idempotent
  • Loading branch information
alexey-igrychev committed Feb 3, 2022
1 parent 70b3f7a commit 4b06e8a753d107f655e4376f9e9297a5be8cb82e
Showing with 21 additions and 1 deletion.
  1. +1 −1 pkg/slug/slug.go
  2. +20 −0 pkg/slug/slug_test.go
@@ -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
}

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

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

@@ -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.