From d74461b4243ad512499534b1cc683a656d74ebc1 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 10 Dec 2024 12:48:04 +0000 Subject: [PATCH] fix: handle Organisation names containing a double space Signed-off-by: Paul Horton --- .github/workflows/build.yml | 4 +++ go.mod | 4 +++ go.sum | 4 +++ scm/types.go | 17 ++++++----- scm/types_test.go | 56 +++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 scm/types_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b141bf1..60199f8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,6 +28,10 @@ jobs: run: | go build -ldflags='-s -w' + - name: Run Tests + run: | + go test -v ./... + sonatype: name: Sonatype Lifecycle runs-on: ubuntu-latest diff --git a/go.mod b/go.mod index 3936b50..3d038ee 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,12 @@ toolchain go1.22.8 require github.com/sirupsen/logrus v1.9.3 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.10.0 // indirect golang.org/x/term v0.27.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( diff --git a/go.sum b/go.sum index a152104..79b38d0 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/sonatype-nexus-community/nexus-iq-api-client-go v0.184.3/go.mod h1:0l github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -24,3 +26,5 @@ golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/scm/types.go b/scm/types.go index 6a8a06a..34f1760 100644 --- a/scm/types.go +++ b/scm/types.go @@ -81,13 +81,16 @@ func (o *Organization) PrintTree(depth int) { } func (o *Organization) SafeName() string { - return strings.Map(func(r rune) rune { - if strings.Contains(BANNED_CHARS_NAME, string(r)) { - return '-' - } else { - return r - } - }, o.Name) + return strings.ReplaceAll( + strings.Map(func(r rune) rune { + if strings.Contains(BANNED_CHARS_NAME, string(r)) { + return '-' + } else { + return r + } + }, o.Name), + " ", "--", + ) } type OrgContents struct { diff --git a/scm/types_test.go b/scm/types_test.go new file mode 100644 index 0000000..ecd314b --- /dev/null +++ b/scm/types_test.go @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2019-present Sonatype, Inc. + * + * Licensed 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 scm + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestScmOrganisationSafeName(t *testing.T) { + + cases := []struct { + input string + expected string + }{ + { + input: "Name", + expected: "Name", + }, + { + input: "Name<", + expected: "Name-", + }, + { + input: "Name[something]", + expected: "Name-something-", + }, + { + input: "Name WithDoubleSpace", + expected: "Name--WithDoubleSpace", + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("TestScmOrganisationSafeName-%d-%s", i, tc.input), func(t *testing.T) { + o := Organization{Name: tc.input} + assert.Equal(t, tc.expected, o.SafeName()) + }) + } +}