Skip to content

Commit

Permalink
Merge pull request #114 from CatalinStratu/main
Browse files Browse the repository at this point in the history
builder: File paths are not relative, bug fixes
  • Loading branch information
swinslow committed Mar 26, 2022
2 parents 4250e11 + f7ea935 commit bea3d05
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 68 deletions.
58 changes: 29 additions & 29 deletions builder/build_test.go
Expand Up @@ -123,8 +123,8 @@ func TestBuild2_1CreatesDocument(t *testing.T) {
if fileEmpty == nil {
t.Fatalf("expected non-nil file, got nil")
}
if fileEmpty.FileName != "/emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/emptyfile.testdata.txt", fileEmpty.FileName)
if fileEmpty.FileName != "./emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
}
if fileEmpty.FileSPDXIdentifier != spdx.ElementID("File0") {
t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
Expand Down Expand Up @@ -153,8 +153,8 @@ func TestBuild2_1CreatesDocument(t *testing.T) {
if file1 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file1.FileName != "/file1.testdata.txt" {
t.Errorf("expected %v, got %v", "/file1.testdata.txt", file1.FileName)
if file1.FileName != "./file1.testdata.txt" {
t.Errorf("expected %v, got %v", "./file1.testdata.txt", file1.FileName)
}
if file1.FileSPDXIdentifier != spdx.ElementID("File1") {
t.Errorf("expected %v, got %v", "File1", file1.FileSPDXIdentifier)
Expand Down Expand Up @@ -183,8 +183,8 @@ func TestBuild2_1CreatesDocument(t *testing.T) {
if file3 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file3.FileName != "/file3.testdata.txt" {
t.Errorf("expected %v, got %v", "/file3.testdata.txt", file3.FileName)
if file3.FileName != "./file3.testdata.txt" {
t.Errorf("expected %v, got %v", "./file3.testdata.txt", file3.FileName)
}
if file3.FileSPDXIdentifier != spdx.ElementID("File2") {
t.Errorf("expected %v, got %v", "File2", file3.FileSPDXIdentifier)
Expand Down Expand Up @@ -213,8 +213,8 @@ func TestBuild2_1CreatesDocument(t *testing.T) {
if file4 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file4.FileName != "/folder1/file4.testdata.txt" {
t.Errorf("expected %v, got %v", "folder1/file4.testdata.txt", file4.FileName)
if file4.FileName != "./folder1/file4.testdata.txt" {
t.Errorf("expected %v, got %v", "./folder1/file4.testdata.txt", file4.FileName)
}
if file4.FileSPDXIdentifier != spdx.ElementID("File3") {
t.Errorf("expected %v, got %v", "File3", file4.FileSPDXIdentifier)
Expand Down Expand Up @@ -243,8 +243,8 @@ func TestBuild2_1CreatesDocument(t *testing.T) {
if lastfile == nil {
t.Fatalf("expected non-nil file, got nil")
}
if lastfile.FileName != "/lastfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/lastfile.testdata.txt", lastfile.FileName)
if lastfile.FileName != "./lastfile.testdata.txt" {
t.Errorf("expected %v, got %v", "./lastfile.testdata.txt", lastfile.FileName)
}
if lastfile.FileSPDXIdentifier != spdx.ElementID("File4") {
t.Errorf("expected %v, got %v", "File4", lastfile.FileSPDXIdentifier)
Expand Down Expand Up @@ -331,31 +331,31 @@ func TestBuild2_1CanIgnoreFiles(t *testing.T) {
t.Fatalf("expected len %d, got %d", 5, len(pkg.Files))
}

want := "/dontscan.txt"
want := "./dontscan.txt"
got := pkg.Files[spdx.ElementID("File0")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep/keep.txt"
want = "./keep/keep.txt"
got = pkg.Files[spdx.ElementID("File1")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep.txt"
want = "./keep.txt"
got = pkg.Files[spdx.ElementID("File2")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/dontscan.txt"
want = "./subdir/keep/dontscan.txt"
got = pkg.Files[spdx.ElementID("File3")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/keep.txt"
want = "./subdir/keep/keep.txt"
got = pkg.Files[spdx.ElementID("File4")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
Expand Down Expand Up @@ -476,8 +476,8 @@ func TestBuild2_2CreatesDocument(t *testing.T) {
if fileEmpty == nil {
t.Fatalf("expected non-nil file, got nil")
}
if fileEmpty.FileName != "/emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/emptyfile.testdata.txt", fileEmpty.FileName)
if fileEmpty.FileName != "./emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
}
if fileEmpty.FileSPDXIdentifier != spdx.ElementID("File0") {
t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
Expand Down Expand Up @@ -513,8 +513,8 @@ func TestBuild2_2CreatesDocument(t *testing.T) {
if file1 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file1.FileName != "/file1.testdata.txt" {
t.Errorf("expected %v, got %v", "/file1.testdata.txt", file1.FileName)
if file1.FileName != "./file1.testdata.txt" {
t.Errorf("expected %v, got %v", "./file1.testdata.txt", file1.FileName)
}
if file1.FileSPDXIdentifier != spdx.ElementID("File1") {
t.Errorf("expected %v, got %v", "File1", file1.FileSPDXIdentifier)
Expand Down Expand Up @@ -550,8 +550,8 @@ func TestBuild2_2CreatesDocument(t *testing.T) {
if file3 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file3.FileName != "/file3.testdata.txt" {
t.Errorf("expected %v, got %v", "/file3.testdata.txt", file3.FileName)
if file3.FileName != "./file3.testdata.txt" {
t.Errorf("expected %v, got %v", "./file3.testdata.txt", file3.FileName)
}
if file3.FileSPDXIdentifier != spdx.ElementID("File2") {
t.Errorf("expected %v, got %v", "File2", file3.FileSPDXIdentifier)
Expand Down Expand Up @@ -587,8 +587,8 @@ func TestBuild2_2CreatesDocument(t *testing.T) {
if file4 == nil {
t.Fatalf("expected non-nil file, got nil")
}
if file4.FileName != "/folder1/file4.testdata.txt" {
t.Errorf("expected %v, got %v", "folder1/file4.testdata.txt", file4.FileName)
if file4.FileName != "./folder1/file4.testdata.txt" {
t.Errorf("expected %v, got %v", "./folder1/file4.testdata.txt", file4.FileName)
}
if file4.FileSPDXIdentifier != spdx.ElementID("File3") {
t.Errorf("expected %v, got %v", "File3", file4.FileSPDXIdentifier)
Expand Down Expand Up @@ -624,7 +624,7 @@ func TestBuild2_2CreatesDocument(t *testing.T) {
if lastfile == nil {
t.Fatalf("expected non-nil file, got nil")
}
if lastfile.FileName != "/lastfile.testdata.txt" {
if lastfile.FileName != "./lastfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/lastfile.testdata.txt", lastfile.FileName)
}
if lastfile.FileSPDXIdentifier != spdx.ElementID("File4") {
Expand Down Expand Up @@ -719,31 +719,31 @@ func TestBuild2_2CanIgnoreFiles(t *testing.T) {
t.Fatalf("expected len %d, got %d", 5, len(pkg.Files))
}

want := "/dontscan.txt"
want := "./dontscan.txt"
got := pkg.Files[spdx.ElementID("File0")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep/keep.txt"
want = "./keep/keep.txt"
got = pkg.Files[spdx.ElementID("File1")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep.txt"
want = "./keep.txt"
got = pkg.Files[spdx.ElementID("File2")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/dontscan.txt"
want = "./subdir/keep/dontscan.txt"
got = pkg.Files[spdx.ElementID("File3")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/keep.txt"
want = "./subdir/keep/keep.txt"
got = pkg.Files[spdx.ElementID("File4")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
Expand Down
24 changes: 21 additions & 3 deletions builder/builder2v1/build_package.go
Expand Up @@ -4,9 +4,11 @@ package builder2v1

import (
"fmt"

"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/utils"
"path/filepath"
"regexp"
"runtime"
)

// BuildPackageSection2_1 creates an SPDX Package (version 2.1), returning
Expand All @@ -21,18 +23,34 @@ func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []st
if err != nil {
return nil, err
}
osType := runtime.GOOS

re, ok := regexp.Compile("/+")
if ok != nil {
return nil, err
}

dirRootLen := 0
if osType == "windows" {
dirRootLen = len(dirRoot)
}

files := map[spdx.ElementID]*spdx.File2_1{}
fileNumber := 0
for _, fp := range filepaths {
newFile, err := BuildFileSection2_1(fp, dirRoot, fileNumber)
newFilePatch := ""
if osType == "windows" {
newFilePatch = filepath.FromSlash("." + fp[dirRootLen:])
} else {
newFilePatch = filepath.FromSlash("./" + fp)
}
newFile, err := BuildFileSection2_1(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber)
if err != nil {
return nil, err
}
files[newFile.FileSPDXIdentifier] = newFile
fileNumber++
}

// get the verification code
code, err := utils.GetVerificationCode2_1(files, "")
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions builder/builder2v1/build_package_test.go
Expand Up @@ -65,8 +65,8 @@ func TestBuilder2_1CanBuildPackageSection(t *testing.T) {
if fileEmpty == nil {
t.Fatalf("expected non-nil file, got nil")
}
if fileEmpty.FileName != "/emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/emptyfile.testdata.txt", fileEmpty.FileName)
if fileEmpty.FileName != "./emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
}
if fileEmpty.FileSPDXIdentifier != spdx.ElementID("File0") {
t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
Expand Down Expand Up @@ -113,31 +113,31 @@ func TestBuilder2_1CanIgnoreFiles(t *testing.T) {
t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
}

want := "/dontscan.txt"
want := "./dontscan.txt"
got := pkg.Files[spdx.ElementID("File0")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep/keep.txt"
want = "./keep/keep.txt"
got = pkg.Files[spdx.ElementID("File1")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep.txt"
want = "./keep.txt"
got = pkg.Files[spdx.ElementID("File2")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/dontscan.txt"
want = "./subdir/keep/dontscan.txt"
got = pkg.Files[spdx.ElementID("File3")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/keep.txt"
want = "./subdir/keep/keep.txt"
got = pkg.Files[spdx.ElementID("File4")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
Expand Down
23 changes: 21 additions & 2 deletions builder/builder2v2/build_package.go
Expand Up @@ -4,9 +4,11 @@ package builder2v2

import (
"fmt"

"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/utils"
"path/filepath"
"regexp"
"runtime"
)

// BuildPackageSection2_2 creates an SPDX Package (version 2.2), returning
Expand All @@ -18,14 +20,31 @@ func BuildPackageSection2_2(packageName string, dirRoot string, pathsIgnore []st
// build the file section first, so we'll have it available
// for calculating the package verification code
filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore)
osType := runtime.GOOS

if err != nil {
return nil, err
}

re, ok := regexp.Compile("/+")
if ok != nil {
return nil, err
}
dirRootLen := 0
if osType == "windows" {
dirRootLen = len(dirRoot)
}

files := map[spdx.ElementID]*spdx.File2_2{}
fileNumber := 0
for _, fp := range filepaths {
newFile, err := BuildFileSection2_2(fp, dirRoot, fileNumber)
newFilePatch := ""
if osType == "windows" {
newFilePatch = filepath.FromSlash("." + fp[dirRootLen:])
} else {
newFilePatch = filepath.FromSlash("./" + fp)
}
newFile, err := BuildFileSection2_2(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions builder/builder2v2/build_package_test.go
Expand Up @@ -65,8 +65,8 @@ func TestBuilder2_2CanBuildPackageSection(t *testing.T) {
if fileEmpty == nil {
t.Fatalf("expected non-nil file, got nil")
}
if fileEmpty.FileName != "/emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "/emptyfile.testdata.txt", fileEmpty.FileName)
if fileEmpty.FileName != "./emptyfile.testdata.txt" {
t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
}
if fileEmpty.FileSPDXIdentifier != spdx.ElementID("File0") {
t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
Expand Down Expand Up @@ -120,31 +120,31 @@ func TestBuilder2_2CanIgnoreFiles(t *testing.T) {
t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
}

want := "/dontscan.txt"
want := "./dontscan.txt"
got := pkg.Files[spdx.ElementID("File0")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep/keep.txt"
want = "./keep/keep.txt"
got = pkg.Files[spdx.ElementID("File1")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/keep.txt"
want = "./keep.txt"
got = pkg.Files[spdx.ElementID("File2")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/dontscan.txt"
want = "./subdir/keep/dontscan.txt"
got = pkg.Files[spdx.ElementID("File3")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "/subdir/keep/keep.txt"
want = "./subdir/keep/keep.txt"
got = pkg.Files[spdx.ElementID("File4")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
Expand Down
1 change: 1 addition & 0 deletions examples/3-build/example_build.go
Expand Up @@ -5,6 +5,7 @@
// This example demonstrates building an 'empty' SPDX document in memory that
// corresponds to a given directory's contents, including all files with their
// hashes and the package's verification code, and saving the document to disk.
// Run project: go run example_build.go project2 ../../testdata/project2 test.spdx

package main

Expand Down

0 comments on commit bea3d05

Please sign in to comment.