Skip to content

Commit

Permalink
Merge pull request #4 from ozonru/tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
oxdef authored Feb 26, 2020
2 parents 7bfe4cf + c71cc0d commit 2cee707
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
25 changes: 15 additions & 10 deletions internal/bom/bom.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,11 @@ type BOM struct {
Components []Component `xml:"components>component"`
}

func Generate() (string, error) {
func GenerateFromJSON(j []byte) (string, error) {
var result string

cmd := exec.Command("go", "list", "-json", "-m", "all")
out, err := cmd.Output()

if err != nil {
return result, err
}

bom := BOM{XMLNs: "http://cyclonedx.org/schema/bom/1.1", Version: 1}
bom.SerialNumber = uuid.New().URN()
dec := json.NewDecoder(bytes.NewReader(out))
dec := json.NewDecoder(bytes.NewReader(j))
var components []Component

for {
Expand Down Expand Up @@ -99,3 +91,16 @@ func Generate() (string, error) {
result = xml.Header + string(xmlOut)
return result, nil
}

func Generate() (string, error) {
var result string

cmd := exec.Command("go", "list", "-json", "-m", "all")
out, err := cmd.Output()

if err != nil {
return result, err
}

return GenerateFromJSON(out)
}
42 changes: 42 additions & 0 deletions internal/bom/bom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bom

import (
"testing"
"encoding/xml"
)

var inputData []byte = []byte(`{
"Path": "github.com/google/uuid",
"Version": "v1.1.1",
"Time": "2019-02-27T21:05:49Z",
"Dir": "/go/pkg/mod/github.com/google/[email protected]",
"GoMod": "/go/pkg/mod/cache/download/github.com/google/uuid/@v/v1.1.1.mod"
}`)

func TestName(t *testing.T) {
var b BOM
want := "github.com/google/uuid"
xmlResult, _ := GenerateFromJSON(inputData)
_ = xml.Unmarshal([]byte(xmlResult), &b)
if got := b.Components[0].Name; got != want {
t.Errorf(
"Package name from result CycloneDX BOM = %q, want %q",
got,
want,
)
}
}

func TestVersion(t *testing.T) {
var b BOM
want := "1.1.1"
xmlResult, _ := GenerateFromJSON(inputData)
_ = xml.Unmarshal([]byte(xmlResult), &b)
if got := b.Components[0].Version; got != want {
t.Errorf(
"Package version from result CycloneDX BOM = %q, want %q",
got,
want,
)
}
}

0 comments on commit 2cee707

Please sign in to comment.