Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/gomod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/rs/zerolog"
"golang.org/x/exp/slices"
"golang.org/x/mod/semver"
"golang.org/x/mod/sumdb/dirhash"
"io"
"path/filepath"
"strings"
"sync"

"github.com/rs/zerolog"
"golang.org/x/exp/slices"
"golang.org/x/mod/semver"
"golang.org/x/mod/sumdb/dirhash"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports must have been sorted by gofmt

"github.com/CycloneDX/cyclonedx-gomod/internal/gocmd"
"github.com/CycloneDX/cyclonedx-gomod/internal/util"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func (m Module) PackageURL() string {
envMap, _ = gocmd.GetEnv(zerolog.Nop())
})

return fmt.Sprintf("pkg:golang/%s?type=module&goos=%s&goarch=%s", m.Coordinates(), envMap["GOOS"], envMap["GOARCH"])
return fmt.Sprintf("pkg:golang/%s?goarch=%s&goos=%s&type=module", m.Coordinates(), envMap["GOARCH"], envMap["GOOS"])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous order of qualifiers was not compliant with the package URL spec since the qualifiers were not sorted lexicographically.

}

// IsModule determines whether dir is a Go module.
Expand Down
11 changes: 10 additions & 1 deletion internal/gomod/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"testing"

"github.com/package-url/packageurl-go"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -89,7 +90,15 @@ func TestModule_PackageURL(t *testing.T) {
Path: "github.com/CycloneDX/cyclonedx-go",
Version: "v0.1.0",
}
assert.Equal(t, "pkg:golang/github.com/CycloneDX/[email protected]?type=module&goos="+goos+"&goarch="+goarch, module.PackageURL())

qualifiers := packageurl.Qualifiers{
{Key: "type", Value: "module"},
{Key: "goos", Value: goos},
{Key: "goarch", Value: goarch},
}
require.NoError(t, qualifiers.Normalize())

assert.Equal(t, "pkg:golang/github.com/CycloneDX/[email protected]?"+qualifiers.String(), module.PackageURL())
}

func TestIsModule(t *testing.T) {
Expand Down
28 changes: 25 additions & 3 deletions internal/sbom/convert/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/package-url/packageurl-go"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -195,11 +196,18 @@ func TestToComponent(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, component)

qualifiers := packageurl.Qualifiers{
{Key: "type", Value: "module"},
{Key: "goos", Value: goos},
{Key: "goarch", Value: goarch},
}
require.NoError(t, qualifiers.Normalize())

require.Equal(t, "pkg:golang/path@version?type=module", component.BOMRef)
require.Equal(t, cdx.ComponentTypeLibrary, component.Type)
require.Equal(t, "path", component.Name)
require.Equal(t, "version", component.Version)
require.Equal(t, "pkg:golang/path@version?type=module&goos="+goos+"&goarch="+goarch, component.PackageURL)
require.Equal(t, "pkg:golang/path@version?"+qualifiers.String(), component.PackageURL)
require.Equal(t, cdx.ScopeRequired, component.Scope)
})

Expand All @@ -214,11 +222,18 @@ func TestToComponent(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, component)

qualifiers := packageurl.Qualifiers{
{Key: "type", Value: "module"},
{Key: "goos", Value: goos},
{Key: "goarch", Value: goarch},
}
require.NoError(t, qualifiers.Normalize())

require.Equal(t, "pkg:golang/path@version?type=module", component.BOMRef)
require.Equal(t, cdx.ComponentTypeLibrary, component.Type)
require.Equal(t, "path", component.Name)
require.Equal(t, "version", component.Version)
require.Equal(t, "pkg:golang/path@version?type=module&goos="+goos+"&goarch="+goarch, component.PackageURL)
require.Equal(t, "pkg:golang/path@version?"+qualifiers.String(), component.PackageURL)
require.Equal(t, cdx.ScopeOptional, component.Scope)
})

Expand All @@ -236,11 +251,18 @@ func TestToComponent(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, component)

qualifiers := packageurl.Qualifiers{
{Key: "type", Value: "module"},
{Key: "goos", Value: goos},
{Key: "goarch", Value: goarch},
}
require.NoError(t, qualifiers.Normalize())

require.Equal(t, "pkg:golang/pathReplace@versionReplace?type=module", component.BOMRef)
require.Equal(t, cdx.ComponentTypeLibrary, component.Type)
require.Equal(t, "pathReplace", component.Name)
require.Equal(t, "versionReplace", component.Version)
require.Equal(t, "pkg:golang/pathReplace@versionReplace?type=module&goos="+goos+"&goarch="+goarch, component.PackageURL)
require.Equal(t, "pkg:golang/pathReplace@versionReplace?"+qualifiers.String(), component.PackageURL)
require.Equal(t, cdx.ScopeRequired, component.Scope)
})

Expand Down
Loading