This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new c5777c3  fix: Improve how SanitizeName handles paths and URLs
c5777c3 is described below

commit c5777c33b958b07186dc54facaabfad80ac8077c
Author: James Netherton <jamesnether...@gmail.com>
AuthorDate: Wed Mar 13 09:06:59 2019 +0000

    fix: Improve how SanitizeName handles paths and URLs
    
    fixes #548
---
 pkg/util/kubernetes/sanitize.go      | 3 +--
 pkg/util/kubernetes/sanitize_test.go | 6 +++++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/pkg/util/kubernetes/sanitize.go b/pkg/util/kubernetes/sanitize.go
index 46428d9..fd6bef7 100644
--- a/pkg/util/kubernetes/sanitize.go
+++ b/pkg/util/kubernetes/sanitize.go
@@ -30,9 +30,8 @@ var disallowedChars = regexp.MustCompile(`[^a-z0-9-]`)
 
 // SanitizeName sanitizes the given name to be compatible with k8s
 func SanitizeName(name string) string {
-       name = strings.TrimPrefix(name, "./")
-       name = strings.Split(name, ".")[0]
        name = path.Base(name)
+       name = strings.Split(name, ".")[0]
        name = scase.KebabCase(name)
        name = strings.ToLower(name)
        name = disallowedChars.ReplaceAllString(name, "")
diff --git a/pkg/util/kubernetes/sanitize_test.go 
b/pkg/util/kubernetes/sanitize_test.go
index 608b612..839bf05 100644
--- a/pkg/util/kubernetes/sanitize_test.go
+++ b/pkg/util/kubernetes/sanitize_test.go
@@ -24,16 +24,20 @@ import (
 func TestSanitizeName(t *testing.T) {
        cases := []map[string]string{
                {"input": "./abc.java", "expect": "abc"},
+               {"input": "../../abc.java", "expect": "abc"},
                {"input": "/path/to/abc.js", "expect": "abc"},
                {"input": "abc.xml", "expect": "abc"},
                {"input": "./path/to/abc.kts", "expect": "abc"},
                {"input": "fooToBar.groovy", "expect": "foo-to-bar"},
                {"input": "foo-to-bar", "expect": "foo-to-bar"},
+               {"input": "http://foo.bar.com/cheese/wine/beer/abc.java";, 
"expect": "abc"},
+               {"input": "http://foo.bar.com/cheese";, "expect": "cheese"},
+               {"input": "http://foo.bar.com";, "expect": "foo"},
        }
 
        for _, c := range cases {
                if name := SanitizeName(c["input"]); name != c["expect"] {
-                       t.Errorf("result of %s should be %s, instead of %s", 
c["input"], c["output"], name)
+                       t.Errorf("result of %s should be %s, instead of %s", 
c["input"], c["expect"], name)
                }
        }
 }

Reply via email to