Skip to content

Commit 5afeb4a

Browse files
patrick.rissmann@l3montree.compatrick.rissmann@l3montree.com
authored andcommitted
fixed a bug with leading slashes and wrote tests for thenew function
1 parent ed7ce99 commit 5afeb4a

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

internal/core/integrations/thirdparty_integration.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package integrations
33
import (
44
"bytes"
55
"context"
6-
"fmt"
76
"io"
87
"log/slog"
98
"strconv"
@@ -191,7 +190,7 @@ func RenderPathToComponent(componentRepository core.ComponentRepository, assetID
191190
var nodeContent string
192191

193192
for i, componentName := range componentList[1:] {
194-
fmt.Printf("Component Name: %s", componentName)
193+
195194
nodeContent, err = BeautifyPURL(componentName)
196195
if err != nil {
197196
nodeContent = componentName
@@ -212,6 +211,11 @@ func BeautifyPURL(pURL string) (string, error) {
212211
slog.Error("cannot convert to purl struct")
213212
return pURL, err
214213
}
214+
//if the namespace is empty we don't want any leading slashes
215+
if p.Namespace == "" {
216+
return p.Name, nil
217+
} else {
218+
return p.Namespace + "/" + p.Name, nil
219+
}
215220

216-
return p.Namespace + "/" + p.Name, nil
217221
}

internal/core/integrations/thirdparty_integration_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,26 @@ func TestRenderPathToComponent(t *testing.T) {
7373
})
7474
}
7575

76-
func TestFormatNode(t *testing.T) {
77-
t.Run("Empty String should also return an empty string back", func(t *testing.T) {
76+
func TestBeautifyPURL(t *testing.T) {
77+
t.Run("empty String should also return an empty string back", func(t *testing.T) {
7878
inputString := ""
79-
result := integrations.BeautifyPURL(inputString)
79+
result, _ := integrations.BeautifyPURL(inputString)
8080
assert.Equal(t, "", result)
8181
})
82-
t.Run("Should change nothing when there are less than 2 slashes in the input string", func(t *testing.T) {
83-
inputString := "StringWIthOnlyOne/"
84-
result := integrations.BeautifyPURL(inputString)
82+
t.Run("invalid purl format should also be returned unchanged", func(t *testing.T) {
83+
inputString := "this is definitely not a valid purl"
84+
result, _ := integrations.BeautifyPURL(inputString)
8585
assert.Equal(t, inputString, result)
8686
})
87-
t.Run("Should put a line break behind the second slash", func(t *testing.T) {
88-
inputString := "StringWIthOnlyOne//"
89-
result := integrations.BeautifyPURL(inputString)
90-
assert.Equal(t, "StringWIthOnlyOne//\n", result)
87+
t.Run("should return only the namespace and the name of a valid purl and cut the rest", func(t *testing.T) {
88+
inputString := "pkg:npm/@ory/integrations@v0.0.1"
89+
result, _ := integrations.BeautifyPURL(inputString)
90+
assert.Equal(t, "@ory/integrations", result)
9191
})
92-
t.Run("Should put a line break behind every second slash", func(t *testing.T) {
93-
inputString := "StringWIthOnlyOne//moreText/newText/nowTHefinalTextChallenge//"
94-
result := integrations.BeautifyPURL(inputString)
95-
assert.Equal(t, "StringWIthOnlyOne//\nmoreText/newText/\nnowTHefinalTextChallenge//\n", result)
92+
t.Run("should return no leading slash if the namespace is empty", func(t *testing.T) {
93+
inputString := "pkg:npm/integrations@v0.0.1"
94+
result, _ := integrations.BeautifyPURL(inputString)
95+
assert.Equal(t, "integrations", result)
9696
})
97+
9798
}

0 commit comments

Comments
 (0)