Skip to content

Commit 4a72729

Browse files
committed
improves tests for mermaidjs renderPathToComponent
1 parent 01650fb commit 4a72729

5 files changed

Lines changed: 19 additions & 23 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ clean::
77
docker compose down -v && docker compose up -d
88

99
mocks::
10-
mockery --config=.mockery.yaml
10+
mockery --config=.mockery.yaml
1111

1212
lint::
1313
golangci-lint run ./...

internal/core/integrations/github_integration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func (g *githubIntegration) CreateIssue(ctx context.Context, asset models.Asset,
666666

667667
assetSlug := asset.Slug
668668
labels := getLabels(&dependencyVuln, "open")
669-
componentTree, err := RenderPathToComponent(g.componentRepository, asset.ID, assetVersionName, dependencyVuln.ScannerID, exp.AffectedComponentName)
669+
componentTree, err := renderPathToComponent(g.componentRepository, asset.ID, assetVersionName, dependencyVuln.ScannerID, exp.AffectedComponentName)
670670
if err != nil {
671671
return err
672672
}

internal/core/integrations/gitlab_integration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func NewGitLabIntegration(db core.DB) *gitlabIntegration {
111111
assetRepository := repositories.NewAssetRepository(db)
112112
assetVersionRepository := repositories.NewAssetVersionRepository(db)
113113
projectRepository := repositories.NewProjectRepository(db)
114-
componentRepositoy := repositories.NewComponentRepository(db)
114+
componentRepository := repositories.NewComponentRepository(db)
115115

116116
frontendUrl := os.Getenv("FRONTEND_URL")
117117
if frontendUrl == "" {
@@ -128,7 +128,7 @@ func NewGitLabIntegration(db core.DB) *gitlabIntegration {
128128
assetVersionRepository: assetVersionRepository,
129129
externalUserRepository: externalUserRepository,
130130
projectRepository: projectRepository,
131-
componentRepository: componentRepositoy,
131+
componentRepository: componentRepository,
132132

133133
gitlabClientFactory: func(id uuid.UUID) (gitlabClientFacade, error) {
134134
integration, err := gitlabIntegrationRepository.Read(id)
@@ -1043,7 +1043,7 @@ func (g *gitlabIntegration) CreateIssue(ctx context.Context, asset models.Asset,
10431043

10441044
assetSlug := asset.Slug
10451045
labels := getLabels(&dependencyVuln, "open")
1046-
componentTree, err := RenderPathToComponent(g.componentRepository, asset.ID, assetVersionName, dependencyVuln.ScannerID, exp.AffectedComponentName)
1046+
componentTree, err := renderPathToComponent(g.componentRepository, asset.ID, assetVersionName, dependencyVuln.ScannerID, exp.AffectedComponentName)
10471047
if err != nil {
10481048
return err
10491049
}

internal/core/integrations/thirdparty_integration.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func NewThirdPartyIntegrations(integrations ...core.ThirdPartyIntegration) *thir
161161
}
162162

163163
// this function returns a string containing a mermaids js flow chart to the given pURL
164-
func RenderPathToComponent(componentRepository core.ComponentRepository, assetID uuid.UUID, assetVersionName string, scannerID string, pURL string) (string, error) {
164+
func renderPathToComponent(componentRepository core.ComponentRepository, assetID uuid.UUID, assetVersionName string, scannerID string, pURL string) (string, error) {
165165

166166
//basic string to tell markdown that we have a mermaid flow chart with given parameters
167167
mermaidFlowChart := "mermaid \n %%{init: { 'theme':'dark' } }%%\n flowchart TD\n"
@@ -191,7 +191,7 @@ func RenderPathToComponent(componentRepository core.ComponentRepository, assetID
191191

192192
for i, componentName := range componentList[1:] {
193193

194-
nodeContent, err = BeautifyPURL(componentName)
194+
nodeContent, err = beautifyPURL(componentName)
195195
if err != nil {
196196
nodeContent = componentName
197197
}
@@ -204,8 +204,7 @@ func RenderPathToComponent(componentRepository core.ComponentRepository, assetID
204204
}
205205

206206
// function to make purl look more visually appealing
207-
func BeautifyPURL(pURL string) (string, error) {
208-
207+
func beautifyPURL(pURL string) (string, error) {
209208
p, err := packageurl.FromString(pURL)
210209
if err != nil {
211210
slog.Error("cannot convert to purl struct")
@@ -217,5 +216,4 @@ func BeautifyPURL(pURL string) (string, error) {
217216
} else {
218217
return p.Namespace + "/" + p.Name, nil
219218
}
220-
221219
}

internal/core/integrations/thirdparty_integration_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package integrations_test
1+
package integrations
22

33
import (
44
"fmt"
55
"testing"
66

77
"github.com/google/uuid"
8-
"github.com/l3montree-dev/devguard/internal/core/integrations"
98
"github.com/l3montree-dev/devguard/internal/database/models"
109
"github.com/l3montree-dev/devguard/internal/utils"
1110
"github.com/l3montree-dev/devguard/mocks"
@@ -14,7 +13,7 @@ import (
1413
)
1514

1615
func TestRenderPathToComponent(t *testing.T) {
17-
t.Run("Everythings works as expected with empty lists", func(t *testing.T) {
16+
t.Run("Everything works as expected with empty lists", func(t *testing.T) {
1817

1918
components := []models.ComponentDependency{}
2019
componentRepository := mocks.NewCoreComponentRepository(t)
@@ -25,7 +24,7 @@ func TestRenderPathToComponent(t *testing.T) {
2524
scannerID := "SBOM-File-Upload"
2625
pURL := "pkg:npm:test"
2726

28-
result, err := integrations.RenderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
27+
result, err := renderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
2928
if err != nil {
3029
t.Fail()
3130
}
@@ -42,15 +41,15 @@ func TestRenderPathToComponent(t *testing.T) {
4241
scannerID := "SBOM-File-Upload"
4342
pURL := "pkg:npm:test"
4443

45-
_, err := integrations.RenderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
44+
_, err := renderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
4645
if err == nil {
4746
t.Fail()
4847
}
4948

5049
})
5150
t.Run("Everything works as expeted with a non empty component list", func(t *testing.T) {
5251
components := []models.ComponentDependency{
53-
{ComponentPurl: utils.Ptr("testPURL"), DependencyPurl: "testDependency"},
52+
{ComponentPurl: nil, DependencyPurl: "testDependency"}, // root --> testDependency
5453
{ComponentPurl: utils.Ptr("testomatL"), DependencyPurl: "testPURL"},
5554
{ComponentPurl: utils.Ptr("testDependency"), DependencyPurl: "testPURL"},
5655
}
@@ -62,37 +61,36 @@ func TestRenderPathToComponent(t *testing.T) {
6261
scannerID := "SBOM-File-Upload"
6362
pURL := "pkg:npm:test"
6463

65-
result, err := integrations.RenderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
64+
result, err := renderPathToComponent(componentRepository, assetID, assetVersionName, scannerID, pURL)
6665
if err != nil {
6766
t.Fail()
6867
}
6968

7069
//String for the empty graph + 1 node being root with a linebreak
71-
assert.Equal(t, "```mermaid \n %%{init: { 'theme':'dark' } }%%\n flowchart TD\nroot\n```\n", result)
70+
assert.Equal(t, "```mermaid \n %%{init: { 'theme':'dark' } }%%\n flowchart TD\nroot --> \nnode0[testDependency] --> \nnode1[testPURL]\n```\n", result)
7271

7372
})
7473
}
7574

7675
func TestBeautifyPURL(t *testing.T) {
7776
t.Run("empty String should also return an empty string back", func(t *testing.T) {
7877
inputString := ""
79-
result, _ := integrations.BeautifyPURL(inputString)
78+
result, _ := beautifyPURL(inputString)
8079
assert.Equal(t, "", result)
8180
})
8281
t.Run("invalid purl format should also be returned unchanged", func(t *testing.T) {
8382
inputString := "this is definitely not a valid purl"
84-
result, _ := integrations.BeautifyPURL(inputString)
83+
result, _ := beautifyPURL(inputString)
8584
assert.Equal(t, inputString, result)
8685
})
8786
t.Run("should return only the namespace and the name of a valid purl and cut the rest", func(t *testing.T) {
8887
inputString := "pkg:npm/@ory/integrations@v0.0.1"
89-
result, _ := integrations.BeautifyPURL(inputString)
88+
result, _ := beautifyPURL(inputString)
9089
assert.Equal(t, "@ory/integrations", result)
9190
})
9291
t.Run("should return no leading slash if the namespace is empty", func(t *testing.T) {
9392
inputString := "pkg:npm/integrations@v0.0.1"
94-
result, _ := integrations.BeautifyPURL(inputString)
93+
result, _ := beautifyPURL(inputString)
9594
assert.Equal(t, "integrations", result)
9695
})
97-
9896
}

0 commit comments

Comments
 (0)