Skip to content

Cross Platform Testing TestBuilder2_1CanBuildPackageSection #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion builder/builder2v1/build_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []st
for _, fp := range filepaths {
newFilePatch := ""
if osType == "windows" {
newFilePatch = filepath.FromSlash("." + fp[dirRootLen:])
newFilePatch = filepath.FromSlash(fp[dirRootLen:])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"./"

} else {
newFilePatch = filepath.FromSlash("./" + fp)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
//go:build linux
// +build linux

// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package builder2v1

import (
Expand Down
173 changes: 173 additions & 0 deletions builder/builder2v1/build_package_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//go:build windows
// +build windows

// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package builder2v1

import (
"os"
"testing"

"github.com/spdx/tools-golang/spdx"
)

// Verify if file exists
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

// ===== Package section builder tests =====
func TestBuilder2_1CanBuildPackageSection(t *testing.T) {
packageName := "project1"
dirRoot := "../../testdata/project1_windows/"

// check if the file exists
if !fileExists("../../testdata/project1_windows/symbolic-link") {
// create a new symbolic link
err := os.Symlink("../../testdata/project1_windows/file3.testdata.txt", "../../testdata/project1_windows/symbolic-link")
if err != nil {
t.Errorf("Do not have administrator rights : %v", err)
os.Exit(1)
}
}
wantVerificationCode := "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"

pkg, err := BuildPackageSection2_1(packageName, dirRoot, nil)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}

if pkg == nil {
t.Fatalf("expected non-nil Package, got nil")
}
if pkg.PackageName != "project1" {
t.Errorf("expected %v, got %v", "project1", pkg.PackageName)
}
if pkg.PackageSPDXIdentifier != spdx.ElementID("Package-project1") {
t.Errorf("expected %v, got %v", "Package-project1", pkg.PackageSPDXIdentifier)
}
if pkg.PackageDownloadLocation != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageDownloadLocation)
}
if pkg.FilesAnalyzed != true {
t.Errorf("expected %v, got %v", true, pkg.FilesAnalyzed)
}
if pkg.IsFilesAnalyzedTagPresent != true {
t.Errorf("expected %v, got %v", true, pkg.IsFilesAnalyzedTagPresent)
}
if pkg.PackageVerificationCode != wantVerificationCode {
t.Errorf("expected %v, got %v", wantVerificationCode, pkg.PackageVerificationCode)
}
if pkg.PackageLicenseConcluded != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseConcluded)
}
if len(pkg.PackageLicenseInfoFromFiles) != 0 {
t.Errorf("expected %v, got %v", 0, len(pkg.PackageLicenseInfoFromFiles))
}
if pkg.PackageLicenseDeclared != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseDeclared)
}
if pkg.PackageCopyrightText != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageCopyrightText)
}

// and make sure we got the right number of files, and check the first one
if pkg.Files == nil {
t.Fatalf("expected non-nil pkg.Files, got nil")
}
if len(pkg.Files) != 5 {
t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
}
fileEmpty := pkg.Files[spdx.ElementID("File0")]
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.FileSPDXIdentifier != spdx.ElementID("File0") {
t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
}
if fileEmpty.FileChecksumSHA1 != "da39a3ee5e6b4b0d3255bfef95601890afd80709" {
t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", fileEmpty.FileChecksumSHA1)
}
if fileEmpty.FileChecksumSHA256 != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", fileEmpty.FileChecksumSHA256)
}
if fileEmpty.FileChecksumMD5 != "d41d8cd98f00b204e9800998ecf8427e" {
t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", fileEmpty.FileChecksumMD5)
}
if fileEmpty.LicenseConcluded != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded)
}
if len(fileEmpty.LicenseInfoInFile) != 1 {
t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFile))
} else {
if fileEmpty.LicenseInfoInFile[0] != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFile[0])
}
}
if fileEmpty.FileCopyrightText != "NOASSERTION" {
t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText)
}

// Remove symbolic link after test
err = os.Remove("../../testdata/project1_windows/symbolic-link")
if err != nil {
t.Errorf("the file could not be deleted : %v", err)
}
}

func TestBuilder2_1CanIgnoreFiles(t *testing.T) {
packageName := "project3"
dirRoot := "../../testdata/project3/"
pathsIgnored := []string{
"**/ignoredir/",
"/excludedir/",
"**/ignorefile.txt",
"/alsoEXCLUDEthis.txt",
}
pkg, err := BuildPackageSection2_1(packageName, dirRoot, pathsIgnored)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}

// make sure we got the right files
if pkg.Files == nil {
t.Fatalf("expected non-nil pkg.Files, got nil")
}
if len(pkg.Files) != 5 {
t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
}

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"
got = pkg.Files[spdx.ElementID("File1")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

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"
got = pkg.Files[spdx.ElementID("File3")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}

want = "./subdir/keep/keep.txt"
got = pkg.Files[spdx.ElementID("File4")].FileName
if want != got {
t.Errorf("expected %v, got %v", want, got)
}
}
2 changes: 1 addition & 1 deletion testdata/project1/symbolic-link
Empty file.
1 change: 1 addition & 0 deletions testdata/project1_windows/file1.testdata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arbitrary content for testing purposes
1 change: 1 addition & 0 deletions testdata/project1_windows/file3.testdata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file contains some sort of documentation or whatever
2 changes: 2 additions & 0 deletions testdata/project1_windows/folder1/file4.testdata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
blah blah blah
this file has text in it
3 changes: 3 additions & 0 deletions testdata/project1_windows/lastfile.testdata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

somebody was kind enough to stick an spdx short-form ID in here