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

acosentino 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 9da7fd0  Remove the "./" prefix before sanitizing the integration 
name. (#309)
9da7fd0 is described below

commit 9da7fd0e0d6794ef09573a0f8fb0560a0a2ca1b6
Author: crystaldust <[email protected]>
AuthorDate: Fri Dec 21 17:32:20 2018 +0800

    Remove the "./" prefix before sanitizing the integration name. (#309)
    
    Makes the paths like "./integration_name.java" work
    
    Add the unit test file for the sanitization
---
 pkg/util/kubernetes/sanitize.go                    |  1 +
 .../kubernetes/{sanitize.go => sanitize_test.go}   | 35 +++++++++-------------
 2 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/pkg/util/kubernetes/sanitize.go b/pkg/util/kubernetes/sanitize.go
index e3351a3..4e02170 100644
--- a/pkg/util/kubernetes/sanitize.go
+++ b/pkg/util/kubernetes/sanitize.go
@@ -30,6 +30,7 @@ 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 = strcase.KebabCase(name)
diff --git a/pkg/util/kubernetes/sanitize.go 
b/pkg/util/kubernetes/sanitize_test.go
similarity index 58%
copy from pkg/util/kubernetes/sanitize.go
copy to pkg/util/kubernetes/sanitize_test.go
index e3351a3..43078ae 100644
--- a/pkg/util/kubernetes/sanitize.go
+++ b/pkg/util/kubernetes/sanitize_test.go
@@ -18,27 +18,20 @@ limitations under the License.
 package kubernetes
 
 import (
-       "path"
-       "regexp"
-       "strings"
-       "unicode"
-
-       "github.com/stoewer/go-strcase"
+       "testing"
 )
 
-var disallowedChars = regexp.MustCompile(`[^a-z0-9-]`)
-
-// SanitizeName sanitizes the given name to be compatible with k8s
-func SanitizeName(name string) string {
-       name = strings.Split(name, ".")[0]
-       name = path.Base(name)
-       name = strcase.KebabCase(name)
-       name = strings.ToLower(name)
-       name = disallowedChars.ReplaceAllString(name, "")
-       name = strings.TrimFunc(name, isDisallowedStartEndChar)
-       return name
-}
-
-func isDisallowedStartEndChar(rune rune) bool {
-       return !unicode.IsLetter(rune)
+func TestSanitizeName(t *testing.T) {
+       cases := []map[string]string{
+               {"input": "./abc.java", "expect": "abc"},
+               {"input": "/path/to/abc.js", "expect": "abc"},
+               {"input": "abc.xml", "expect": "abc"},
+               {"input": "./path/to/abc.kts", "expect": "abc"},
+       }
+
+       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)
+               }
+       }
 }

Reply via email to