aboutsummaryrefslogtreecommitdiff
path: root/reporter
diff options
context:
space:
mode:
authorKeith Zantow <kzantow@gmail.com>2022-10-06 19:35:47 -0400
committerKeith Zantow <kzantow@gmail.com>2022-10-06 19:35:47 -0400
commit53dc87df2e400331330eeb8299bd3459d66ae95d (patch)
tree875fff7b51a730e4b3c58d1935f172cff31092a7 /reporter
parent6fda8118533aec0a73ca431cb32c7ca951b58200 (diff)
downloadspdx-tools-53dc87df2e400331330eeb8299bd3459d66ae95d.tar.gz
chore: Add v2.3 data model to reporter
Signed-off-by: Keith Zantow <kzantow@gmail.com>
Diffstat (limited to 'reporter')
-rw-r--r--reporter/reporter.go64
-rw-r--r--reporter/reporter_test.go125
2 files changed, 189 insertions, 0 deletions
diff --git a/reporter/reporter.go b/reporter/reporter.go
index 3d07668..0a492bc 100644
--- a/reporter/reporter.go
+++ b/reporter/reporter.go
@@ -12,6 +12,7 @@ import (
"github.com/spdx/tools-golang/spdx/v2_1"
"github.com/spdx/tools-golang/spdx/v2_2"
+ "github.com/spdx/tools-golang/spdx/v2_3"
)
// ===== 2.1 Reporter functions =====
@@ -139,3 +140,66 @@ func countLicenses2_2(pkg *v2_2.Package) (int, int, map[string]int) {
return totalFound, totalNotFound, foundCounts
}
+
+// ===== 2.3 Reporter functions =====
+
+// Generate2_3 takes a Package whose Files have been analyzed and an
+// io.Writer, and outputs to the io.Writer a tabulated count of
+// the number of Files for each unique LicenseConcluded in the set.
+func Generate2_3(pkg *v2_3.Package, w io.Writer) error {
+ if pkg.FilesAnalyzed == false {
+ return fmt.Errorf("Package FilesAnalyzed is false")
+ }
+ totalFound, totalNotFound, foundCounts := countLicenses2_3(pkg)
+
+ wr := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)
+
+ fmt.Fprintf(wr, "%d\t License found\n", totalFound)
+ fmt.Fprintf(wr, "%d\t License not found\n", totalNotFound)
+ fmt.Fprintf(wr, "%d\t TOTAL\n", totalFound+totalNotFound)
+ fmt.Fprintf(wr, "\n")
+
+ counts := []struct {
+ lic string
+ count int
+ }{}
+ for k, v := range foundCounts {
+ var entry struct {
+ lic string
+ count int
+ }
+ entry.lic = k
+ entry.count = v
+ counts = append(counts, entry)
+ }
+
+ sort.Slice(counts, func(i, j int) bool { return counts[i].count > counts[j].count })
+
+ for _, c := range counts {
+ fmt.Fprintf(wr, "%d\t %s\n", c.count, c.lic)
+ }
+ fmt.Fprintf(wr, "%d\t TOTAL FOUND\n", totalFound)
+
+ wr.Flush()
+ return nil
+}
+
+func countLicenses2_3(pkg *v2_3.Package) (int, int, map[string]int) {
+ if pkg == nil || pkg.Files == nil {
+ return 0, 0, nil
+ }
+
+ totalFound := 0
+ totalNotFound := 0
+ foundCounts := map[string]int{}
+ for _, f := range pkg.Files {
+ if f.LicenseConcluded == "" || f.LicenseConcluded == "NOASSERTION" {
+ totalNotFound++
+ } else {
+ totalFound++
+ foundCounts[f.LicenseConcluded]++
+ }
+ }
+
+ return totalFound, totalNotFound, foundCounts
+}
diff --git a/reporter/reporter_test.go b/reporter/reporter_test.go
index 9795593..7e6fc88 100644
--- a/reporter/reporter_test.go
+++ b/reporter/reporter_test.go
@@ -8,6 +8,7 @@ import (
"github.com/spdx/tools-golang/spdx/v2_1"
"github.com/spdx/tools-golang/spdx/v2_2"
+ "github.com/spdx/tools-golang/spdx/v2_3"
)
// ===== 2.1 Reporter top-level function tests =====
@@ -257,3 +258,127 @@ func Test2_2NilPackageReturnsZeroCountsOfLicenses(t *testing.T) {
t.Fatalf("expected %v, got %v", 0, len(foundCounts))
}
}
+
+// ===== 2.3 Reporter top-level function tests =====
+func Test2_3ReporterCanMakeReportFromPackage(t *testing.T) {
+ pkg := &v2_3.Package{
+ FilesAnalyzed: true,
+ Files: []*v2_3.File{
+ {FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
+ {FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
+ {FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
+ },
+ }
+
+ // what we want to get, as a buffer of bytes
+ want := bytes.NewBufferString(` 9 License found
+ 3 License not found
+ 12 TOTAL
+
+ 5 GPL-2.0-only
+ 4 MIT
+ 9 TOTAL FOUND
+`)
+
+ // render as buffer of bytes
+ var got bytes.Buffer
+ err := Generate2_3(pkg, &got)
+ if err != nil {
+ t.Errorf("Expected nil error, got %v", err)
+ }
+
+ // check that they match
+ c := bytes.Compare(want.Bytes(), got.Bytes())
+ if c != 0 {
+ t.Errorf("Expected %v, got %v", want.String(), got.String())
+ }
+}
+
+func Test2_3ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) {
+ pkg := &v2_3.Package{
+ FilesAnalyzed: false,
+ }
+
+ // render as buffer of bytes
+ var got bytes.Buffer
+ err := Generate2_3(pkg, &got)
+ if err == nil {
+ t.Errorf("Expected non-nil error, got nil")
+ }
+}
+
+// ===== 2.3 Utility functions =====
+
+func Test2_3CanGetCountsOfLicenses(t *testing.T) {
+ pkg := &v2_3.Package{
+ FilesAnalyzed: true,
+ Files: []*v2_3.File{
+ {FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
+ {FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
+ {FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
+ {FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
+ {FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
+ },
+ }
+
+ totalFound, totalNotFound, foundCounts := countLicenses2_3(pkg)
+ if totalFound != 9 {
+ t.Errorf("expected %v, got %v", 9, totalFound)
+ }
+ if totalNotFound != 3 {
+ t.Errorf("expected %v, got %v", 3, totalNotFound)
+ }
+ if len(foundCounts) != 2 {
+ t.Fatalf("expected %v, got %v", 2, len(foundCounts))
+ }
+
+ // foundCounts is a map of license ID to count of licenses
+ // confirm that the results are as expected
+ if foundCounts["GPL-2.0-only"] != 5 {
+ t.Errorf("expected %v, got %v", 5, foundCounts["GPL-2.0-only"])
+ }
+ if foundCounts["MIT"] != 4 {
+ t.Errorf("expected %v, got %v", 4, foundCounts["MIT"])
+ }
+}
+
+func Test2_3NilPackageReturnsZeroCountsOfLicenses(t *testing.T) {
+ totalFound, totalNotFound, foundCounts := countLicenses2_3(nil)
+ if totalFound != 0 {
+ t.Errorf("expected %v, got %v", 0, totalFound)
+ }
+ if totalNotFound != 0 {
+ t.Errorf("expected %v, got %v", 0, totalNotFound)
+ }
+ if len(foundCounts) != 0 {
+ t.Fatalf("expected %v, got %v", 0, len(foundCounts))
+ }
+
+ pkg := &v2_3.Package{}
+ totalFound, totalNotFound, foundCounts = countLicenses2_3(pkg)
+ if totalFound != 0 {
+ t.Errorf("expected %v, got %v", 0, totalFound)
+ }
+ if totalNotFound != 0 {
+ t.Errorf("expected %v, got %v", 0, totalNotFound)
+ }
+ if len(foundCounts) != 0 {
+ t.Fatalf("expected %v, got %v", 0, len(foundCounts))
+ }
+}