From babc13c1ae788d31a1b2191f3ed5d122fa50a4f8 Mon Sep 17 00:00:00 2001 From: crystaldust Date: Fri, 21 Dec 2018 17:32:20 +0800 Subject: [PATCH] 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 + pkg/util/kubernetes/sanitize_test.go | 37 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 pkg/util/kubernetes/sanitize_test.go diff --git a/pkg/util/kubernetes/sanitize.go b/pkg/util/kubernetes/sanitize.go index e3351a36fa..4e021707e4 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_test.go b/pkg/util/kubernetes/sanitize_test.go new file mode 100644 index 0000000000..43078ae820 --- /dev/null +++ b/pkg/util/kubernetes/sanitize_test.go @@ -0,0 +1,37 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubernetes + +import ( + "testing" +) + +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) + } + } +}