diff --git a/README.md b/README.md index ce1642a8..f6661a04 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ # SPDX tools-golang -tools-golang is a collection of Go packages intended to make it easier for +`tools-golang` is a collection of Go packages intended to make it easier for Go programs to work with [SPDX®](https://spdx.dev/) files. ## Recent news @@ -23,15 +23,14 @@ of the SPDX specification, available at: https://spdx.dev/specifications tools-golang provides the following packages: * *spdx* - in-memory data model for the sections of an SPDX document -* *tvloader* - tag-value document loader -* *tvsaver* - tag-value document saver -* *rdfloader* - RDF document loader -* *json* - JSON document parser and writer -* *yaml* - YAML document parser and writer +* *tagvalue* - tag-value document reader and writer +* *rdf* - RDF document reader +* *json* - JSON document reader and writer +* *yaml* - YAML document reader and writer * *builder* - builds "empty" SPDX document (with hashes) for directory contents -* *idsearcher* - searches for [SPDX short-form IDs](https://spdx.org/ids/) and builds SPDX document +* *idsearcher* - searches for [SPDX short-form IDs](https://spdx.org/ids/) and builds an SPDX document * *licensediff* - compares concluded licenses between files in two packages -* *reporter* - generates basic license count report from SPDX document +* *reporter* - generates basic license count report from an SPDX document * *spdxlib* - various utility functions for manipulating SPDX documents in memory * *utils* - various utility functions that support the other tools-golang packages @@ -40,15 +39,7 @@ directory. ## What it doesn't do -tools-golang doesn't currently do any of the following: - -* work with files under any version of the SPDX spec prior to v2.1 -* convert between different versions of SPDX documents (e.g., from 2.1 to 2.2) -* enable applications to interact with SPDX files without needing to care - (too much) about the particular SPDX file version - -We are working towards adding functionality for all of these. Code contributions -are welcome! +`tools-golang` doesn't currently support files under any version of the SPDX spec prior to v2.1 ## Documentation diff --git a/builder/build.go b/builder/build.go index 3c670a00..dca03377 100644 --- a/builder/build.go +++ b/builder/build.go @@ -7,90 +7,14 @@ package builder import ( "fmt" - "github.com/spdx/tools-golang/builder/builder2v1" - "github.com/spdx/tools-golang/builder/builder2v2" - "github.com/spdx/tools-golang/builder/builder2v3" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 builder ===== - -// Config2_1 is a collection of configuration settings for builder -// (for version 2.1 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_1. -type Config2_1 struct { - // NamespacePrefix should be a URI representing a prefix for the - // namespace with which the SPDX Document will be associated. - // It will be used in the DocumentNamespace field in the CreationInfo - // section, followed by the per-Document package name and a random UUID. - NamespacePrefix string - - // CreatorType should be one of "Person", "Organization" or "Tool". - // If not one of those strings, it will be interpreted as "Person". - CreatorType string - - // Creator will be filled in for the given CreatorType. - Creator string - - // PathsIgnored lists certain paths to be omitted from the built document. - // Each string should be a path, relative to the package's dirRoot, - // to a specific file or (for all files in a directory) ending in a slash. - // Prefix the string with "**" to omit all instances of that file / - // directory, regardless of where it is in the file tree. - PathsIgnored []string - - // TestValues is used to pass fixed values for testing purposes - // only, and should be set to nil for production use. It is only - // exported so that it will be accessible within builder2v1. - TestValues map[string]string -} - -// Build2_1 creates an SPDX Document (version 2.1), returning that document or -// error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - config: Config object -func Build2_1(packageName string, dirRoot string, config *Config2_1) (*v2_1.Document, error) { - // build Package section first -- will include Files and make the - // package verification code available - pkg, err := builder2v1.BuildPackageSection2_1(packageName, dirRoot, config.PathsIgnored) - if err != nil { - return nil, err - } - - ci, err := builder2v1.BuildCreationInfoSection2_1(config.CreatorType, config.Creator, config.TestValues) - if err != nil { - return nil, err - } - - rln, err := builder2v1.BuildRelationshipSection2_1(packageName) - if err != nil { - return nil, err - } - - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - DocumentName: packageName, - DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), - CreationInfo: ci, - Packages: []*v2_1.Package{pkg}, - Relationships: []*v2_1.Relationship{rln}, - } - - return doc, nil -} - -// ===== 2.2 builder ===== - -// Config2_2 is a collection of configuration settings for builder -// (for version 2.2 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_2. -type Config2_2 struct { +// Config is a collection of configuration settings for builder. +// A few mandatory fields are set here +// so that they can be repeatedly reused in multiple calls to Build. +type Config struct { // NamespacePrefix should be a URI representing a prefix for the // namespace with which the SPDX Document will be associated. // It will be used in the DocumentNamespace field in the CreationInfo @@ -113,111 +37,48 @@ type Config2_2 struct { // TestValues is used to pass fixed values for testing purposes // only, and should be set to nil for production use. It is only - // exported so that it will be accessible within builder2v2. + // exported so that it will be accessible within builder. TestValues map[string]string } -// Build2_2 creates an SPDX Document (version 2.2), returning that document or +// Build creates an SPDX Document, returning that document or // error if any is encountered. Arguments: // - packageName: name of package / directory // - dirRoot: path to directory to be analyzed // - config: Config object -func Build2_2(packageName string, dirRoot string, config *Config2_2) (*v2_2.Document, error) { +func Build(packageName string, dirRoot string, config *Config) (*spdx.Document, error) { // build Package section first -- will include Files and make the // package verification code available - pkg, err := builder2v2.BuildPackageSection2_2(packageName, dirRoot, config.PathsIgnored) + pkg, err := BuildPackageSection(packageName, dirRoot, config.PathsIgnored) if err != nil { return nil, err } - ci, err := builder2v2.BuildCreationInfoSection2_2(config.CreatorType, config.Creator, config.TestValues) + ci, err := BuildCreationInfoSection(config.CreatorType, config.Creator, config.TestValues) if err != nil { return nil, err } - rln, err := builder2v2.BuildRelationshipSection2_2(packageName) + rln, err := BuildRelationshipSection(packageName) if err != nil { return nil, err } - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - DocumentName: packageName, - DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), - CreationInfo: ci, - Packages: []*v2_2.Package{pkg}, - Relationships: []*v2_2.Relationship{rln}, - } - - return doc, nil -} - -// ===== 2.3 builder ===== - -// Config2_3 is a collection of configuration settings for builder -// (for version 2.3 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_3. -type Config2_3 struct { - // NamespacePrefix should be a URI representing a prefix for the - // namespace with which the SPDX Document will be associated. - // It will be used in the DocumentNamespace field in the CreationInfo - // section, followed by the per-Document package name and a random UUID. - NamespacePrefix string - - // CreatorType should be one of "Person", "Organization" or "Tool". - // If not one of those strings, it will be interpreted as "Person". - CreatorType string + var packageVerificationCode common.PackageVerificationCode - // Creator will be filled in for the given CreatorType. - Creator string - - // PathsIgnored lists certain paths to be omitted from the built document. - // Each string should be a path, relative to the package's dirRoot, - // to a specific file or (for all files in a directory) ending in a slash. - // Prefix the string with "**" to omit all instances of that file / - // directory, regardless of where it is in the file tree. - PathsIgnored []string - - // TestValues is used to pass fixed values for testing purposes - // only, and should be set to nil for production use. It is only - // exported so that it will be accessible within builder2v3. - TestValues map[string]string -} - -// Build2_3 creates an SPDX Document (version 2.3), returning that document or -// error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - config: Config object -func Build2_3(packageName string, dirRoot string, config *Config2_3) (*v2_3.Document, error) { - // build Package section first -- will include Files and make the - // package verification code available - pkg, err := builder2v3.BuildPackageSection2_3(packageName, dirRoot, config.PathsIgnored) - if err != nil { - return nil, err - } - - ci, err := builder2v3.BuildCreationInfoSection2_3(config.CreatorType, config.Creator, config.TestValues) - if err != nil { - return nil, err - } - - rln, err := builder2v3.BuildRelationshipSection2_3(packageName) - if err != nil { - return nil, err + if pkg.PackageVerificationCode != nil { + packageVerificationCode = *pkg.PackageVerificationCode } - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), DocumentName: packageName, - DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), + DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, packageVerificationCode), CreationInfo: ci, - Packages: []*v2_3.Package{pkg}, - Relationships: []*v2_3.Relationship{rln}, + Packages: []*spdx.Package{pkg}, + Relationships: []*spdx.Relationship{rln}, } return doc, nil diff --git a/builder/builder2v3/build_creation_info.go b/builder/build_creation_info.go similarity index 75% rename from builder/builder2v3/build_creation_info.go rename to builder/build_creation_info.go index 04a8e16f..1916c4eb 100644 --- a/builder/builder2v3/build_creation_info.go +++ b/builder/build_creation_info.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v3 +package builder import ( "time" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// BuildCreationInfoSection2_3 creates an SPDX Package (version 2.3), returning that +// BuildCreationInfoSection creates an SPDX Package, returning that // package or error if any is encountered. Arguments: // - packageName: name of package / directory // - code: verification code from Package @@ -17,7 +17,7 @@ import ( // - creatorType: one of Person, Organization or Tool // - creator: creator string // - testValues: for testing only; call with nil when using in production -func BuildCreationInfoSection2_3(creatorType string, creator string, testValues map[string]string) (*v2_3.CreationInfo, error) { +func BuildCreationInfoSection(creatorType string, creator string, testValues map[string]string) (*spdx.CreationInfo, error) { // build creator slices creators := []common.Creator{ // add builder as a tool @@ -39,7 +39,7 @@ func BuildCreationInfoSection2_3(creatorType string, creator string, testValues created = testVal } - ci := &v2_3.CreationInfo{ + ci := &spdx.CreationInfo{ Creators: creators, Created: created, } diff --git a/builder/builder2v2/build_creation_info_test.go b/builder/build_creation_info_test.go similarity index 77% rename from builder/builder2v2/build_creation_info_test.go rename to builder/build_creation_info_test.go index 48a06540..a9d13e97 100644 --- a/builder/builder2v2/build_creation_info_test.go +++ b/builder/build_creation_info_test.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v2 +package builder import ( "testing" ) // ===== CreationInfo section builder tests ===== -func TestBuilder2_2CanBuildCreationInfoSection(t *testing.T) { +func TestBuilderCanBuildCreationInfoSection(t *testing.T) { creatorType := "Organization" creator := "Jane Doe LLC" testValues := make(map[string]string) testValues["Created"] = "2018-10-20T16:48:00Z" - ci, err := BuildCreationInfoSection2_2(creatorType, creator, testValues) + ci, err := BuildCreationInfoSection(creatorType, creator, testValues) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -35,13 +35,13 @@ func TestBuilder2_2CanBuildCreationInfoSection(t *testing.T) { } } -func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) { +func TestBuilderCanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) { creatorType := "Person" creator := "John Doe" testValues := make(map[string]string) testValues["Created"] = "2018-10-20T16:48:00Z" - ci, err := BuildCreationInfoSection2_2(creatorType, creator, testValues) + ci, err := BuildCreationInfoSection(creatorType, creator, testValues) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -60,13 +60,13 @@ func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) { } } -func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorTool(t *testing.T) { +func TestBuilderCanBuildCreationInfoSectionWithCreatorTool(t *testing.T) { creatorType := "Tool" - creator := "some-other-tool-2.1" + creator := "some-other-tool" testValues := make(map[string]string) testValues["Created"] = "2018-10-20T16:48:00Z" - ci, err := BuildCreationInfoSection2_2(creatorType, creator, testValues) + ci, err := BuildCreationInfoSection(creatorType, creator, testValues) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -80,18 +80,18 @@ func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorTool(t *testing.T) { if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0]) } - if ci.Creators[1].Creator != "some-other-tool-2.1" { - t.Errorf("expected %s, got %s", "some-other-tool-2.1", ci.Creators[1]) + if ci.Creators[1].Creator != "some-other-tool" { + t.Errorf("expected %s, got %s", "some-other-tool", ci.Creators[1]) } } -func TestBuilder2_2CanBuildCreationInfoSectionWithInvalidPerson(t *testing.T) { +func TestBuilderCanBuildCreationInfoSectionWithInvalidPerson(t *testing.T) { creatorType := "Whatever" creator := "John Doe" testValues := make(map[string]string) testValues["Created"] = "2018-10-20T16:48:00Z" - ci, err := BuildCreationInfoSection2_2(creatorType, creator, testValues) + ci, err := BuildCreationInfoSection(creatorType, creator, testValues) if err != nil { t.Fatalf("expected nil error, got %v", err) } diff --git a/builder/builder2v3/build_file.go b/builder/build_file.go similarity index 79% rename from builder/builder2v3/build_file.go rename to builder/build_file.go index bce8e66f..f643b842 100644 --- a/builder/builder2v3/build_file.go +++ b/builder/build_file.go @@ -1,22 +1,22 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v3 +package builder import ( "fmt" "path/filepath" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" "github.com/spdx/tools-golang/utils" ) -// BuildFileSection2_3 creates an SPDX File (version 2.3), returning that +// BuildFileSection creates an SPDX File, returning that // file or error if any is encountered. Arguments: // - filePath: path to file, relative to prefix // - prefix: relative directory for filePath // - fileNumber: integer index (unique within package) to use in identifier -func BuildFileSection2_3(filePath string, prefix string, fileNumber int) (*v2_3.File, error) { +func BuildFileSection(filePath string, prefix string, fileNumber int) (*spdx.File, error) { // build the full file path p := filepath.Join(prefix, filePath) @@ -30,7 +30,7 @@ func BuildFileSection2_3(filePath string, prefix string, fileNumber int) (*v2_3. i := fmt.Sprintf("File%d", fileNumber) // now build the File section - f := &v2_3.File{ + f := &spdx.File{ FileName: filePath, FileSPDXIdentifier: common.ElementID(i), Checksums: []common.Checksum{ diff --git a/builder/builder2v3/build_file_test.go b/builder/build_file_test.go similarity index 84% rename from builder/builder2v3/build_file_test.go rename to builder/build_file_test.go index c3eaee68..dc9c67a8 100644 --- a/builder/builder2v3/build_file_test.go +++ b/builder/build_file_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v3 +package builder import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== File section builder tests ===== -func TestBuilder2_3CanBuildFileSection(t *testing.T) { +func TestBuilderCanBuildFileSection(t *testing.T) { filePath := "/file1.testdata.txt" - prefix := "../../testdata/project1/" + prefix := "../testdata/project1/" fileNumber := 17 - file1, err := BuildFileSection2_3(filePath, prefix, fileNumber) + file1, err := BuildFileSection(filePath, prefix, fileNumber) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -62,12 +62,12 @@ func TestBuilder2_3CanBuildFileSection(t *testing.T) { } -func TestBuilder2_3BuildFileSectionFailsForInvalidFilePath(t *testing.T) { +func TestBuilderBuildFileSectionFailsForInvalidFilePath(t *testing.T) { filePath := "/file1.testdata.txt" prefix := "oops/wrong/path" fileNumber := 11 - _, err := BuildFileSection2_3(filePath, prefix, fileNumber) + _, err := BuildFileSection(filePath, prefix, fileNumber) if err == nil { t.Fatalf("expected non-nil error, got nil") } diff --git a/builder/builder2v3/build_package.go b/builder/build_package.go similarity index 76% rename from builder/builder2v3/build_package.go rename to builder/build_package.go index f278a597..c31c32de 100644 --- a/builder/builder2v3/build_package.go +++ b/builder/build_package.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v3 +package builder import ( "fmt" @@ -8,17 +8,17 @@ import ( "regexp" "runtime" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" "github.com/spdx/tools-golang/utils" ) -// BuildPackageSection2_3 creates an SPDX Package (version 2.3), returning +// BuildPackageSection creates an SPDX Package, returning // that package or error if any is encountered. Arguments: // - packageName: name of package / directory // - dirRoot: path to directory to be analyzed // - pathsIgnore: slice of strings for filepaths to ignore -func BuildPackageSection2_3(packageName string, dirRoot string, pathsIgnore []string) (*v2_3.Package, error) { +func BuildPackageSection(packageName string, dirRoot string, pathsIgnore []string) (*spdx.Package, error) { // build the file section first, so we'll have it available // for calculating the package verification code filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore) @@ -37,7 +37,7 @@ func BuildPackageSection2_3(packageName string, dirRoot string, pathsIgnore []st dirRootLen = len(dirRoot) } - files := []*v2_3.File{} + files := []*spdx.File{} fileNumber := 0 for _, fp := range filepaths { newFilePatch := "" @@ -46,7 +46,7 @@ func BuildPackageSection2_3(packageName string, dirRoot string, pathsIgnore []st } else { newFilePatch = filepath.FromSlash("./" + fp) } - newFile, err := BuildFileSection2_3(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber) + newFile, err := BuildFileSection(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber) if err != nil { return nil, err } @@ -55,13 +55,13 @@ func BuildPackageSection2_3(packageName string, dirRoot string, pathsIgnore []st } // get the verification code - code, err := utils.GetVerificationCode2_3(files, "") + code, err := utils.GetVerificationCode(files, "") if err != nil { return nil, err } // now build the package section - pkg := &v2_3.Package{ + pkg := &spdx.Package{ PackageName: packageName, PackageSPDXIdentifier: common.ElementID(fmt.Sprintf("Package-%s", packageName)), PackageDownloadLocation: "NOASSERTION", diff --git a/builder/builder2v1/build_package_test.go b/builder/build_package_test.go similarity index 92% rename from builder/builder2v1/build_package_test.go rename to builder/build_package_test.go index 959ae651..30d40391 100644 --- a/builder/builder2v1/build_package_test.go +++ b/builder/build_package_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v1 +package builder import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== Package section builder tests ===== -func TestBuilder2_1CanBuildPackageSection(t *testing.T) { +func TestBuilderCanBuildPackageSection(t *testing.T) { packageName := "project1" - dirRoot := "../../testdata/project1/" + dirRoot := "../testdata/project1/" wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"} - pkg, err := BuildPackageSection2_1(packageName, dirRoot, nil) + pkg, err := BuildPackageSection(packageName, dirRoot, nil) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -102,16 +102,16 @@ func TestBuilder2_1CanBuildPackageSection(t *testing.T) { } } -func TestBuilder2_1CanIgnoreFiles(t *testing.T) { +func TestBuilderCanIgnoreFiles(t *testing.T) { packageName := "project3" - dirRoot := "../../testdata/project3/" + dirRoot := "../testdata/project3/" pathsIgnored := []string{ "**/ignoredir/", "/excludedir/", "**/ignorefile.txt", "/alsoEXCLUDEthis.txt", } - pkg, err := BuildPackageSection2_1(packageName, dirRoot, pathsIgnored) + pkg, err := BuildPackageSection(packageName, dirRoot, pathsIgnored) if err != nil { t.Fatalf("expected nil error, got %v", err) } diff --git a/builder/builder2v2/build_relationship.go b/builder/build_relationship.go similarity index 61% rename from builder/builder2v2/build_relationship.go rename to builder/build_relationship.go index 1dd8f2b0..fa534add 100644 --- a/builder/builder2v2/build_relationship.go +++ b/builder/build_relationship.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v2 +package builder import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// BuildRelationshipSection2_2 creates an SPDX Relationship (version 2.2) +// BuildRelationshipSection creates an SPDX Relationship // solely for the document "DESCRIBES" package relationship, returning that // relationship or error if any is encountered. Arguments: // - packageName: name of package / directory -func BuildRelationshipSection2_2(packageName string) (*v2_2.Relationship, error) { - rln := &v2_2.Relationship{ +func BuildRelationshipSection(packageName string) (*spdx.Relationship, error) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", fmt.Sprintf("Package-%s", packageName)), Relationship: "DESCRIBES", diff --git a/builder/builder2v3/build_relationship_test.go b/builder/build_relationship_test.go similarity index 78% rename from builder/builder2v3/build_relationship_test.go rename to builder/build_relationship_test.go index cbf27a75..fa5a3bee 100644 --- a/builder/builder2v3/build_relationship_test.go +++ b/builder/build_relationship_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package builder2v3 +package builder import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== Relationship section builder tests ===== -func TestBuilder2_3CanBuildRelationshipSection(t *testing.T) { +func TestBuilderCanBuildRelationshipSection(t *testing.T) { packageName := "project17" - rln, err := BuildRelationshipSection2_3(packageName) + rln, err := BuildRelationshipSection(packageName) if err != nil { t.Fatalf("expected nil error, got %v", err) } diff --git a/builder/build_test.go b/builder/build_test.go index ff881de9..d1f955db 100644 --- a/builder/build_test.go +++ b/builder/build_test.go @@ -6,14 +6,14 @@ import ( "fmt" "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 Builder top-level Document test ===== -func TestBuild2_1CreatesDocument(t *testing.T) { +func TestBuildCreatesDocument(t *testing.T) { dirRoot := "../testdata/project1/" - config := &Config2_1{ + config := &Config{ NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", CreatorType: "Person", Creator: "John Doe", @@ -23,7 +23,7 @@ func TestBuild2_1CreatesDocument(t *testing.T) { wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"} - doc, err := Build2_1("project1", dirRoot, config) + doc, err := Build("project1", dirRoot, config) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -35,422 +35,11 @@ func TestBuild2_1CreatesDocument(t *testing.T) { if doc.CreationInfo == nil { t.Fatalf("expected non-nil CreationInfo section, got nil") } - if doc.SPDXVersion != "SPDX-2.1" { - t.Errorf("expected %s, got %s", "SPDX-2.1", doc.SPDXVersion) + if doc.SPDXVersion != spdx.Version { + t.Errorf("expected %s, got %s", spdx.Version, doc.SPDXVersion) } - if doc.DataLicense != "CC0-1.0" { - t.Errorf("expected %s, got %s", "CC0-1.0", doc.DataLicense) - } - if doc.SPDXIdentifier != common.ElementID("DOCUMENT") { - t.Errorf("expected %s, got %v", "DOCUMENT", doc.SPDXIdentifier) - } - if doc.DocumentName != "project1" { - t.Errorf("expected %s, got %s", "project1", doc.DocumentName) - } - wantNamespace := fmt.Sprintf("https://github.com/swinslow/spdx-docs/spdx-go/testdata-project1-%s", wantVerificationCode) - if doc.DocumentNamespace != wantNamespace { - t.Errorf("expected %s, got %s", wantNamespace, doc.DocumentNamespace) - } - if len(doc.CreationInfo.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(doc.CreationInfo.Creators)) - } - if doc.CreationInfo.Creators[1].Creator != "John Doe" { - t.Errorf("expected %s, got %+v", "John Doe", doc.CreationInfo.Creators[1]) - } - if doc.CreationInfo.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %+v", "github.com/spdx/tools-golang/builder", doc.CreationInfo.Creators[0]) - } - if doc.CreationInfo.Created != "2018-10-19T04:38:00Z" { - t.Errorf("expected %s, got %s", "2018-10-19T04:38:00Z", doc.CreationInfo.Created) - } - - // check Package section - if doc.Packages == nil { - t.Fatalf("expected non-nil doc.Packages, got nil") - } - if len(doc.Packages) != 1 { - t.Fatalf("expected %d, got %d", 1, len(doc.Packages)) - } - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - if pkg.PackageName != "project1" { - t.Errorf("expected %v, got %v", "project1", pkg.PackageName) - } - if pkg.PackageSPDXIdentifier != common.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.PackageVerificationCode.Value != wantVerificationCode.Value { - 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) - } - - // check Files section - 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)) - } - - // files should be in order of identifier, which is numeric, - // created based on alphabetical order of files: - // emptyfile, file1, file3, folder/file4, lastfile - - // check emptyfile.testdata.txt - fileEmpty := pkg.Files[0] - 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 != common.ElementID("File0") { - t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier) - } - - for _, checksum := range fileEmpty.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "da39a3ee5e6b4b0d3255bfef95601890afd80709" { - t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", checksum.Value) - } - case common.SHA256: - if checksum.Value != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { - t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", checksum.Value) - } - case common.MD5: - if checksum.Value != "d41d8cd98f00b204e9800998ecf8427e" { - t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", checksum.Value) - } - } - } - - if fileEmpty.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded) - } - if len(fileEmpty.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFiles)) - } else { - if fileEmpty.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFiles[0]) - } - } - if fileEmpty.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText) - } - - // check file1.testdata.txt - file1 := pkg.Files[1] - 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.FileSPDXIdentifier != common.ElementID("File1") { - t.Errorf("expected %v, got %v", "File1", file1.FileSPDXIdentifier) - } - - for _, checksum := range file1.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "024f870eb6323f532515f7a09d5646a97083b819" { - t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", checksum.Value) - } - case common.SHA256: - if checksum.Value != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" { - t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", checksum.Value) - } - case common.MD5: - if checksum.Value != "37c8208479dfe42d2bb29debd6e32d4a" { - t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", checksum.Value) - } - } - } - if file1.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseConcluded) - } - if len(file1.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(file1.LicenseInfoInFiles)) - } else { - if file1.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseInfoInFiles[0]) - } - } - if file1.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.FileCopyrightText) - } - - // check file3.testdata.txt - file3 := pkg.Files[2] - 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.FileSPDXIdentifier != common.ElementID("File2") { - t.Errorf("expected %v, got %v", "File2", file3.FileSPDXIdentifier) - } - - for _, checksum := range file3.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "a46114b70e163614f01c64adf44cdd438f158fce" { - t.Errorf("expected %v, got %v", "a46114b70e163614f01c64adf44cdd438f158fce", checksum.Value) - } - case common.SHA256: - if checksum.Value != "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4" { - t.Errorf("expected %v, got %v", "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4", checksum.Value) - } - case common.MD5: - if checksum.Value != "3e02d3ab9c58eec6911dbba37570934f" { - t.Errorf("expected %v, got %v", "3e02d3ab9c58eec6911dbba37570934f", checksum.Value) - } - } - } - if file3.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseConcluded) - } - if len(file3.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(file3.LicenseInfoInFiles)) - } else { - if file3.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseInfoInFiles[0]) - } - } - if file3.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file3.FileCopyrightText) - } - - // check folder1/file4.testdata.txt - file4 := pkg.Files[3] - 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.FileSPDXIdentifier != common.ElementID("File3") { - t.Errorf("expected %v, got %v", "File3", file4.FileSPDXIdentifier) - } - - for _, checksum := range file4.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "e623d7d7d782a7c8323c4d436acee4afab34320f" { - t.Errorf("expected %v, got %v", "e623d7d7d782a7c8323c4d436acee4afab34320f", checksum.Value) - } - case common.SHA256: - if checksum.Value != "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59" { - t.Errorf("expected %v, got %v", "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59", checksum.Value) - } - case common.MD5: - if checksum.Value != "96e6a25d35df5b1c477710ef4d0c7210" { - t.Errorf("expected %v, got %v", "96e6a25d35df5b1c477710ef4d0c7210", checksum.Value) - } - } - } - if file4.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseConcluded) - } - if len(file4.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(file4.LicenseInfoInFiles)) - } else { - if file4.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseInfoInFiles[0]) - } - } - if file4.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file4.FileCopyrightText) - } - - // check lastfile.testdata.txt - lastfile := pkg.Files[4] - 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.FileSPDXIdentifier != common.ElementID("File4") { - t.Errorf("expected %v, got %v", "File4", lastfile.FileSPDXIdentifier) - } - - for _, checksum := range lastfile.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "26d6221d682d9ba59116f9753a701f34271c8ce1" { - t.Errorf("expected %v, got %v", "26d6221d682d9ba59116f9753a701f34271c8ce1", checksum.Value) - } - case common.SHA256: - if checksum.Value != "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805" { - t.Errorf("expected %v, got %v", "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805", checksum.Value) - } - case common.MD5: - if checksum.Value != "f60baa793870d9085461ad6bbab50b7f" { - t.Errorf("expected %v, got %v", "f60baa793870d9085461ad6bbab50b7f", checksum.Value) - } - } - } - if lastfile.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseConcluded) - } - if len(lastfile.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(lastfile.LicenseInfoInFiles)) - } else { - if lastfile.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseInfoInFiles[0]) - } - } - if lastfile.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.FileCopyrightText) - } - - // check Relationship section -- should be a relationship for doc DESCRIBES pkg - if doc.Relationships == nil { - t.Fatalf("expected non-nil Relationships section, got nil") - } - if len(doc.Relationships) == 0 { - t.Fatalf("expected %v, got %v", 0, len(doc.Relationships)) - } - rln := doc.Relationships[0] - if rln == nil { - t.Fatalf("expected non-nil Relationship, got nil") - } - if rln.RefA != common.MakeDocElementID("", "DOCUMENT") { - t.Errorf("expected %v, got %v", "DOCUMENT", rln.RefA) - } - if rln.RefB != common.MakeDocElementID("", "Package-project1") { - t.Errorf("expected %v, got %v", "Package-project1", rln.RefB) - } - if rln.Relationship != "DESCRIBES" { - t.Errorf("expected %v, got %v", "DESCRIBES", rln.Relationship) - } - - // and check that other sections are present, but empty - if doc.OtherLicenses != nil { - t.Fatalf("expected nil OtherLicenses section, got non-nil") - } - if doc.Annotations != nil { - t.Fatalf("expected nil Annotations section, got non-nil") - } - if doc.Reviews != nil { - t.Fatalf("expected nil Reviews section, got non-nil") - } - -} - -func TestBuild2_1CanIgnoreFiles(t *testing.T) { - dirRoot := "../testdata/project3/" - - config := &Config2_1{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - CreatorType: "Person", - Creator: "John Doe", - PathsIgnored: []string{ - "**/ignoredir/", - "/excludedir/", - "**/ignorefile.txt", - "/alsoEXCLUDEthis.txt", - }, - TestValues: make(map[string]string), - } - config.TestValues["Created"] = "2018-10-19T04:38:00Z" - - doc, err := Build2_1("project1", dirRoot, config) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - if len(pkg.Files) != 5 { - t.Fatalf("expected len %d, got %d", 5, len(pkg.Files)) - } - - want := "./dontscan.txt" - got := pkg.Files[0].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep/keep.txt" - got = pkg.Files[1].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep.txt" - got = pkg.Files[2].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/dontscan.txt" - got = pkg.Files[3].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/keep.txt" - got = pkg.Files[4].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } -} - -// ===== 2.2 Builder top-level Document test ===== -func TestBuild2_2CreatesDocument(t *testing.T) { - dirRoot := "../testdata/project1/" - - config := &Config2_2{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - CreatorType: "Person", - Creator: "John Doe", - TestValues: make(map[string]string), - } - config.TestValues["Created"] = "2018-10-19T04:38:00Z" - - wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"} - - doc, err := Build2_2("project1", dirRoot, config) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if doc == nil { - t.Fatalf("expected non-nil Document, got nil") - } - - // check CI section - if doc.CreationInfo == nil { - t.Fatalf("expected non-nil CreationInfo section, got nil") - } - if doc.SPDXVersion != "SPDX-2.2" { - t.Errorf("expected %s, got %s", "SPDX-2.2", doc.SPDXVersion) - } - if doc.DataLicense != "CC0-1.0" { - t.Errorf("expected %s, got %s", "CC0-1.0", doc.DataLicense) + if doc.DataLicense != spdx.DataLicense { + t.Errorf("expected %s, got %s", spdx.DataLicense, doc.DataLicense) } if doc.SPDXIdentifier != common.ElementID("DOCUMENT") { t.Errorf("expected %s, got %v", "DOCUMENT", doc.SPDXIdentifier) @@ -765,10 +354,10 @@ func TestBuild2_2CreatesDocument(t *testing.T) { } -func TestBuild2_2CanIgnoreFiles(t *testing.T) { +func TestBuildCanIgnoreFiles(t *testing.T) { dirRoot := "../testdata/project3/" - config := &Config2_2{ + config := &Config{ NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", CreatorType: "Person", Creator: "John Doe", @@ -782,7 +371,7 @@ func TestBuild2_2CanIgnoreFiles(t *testing.T) { } config.TestValues["Created"] = "2018-10-19T04:38:00Z" - doc, err := Build2_2("project1", dirRoot, config) + doc, err := Build("project1", dirRoot, config) if err != nil { t.Errorf("expected nil error, got %v", err) } diff --git a/builder/builder2v1/build_creation_info.go b/builder/builder2v1/build_creation_info.go deleted file mode 100644 index e128fb95..00000000 --- a/builder/builder2v1/build_creation_info.go +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "time" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" -) - -// BuildCreationInfoSection2_1 creates an SPDX Package (version 2.1), returning that -// package or error if any is encountered. Arguments: -// - creatorType: one of Person, Organization or Tool -// - creator: creator string -// - testValues: for testing only; call with nil when using in production -func BuildCreationInfoSection2_1(creatorType string, creator string, testValues map[string]string) (*v2_1.CreationInfo, error) { - // build creator slices - creators := []common.Creator{ - // add builder as a tool - { - Creator: "github.com/spdx/tools-golang/builder", - CreatorType: "Tool", - }, - { - Creator: creator, - CreatorType: creatorType, - }, - } - - // use test Created time if passing test values - location, _ := time.LoadLocation("UTC") - locationTime := time.Now().In(location) - created := locationTime.Format("2006-01-02T15:04:05Z") - if testVal := testValues["Created"]; testVal != "" { - created = testVal - } - - ci := &v2_1.CreationInfo{ - Creators: creators, - Created: created, - } - return ci, nil -} diff --git a/builder/builder2v1/build_creation_info_test.go b/builder/builder2v1/build_creation_info_test.go deleted file mode 100644 index 9684fdeb..00000000 --- a/builder/builder2v1/build_creation_info_test.go +++ /dev/null @@ -1,111 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "testing" -) - -// ===== CreationInfo section builder tests ===== -func TestBuilder2_1CanBuildCreationInfoSection(t *testing.T) { - creatorType := "Organization" - creator := "Jane Doe LLC" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_1(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "Jane Doe LLC" { - t.Errorf("expected %s, got %s", "Jane Doe LLC", ci.Creators[1].Creator) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0].Creator) - } - if ci.Created != "2018-10-20T16:48:00Z" { - t.Errorf("expected %s, got %s", "2018-10-20T16:48:00Z", ci.Created) - } -} - -func TestBuilder2_1CanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) { - creatorType := "Person" - creator := "John Doe" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_1(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "John Doe" { - t.Errorf("expected %s, got %s", "John Doe", ci.Creators[1].Creator) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0].Creator) - } -} - -func TestBuilder2_1CanBuildCreationInfoSectionWithCreatorTool(t *testing.T) { - creatorType := "Tool" - creator := "some-other-tool-2.1" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_1(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0]) - } - if ci.Creators[1].Creator != "some-other-tool-2.1" { - t.Errorf("expected %s, got %s", "some-other-tool-2.1", ci.Creators[1]) - } -} - -func TestBuilder2_1CanBuildCreationInfoSectionWithInvalidPerson(t *testing.T) { - creatorType := "Whatever" - creator := "John Doe" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_1(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "John Doe" { - t.Errorf("expected %s, got %s", "John Doe", ci.Creators[1]) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0]) - } -} diff --git a/builder/builder2v1/build_file.go b/builder/builder2v1/build_file.go deleted file mode 100644 index 57cc2bc9..00000000 --- a/builder/builder2v1/build_file.go +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "fmt" - "path/filepath" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/utils" -) - -// BuildFileSection2_1 creates an SPDX File (version 2.1), returning that -// file or error if any is encountered. Arguments: -// - filePath: path to file, relative to prefix -// - prefix: relative directory for filePath -// - fileNumber: integer index (unique within package) to use in identifier -func BuildFileSection2_1(filePath string, prefix string, fileNumber int) (*v2_1.File, error) { - // build the full file path - p := filepath.Join(prefix, filePath) - - // make sure we can get the file and its hashes - ssha1, ssha256, smd5, err := utils.GetHashesForFilePath(p) - if err != nil { - return nil, err - } - - // build the identifier - i := fmt.Sprintf("File%d", fileNumber) - - // now build the File section - f := &v2_1.File{ - FileName: filePath, - FileSPDXIdentifier: common.ElementID(i), - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: ssha1, - }, - { - Algorithm: common.SHA256, - Value: ssha256, - }, - { - Algorithm: common.MD5, - Value: smd5, - }, - }, - LicenseConcluded: "NOASSERTION", - LicenseInfoInFiles: []string{"NOASSERTION"}, - FileCopyrightText: "NOASSERTION", - } - - return f, nil -} diff --git a/builder/builder2v1/build_file_test.go b/builder/builder2v1/build_file_test.go deleted file mode 100644 index 5da0390b..00000000 --- a/builder/builder2v1/build_file_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== File section builder tests ===== -func TestBuilder2_1CanBuildFileSection(t *testing.T) { - filePath := "/file1.testdata.txt" - prefix := "../../testdata/project1/" - fileNumber := 17 - - file1, err := BuildFileSection2_1(filePath, prefix, fileNumber) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - 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.FileSPDXIdentifier != common.ElementID("File17") { - t.Errorf("expected %v, got %v", "File17", file1.FileSPDXIdentifier) - } - - for _, checksum := range file1.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "024f870eb6323f532515f7a09d5646a97083b819" { - t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", checksum.Value) - } - case common.SHA256: - if checksum.Value != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" { - t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", checksum.Value) - } - case common.MD5: - if checksum.Value != "37c8208479dfe42d2bb29debd6e32d4a" { - t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", checksum.Value) - } - } - } - - if file1.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseConcluded) - } - if len(file1.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(file1.LicenseInfoInFiles)) - } else { - if file1.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseInfoInFiles[0]) - } - } - if file1.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.FileCopyrightText) - } - -} - -func TestBuilder2_1BuildFileSectionFailsForInvalidFilePath(t *testing.T) { - filePath := "/file1.testdata.txt" - prefix := "oops/wrong/path" - fileNumber := 11 - - _, err := BuildFileSection2_1(filePath, prefix, fileNumber) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} diff --git a/builder/builder2v1/build_package.go b/builder/builder2v1/build_package.go deleted file mode 100644 index 7d8ddbb1..00000000 --- a/builder/builder2v1/build_package.go +++ /dev/null @@ -1,78 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "fmt" - "path/filepath" - "regexp" - "runtime" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/utils" -) - -// BuildPackageSection2_1 creates an SPDX Package (version 2.1), returning -// that package or error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - pathsIgnore: slice of strings for filepaths to ignore -func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []string) (*v2_1.Package, error) { - // build the file section first, so we'll have it available - // for calculating the package verification code - filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore) - 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 := []*v2_1.File{} - fileNumber := 0 - for _, fp := range filepaths { - 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 = append(files, newFile) - fileNumber++ - } - // get the verification code - code, err := utils.GetVerificationCode2_1(files, "") - if err != nil { - return nil, err - } - - // now build the package section - pkg := &v2_1.Package{ - PackageName: packageName, - PackageSPDXIdentifier: common.ElementID(fmt.Sprintf("Package-%s", packageName)), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - PackageVerificationCode: code, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{}, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: files, - } - - return pkg, nil -} diff --git a/builder/builder2v1/build_relationship.go b/builder/builder2v1/build_relationship.go deleted file mode 100644 index 9f11d583..00000000 --- a/builder/builder2v1/build_relationship.go +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "fmt" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" -) - -// BuildRelationshipSection2_1 creates an SPDX Relationship (version 2.1) -// solely for the document "DESCRIBES" package relationship, returning that -// relationship or error if any is encountered. Arguments: -// - packageName: name of package / directory -func BuildRelationshipSection2_1(packageName string) (*v2_1.Relationship, error) { - rln := &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", fmt.Sprintf("Package-%s", packageName)), - Relationship: "DESCRIBES", - } - - return rln, nil -} diff --git a/builder/builder2v1/build_relationship_test.go b/builder/builder2v1/build_relationship_test.go deleted file mode 100644 index a24dedcd..00000000 --- a/builder/builder2v1/build_relationship_test.go +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v1 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== Relationship section builder tests ===== -func TestBuilder2_1CanBuildRelationshipSection(t *testing.T) { - packageName := "project17" - - rln, err := BuildRelationshipSection2_1(packageName) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if rln == nil { - t.Fatalf("expected non-nil relationship, got nil") - } - if rln.RefA != common.MakeDocElementID("", "DOCUMENT") { - t.Errorf("expected %v, got %v", "DOCUMENT", rln.RefA) - } - if rln.RefB != common.MakeDocElementID("", "Package-project17") { - t.Errorf("expected %v, got %v", "Package-project17", rln.RefB) - } - if rln.Relationship != "DESCRIBES" { - t.Errorf("expected %v, got %v", "DESCRIBES", rln.Relationship) - } - -} diff --git a/builder/builder2v2/build_creation_info.go b/builder/builder2v2/build_creation_info.go deleted file mode 100644 index 74b43fb5..00000000 --- a/builder/builder2v2/build_creation_info.go +++ /dev/null @@ -1,47 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "time" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" -) - -// BuildCreationInfoSection2_2 creates an SPDX Package (version 2.2), returning that -// package or error if any is encountered. Arguments: -// - packageName: name of package / directory -// - code: verification code from Package -// - namespacePrefix: prefix for DocumentNamespace (packageName and code will be added) -// - creatorType: one of Person, Organization or Tool -// - creator: creator string -// - testValues: for testing only; call with nil when using in production -func BuildCreationInfoSection2_2(creatorType string, creator string, testValues map[string]string) (*v2_2.CreationInfo, error) { - // build creator slices - creators := []common.Creator{ - // add builder as a tool - { - Creator: "github.com/spdx/tools-golang/builder", - CreatorType: "Tool", - }, - { - Creator: creator, - CreatorType: creatorType, - }, - } - - // use test Created time if passing test values - location, _ := time.LoadLocation("UTC") - locationTime := time.Now().In(location) - created := locationTime.Format("2006-01-02T15:04:05Z") - if testVal := testValues["Created"]; testVal != "" { - created = testVal - } - - ci := &v2_2.CreationInfo{ - Creators: creators, - Created: created, - } - return ci, nil -} diff --git a/builder/builder2v2/build_file.go b/builder/builder2v2/build_file.go deleted file mode 100644 index 4e69f58b..00000000 --- a/builder/builder2v2/build_file.go +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "fmt" - "path/filepath" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/utils" -) - -// BuildFileSection2_2 creates an SPDX File (version 2.2), returning that -// file or error if any is encountered. Arguments: -// - filePath: path to file, relative to prefix -// - prefix: relative directory for filePath -// - fileNumber: integer index (unique within package) to use in identifier -func BuildFileSection2_2(filePath string, prefix string, fileNumber int) (*v2_2.File, error) { - // build the full file path - p := filepath.Join(prefix, filePath) - - // make sure we can get the file and its hashes - ssha1, ssha256, smd5, err := utils.GetHashesForFilePath(p) - if err != nil { - return nil, err - } - - // build the identifier - i := fmt.Sprintf("File%d", fileNumber) - - // now build the File section - f := &v2_2.File{ - FileName: filePath, - FileSPDXIdentifier: common.ElementID(i), - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: ssha1, - }, - { - Algorithm: common.SHA256, - Value: ssha256, - }, - { - Algorithm: common.MD5, - Value: smd5, - }, - }, - LicenseConcluded: "NOASSERTION", - LicenseInfoInFiles: []string{"NOASSERTION"}, - FileCopyrightText: "NOASSERTION", - } - - return f, nil -} diff --git a/builder/builder2v2/build_file_test.go b/builder/builder2v2/build_file_test.go deleted file mode 100644 index 86915837..00000000 --- a/builder/builder2v2/build_file_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== File section builder tests ===== -func TestBuilder2_2CanBuildFileSection(t *testing.T) { - filePath := "/file1.testdata.txt" - prefix := "../../testdata/project1/" - fileNumber := 17 - - file1, err := BuildFileSection2_2(filePath, prefix, fileNumber) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - 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.FileSPDXIdentifier != common.ElementID("File17") { - t.Errorf("expected %v, got %v", "File17", file1.FileSPDXIdentifier) - } - - for _, checksum := range file1.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "024f870eb6323f532515f7a09d5646a97083b819" { - t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", checksum.Value) - } - case common.SHA256: - if checksum.Value != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" { - t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", checksum.Value) - } - case common.MD5: - if checksum.Value != "37c8208479dfe42d2bb29debd6e32d4a" { - t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", checksum.Value) - } - } - } - - if file1.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseConcluded) - } - if len(file1.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(file1.LicenseInfoInFiles)) - } else { - if file1.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseInfoInFiles[0]) - } - } - if file1.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", file1.FileCopyrightText) - } - -} - -func TestBuilder2_2BuildFileSectionFailsForInvalidFilePath(t *testing.T) { - filePath := "/file1.testdata.txt" - prefix := "oops/wrong/path" - fileNumber := 11 - - _, err := BuildFileSection2_2(filePath, prefix, fileNumber) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} diff --git a/builder/builder2v2/build_package.go b/builder/builder2v2/build_package.go deleted file mode 100644 index 7cccf630..00000000 --- a/builder/builder2v2/build_package.go +++ /dev/null @@ -1,79 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "fmt" - "path/filepath" - "regexp" - "runtime" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/utils" -) - -// BuildPackageSection2_2 creates an SPDX Package (version 2.2), returning -// that package or error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - pathsIgnore: slice of strings for filepaths to ignore -func BuildPackageSection2_2(packageName string, dirRoot string, pathsIgnore []string) (*v2_2.Package, error) { - // 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 := []*v2_2.File{} - fileNumber := 0 - for _, fp := range filepaths { - 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 - } - files = append(files, newFile) - fileNumber++ - } - - // get the verification code - code, err := utils.GetVerificationCode2_2(files, "") - if err != nil { - return nil, err - } - - // now build the package section - pkg := &v2_2.Package{ - PackageName: packageName, - PackageSPDXIdentifier: common.ElementID(fmt.Sprintf("Package-%s", packageName)), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - PackageVerificationCode: code, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{}, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: files, - } - - return pkg, nil -} diff --git a/builder/builder2v2/build_package_test.go b/builder/builder2v2/build_package_test.go deleted file mode 100644 index 8a214163..00000000 --- a/builder/builder2v2/build_package_test.go +++ /dev/null @@ -1,156 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== Package section builder tests ===== -func TestBuilder2_2CanBuildPackageSection(t *testing.T) { - packageName := "project1" - dirRoot := "../../testdata/project1/" - - wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"} - - pkg, err := BuildPackageSection2_2(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 != common.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.Value != wantVerificationCode.Value { - 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[0] - 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 != common.ElementID("File0") { - t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier) - } - for _, checksum := range fileEmpty.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "da39a3ee5e6b4b0d3255bfef95601890afd80709" { - t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", checksum.Value) - } - case common.SHA256: - if checksum.Value != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { - t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", checksum.Value) - } - case common.MD5: - if checksum.Value != "d41d8cd98f00b204e9800998ecf8427e" { - t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", checksum.Value) - } - } - } - if fileEmpty.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded) - } - if len(fileEmpty.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFiles)) - } else { - if fileEmpty.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFiles[0]) - } - } - if fileEmpty.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText) - } -} - -func TestBuilder2_2CanIgnoreFiles(t *testing.T) { - packageName := "project3" - dirRoot := "../../testdata/project3/" - pathsIgnored := []string{ - "**/ignoredir/", - "/excludedir/", - "**/ignorefile.txt", - "/alsoEXCLUDEthis.txt", - } - pkg, err := BuildPackageSection2_2(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[0].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep/keep.txt" - got = pkg.Files[1].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep.txt" - got = pkg.Files[2].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/dontscan.txt" - got = pkg.Files[3].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/keep.txt" - got = pkg.Files[4].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } -} diff --git a/builder/builder2v2/build_relationship_test.go b/builder/builder2v2/build_relationship_test.go deleted file mode 100644 index 126ddbea..00000000 --- a/builder/builder2v2/build_relationship_test.go +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v2 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== Relationship section builder tests ===== -func TestBuilder2_2CanBuildRelationshipSection(t *testing.T) { - packageName := "project17" - - rln, err := BuildRelationshipSection2_2(packageName) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if rln == nil { - t.Fatalf("expected non-nil relationship, got nil") - } - if rln.RefA != common.MakeDocElementID("", "DOCUMENT") { - t.Errorf("expected %v, got %v", "DOCUMENT", rln.RefA) - } - if rln.RefB != common.MakeDocElementID("", "Package-project17") { - t.Errorf("expected %v, got %v", "Package-project17", rln.RefB) - } - if rln.Relationship != "DESCRIBES" { - t.Errorf("expected %v, got %v", "DESCRIBES", rln.Relationship) - } - -} diff --git a/builder/builder2v3/build_creation_info_test.go b/builder/builder2v3/build_creation_info_test.go deleted file mode 100644 index f1868f0a..00000000 --- a/builder/builder2v3/build_creation_info_test.go +++ /dev/null @@ -1,111 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v3 - -import ( - "testing" -) - -// ===== CreationInfo section builder tests ===== -func TestBuilder2_3CanBuildCreationInfoSection(t *testing.T) { - creatorType := "Organization" - creator := "Jane Doe LLC" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_3(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "Jane Doe LLC" { - t.Errorf("expected %s, got %s", "Jane Doe LLC", ci.Creators[0].Creator) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[1].Creator) - } - if ci.Created != "2018-10-20T16:48:00Z" { - t.Errorf("expected %s, got %s", "2018-10-20T16:48:00Z", ci.Created) - } -} - -func TestBuilder2_3CanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) { - creatorType := "Person" - creator := "John Doe" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_3(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "John Doe" { - t.Errorf("expected %s, got %s", "John Doe", ci.Creators[0].Creator) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[1].Creator) - } -} - -func TestBuilder2_3CanBuildCreationInfoSectionWithCreatorTool(t *testing.T) { - creatorType := "Tool" - creator := "some-other-tool-2.1" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_3(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0]) - } - if ci.Creators[1].Creator != "some-other-tool-2.1" { - t.Errorf("expected %s, got %s", "some-other-tool-2.1", ci.Creators[1]) - } -} - -func TestBuilder2_3CanBuildCreationInfoSectionWithInvalidPerson(t *testing.T) { - creatorType := "Whatever" - creator := "John Doe" - testValues := make(map[string]string) - testValues["Created"] = "2018-10-20T16:48:00Z" - - ci, err := BuildCreationInfoSection2_3(creatorType, creator, testValues) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - - if ci == nil { - t.Fatalf("expected non-nil CreationInfo, got nil") - } - if len(ci.Creators) != 2 { - t.Fatalf("expected %d, got %d", 2, len(ci.Creators)) - } - if ci.Creators[1].Creator != "John Doe" { - t.Errorf("expected %s, got %s", "John Doe", ci.Creators[1]) - } - if ci.Creators[0].Creator != "github.com/spdx/tools-golang/builder" { - t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.Creators[0]) - } -} diff --git a/builder/builder2v3/build_package_test.go b/builder/builder2v3/build_package_test.go deleted file mode 100644 index 873e3d55..00000000 --- a/builder/builder2v3/build_package_test.go +++ /dev/null @@ -1,156 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v3 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/common" -) - -// ===== Package section builder tests ===== -func TestBuilder2_3CanBuildPackageSection(t *testing.T) { - packageName := "project1" - dirRoot := "../../testdata/project1/" - - wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"} - - pkg, err := BuildPackageSection2_3(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 != common.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.Value != wantVerificationCode.Value { - 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[0] - 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 != common.ElementID("File0") { - t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier) - } - for _, checksum := range fileEmpty.Checksums { - switch checksum.Algorithm { - case common.SHA1: - if checksum.Value != "da39a3ee5e6b4b0d3255bfef95601890afd80709" { - t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", checksum.Value) - } - case common.SHA256: - if checksum.Value != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { - t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", checksum.Value) - } - case common.MD5: - if checksum.Value != "d41d8cd98f00b204e9800998ecf8427e" { - t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", checksum.Value) - } - } - } - if fileEmpty.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded) - } - if len(fileEmpty.LicenseInfoInFiles) != 1 { - t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFiles)) - } else { - if fileEmpty.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFiles[0]) - } - } - if fileEmpty.FileCopyrightText != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText) - } -} - -func TestBuilder2_3CanIgnoreFiles(t *testing.T) { - packageName := "project3" - dirRoot := "../../testdata/project3/" - pathsIgnored := []string{ - "**/ignoredir/", - "/excludedir/", - "**/ignorefile.txt", - "/alsoEXCLUDEthis.txt", - } - pkg, err := BuildPackageSection2_3(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[0].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep/keep.txt" - got = pkg.Files[1].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./keep.txt" - got = pkg.Files[2].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/dontscan.txt" - got = pkg.Files[3].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } - - want = "./subdir/keep/keep.txt" - got = pkg.Files[4].FileName - if want != got { - t.Errorf("expected %v, got %v", want, got) - } -} diff --git a/builder/builder2v3/build_relationship.go b/builder/builder2v3/build_relationship.go deleted file mode 100644 index 45ce5bea..00000000 --- a/builder/builder2v3/build_relationship.go +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package builder2v3 - -import ( - "fmt" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -// BuildRelationshipSection2_3 creates an SPDX Relationship (version 2.3) -// solely for the document "DESCRIBES" package relationship, returning that -// relationship or error if any is encountered. Arguments: -// - packageName: name of package / directory -func BuildRelationshipSection2_3(packageName string) (*v2_3.Relationship, error) { - rln := &v2_3.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", fmt.Sprintf("Package-%s", packageName)), - Relationship: "DESCRIBES", - } - - return rln, nil -} diff --git a/convert/chain.go b/convert/chain.go new file mode 100644 index 00000000..ac96733c --- /dev/null +++ b/convert/chain.go @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package convert + +import ( + "fmt" + "reflect" + + converter "github.com/anchore/go-struct-converter" + + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +var DocumentChain = converter.NewChain( + v2_1.Document{}, + v2_2.Document{}, + v2_3.Document{}, +) + +// Document converts from one document to another document +// For example, converting a document to the latest version could be done like: +// +// sourceDoc := // e.g. a v2_2.Document from somewhere +// var targetDoc spdx.Document // this can be any document version +// err := convert.Document(sourceDoc, &targetDoc) // the target must be passed as a pointer +func Document(from common.AnyDocument, to common.AnyDocument) error { + if !IsPtr(to) { + return fmt.Errorf("struct to convert to must be a pointer") + } + from = FromPtr(from) + if reflect.TypeOf(from) == reflect.TypeOf(FromPtr(to)) { + reflect.ValueOf(to).Elem().Set(reflect.ValueOf(from)) + return nil + } + return DocumentChain.Convert(from, to) +} diff --git a/convert/spdx_document_conversion_test.go b/convert/spdx_document_conversion_test.go new file mode 100644 index 00000000..1cced995 --- /dev/null +++ b/convert/spdx_document_conversion_test.go @@ -0,0 +1,2135 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package convert + +import ( + "encoding/json" + "reflect" + "testing" + + "github.com/anchore/go-struct-converter" + "github.com/stretchr/testify/require" + + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" +) + +func Test_ConvertSPDXDocuments(t *testing.T) { + tests := []struct { + name string + source interface{} + expected interface{} + }{ + { + name: "basic v2_2 to v2_3", + source: v2_2.Document{ + SPDXVersion: v2_2.Version, + Packages: []*v2_2.Package{ + { + PackageName: "Pkg 1", + Files: []*v2_2.File{ + { + FileName: "File 1", + }, + { + FileName: "File 2", + }, + }, + PackageVerificationCode: common.PackageVerificationCode{ + Value: "verification code value", + ExcludedFiles: []string{ + "a", + "b", + }, + }, + }, + }, + }, + expected: spdx.Document{ + SPDXVersion: spdx.Version, + Packages: []*spdx.Package{ + { + PackageName: "Pkg 1", + Files: []*spdx.File{ + { + FileName: "File 1", + }, + { + FileName: "File 2", + }, + }, + PackageVerificationCode: &common.PackageVerificationCode{ + Value: "verification code value", + ExcludedFiles: []string{ + "a", + "b", + }, + }, + }, + }, + }, + }, + { + name: "full 2.1 -> 2.3 document", + source: v2_1.Document{ + SPDXVersion: "SPDX-2.2", + DataLicense: "data license", + SPDXIdentifier: "spdx id", + DocumentName: "doc name", + DocumentNamespace: "doc namespace", + ExternalDocumentReferences: []v2_1.ExternalDocumentRef{ + { + DocumentRefID: "doc ref id 1", + URI: "uri 1", + Checksum: common.Checksum{ + Algorithm: "algo 1", + Value: "value 1", + }, + }, + { + DocumentRefID: "doc ref id 2", + URI: "uri 2", + Checksum: common.Checksum{ + Algorithm: "algo 2", + Value: "value 2", + }, + }, + }, + DocumentComment: "doc comment", + CreationInfo: &v2_1.CreationInfo{ + LicenseListVersion: "license list version", + Creators: []common.Creator{ + { + Creator: "creator 1", + CreatorType: "type 1", + }, + { + Creator: "creator 2", + CreatorType: "type 2", + }, + }, + Created: "created date", + CreatorComment: "creator comment", + }, + Packages: []*v2_1.Package{ + { + PackageName: "package name 1", + PackageSPDXIdentifier: "id 1", + PackageVersion: "version 1", + PackageFileName: "file 1", + PackageSupplier: nil, + PackageOriginator: nil, + PackageDownloadLocation: "", + FilesAnalyzed: true, + IsFilesAnalyzedTagPresent: true, + PackageVerificationCode: common.PackageVerificationCode{ + Value: "value 1", + ExcludedFiles: []string{"a", "b"}, + }, + PackageChecksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + PackageHomePage: "home page 1", + PackageSourceInfo: "source info 1", + PackageLicenseConcluded: "license concluded 1", + PackageLicenseInfoFromFiles: []string{"a", "b"}, + PackageLicenseDeclared: "license declared 1", + PackageLicenseComments: "license comments 1", + PackageCopyrightText: "copyright text 1", + PackageSummary: "summary 1", + PackageDescription: "description 1", + PackageComment: "comment 1", + PackageExternalReferences: []*v2_1.PackageExternalReference{ + { + Category: "cat 1", + RefType: "type 1", + Locator: "locator 1", + ExternalRefComment: "comment 1", + }, + { + Category: "cat 2", + RefType: "type 2", + Locator: "locator 2", + ExternalRefComment: "comment 2", + }, + }, + Files: []*v2_1.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"a", "b"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "license concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy text 1", + ArtifactOfProjects: []*v2_1.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"dep1", "dep2", "dep3"}, + Snippets: map[common.ElementID]*v2_1.Snippet{ + common.ElementID("e1"): { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + common.ElementID("e2"): { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + }, + Annotations: []v2_1.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Annotations: []v2_1.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Files: []*v2_1.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"t1", "t2"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy 1", + ArtifactOfProjects: []*v2_1.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + { + FileName: "file 2", + FileSPDXIdentifier: "id 2", + FileTypes: []string{"t3", "t4"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 2", + Value: "val 2", + }, + { + Algorithm: "alg 3", + Value: "val 3", + }, + }, + LicenseConcluded: "concluded 2", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 2", + FileCopyrightText: "copy 2", + ArtifactOfProjects: []*v2_1.ArtifactOfProject{ + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + { + Name: "name 4", + HomePage: "home 4", + URI: "uri 4", + }, + }, + FileComment: "comment 2", + FileNotice: "notice 2", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + }, + OtherLicenses: []*v2_1.OtherLicense{ + { + LicenseIdentifier: "id 1", + ExtractedText: "text 1", + LicenseName: "name 1", + LicenseCrossReferences: []string{"x1", "x2", "x3"}, + LicenseComment: "comment 1", + }, + { + LicenseIdentifier: "id 2", + ExtractedText: "text 2", + LicenseName: "name 2", + LicenseCrossReferences: []string{"x4", "x5", "x6"}, + LicenseComment: "comment 2", + }, + }, + Relationships: []*v2_1.Relationship{ + { + RefA: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + Relationship: "type 1", + RelationshipComment: "comment 1", + }, + { + RefA: common.DocElementID{ + DocumentRefID: "doc 3", + ElementRefID: "elem 3", + SpecialID: "spec 3", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 4", + ElementRefID: "elem 4", + SpecialID: "spec 4", + }, + Relationship: "type 2", + RelationshipComment: "comment 2", + }, + }, + Annotations: []*v2_1.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "annotator 1", + AnnotatorType: "annotator type 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "annotator 2", + AnnotatorType: "annotator type 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + Snippets: []v2_1.Snippet{ + { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + }, + Reviews: []*v2_1.Review{ + { + Reviewer: "reviewer 1", + ReviewerType: "type 1", + ReviewDate: "date 1", + ReviewComment: "comment 1", + }, + { + Reviewer: "reviewer 2", + ReviewerType: "type 2", + ReviewDate: "date 2", + ReviewComment: "comment 2", + }, + }, + }, + expected: spdx.Document{ + SPDXVersion: "SPDX-2.3", // ConvertFrom updates this value + DataLicense: "data license", + SPDXIdentifier: "spdx id", + DocumentName: "doc name", + DocumentNamespace: "doc namespace", + ExternalDocumentReferences: []spdx.ExternalDocumentRef{ + { + DocumentRefID: "doc ref id 1", + URI: "uri 1", + Checksum: common.Checksum{ + Algorithm: "algo 1", + Value: "value 1", + }, + }, + { + DocumentRefID: "doc ref id 2", + URI: "uri 2", + Checksum: common.Checksum{ + Algorithm: "algo 2", + Value: "value 2", + }, + }, + }, + DocumentComment: "doc comment", + CreationInfo: &spdx.CreationInfo{ + LicenseListVersion: "license list version", + Creators: []common.Creator{ + { + Creator: "creator 1", + CreatorType: "type 1", + }, + { + Creator: "creator 2", + CreatorType: "type 2", + }, + }, + Created: "created date", + CreatorComment: "creator comment", + }, + Packages: []*spdx.Package{ + { + IsUnpackaged: true, + PackageName: "package name 1", + PackageSPDXIdentifier: "id 1", + PackageVersion: "version 1", + PackageFileName: "file 1", + PackageSupplier: nil, + PackageOriginator: nil, + PackageDownloadLocation: "", + FilesAnalyzed: true, + IsFilesAnalyzedTagPresent: true, + PackageVerificationCode: &common.PackageVerificationCode{ + Value: "value 1", + ExcludedFiles: []string{"a", "b"}, + }, + PackageChecksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + PackageHomePage: "home page 1", + PackageSourceInfo: "source info 1", + PackageLicenseConcluded: "license concluded 1", + PackageLicenseInfoFromFiles: []string{"a", "b"}, + PackageLicenseDeclared: "license declared 1", + PackageLicenseComments: "license comments 1", + PackageCopyrightText: "copyright text 1", + PackageSummary: "summary 1", + PackageDescription: "description 1", + PackageComment: "comment 1", + PackageExternalReferences: []*spdx.PackageExternalReference{ + { + Category: "cat 1", + RefType: "type 1", + Locator: "locator 1", + ExternalRefComment: "comment 1", + }, + { + Category: "cat 2", + RefType: "type 2", + Locator: "locator 2", + ExternalRefComment: "comment 2", + }, + }, + Files: []*spdx.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"a", "b"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "license concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy text 1", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"dep1", "dep2", "dep3"}, + Snippets: map[common.ElementID]*spdx.Snippet{ + common.ElementID("e1"): { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + common.ElementID("e2"): { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + }, + Annotations: []spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Annotations: []spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Files: []*spdx.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"t1", "t2"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy 1", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + { + FileName: "file 2", + FileSPDXIdentifier: "id 2", + FileTypes: []string{"t3", "t4"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 2", + Value: "val 2", + }, + { + Algorithm: "alg 3", + Value: "val 3", + }, + }, + LicenseConcluded: "concluded 2", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 2", + FileCopyrightText: "copy 2", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + { + Name: "name 4", + HomePage: "home 4", + URI: "uri 4", + }, + }, + FileComment: "comment 2", + FileNotice: "notice 2", + FileContributors: []string{"c1", "c2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + }, + OtherLicenses: []*spdx.OtherLicense{ + { + LicenseIdentifier: "id 1", + ExtractedText: "text 1", + LicenseName: "name 1", + LicenseCrossReferences: []string{"x1", "x2", "x3"}, + LicenseComment: "comment 1", + }, + { + LicenseIdentifier: "id 2", + ExtractedText: "text 2", + LicenseName: "name 2", + LicenseCrossReferences: []string{"x4", "x5", "x6"}, + LicenseComment: "comment 2", + }, + }, + Relationships: []*spdx.Relationship{ + { + RefA: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + Relationship: "type 1", + RelationshipComment: "comment 1", + }, + { + RefA: common.DocElementID{ + DocumentRefID: "doc 3", + ElementRefID: "elem 3", + SpecialID: "spec 3", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 4", + ElementRefID: "elem 4", + SpecialID: "spec 4", + }, + Relationship: "type 2", + RelationshipComment: "comment 2", + }, + }, + Annotations: []*spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "annotator 1", + AnnotatorType: "annotator type 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "annotator 2", + AnnotatorType: "annotator type 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + Snippets: []spdx.Snippet{ + { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + }, + }, + Reviews: []*spdx.Review{ + { + Reviewer: "reviewer 1", + ReviewerType: "type 1", + ReviewDate: "date 1", + ReviewComment: "comment 1", + }, + { + Reviewer: "reviewer 2", + ReviewerType: "type 2", + ReviewDate: "date 2", + ReviewComment: "comment 2", + }, + }, + }, + }, + { + name: "full 2.2 -> 2.3 document", + source: v2_2.Document{ + SPDXVersion: "SPDX-2.2", + DataLicense: "data license", + SPDXIdentifier: "spdx id", + DocumentName: "doc name", + DocumentNamespace: "doc namespace", + ExternalDocumentReferences: []v2_2.ExternalDocumentRef{ + { + DocumentRefID: "doc ref id 1", + URI: "uri 1", + Checksum: common.Checksum{ + Algorithm: "algo 1", + Value: "value 1", + }, + }, + { + DocumentRefID: "doc ref id 2", + URI: "uri 2", + Checksum: common.Checksum{ + Algorithm: "algo 2", + Value: "value 2", + }, + }, + }, + DocumentComment: "doc comment", + CreationInfo: &v2_2.CreationInfo{ + LicenseListVersion: "license list version", + Creators: []common.Creator{ + { + Creator: "creator 1", + CreatorType: "type 1", + }, + { + Creator: "creator 2", + CreatorType: "type 2", + }, + }, + Created: "created date", + CreatorComment: "creator comment", + }, + Packages: []*v2_2.Package{ + { + IsUnpackaged: true, + PackageName: "package name 1", + PackageSPDXIdentifier: "id 1", + PackageVersion: "version 1", + PackageFileName: "file 1", + PackageSupplier: nil, + PackageOriginator: nil, + PackageDownloadLocation: "", + FilesAnalyzed: true, + IsFilesAnalyzedTagPresent: true, + PackageVerificationCode: common.PackageVerificationCode{ + Value: "value 1", + ExcludedFiles: []string{"a", "b"}, + }, + PackageChecksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + PackageHomePage: "home page 1", + PackageSourceInfo: "source info 1", + PackageLicenseConcluded: "license concluded 1", + PackageLicenseInfoFromFiles: []string{"a", "b"}, + PackageLicenseDeclared: "license declared 1", + PackageLicenseComments: "license comments 1", + PackageCopyrightText: "copyright text 1", + PackageSummary: "summary 1", + PackageDescription: "description 1", + PackageComment: "comment 1", + PackageExternalReferences: []*v2_2.PackageExternalReference{ + { + Category: "cat 1", + RefType: "type 1", + Locator: "locator 1", + ExternalRefComment: "comment 1", + }, + { + Category: "cat 2", + RefType: "type 2", + Locator: "locator 2", + ExternalRefComment: "comment 2", + }, + }, + PackageAttributionTexts: []string{"a", "b", "c"}, + Files: []*v2_2.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"a", "b"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "license concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy text 1", + ArtifactOfProjects: []*v2_2.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"dep1", "dep2", "dep3"}, + Snippets: map[common.ElementID]*v2_2.Snippet{ + common.ElementID("e1"): { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + common.ElementID("e2"): { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + }, + Annotations: []v2_2.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Annotations: []v2_2.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Files: []*v2_2.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"t1", "t2"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy 1", + ArtifactOfProjects: []*v2_2.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + { + FileName: "file 2", + FileSPDXIdentifier: "id 2", + FileTypes: []string{"t3", "t4"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 2", + Value: "val 2", + }, + { + Algorithm: "alg 3", + Value: "val 3", + }, + }, + LicenseConcluded: "concluded 2", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 2", + FileCopyrightText: "copy 2", + ArtifactOfProjects: []*v2_2.ArtifactOfProject{ + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + { + Name: "name 4", + HomePage: "home 4", + URI: "uri 4", + }, + }, + FileComment: "comment 2", + FileNotice: "notice 2", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + }, + OtherLicenses: []*v2_2.OtherLicense{ + { + LicenseIdentifier: "id 1", + ExtractedText: "text 1", + LicenseName: "name 1", + LicenseCrossReferences: []string{"x1", "x2", "x3"}, + LicenseComment: "comment 1", + }, + { + LicenseIdentifier: "id 2", + ExtractedText: "text 2", + LicenseName: "name 2", + LicenseCrossReferences: []string{"x4", "x5", "x6"}, + LicenseComment: "comment 2", + }, + }, + Relationships: []*v2_2.Relationship{ + { + RefA: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + Relationship: "type 1", + RelationshipComment: "comment 1", + }, + { + RefA: common.DocElementID{ + DocumentRefID: "doc 3", + ElementRefID: "elem 3", + SpecialID: "spec 3", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 4", + ElementRefID: "elem 4", + SpecialID: "spec 4", + }, + Relationship: "type 2", + RelationshipComment: "comment 2", + }, + }, + Annotations: []*v2_2.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "annotator 1", + AnnotatorType: "annotator type 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "annotator 2", + AnnotatorType: "annotator type 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + Snippets: []v2_2.Snippet{ + { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + }, + Reviews: []*v2_2.Review{ + { + Reviewer: "reviewer 1", + ReviewerType: "type 1", + ReviewDate: "date 1", + ReviewComment: "comment 1", + }, + { + Reviewer: "reviewer 2", + ReviewerType: "type 2", + ReviewDate: "date 2", + ReviewComment: "comment 2", + }, + }, + }, + expected: spdx.Document{ + SPDXVersion: "SPDX-2.3", // ConvertFrom updates this value + DataLicense: "data license", + SPDXIdentifier: "spdx id", + DocumentName: "doc name", + DocumentNamespace: "doc namespace", + ExternalDocumentReferences: []spdx.ExternalDocumentRef{ + { + DocumentRefID: "doc ref id 1", + URI: "uri 1", + Checksum: common.Checksum{ + Algorithm: "algo 1", + Value: "value 1", + }, + }, + { + DocumentRefID: "doc ref id 2", + URI: "uri 2", + Checksum: common.Checksum{ + Algorithm: "algo 2", + Value: "value 2", + }, + }, + }, + DocumentComment: "doc comment", + CreationInfo: &spdx.CreationInfo{ + LicenseListVersion: "license list version", + Creators: []common.Creator{ + { + Creator: "creator 1", + CreatorType: "type 1", + }, + { + Creator: "creator 2", + CreatorType: "type 2", + }, + }, + Created: "created date", + CreatorComment: "creator comment", + }, + Packages: []*spdx.Package{ + { + IsUnpackaged: true, + PackageName: "package name 1", + PackageSPDXIdentifier: "id 1", + PackageVersion: "version 1", + PackageFileName: "file 1", + PackageSupplier: nil, + PackageOriginator: nil, + PackageDownloadLocation: "", + FilesAnalyzed: true, + IsFilesAnalyzedTagPresent: true, + PackageVerificationCode: &common.PackageVerificationCode{ + Value: "value 1", + ExcludedFiles: []string{"a", "b"}, + }, + PackageChecksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + PackageHomePage: "home page 1", + PackageSourceInfo: "source info 1", + PackageLicenseConcluded: "license concluded 1", + PackageLicenseInfoFromFiles: []string{"a", "b"}, + PackageLicenseDeclared: "license declared 1", + PackageLicenseComments: "license comments 1", + PackageCopyrightText: "copyright text 1", + PackageSummary: "summary 1", + PackageDescription: "description 1", + PackageComment: "comment 1", + PackageExternalReferences: []*spdx.PackageExternalReference{ + { + Category: "cat 1", + RefType: "type 1", + Locator: "locator 1", + ExternalRefComment: "comment 1", + }, + { + Category: "cat 2", + RefType: "type 2", + Locator: "locator 2", + ExternalRefComment: "comment 2", + }, + }, + PackageAttributionTexts: []string{"a", "b", "c"}, + Files: []*spdx.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"a", "b"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "license concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy text 1", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"dep1", "dep2", "dep3"}, + Snippets: map[common.ElementID]*spdx.Snippet{ + common.ElementID("e1"): { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + common.ElementID("e2"): { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + }, + Annotations: []spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Annotations: []spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "ann 1", + AnnotatorType: "typ 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "ann 2", + AnnotatorType: "typ 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + }, + }, + Files: []*spdx.File{ + { + FileName: "file 1", + FileSPDXIdentifier: "id 1", + FileTypes: []string{"t1", "t2"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 1", + Value: "val 1", + }, + { + Algorithm: "alg 2", + Value: "val 2", + }, + }, + LicenseConcluded: "concluded 1", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 1", + FileCopyrightText: "copy 1", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 1", + HomePage: "home 1", + URI: "uri 1", + }, + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + }, + FileComment: "comment 1", + FileNotice: "notice 1", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + { + FileName: "file 2", + FileSPDXIdentifier: "id 2", + FileTypes: []string{"t3", "t4"}, + Checksums: []common.Checksum{ + { + Algorithm: "alg 2", + Value: "val 2", + }, + { + Algorithm: "alg 3", + Value: "val 3", + }, + }, + LicenseConcluded: "concluded 2", + LicenseInfoInFiles: []string{"f1", "f2", "f3"}, + LicenseComments: "comments 2", + FileCopyrightText: "copy 2", + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + { + Name: "name 2", + HomePage: "home 2", + URI: "uri 2", + }, + { + Name: "name 4", + HomePage: "home 4", + URI: "uri 4", + }, + }, + FileComment: "comment 2", + FileNotice: "notice 2", + FileContributors: []string{"c1", "c2"}, + FileAttributionTexts: []string{"att1", "att2"}, + FileDependencies: []string{"d1", "d2", "d3", "d4"}, + Snippets: nil, // already have snippets elsewhere + Annotations: nil, // already have annotations elsewhere + }, + }, + OtherLicenses: []*spdx.OtherLicense{ + { + LicenseIdentifier: "id 1", + ExtractedText: "text 1", + LicenseName: "name 1", + LicenseCrossReferences: []string{"x1", "x2", "x3"}, + LicenseComment: "comment 1", + }, + { + LicenseIdentifier: "id 2", + ExtractedText: "text 2", + LicenseName: "name 2", + LicenseCrossReferences: []string{"x4", "x5", "x6"}, + LicenseComment: "comment 2", + }, + }, + Relationships: []*spdx.Relationship{ + { + RefA: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + Relationship: "type 1", + RelationshipComment: "comment 1", + }, + { + RefA: common.DocElementID{ + DocumentRefID: "doc 3", + ElementRefID: "elem 3", + SpecialID: "spec 3", + }, + RefB: common.DocElementID{ + DocumentRefID: "doc 4", + ElementRefID: "elem 4", + SpecialID: "spec 4", + }, + Relationship: "type 2", + RelationshipComment: "comment 2", + }, + }, + Annotations: []*spdx.Annotation{ + { + Annotator: common.Annotator{ + Annotator: "annotator 1", + AnnotatorType: "annotator type 1", + }, + AnnotationDate: "date 1", + AnnotationType: "type 1", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 1", + ElementRefID: "elem 1", + SpecialID: "spec 1", + }, + AnnotationComment: "comment 1", + }, + { + Annotator: common.Annotator{ + Annotator: "annotator 2", + AnnotatorType: "annotator type 2", + }, + AnnotationDate: "date 2", + AnnotationType: "type 2", + AnnotationSPDXIdentifier: common.DocElementID{ + DocumentRefID: "doc 2", + ElementRefID: "elem 2", + SpecialID: "spec 2", + }, + AnnotationComment: "comment 2", + }, + }, + Snippets: []spdx.Snippet{ + { + SnippetSPDXIdentifier: "id1", + SnippetFromFileSPDXIdentifier: "file1", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 1, + LineNumber: 2, + FileSPDXIdentifier: "f1", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 3, + LineNumber: 4, + FileSPDXIdentifier: "f2", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + { + SnippetSPDXIdentifier: "id2", + SnippetFromFileSPDXIdentifier: "file2", + Ranges: []common.SnippetRange{ + { + StartPointer: common.SnippetRangePointer{ + Offset: 5, + LineNumber: 6, + FileSPDXIdentifier: "f3", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 7, + LineNumber: 8, + FileSPDXIdentifier: "f4", + }, + }, + { + StartPointer: common.SnippetRangePointer{ + Offset: 9, + LineNumber: 10, + FileSPDXIdentifier: "f13", + }, + EndPointer: common.SnippetRangePointer{ + Offset: 11, + LineNumber: 12, + FileSPDXIdentifier: "f14", + }, + }, + }, + SnippetLicenseConcluded: "license 1", + LicenseInfoInSnippet: []string{"a", "b"}, + SnippetLicenseComments: "license comment 1", + SnippetCopyrightText: "copy 1", + SnippetComment: "comment 1", + SnippetName: "name 1", + SnippetAttributionTexts: []string{"att1", "att2", "att3"}, + }, + }, + Reviews: []*spdx.Review{ + { + Reviewer: "reviewer 1", + ReviewerType: "type 1", + ReviewDate: "date 1", + ReviewComment: "comment 1", + }, + { + Reviewer: "reviewer 2", + ReviewerType: "type 2", + ReviewDate: "date 2", + ReviewComment: "comment 2", + }, + }, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + outType := reflect.TypeOf(test.expected) + outInstance := reflect.New(outType).Interface() + err := converter.Convert(test.source, outInstance) + if err != nil { + t.Fatalf("error converting: %v", err) + } + outInstance = reflect.ValueOf(outInstance).Elem().Interface() + + // use JSONEq here because it is much easier to see differences + require.JSONEq(t, toJSON(test.expected), toJSON(outInstance)) + }) + } +} + +func toJSON(data interface{}) string { + bytes, err := json.Marshal(data) + if err != nil { + panic(err) + } + return string(bytes) +} diff --git a/convert/struct.go b/convert/struct.go new file mode 100644 index 00000000..7223dbdb --- /dev/null +++ b/convert/struct.go @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package convert + +import ( + "fmt" + "reflect" + + "github.com/spdx/tools-golang/spdx/common" +) + +// FromPtr accepts a document or a document pointer and returns the direct struct reference +func FromPtr(doc common.AnyDocument) common.AnyDocument { + value := reflect.ValueOf(doc) + for value.Type().Kind() == reflect.Ptr { + value = value.Elem() + } + return value.Interface() +} + +func IsPtr(obj common.AnyDocument) bool { + t := reflect.TypeOf(obj) + if t.Kind() == reflect.Interface { + t = t.Elem() + } + return t.Kind() == reflect.Ptr +} + +func Describe(o interface{}) string { + value := reflect.ValueOf(o) + typ := value.Type() + prefix := "" + for typ.Kind() == reflect.Ptr { + prefix += "*" + value = value.Elem() + typ = value.Type() + } + str := limit(fmt.Sprintf("%+v", value.Interface()), 300) + name := fmt.Sprintf("%s.%s%s", typ.PkgPath(), prefix, typ.Name()) + return fmt.Sprintf("%s: %s", name, str) +} + +func limit(text string, length int) string { + if length <= 0 || len(text) <= length+3 { + return text + } + r := []rune(text) + r = r[:length] + return string(r) + "..." +} diff --git a/convert/struct_test.go b/convert/struct_test.go new file mode 100644 index 00000000..210ffc57 --- /dev/null +++ b/convert/struct_test.go @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package convert + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_FromPtr(t *testing.T) { + type t1 struct{} + tests := []struct { + name string + input interface{} + expected interface{} + }{ + { + name: "struct", + input: t1{}, + expected: t1{}, + }, + { + name: "ptr", + input: &t1{}, + expected: t1{}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + out := FromPtr(test.input) + assert.Equal(t, test.expected, out) + }) + } +} + +func Test_Describe(t *testing.T) { + type t1 struct { + text string + } + tests := []struct { + name string + input interface{} + expected string + }{ + { + name: "struct", + input: t1{ + text: "some-text", + }, + expected: "github.com/spdx/tools-golang/convert.t1: {text:some-text}", + }, + { + name: "ptr", + input: &t1{ + text: "some-text", + }, + expected: "github.com/spdx/tools-golang/convert.*t1: {text:some-text}", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + out := Describe(test.input) + assert.Equal(t, test.expected, out) + }) + } +} + +func Test_limit(t *testing.T) { + tests := []struct { + expected string + input string + length int + }{ + { + expected: "abc", + input: "abc", + length: 3, + }, + { + expected: "abc...", + input: "abcdefg", + length: 3, + }, + { + expected: "abcdef", + input: "abcdef", + length: 3, + }, + { + expected: "abcd", + input: "abcd", + length: -1, + }, + { + expected: "", + input: "", + length: 100, + }, + } + + for _, test := range tests { + t.Run(test.expected, func(t *testing.T) { + out := limit(test.input, test.length) + assert.Equal(t, test.expected, out) + }) + } +} diff --git a/examples/1-load/example_load.go b/examples/1-load/example_load.go index 328d349c..3d19a040 100644 --- a/examples/1-load/example_load.go +++ b/examples/1-load/example_load.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *tvloader*, *spdx* +// Example for: *tagvalue*, *spdx* // This example demonstrates loading an SPDX tag-value file from disk into // memory, and printing some of its contents to standard output. @@ -13,7 +13,7 @@ import ( "os" "github.com/spdx/tools-golang/spdxlib" - "github.com/spdx/tools-golang/tvloader" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -22,7 +22,7 @@ func main() { args := os.Args if len(args) != 2 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file , and\n") + fmt.Printf(" Load SPDX tag-value file , and\n") fmt.Printf(" print a portion of its contents.\n") return } @@ -36,8 +36,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a tag-value file, version 2.2 - doc, err := tvloader.Load2_2(r) + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", filename, err) return @@ -56,7 +56,7 @@ func main() { fmt.Printf("%#v\n\n", doc.CreationInfo) // check whether the SPDX file has at least one package that it describes - pkgIDs, err := spdxlib.GetDescribedPackageIDs2_2(doc) + pkgIDs, err := spdxlib.GetDescribedPackageIDs(doc) if err != nil { fmt.Printf("Unable to get describe packages from SPDX document: %v\n", err) return diff --git a/examples/10-jsonloader/example_json_loader.go b/examples/10-jsonloader/example_json_loader.go index 1ccf4d0f..8ecf173b 100644 --- a/examples/10-jsonloader/example_json_loader.go +++ b/examples/10-jsonloader/example_json_loader.go @@ -12,7 +12,7 @@ import ( "os" "strings" - spdx_json "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/json" ) func main() { @@ -21,7 +21,7 @@ func main() { args := os.Args if len(args) != 2 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 JSON file , and\n") + fmt.Printf(" Load SPDX JSON file , and\n") fmt.Printf(" print portions of its creation info data.\n") return } @@ -35,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a json file, version 2.2 - doc, err := spdx_json.Load2_2(r) + // try to load the SPDX file's contents as a json file + doc, err := json.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", args[1], err) return diff --git a/examples/11-yamltotv/exampleyamltotv.go b/examples/11-yamltotv/exampleyamltotv.go index d14201c3..bb463025 100644 --- a/examples/11-yamltotv/exampleyamltotv.go +++ b/examples/11-yamltotv/exampleyamltotv.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *yaml* *tvsaver* +// Example for: *yaml* *tagvalue* // This example demonstrates loading an SPDX tag-value file from disk into memory, // and re-saving it to a different file on disk. @@ -12,8 +12,8 @@ import ( "fmt" "os" - "github.com/spdx/tools-golang/tvsaver" - spdx_yaml "github.com/spdx/tools-golang/yaml" + "github.com/spdx/tools-golang/tagvalue" + "github.com/spdx/tools-golang/yaml" ) func main() { @@ -37,7 +37,7 @@ func main() { defer r.Close() // try to load the SPDX file's contents as a YAML file - doc, err := spdx_yaml.Load2_2(r) + doc, err := yaml.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", fileIn, err) return @@ -46,7 +46,7 @@ func main() { // if we got here, the file is now loaded into memory. fmt.Printf("Successfully loaded %s\n", fileIn) - // we can now save it back to disk, using spdx_yaml, but tvsaver work also. + // we can now save it back to disk, using yaml, but tagvalue work also. // create a new file for writing fileOut := args[2] @@ -57,8 +57,8 @@ func main() { } defer w.Close() - // try to save the document to disk as an SPDX tag-value file, version 2.2 - err = tvsaver.Save2_2(doc, w) + // try to save the document to disk as an SPDX tag-value file + err = tagvalue.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/12-tvtoyaml/exampletvtoyaml.go b/examples/12-tvtoyaml/exampletvtoyaml.go index 19abde70..baef1479 100644 --- a/examples/12-tvtoyaml/exampletvtoyaml.go +++ b/examples/12-tvtoyaml/exampletvtoyaml.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *tvloader*, *yaml* +// Example for: *tagvalue*, *yaml* // This example demonstrates loading an SPDX tag-value file from disk into memory, // and re-saving it to a different json file on disk. @@ -11,8 +11,8 @@ import ( "fmt" "os" - "github.com/spdx/tools-golang/tvloader" - spdx_yaml "github.com/spdx/tools-golang/yaml" + "github.com/spdx/tools-golang/tagvalue" + "github.com/spdx/tools-golang/yaml" ) func main() { @@ -21,7 +21,7 @@ func main() { args := os.Args if len(args) != 3 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file , and\n") + fmt.Printf(" Load SPDX tag-value file , and\n") fmt.Printf(" save it out to .\n") return } @@ -35,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a tag-value file, version 2.2 - doc, err := tvloader.Load2_2(r) + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", args[1], err) return @@ -57,7 +57,7 @@ func main() { defer w.Close() // try to save the document to disk as an YAML file - err = spdx_yaml.Save2_2(doc, w) + err = yaml.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/13-yamlloader/exampleYAMLLoader.go b/examples/13-yamlloader/exampleYAMLLoader.go index a032eceb..42552533 100644 --- a/examples/13-yamlloader/exampleYAMLLoader.go +++ b/examples/13-yamlloader/exampleYAMLLoader.go @@ -12,7 +12,7 @@ import ( "os" "strings" - spdx_yaml "github.com/spdx/tools-golang/yaml" + "github.com/spdx/tools-golang/yaml" ) func main() { @@ -21,7 +21,7 @@ func main() { args := os.Args if len(args) != 2 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 JSON file , and\n") + fmt.Printf(" Load SPDX JSON file , and\n") fmt.Printf(" print portions of its creation info data.\n") return } @@ -35,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a json file, version 2.2 - doc, err := spdx_yaml.Load2_2(r) + // try to load the SPDX file's contents as a json file + doc, err := yaml.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", args[1], err) return diff --git a/examples/2-load-save/example_load_save.go b/examples/2-load-save/example_load_save.go index 7d234031..72fefc56 100644 --- a/examples/2-load-save/example_load_save.go +++ b/examples/2-load-save/example_load_save.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *tvloader*, *tvsaver* +// Example for: *tagvalue*, *tagvalue* // This example demonstrates loading an SPDX tag-value file from disk into memory, // and re-saving it to a different file on disk. @@ -12,8 +12,7 @@ import ( "fmt" "os" - "github.com/spdx/tools-golang/tvloader" - "github.com/spdx/tools-golang/tvsaver" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -22,7 +21,7 @@ func main() { args := os.Args if len(args) != 3 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file , and\n") + fmt.Printf(" Load SPDX tag-value file , and\n") fmt.Printf(" save it out to .\n") return } @@ -36,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a tag-value file, version 2.2 - doc, err := tvloader.Load2_2(r) + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", fileIn, err) return @@ -46,7 +45,7 @@ func main() { // if we got here, the file is now loaded into memory. fmt.Printf("Successfully loaded %s\n", fileIn) - // we can now save it back to disk, using tvsaver. + // we can now save it back to disk, using tagvalue. // create a new file for writing fileOut := args[2] @@ -57,8 +56,8 @@ func main() { } defer w.Close() - // try to save the document to disk as an SPDX tag-value file, version 2.2 - err = tvsaver.Save2_2(doc, w) + // try to save the document to disk as an SPDX tag-value file + err = tagvalue.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/3-build/example_build.go b/examples/3-build/example_build.go index c6051cca..2884837e 100644 --- a/examples/3-build/example_build.go +++ b/examples/3-build/example_build.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *builder*, *tvsaver* +// Example for: *builder*, *tagvalue* // This example demonstrates building an 'empty' SPDX document in memory that // corresponds to a given directory's contents, including all files with their @@ -14,7 +14,7 @@ import ( "os" "github.com/spdx/tools-golang/builder" - "github.com/spdx/tools-golang/tvsaver" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -23,7 +23,7 @@ func main() { args := os.Args if len(args) != 4 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Build a SPDX 2.2 document with one package called ;\n") + fmt.Printf(" Build a SPDX document with one package called ;\n") fmt.Printf(" create files with hashes corresponding to the files in ;\n") fmt.Printf(" and save it out as a tag-value file to .\n") return @@ -35,9 +35,9 @@ func main() { fileOut := args[3] // to use the SPDX builder package, the first step is to define a - // builder.Config2_2 struct. this config data can be reused, in case you + // builder.Config struct. this config data can be reused, in case you // are building SPDX documents for several directories in sequence. - config := &builder.Config2_2{ + config := &builder.Config{ // NamespacePrefix is a prefix that will be used to populate the // mandatory DocumentNamespace field in the Creation Info section. @@ -87,7 +87,7 @@ func main() { // - what to name the package; and // - where the directory is located on disk; and // - the config object we just defined. - doc, err := builder.Build2_2(packageName, packageRootDir, config) + doc, err := builder.Build(packageName, packageRootDir, config) if err != nil { fmt.Printf("Error while building document: %v\n", err) return @@ -98,7 +98,7 @@ func main() { // the package verification code have been filled in appropriately. fmt.Printf("Successfully created document for package %s\n", packageName) - // we can now save it to disk, using tvsaver. + // we can now save it to disk, using tagvalue. // create a new file for writing w, err := os.Create(fileOut) @@ -108,7 +108,7 @@ func main() { } defer w.Close() - err = tvsaver.Save2_2(doc, w) + err = tagvalue.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/4-search/example_search.go b/examples/4-search/example_search.go index 52f8b075..ec8024d7 100644 --- a/examples/4-search/example_search.go +++ b/examples/4-search/example_search.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *idsearcher*, *tvsaver* +// Example for: *idsearcher*, *tagvalue* // This example demonstrates building an SPDX document for a directory's // contents (implicitly using *builder*); searching through that directory for @@ -16,8 +16,7 @@ import ( "os" "github.com/spdx/tools-golang/idsearcher" - - "github.com/spdx/tools-golang/tvsaver" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -26,7 +25,7 @@ func main() { args := os.Args if len(args) != 4 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Build a SPDX 2.2 document with one package called ;\n") + fmt.Printf(" Build a SPDX document with one package called ;\n") fmt.Printf(" create files with hashes corresponding to the files in ;\n") fmt.Printf(" search for SPDX short-form IDs, and use them to fill in license data\n") fmt.Printf(" where possible; and save it out as a tag-value file to .\n") @@ -39,9 +38,9 @@ func main() { fileOut := args[3] // to use the SPDX idsearcher package, the first step is to define a - // idsearcher.Config2_2 struct. this config data can be reused, in case you + // idsearcher.Config struct. this config data can be reused, in case you // are building SPDX documents for several directories in sequence. - config := &idsearcher.Config2_2{ + config := &idsearcher.Config{ // NamespacePrefix is a prefix that will be used to populate the // mandatory DocumentNamespace field in the Creation Info section. @@ -50,8 +49,8 @@ func main() { // appended to this prefix. NamespacePrefix: "https://example.com/whatever/testdata-", - // CreatorType and Creator, from builder.Config2_2, are not needed for - // idsearcher.Config2_2. Because it is automated and doesn't assume + // CreatorType and Creator, from builder.Config, are not needed for + // idsearcher.Config. Because it is automated and doesn't assume // further review, the following two Creator fields are filled in: // Creator: Tool: github.com/spdx/tools-golang/builder // Creator: Tool: github.com/spdx/tools-golang/idsearcher @@ -110,7 +109,7 @@ func main() { // these are the same arguments needed for builder, and in fact they get // passed through to builder (with the relevant data from the config // object extracted behind the scenes). - doc, err := idsearcher.BuildIDsDocument2_2(packageName, packageRootDir, config) + doc, err := idsearcher.BuildIDsDocument(packageName, packageRootDir, config) if err != nil { fmt.Printf("Error while building document: %v\n", err) return @@ -129,7 +128,7 @@ func main() { // identifiers, to confirm that they are e.g. on the SPDX License List // or in other appropriate format (e.g., LicenseRef-...) - // we can now save it to disk, using tvsaver. + // we can now save it to disk, using tagvalue. // create a new file for writing w, err := os.Create(fileOut) @@ -139,7 +138,7 @@ func main() { } defer w.Close() - err = tvsaver.Save2_2(doc, w) + err = tagvalue.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/5-report/example_report.go b/examples/5-report/example_report.go index 1197547a..fa5a4742 100644 --- a/examples/5-report/example_report.go +++ b/examples/5-report/example_report.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *reporter*, *tvloader* +// Example for: *reporter*, *tagvalue* // This example demonstrates loading an SPDX tag-value file from disk into memory, // generating a basic report listing counts of the concluded licenses for its @@ -14,7 +14,7 @@ import ( "github.com/spdx/tools-golang/reporter" "github.com/spdx/tools-golang/spdxlib" - "github.com/spdx/tools-golang/tvloader" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -23,7 +23,7 @@ func main() { args := os.Args if len(args) != 2 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file , and\n") + fmt.Printf(" Load SPDX tag-value file , and\n") fmt.Printf(" generate and print a report of its concluded licenses.\n") return } @@ -37,8 +37,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a tag-value file, version 2.2 - doc, err := tvloader.Load2_2(r) + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", filename, err) return @@ -48,7 +48,7 @@ func main() { fmt.Printf("Successfully loaded %s\n\n", filename) // check whether the SPDX file has at least one package that it describes - pkgIDs, err := spdxlib.GetDescribedPackageIDs2_2(doc) + pkgIDs, err := spdxlib.GetDescribedPackageIDs(doc) if err != nil { fmt.Printf("Unable to get describe packages from SPDX document: %v\n", err) return @@ -91,7 +91,7 @@ func main() { // values, sorted by # of occurrences fmt.Printf("============================\n") fmt.Printf("Package %s (%s)\n", string(pkgID), pkg.PackageName) - err = reporter.Generate2_2(pkg, os.Stdout) + err = reporter.Generate(pkg, os.Stdout) if err != nil { fmt.Printf("Error while generating report: %v\n", err) } diff --git a/examples/6-licensediff/example_licensediff.go b/examples/6-licensediff/example_licensediff.go index 39c369d2..bff1c8d5 100644 --- a/examples/6-licensediff/example_licensediff.go +++ b/examples/6-licensediff/example_licensediff.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *licensediff*, *tvloader* +// Example for: *licensediff*, *tagvalue* // This example demonstrates loading two SPDX tag-value files from disk into // memory, and generating a diff of the concluded licenses for Files in @@ -16,9 +16,9 @@ import ( "os" "github.com/spdx/tools-golang/licensediff" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx" "github.com/spdx/tools-golang/spdxlib" - "github.com/spdx/tools-golang/tvloader" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -27,7 +27,7 @@ func main() { args := os.Args if len(args) != 3 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value files and ,\n") + fmt.Printf(" Load SPDX tag-value files and ,\n") fmt.Printf(" run a diff between their concluded licenses, and print basic results.\n") return } @@ -41,14 +41,14 @@ func main() { } defer r.Close() - // try to load the first SPDX file's contents as a tag-value file, version 2.2 - docFirst, err := tvloader.Load2_2(r) + // try to load the first SPDX file's contents as a tag-value file + docFirst, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", filenameFirst, err) return } // check whether the SPDX file has at least one package that it describes - pkgIDsFirst, err := spdxlib.GetDescribedPackageIDs2_2(docFirst) + pkgIDsFirst, err := spdxlib.GetDescribedPackageIDs(docFirst) if err != nil { fmt.Printf("Unable to get describe packages from first SPDX document: %v\n", err) return @@ -66,14 +66,14 @@ func main() { } defer r.Close() - // try to load the second SPDX file's contents as a tag-value file, version 2.2 - docSecond, err := tvloader.Load2_2(r) + // try to load the second SPDX file's contents as a tag-value file + docSecond, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", filenameSecond, err) return } // check whether the SPDX file has at least one package that it describes - pkgIDsSecond, err := spdxlib.GetDescribedPackageIDs2_2(docSecond) + pkgIDsSecond, err := spdxlib.GetDescribedPackageIDs(docSecond) if err != nil { fmt.Printf("Unable to get describe packages from second SPDX document: %v\n", err) return @@ -87,7 +87,7 @@ func main() { for _, pkgID := range pkgIDsFirst { fmt.Printf("================================\n") - var p1, p2 *v2_2.Package + var p1, p2 *spdx.Package var okFirst, okSecond bool for _, pkg := range docFirst.Packages { if pkg.PackageSPDXIdentifier == pkgID { @@ -116,7 +116,7 @@ func main() { } // now, run a diff between the two - pairs, err := licensediff.MakePairs2_2(p1, p2) + pairs, err := licensediff.MakePairs(p1, p2) if err != nil { fmt.Printf(" Error generating licensediff pairs: %v\n", err) continue @@ -139,7 +139,7 @@ func main() { // now report if there are any package IDs in the second set that aren't // in the first for _, pkgID := range pkgIDsSecond { - var p2 *v2_2.Package + var p2 *spdx.Package var okFirst, okSecond bool for _, pkg := range docSecond.Packages { if pkg.PackageSPDXIdentifier == pkgID { diff --git a/examples/7-rdfloader/exampleRDFLoader.go b/examples/7-rdfloader/exampleRDFLoader.go index 81206a43..3b7ef4fa 100644 --- a/examples/7-rdfloader/exampleRDFLoader.go +++ b/examples/7-rdfloader/exampleRDFLoader.go @@ -7,7 +7,7 @@ import ( "os" "strings" - "github.com/spdx/tools-golang/rdfloader" + "github.com/spdx/tools-golang/rdf" ) func getFilePathFromUser() (string, error) { @@ -32,7 +32,7 @@ func main() { } // loading the spdx-document - doc, err := rdfloader.Load2_2(file) + doc, err := rdf.Read(file) if err != nil { fmt.Println(fmt.Errorf("error parsing given spdx document: %s", err)) os.Exit(1) diff --git a/examples/8-jsontotv/examplejsontotv.go b/examples/8-jsontotv/examplejsontotv.go index ee10bd27..c5f6f32f 100644 --- a/examples/8-jsontotv/examplejsontotv.go +++ b/examples/8-jsontotv/examplejsontotv.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *json*, *tvsaver* +// Example for: *json*, *tagvalue* // This example demonstrates loading an SPDX json from disk into memory, // and then re-saving it to a different file on disk in tag-value format . @@ -11,8 +11,8 @@ import ( "fmt" "os" - spdx_json "github.com/spdx/tools-golang/json" - "github.com/spdx/tools-golang/tvsaver" + "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -35,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a json file, version 2.2 - doc, err := spdx_json.Load2_2(r) + // try to load the SPDX file's contents as a json file + doc, err := json.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", args[1], err) return @@ -45,7 +45,7 @@ func main() { // if we got here, the file is now loaded into memory. fmt.Printf("Successfully loaded %s\n", args[1]) - // we can now save it back to disk, using tvsaver. + // we can now save it back to disk, using tagvalue. // create a new file for writing fileOut := args[2] @@ -56,8 +56,8 @@ func main() { } defer w.Close() - // try to save the document to disk as an SPDX tag-value file, version 2.2 - err = tvsaver.Save2_2(doc, w) + // try to save the document to disk as an SPDX tag-value file + err = tagvalue.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/9-tvtojson/exampletvtojson.go b/examples/9-tvtojson/exampletvtojson.go index 909677ff..5e625619 100644 --- a/examples/9-tvtojson/exampletvtojson.go +++ b/examples/9-tvtojson/exampletvtojson.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *tvloader*, *json* +// Example for: *tagvalue*, *json* // This example demonstrates loading an SPDX tag-value file from disk into memory, // and re-saving it to a different json file on disk. @@ -11,8 +11,8 @@ import ( "fmt" "os" - spdx_json "github.com/spdx/tools-golang/json" - "github.com/spdx/tools-golang/tvloader" + "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/tagvalue" ) func main() { @@ -21,7 +21,7 @@ func main() { args := os.Args if len(args) != 3 { fmt.Printf("Usage: %v \n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file , and\n") + fmt.Printf(" Load SPDX tag-value file , and\n") fmt.Printf(" save it out to JSON .\n") return } @@ -35,8 +35,8 @@ func main() { } defer r.Close() - // try to load the SPDX file's contents as a tag-value file, version 2.2 - doc, err := tvloader.Load2_2(r) + // try to load the SPDX file's contents as a tag-value file + doc, err := tagvalue.Read(r) if err != nil { fmt.Printf("Error while parsing %v: %v", args[1], err) return @@ -57,7 +57,7 @@ func main() { defer w.Close() // try to save the document to disk as JSON file - err = spdx_json.Save2_2(doc, w) + err = json.Write(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) return diff --git a/examples/README.md b/examples/README.md index 2f1ab345..c496d064 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,7 +8,7 @@ within the example's subdirectory. ## 1-load/ -*tvloader*, *spdx* +*tagvalue*, *spdx* This example demonstrates loading an SPDX tag-value file from disk into memory, and printing some of its contents to standard output. @@ -16,7 +16,7 @@ and printing some of its contents to standard output. ## 2-load-save/ -*tvloader*, *tvsaver* +*tagvalue*, *tagvalue* This example demonstrates loading an SPDX tag-value file from disk into memory, and re-saving it to a different file on disk. @@ -24,7 +24,7 @@ and re-saving it to a different file on disk. ## 3-build/ -*builder*, *tvsaver* +*builder*, *tagvalue* This example demonstrates building an 'empty' SPDX document in memory that corresponds to a given directory's contents, including all files with their @@ -33,7 +33,7 @@ hashes and the package's verification code, and saving the document to disk. ## 4-search/ -*idsearcher*, *tvsaver* +*idsearcher*, *tagvalue* This example demonstrates building an SPDX document for a directory's contents (implicitly using *builder*); searching through that directory for [SPDX @@ -43,7 +43,7 @@ Package and File license fields; and saving the resulting document to disk. ## 5-report/ -*reporter*, *tvloader* +*reporter*, *tagvalue* This example demonstrates loading an SPDX tag-value file from disk into memory, generating a basic report listing counts of the concluded licenses for its @@ -52,7 +52,7 @@ files, and printing the report to standard output. ## 6-licensediff -*licensediff*, *tvloader* +*licensediff*, *tagvalue* This example demonstrates loading two SPDX tag-value files from disk into memory, and generating a diff of the concluded licenses for Files in Packages @@ -73,7 +73,7 @@ and then printing the corresponding spdx struct for the document. ## 8-jsontotv -*json*, *tvsaver* +*json*, *tagvalue* This example demonstrates loading an SPDX json from disk into memory and then re-saving it to a different file on disk in tag-value format. @@ -81,7 +81,7 @@ and then re-saving it to a different file on disk in tag-value format. ## 9-tvtojson -*json*, *tvloader* +*json*, *tagvalue* This example demonstrates loading an SPDX tag-value from disk into memory and then re-saving it to a different file on disk in json format. @@ -97,7 +97,7 @@ and then logging some of the attributes to the console. ## 11-yamltotv -*yaml* *tvsaver* +*yaml* *tagvalue* This example demonstrates loading an SPDX yaml from disk into memory and then re-saving it to a different file on disk in tag-value format. @@ -105,7 +105,7 @@ and then re-saving it to a different file on disk in tag-value format. ## 12-tvtoyaml -*yaml* *tvloader* +*yaml* *tagvalue* This example demonstrates loading an SPDX tag-value from disk into memory and then re-saving it to a different file on disk in yaml format. diff --git a/examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json b/examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json index 7ac6549d..07d1374d 100644 --- a/examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json +++ b/examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json @@ -72,8 +72,7 @@ { "referenceCategory": "SECURITY", "referenceType": "cpe23Type", - "referenceLocator": "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", - "comment": "" + "referenceLocator": "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*" }, { "referenceCategory": "OTHER", @@ -99,6 +98,7 @@ "SPDXID": "SPDXRef-fromDoap-1", "downloadLocation": "NOASSERTION", "homepage": "http://commons.apache.org/proper/commons-lang/", + "licenseConcluded": "NOASSERTION", "licenseDeclared": "NOASSERTION", "copyrightText": "NOASSERTION" }, @@ -115,8 +115,7 @@ { "referenceCategory": "PACKAGE-MANAGER", "referenceType": "purl", - "referenceLocator": "pkg:maven/org.apache.jena/apache-jena@3.12.0", - "comment": "" + "referenceLocator": "pkg:maven/org.apache.jena/apache-jena@3.12.0" } ] }, @@ -304,7 +303,8 @@ { "spdxElementId": "SPDXRef-DOCUMENT", "relatedSpdxElement": "SPDXRef-Package", - "relationshipType": "CONTAINS" + "relationshipType": "CONTAINS", + "comment": "A relationship comment" }, { "spdxElementId": "SPDXRef-DOCUMENT", diff --git a/examples/sample-docs/tv/SPDXTagExample-v2.3.spdx b/examples/sample-docs/tv/SPDXTagExample-v2.3.spdx index 7c5ae3d8..e7483b4a 100644 --- a/examples/sample-docs/tv/SPDXTagExample-v2.3.spdx +++ b/examples/sample-docs/tv/SPDXTagExample-v2.3.spdx @@ -1,6 +1,6 @@ SPDXVersion: SPDX-2.3 DataLicense: CC0-1.0 -SPDXID: SPDXRef-SPDXRef-DOCUMENT +SPDXID: SPDXRef-DOCUMENT DocumentName: SPDX-Tools-v2.0 DocumentNamespace: http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301 ExternalDocumentRef: DocumentRef-DocumentRef-spdx-tool-1.2 http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301 SHA1:d6a770ba38583ed4bb4525bd96e50461655d2759 @@ -17,7 +17,7 @@ compatible system run time libraries. ##### Unpackaged files FileName: ./lib-source/commons-lang3-3.1-sources.jar -SPDXID: SPDXRef-SPDXRef-CommonsLangSrc +SPDXID: SPDXRef-CommonsLangSrc FileType: ARCHIVE FileChecksum: SHA1: c2b4e1c67a2d28fced849ee1bb76e7391b93f125 LicenseConcluded: Apache-2.0 @@ -35,7 +35,7 @@ under the Apache License 2.0 (see: StringUtils.containsWhitespace()) FileContributor: Apache Software Foundation FileName: ./src/org/spdx/parser/DOAPProject.java -SPDXID: SPDXRef-SPDXRef-DoapSource +SPDXID: SPDXRef-DoapSource FileType: SOURCE FileChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 LicenseConcluded: Apache-2.0 @@ -48,7 +48,7 @@ FileContributor: Source Auditor Inc. FileContributor: Black Duck Software In.c FileName: ./package/foo.c -SPDXID: SPDXRef-SPDXRef-File +SPDXID: SPDXRef-File FileType: SOURCE FileChecksum: SHA1: d6a770ba38583ed4bb4525bd96e50461655d2758 FileChecksum: MD5: 624c1abb3664f4b35547e7c73864ad24 @@ -70,7 +70,7 @@ FileContributor: Modified by Paul Mundt lethal@linux-sh.org FileContributor: IBM Corporation FileName: ./lib-source/jena-2.6.3-sources.jar -SPDXID: SPDXRef-SPDXRef-JenaLib +SPDXID: SPDXRef-JenaLib FileType: ARCHIVE FileChecksum: SHA1: 3ab4e1c67a2d28fced849ee1bb76e7391b93f125 LicenseConcluded: LicenseRef-1 @@ -84,7 +84,7 @@ FileContributor: Hewlett Packard Inc. ##### Package: centos PackageName: centos -SPDXID: SPDXRef-SPDXRef-CentOS-7 +SPDXID: SPDXRef-CentOS-7 PackageVersion: centos7.9.2009 PackageFileName: saxonB-8.8.zip PackageDownloadLocation: NOASSERTION @@ -100,7 +100,7 @@ PackageDescription: The CentOS container used to run the application. ##### Package: glibc PackageName: glibc -SPDXID: SPDXRef-SPDXRef-Package +SPDXID: SPDXRef-Package PackageVersion: 2.11.1 PackageFileName: glibc-2.11.1.tar.gz PackageSupplier: Person: Jane Doe (jane.doe@example.com) @@ -129,7 +129,7 @@ PackageAttributionText: The GNU C Library is free software. See the file COPYIN ##### Package: Saxon PackageName: Saxon -SPDXID: SPDXRef-SPDXRef-Saxon +SPDXID: SPDXRef-Saxon PackageVersion: 8.8 PackageFileName: saxonB-8.8.zip PackageDownloadLocation: https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download @@ -145,7 +145,7 @@ PackageDescription: The Saxon package is a collection of tools for processing XM ##### Package: Jena PackageName: Jena -SPDXID: SPDXRef-SPDXRef-fromDoap-0 +SPDXID: SPDXRef-fromDoap-0 PackageVersion: 3.12.0 PackageDownloadLocation: https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz FilesAnalyzed: false @@ -158,10 +158,11 @@ ExternalRef: PACKAGE-MANAGER purl pkg:maven/org.apache.jena/apache-jena@3.12.0 ##### Package: Apache Commons Lang PackageName: Apache Commons Lang -SPDXID: SPDXRef-SPDXRef-fromDoap-1 +SPDXID: SPDXRef-fromDoap-1 PackageDownloadLocation: NOASSERTION FilesAnalyzed: false PackageHomePage: http://commons.apache.org/proper/commons-lang/ +PackageLicenseConcluded: NOASSERTION PackageLicenseDeclared: NOASSERTION PackageCopyrightText: NOASSERTION @@ -294,6 +295,7 @@ LicenseComment: This is tye CyperNeko License ##### Relationships Relationship: SPDXRef-DOCUMENT CONTAINS SPDXRef-Package +RelationshipComment: A relationship comment Relationship: SPDXRef-DOCUMENT COPY_OF DocumentRef-spdx-tool-1.2:SPDXRef-ToolsElement Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-File Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package @@ -320,9 +322,3 @@ AnnotationDate: 2011-03-13T00:00:00Z AnnotationType: REVIEW AnnotationComment: Another example reviewer. -##### Reviews - -Reviewer: Person: joe@example.com -ReviewDate: 2021-11-03T05:43:21Z -ReviewComment: This is a review comment - diff --git a/examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml b/examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml index 611b74cc..52d333f9 100644 --- a/examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml +++ b/examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml @@ -278,8 +278,7 @@ packages: of the Unix operating system, and extensions specific to GNU systems. downloadLocation: http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz externalRefs: - - comment: "" - referenceCategory: SECURITY + - referenceCategory: SECURITY referenceLocator: cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:* referenceType: cpe23Type - comment: This is the external ref for Acme @@ -318,8 +317,7 @@ packages: copyrightText: NOASSERTION downloadLocation: https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz externalRefs: - - comment: "" - referenceCategory: PACKAGE_MANAGER + - referenceCategory: PACKAGE-MANAGER referenceLocator: pkg:maven/org.apache.jena/apache-jena@3.12.0 referenceType: purl homepage: http://www.openjena.org/ @@ -409,4 +407,4 @@ snippets: lineNumber: 5 reference: SPDXRef-DoapSource snippetFromFile: SPDXRef-DoapSource -spdxVersion: SPDX-2.2 +spdxVersion: SPDX-2.3 diff --git a/go.mod b/go.mod index 1017b95c..28faa444 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module github.com/spdx/tools-golang go 1.13 require ( + github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 github.com/google/go-cmp v0.5.7 github.com/spdx/gordf v0.0.0-20201111095634-7098f93598fb + github.com/stretchr/testify v1.8.1 sigs.k8s.io/yaml v1.3.0 ) diff --git a/go.sum b/go.sum index 85ffe1cd..9014b4f3 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,29 @@ +github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 h1:aM1rlcoLz8y5B2r4tTLMiVTrMtpfY0O8EScKJxaSaEc= +github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092/go.mod h1:rYqSE9HbjzpHTI74vwPvae4ZVYZd1lue2ta6xHPdblA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spdx/gordf v0.0.0-20201111095634-7098f93598fb h1:bLo8hvc8XFm9J47r690TUKBzcjSWdJDxmjXJZ+/f92U= github.com/spdx/gordf v0.0.0-20201111095634-7098f93598fb/go.mod h1:uKWaldnbMnjsSAXRurWqqrdyZen1R7kxl8TkmWk2OyM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/idsearcher/idsearcher.go b/idsearcher/idsearcher.go index a5176ca7..7413bb46 100644 --- a/idsearcher/idsearcher.go +++ b/idsearcher/idsearcher.go @@ -7,7 +7,6 @@ package idsearcher import ( "bufio" "fmt" - "github.com/spdx/tools-golang/spdx/v2_3" "os" "path/filepath" "regexp" @@ -15,265 +14,14 @@ import ( "strings" "github.com/spdx/tools-golang/builder" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx" "github.com/spdx/tools-golang/utils" ) -// ===== 2.1 Searcher functions ===== - -// Config2_1 is a collection of configuration settings for docbuilder -// (for version 2.1 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_1. -type Config2_1 struct { - // NamespacePrefix should be a URI representing a prefix for the - // namespace with which the SPDX Document will be associated. - // It will be used in the DocumentNamespace field in the CreationInfo - // section, followed by the per-Document package name and a random UUID. - NamespacePrefix string - - // BuilderPathsIgnored lists certain paths to be omitted from the built - // document. Each string should be a path, relative to the package's - // dirRoot, to a specific file or (for all files in a directory) ending - // in a slash. Prefix the string with "**" to omit all instances of that - // file / directory, regardless of where it is in the file tree. - BuilderPathsIgnored []string - - // SearcherPathsIgnored lists certain paths that should not be searched - // by idsearcher, even if those paths have Files present. It uses the - // same format as BuilderPathsIgnored. - SearcherPathsIgnored []string -} - -// BuildIDsDocument2_1 creates an SPDX Document (version 2.1) and searches for -// short-form IDs in each file, filling in license fields as appropriate. It -// returns that document or error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - namespacePrefix: URI representing a prefix for the -// namespace with which the SPDX Document will be associated -func BuildIDsDocument2_1(packageName string, dirRoot string, idconfig *Config2_1) (*v2_1.Document, error) { - // first, build the Document using builder - bconfig := &builder.Config2_1{ - NamespacePrefix: idconfig.NamespacePrefix, - CreatorType: "Tool", - Creator: "github.com/spdx/tools-golang/idsearcher", - PathsIgnored: idconfig.BuilderPathsIgnored, - } - doc, err := builder.Build2_1(packageName, dirRoot, bconfig) - if err != nil { - return nil, err - } - if doc == nil { - return nil, fmt.Errorf("builder returned nil Document") - } - if doc.Packages == nil { - return nil, fmt.Errorf("builder returned nil Packages map") - } - if len(doc.Packages) != 1 { - return nil, fmt.Errorf("builder returned %d Packages", len(doc.Packages)) - } - - // now, walk through each file and find its licenses (if any) - pkg := doc.Packages[0] - if pkg == nil { - return nil, fmt.Errorf("builder returned nil Package") - } - if pkg.Files == nil { - return nil, fmt.Errorf("builder returned nil Files in Package") - } - licsForPackage := map[string]int{} - for _, f := range pkg.Files { - // start by initializing / clearing values - f.LicenseInfoInFiles = []string{"NOASSERTION"} - f.LicenseConcluded = "NOASSERTION" - - // check whether the searcher should ignore this file - if utils.ShouldIgnore(f.FileName, idconfig.SearcherPathsIgnored) { - continue - } - - fPath := filepath.Join(dirRoot, f.FileName) - // FIXME this is not preferable -- ignoring error - ids, _ := searchFileIDs(fPath) - // FIXME for now, proceed onwards with whatever IDs we obtained. - // FIXME instead of ignoring the error, should probably either log it, - // FIXME and/or enable the caller to configure what should happen. - - // separate out for this file's licenses - licsForFile := map[string]int{} - licsParens := []string{} - for _, lid := range ids { - // get individual elements and add for file and package - licElements := getIndividualLicenses(lid) - for _, elt := range licElements { - licsForFile[elt] = 1 - licsForPackage[elt] = 1 - } - // parenthesize if needed and add to slice for joining - licsParens = append(licsParens, makeElement(lid)) - } - - // OK -- now we can fill in the file's details, or NOASSERTION if none - if len(licsForFile) > 0 { - f.LicenseInfoInFiles = []string{} - for lic := range licsForFile { - f.LicenseInfoInFiles = append(f.LicenseInfoInFiles, lic) - } - sort.Strings(f.LicenseInfoInFiles) - // avoid adding parens and joining for single-ID items - if len(licsParens) == 1 { - f.LicenseConcluded = ids[0] - } else { - f.LicenseConcluded = strings.Join(licsParens, " AND ") - } - } - } - - // and finally, we can fill in the package's details - if len(licsForPackage) == 0 { - pkg.PackageLicenseInfoFromFiles = []string{"NOASSERTION"} - } else { - pkg.PackageLicenseInfoFromFiles = []string{} - for lic := range licsForPackage { - pkg.PackageLicenseInfoFromFiles = append(pkg.PackageLicenseInfoFromFiles, lic) - } - sort.Strings(pkg.PackageLicenseInfoFromFiles) - } - - return doc, nil -} - -// ===== 2.2 Searcher functions ===== - -// Config2_2 is a collection of configuration settings for docbuilder -// (for version 2.2 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_2. -type Config2_2 struct { - // NamespacePrefix should be a URI representing a prefix for the - // namespace with which the SPDX Document will be associated. - // It will be used in the DocumentNamespace field in the CreationInfo - // section, followed by the per-Document package name and a random UUID. - NamespacePrefix string - - // BuilderPathsIgnored lists certain paths to be omitted from the built - // document. Each string should be a path, relative to the package's - // dirRoot, to a specific file or (for all files in a directory) ending - // in a slash. Prefix the string with "**" to omit all instances of that - // file / directory, regardless of where it is in the file tree. - BuilderPathsIgnored []string - - // SearcherPathsIgnored lists certain paths that should not be searched - // by idsearcher, even if those paths have Files present. It uses the - // same format as BuilderPathsIgnored. - SearcherPathsIgnored []string -} - -// BuildIDsDocument2_2 creates an SPDX Document (version 2.2) and searches for -// short-form IDs in each file, filling in license fields as appropriate. It -// returns that document or error if any is encountered. Arguments: -// - packageName: name of package / directory -// - dirRoot: path to directory to be analyzed -// - namespacePrefix: URI representing a prefix for the -// namespace with which the SPDX Document will be associated -func BuildIDsDocument2_2(packageName string, dirRoot string, idconfig *Config2_2) (*v2_2.Document, error) { - // first, build the Document using builder - bconfig := &builder.Config2_2{ - NamespacePrefix: idconfig.NamespacePrefix, - CreatorType: "Tool", - Creator: "github.com/spdx/tools-golang/idsearcher", - PathsIgnored: idconfig.BuilderPathsIgnored, - } - doc, err := builder.Build2_2(packageName, dirRoot, bconfig) - if err != nil { - return nil, err - } - if doc == nil { - return nil, fmt.Errorf("builder returned nil Document") - } - if doc.Packages == nil { - return nil, fmt.Errorf("builder returned nil Packages map") - } - if len(doc.Packages) != 1 { - return nil, fmt.Errorf("builder returned %d Packages", len(doc.Packages)) - } - - // now, walk through each file and find its licenses (if any) - pkg := doc.Packages[0] - if pkg == nil { - return nil, fmt.Errorf("builder returned nil Package") - } - if pkg.Files == nil { - return nil, fmt.Errorf("builder returned nil Files in Package") - } - licsForPackage := map[string]int{} - for _, f := range pkg.Files { - // start by initializing / clearing values - f.LicenseInfoInFiles = []string{"NOASSERTION"} - f.LicenseConcluded = "NOASSERTION" - - // check whether the searcher should ignore this file - if utils.ShouldIgnore(f.FileName, idconfig.SearcherPathsIgnored) { - continue - } - - fPath := filepath.Join(dirRoot, f.FileName) - // FIXME this is not preferable -- ignoring error - ids, _ := searchFileIDs(fPath) - // FIXME for now, proceed onwards with whatever IDs we obtained. - // FIXME instead of ignoring the error, should probably either log it, - // FIXME and/or enable the caller to configure what should happen. - - // separate out for this file's licenses - licsForFile := map[string]int{} - licsParens := []string{} - for _, lid := range ids { - // get individual elements and add for file and package - licElements := getIndividualLicenses(lid) - for _, elt := range licElements { - licsForFile[elt] = 1 - licsForPackage[elt] = 1 - } - // parenthesize if needed and add to slice for joining - licsParens = append(licsParens, makeElement(lid)) - } - - // OK -- now we can fill in the file's details, or NOASSERTION if none - if len(licsForFile) > 0 { - f.LicenseInfoInFiles = []string{} - for lic := range licsForFile { - f.LicenseInfoInFiles = append(f.LicenseInfoInFiles, lic) - } - sort.Strings(f.LicenseInfoInFiles) - // avoid adding parens and joining for single-ID items - if len(licsParens) == 1 { - f.LicenseConcluded = ids[0] - } else { - f.LicenseConcluded = strings.Join(licsParens, " AND ") - } - } - } - - // and finally, we can fill in the package's details - if len(licsForPackage) == 0 { - pkg.PackageLicenseInfoFromFiles = []string{"NOASSERTION"} - } else { - pkg.PackageLicenseInfoFromFiles = []string{} - for lic := range licsForPackage { - pkg.PackageLicenseInfoFromFiles = append(pkg.PackageLicenseInfoFromFiles, lic) - } - sort.Strings(pkg.PackageLicenseInfoFromFiles) - } - - return doc, nil -} - -// ===== 2.3 Searcher functions ===== - -// Config2_3 is a collection of configuration settings for docbuilder -// (for version 2.3 SPDX Documents). A few mandatory fields are set here -// so that they can be repeatedly reused in multiple calls to Build2_3. -type Config2_3 struct { +// Config is a collection of configuration settings for docbuilder. +// A few mandatory fields are set here +// so that they can be repeatedly reused in multiple calls to Build. +type Config struct { // NamespacePrefix should be a URI representing a prefix for the // namespace with which the SPDX Document will be associated. // It will be used in the DocumentNamespace field in the CreationInfo @@ -293,22 +41,22 @@ type Config2_3 struct { SearcherPathsIgnored []string } -// BuildIDsDocument2_3 creates an SPDX Document (version 2.3) and searches for +// BuildIDsDocument creates an SPDX Document and searches for // short-form IDs in each file, filling in license fields as appropriate. It // returns that document or error if any is encountered. Arguments: // - packageName: name of package / directory // - dirRoot: path to directory to be analyzed // - namespacePrefix: URI representing a prefix for the // namespace with which the SPDX Document will be associated -func BuildIDsDocument2_3(packageName string, dirRoot string, idconfig *Config2_3) (*v2_3.Document, error) { +func BuildIDsDocument(packageName string, dirRoot string, idconfig *Config) (*spdx.Document, error) { // first, build the Document using builder - bconfig := &builder.Config2_3{ + bconfig := &builder.Config{ NamespacePrefix: idconfig.NamespacePrefix, CreatorType: "Tool", Creator: "github.com/spdx/tools-golang/idsearcher", PathsIgnored: idconfig.BuilderPathsIgnored, } - doc, err := builder.Build2_3(packageName, dirRoot, bconfig) + doc, err := builder.Build(packageName, dirRoot, bconfig) if err != nil { return nil, err } diff --git a/idsearcher/idsearcher_test.go b/idsearcher/idsearcher_test.go index 7d7a5bbf..119cf17d 100644 --- a/idsearcher/idsearcher_test.go +++ b/idsearcher/idsearcher_test.go @@ -6,15 +6,14 @@ import ( "testing" ) -// ===== 2.1 Searcher top-level function tests ===== -func Test2_1SearcherCanFillInIDs(t *testing.T) { +func TestSearcherCanFillInIDs(t *testing.T) { packageName := "project2" dirRoot := "../testdata/project2/" - config := &Config2_1{ + config := &Config{ NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", } - doc, err := BuildIDsDocument2_1(packageName, dirRoot, config) + doc, err := BuildIDsDocument(packageName, dirRoot, config) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -175,10 +174,10 @@ func Test2_1SearcherCanFillInIDs(t *testing.T) { } -func Test2_1SearcherCanFillInIDsAndIgnorePaths(t *testing.T) { +func TestSearcherCanFillInIDsAndIgnorePaths(t *testing.T) { packageName := "project3" dirRoot := "../testdata/project3/" - config := &Config2_1{ + config := &Config{ NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", BuilderPathsIgnored: []string{ "**/ignoredir/", @@ -191,7 +190,7 @@ func Test2_1SearcherCanFillInIDsAndIgnorePaths(t *testing.T) { }, } - doc, err := BuildIDsDocument2_1(packageName, dirRoot, config) + doc, err := BuildIDsDocument(packageName, dirRoot, config) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -282,592 +281,14 @@ func Test2_1SearcherCanFillInIDsAndIgnorePaths(t *testing.T) { } } -func Test2_1SearcherFailsWithInvalidPath(t *testing.T) { +func TestSearcherFailsWithInvalidPath(t *testing.T) { packageName := "project2" dirRoot := "./oops/invalid" - config := &Config2_1{ + config := &Config{ NamespacePrefix: "whatever", } - _, err := BuildIDsDocument2_1(packageName, dirRoot, config) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.2 Searcher top-level function tests ===== -func Test2_2SearcherCanFillInIDs(t *testing.T) { - packageName := "project2" - dirRoot := "../testdata/project2/" - config := &Config2_2{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - } - - doc, err := BuildIDsDocument2_2(packageName, dirRoot, config) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if doc == nil { - t.Fatalf("expected non-nil Document, got nil") - } - - // not checking all contents of doc, see builder tests for those - - // get the package and its files, checking size of each - if doc.Packages == nil { - t.Fatalf("expected non-nil Packages, got nil") - } - if len(doc.Packages) != 1 { - t.Fatalf("expected Packages len to be 1, got %d", len(doc.Packages)) - } - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - - if pkg.Files == nil { - t.Fatalf("expected non-nil Files, got nil") - } - if len(pkg.Files) != 6 { - t.Fatalf("expected Files len to be 6, got %d", len(pkg.Files)) - } - - fileInFolder := pkg.Files[0] - if fileInFolder.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileInFolder.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileInFolder.LicenseInfoInFiles)) - } - if fileInFolder.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileInFolder.LicenseInfoInFiles[0]) - } - if fileInFolder.LicenseConcluded != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileInFolder.LicenseConcluded) - } - - fileTrailingComment := pkg.Files[1] - if fileTrailingComment.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileTrailingComment.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileTrailingComment.LicenseInfoInFiles)) - } - if fileTrailingComment.LicenseInfoInFiles[0] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileTrailingComment.LicenseInfoInFiles[0]) - } - if fileTrailingComment.LicenseConcluded != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileTrailingComment.LicenseConcluded) - } - - fileHasDuplicateID := pkg.Files[2] - if fileHasDuplicateID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileHasDuplicateID.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileHasDuplicateID.LicenseInfoInFiles)) - } - if fileHasDuplicateID.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileHasDuplicateID.LicenseInfoInFiles[0]) - } - if fileHasDuplicateID.LicenseConcluded != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileHasDuplicateID.LicenseConcluded) - } - - fileHasID := pkg.Files[3] - if fileHasID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileHasID.LicenseInfoInFiles) != 2 { - t.Fatalf("expected LicenseInfoInFiles len to be 2, got %d", len(fileHasID.LicenseInfoInFiles)) - } - if fileHasID.LicenseInfoInFiles[0] != "Apache-2.0" { - t.Errorf("expected %v, got %v", "Apache-2.0", fileHasID.LicenseInfoInFiles[0]) - } - if fileHasID.LicenseInfoInFiles[1] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileHasID.LicenseInfoInFiles[1]) - } - if fileHasID.LicenseConcluded != "Apache-2.0 OR GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "Apache-2.0 OR GPL-2.0-or-later", fileHasID.LicenseConcluded) - } - - fileMultipleIDs := pkg.Files[4] - if fileMultipleIDs.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileMultipleIDs.LicenseInfoInFiles) != 5 { - t.Fatalf("expected LicenseInfoInFiles len to be 5, got %d", len(fileMultipleIDs.LicenseInfoInFiles)) - } - if fileMultipleIDs.LicenseInfoInFiles[0] != "BSD-2-Clause" { - t.Errorf("expected %v, got %v", "BSD-2-Clause", fileMultipleIDs.LicenseInfoInFiles[0]) - } - if fileMultipleIDs.LicenseInfoInFiles[1] != "BSD-3-Clause" { - t.Errorf("expected %v, got %v", "BSD-3-Clause", fileMultipleIDs.LicenseInfoInFiles[1]) - } - // here, DO NOT keep the + - if fileMultipleIDs.LicenseInfoInFiles[2] != "EPL-1.0" { - t.Errorf("expected %v, got %v", "EPL-1.0", fileMultipleIDs.LicenseInfoInFiles[2]) - } - if fileMultipleIDs.LicenseInfoInFiles[3] != "ISC" { - t.Errorf("expected %v, got %v", "ISC", fileMultipleIDs.LicenseInfoInFiles[3]) - } - if fileMultipleIDs.LicenseInfoInFiles[4] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileMultipleIDs.LicenseInfoInFiles[4]) - } - if fileMultipleIDs.LicenseConcluded != "((MIT AND BSD-3-Clause) OR ISC) AND BSD-2-Clause AND EPL-1.0+" { - t.Errorf("expected %v, got %v", "((MIT AND BSD-3-Clause) OR ISC) AND BSD-2-Clause AND EPL-1.0+", fileMultipleIDs.LicenseConcluded) - } - - fileNoID := pkg.Files[5] - if fileNoID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileNoID.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileNoID.LicenseInfoInFiles)) - } - if fileNoID.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileNoID.LicenseInfoInFiles[0]) - } - if fileNoID.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileNoID.LicenseConcluded) - } - - // and finally, the package should have all of these licenses - if pkg.PackageLicenseInfoFromFiles == nil { - t.Fatalf("expected non-nil PackageLicenseInfoFromFiles, got nil") - } - if len(pkg.PackageLicenseInfoFromFiles) != 7 { - t.Fatalf("expected PackageLicenseInfoFromFiles len to be 7, got %d", len(pkg.PackageLicenseInfoFromFiles)) - } - if pkg.PackageLicenseInfoFromFiles[0] != "Apache-2.0" { - t.Errorf("expected %v, got %v", "Apache-2.0", pkg.PackageLicenseInfoFromFiles[0]) - } - if pkg.PackageLicenseInfoFromFiles[1] != "BSD-2-Clause" { - t.Errorf("expected %v, got %v", "BSD-2-Clause", pkg.PackageLicenseInfoFromFiles[1]) - } - if pkg.PackageLicenseInfoFromFiles[2] != "BSD-3-Clause" { - t.Errorf("expected %v, got %v", "BSD-3-Clause", pkg.PackageLicenseInfoFromFiles[2]) - } - // here, DO NOT keep the + - if pkg.PackageLicenseInfoFromFiles[3] != "EPL-1.0" { - t.Errorf("expected %v, got %v", "EPL-1.0", pkg.PackageLicenseInfoFromFiles[3]) - } - if pkg.PackageLicenseInfoFromFiles[4] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", pkg.PackageLicenseInfoFromFiles[4]) - } - if pkg.PackageLicenseInfoFromFiles[5] != "ISC" { - t.Errorf("expected %v, got %v", "ISC", pkg.PackageLicenseInfoFromFiles[5]) - } - if pkg.PackageLicenseInfoFromFiles[6] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", pkg.PackageLicenseInfoFromFiles[6]) - } - -} - -func Test2_2SearcherCanFillInIDsAndIgnorePaths(t *testing.T) { - packageName := "project3" - dirRoot := "../testdata/project3/" - config := &Config2_2{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - BuilderPathsIgnored: []string{ - "**/ignoredir/", - "/excludedir/", - "**/ignorefile.txt", - "/alsoEXCLUDEthis.txt", - }, - SearcherPathsIgnored: []string{ - "**/dontscan.txt", - }, - } - - doc, err := BuildIDsDocument2_2(packageName, dirRoot, config) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if doc == nil { - t.Fatalf("expected non-nil Document, got nil") - } - - // not checking all contents of doc, see builder tests for those - - // get the package and its files, checking licenses for each, and - // confirming NOASSERTION for those that are skipped - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - if len(pkg.Files) != 5 { - t.Fatalf("expected len %d, got %d", 5, len(pkg.Files)) - } - - f := pkg.Files[0] - if f.FileName != "./dontscan.txt" { - t.Errorf("expected %v, got %v", "./dontscan.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[1] - if f.FileName != "./keep/keep.txt" { - t.Errorf("expected %v, got %v", "./keep/keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseConcluded) - } - - f = pkg.Files[2] - if f.FileName != "./keep.txt" { - t.Errorf("expected %v, got %v", "./keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[3] - if f.FileName != "./subdir/keep/dontscan.txt" { - t.Errorf("expected %v, got %v", "./subdir/keep/dontscan.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[4] - if f.FileName != "./subdir/keep/keep.txt" { - t.Errorf("expected %v, got %v", "./subdir/keep/keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseConcluded) - } -} - -func Test2_2SearcherFailsWithInvalidPath(t *testing.T) { - packageName := "project2" - dirRoot := "./oops/invalid" - config := &Config2_2{ - NamespacePrefix: "whatever", - } - - _, err := BuildIDsDocument2_2(packageName, dirRoot, config) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.3 Searcher top-level function tests ===== -func Test2_3SearcherCanFillInIDs(t *testing.T) { - packageName := "project2" - dirRoot := "../testdata/project2/" - config := &Config2_3{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - } - - doc, err := BuildIDsDocument2_3(packageName, dirRoot, config) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if doc == nil { - t.Fatalf("expected non-nil Document, got nil") - } - - // not checking all contents of doc, see builder tests for those - - // get the package and its files, checking size of each - if doc.Packages == nil { - t.Fatalf("expected non-nil Packages, got nil") - } - if len(doc.Packages) != 1 { - t.Fatalf("expected Packages len to be 1, got %d", len(doc.Packages)) - } - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - - if pkg.Files == nil { - t.Fatalf("expected non-nil Files, got nil") - } - if len(pkg.Files) != 6 { - t.Fatalf("expected Files len to be 6, got %d", len(pkg.Files)) - } - - fileInFolder := pkg.Files[0] - if fileInFolder.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileInFolder.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileInFolder.LicenseInfoInFiles)) - } - if fileInFolder.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileInFolder.LicenseInfoInFiles[0]) - } - if fileInFolder.LicenseConcluded != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileInFolder.LicenseConcluded) - } - - fileTrailingComment := pkg.Files[1] - if fileTrailingComment.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileTrailingComment.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileTrailingComment.LicenseInfoInFiles)) - } - if fileTrailingComment.LicenseInfoInFiles[0] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileTrailingComment.LicenseInfoInFiles[0]) - } - if fileTrailingComment.LicenseConcluded != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileTrailingComment.LicenseConcluded) - } - - fileHasDuplicateID := pkg.Files[2] - if fileHasDuplicateID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileHasDuplicateID.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileHasDuplicateID.LicenseInfoInFiles)) - } - if fileHasDuplicateID.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileHasDuplicateID.LicenseInfoInFiles[0]) - } - if fileHasDuplicateID.LicenseConcluded != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileHasDuplicateID.LicenseConcluded) - } - - fileHasID := pkg.Files[3] - if fileHasID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileHasID.LicenseInfoInFiles) != 2 { - t.Fatalf("expected LicenseInfoInFiles len to be 2, got %d", len(fileHasID.LicenseInfoInFiles)) - } - if fileHasID.LicenseInfoInFiles[0] != "Apache-2.0" { - t.Errorf("expected %v, got %v", "Apache-2.0", fileHasID.LicenseInfoInFiles[0]) - } - if fileHasID.LicenseInfoInFiles[1] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", fileHasID.LicenseInfoInFiles[1]) - } - if fileHasID.LicenseConcluded != "Apache-2.0 OR GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "Apache-2.0 OR GPL-2.0-or-later", fileHasID.LicenseConcluded) - } - - fileMultipleIDs := pkg.Files[4] - if fileMultipleIDs.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileMultipleIDs.LicenseInfoInFiles) != 5 { - t.Fatalf("expected LicenseInfoInFiles len to be 5, got %d", len(fileMultipleIDs.LicenseInfoInFiles)) - } - if fileMultipleIDs.LicenseInfoInFiles[0] != "BSD-2-Clause" { - t.Errorf("expected %v, got %v", "BSD-2-Clause", fileMultipleIDs.LicenseInfoInFiles[0]) - } - if fileMultipleIDs.LicenseInfoInFiles[1] != "BSD-3-Clause" { - t.Errorf("expected %v, got %v", "BSD-3-Clause", fileMultipleIDs.LicenseInfoInFiles[1]) - } - // here, DO NOT keep the + - if fileMultipleIDs.LicenseInfoInFiles[2] != "EPL-1.0" { - t.Errorf("expected %v, got %v", "EPL-1.0", fileMultipleIDs.LicenseInfoInFiles[2]) - } - if fileMultipleIDs.LicenseInfoInFiles[3] != "ISC" { - t.Errorf("expected %v, got %v", "ISC", fileMultipleIDs.LicenseInfoInFiles[3]) - } - if fileMultipleIDs.LicenseInfoInFiles[4] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", fileMultipleIDs.LicenseInfoInFiles[4]) - } - if fileMultipleIDs.LicenseConcluded != "((MIT AND BSD-3-Clause) OR ISC) AND BSD-2-Clause AND EPL-1.0+" { - t.Errorf("expected %v, got %v", "((MIT AND BSD-3-Clause) OR ISC) AND BSD-2-Clause AND EPL-1.0+", fileMultipleIDs.LicenseConcluded) - } - - fileNoID := pkg.Files[5] - if fileNoID.LicenseInfoInFiles == nil { - t.Fatalf("expected non-nil LicenseInfoInFiles, got nil") - } - if len(fileNoID.LicenseInfoInFiles) != 1 { - t.Fatalf("expected LicenseInfoInFiles len to be 1, got %d", len(fileNoID.LicenseInfoInFiles)) - } - if fileNoID.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileNoID.LicenseInfoInFiles[0]) - } - if fileNoID.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %v, got %v", "NOASSERTION", fileNoID.LicenseConcluded) - } - - // and finally, the package should have all of these licenses - if pkg.PackageLicenseInfoFromFiles == nil { - t.Fatalf("expected non-nil PackageLicenseInfoFromFiles, got nil") - } - if len(pkg.PackageLicenseInfoFromFiles) != 7 { - t.Fatalf("expected PackageLicenseInfoFromFiles len to be 7, got %d", len(pkg.PackageLicenseInfoFromFiles)) - } - if pkg.PackageLicenseInfoFromFiles[0] != "Apache-2.0" { - t.Errorf("expected %v, got %v", "Apache-2.0", pkg.PackageLicenseInfoFromFiles[0]) - } - if pkg.PackageLicenseInfoFromFiles[1] != "BSD-2-Clause" { - t.Errorf("expected %v, got %v", "BSD-2-Clause", pkg.PackageLicenseInfoFromFiles[1]) - } - if pkg.PackageLicenseInfoFromFiles[2] != "BSD-3-Clause" { - t.Errorf("expected %v, got %v", "BSD-3-Clause", pkg.PackageLicenseInfoFromFiles[2]) - } - // here, DO NOT keep the + - if pkg.PackageLicenseInfoFromFiles[3] != "EPL-1.0" { - t.Errorf("expected %v, got %v", "EPL-1.0", pkg.PackageLicenseInfoFromFiles[3]) - } - if pkg.PackageLicenseInfoFromFiles[4] != "GPL-2.0-or-later" { - t.Errorf("expected %v, got %v", "GPL-2.0-or-later", pkg.PackageLicenseInfoFromFiles[4]) - } - if pkg.PackageLicenseInfoFromFiles[5] != "ISC" { - t.Errorf("expected %v, got %v", "ISC", pkg.PackageLicenseInfoFromFiles[5]) - } - if pkg.PackageLicenseInfoFromFiles[6] != "MIT" { - t.Errorf("expected %v, got %v", "MIT", pkg.PackageLicenseInfoFromFiles[6]) - } - -} - -func Test2_3SearcherCanFillInIDsAndIgnorePaths(t *testing.T) { - packageName := "project3" - dirRoot := "../testdata/project3/" - config := &Config2_3{ - NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-", - BuilderPathsIgnored: []string{ - "**/ignoredir/", - "/excludedir/", - "**/ignorefile.txt", - "/alsoEXCLUDEthis.txt", - }, - SearcherPathsIgnored: []string{ - "**/dontscan.txt", - }, - } - - doc, err := BuildIDsDocument2_3(packageName, dirRoot, config) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if doc == nil { - t.Fatalf("expected non-nil Document, got nil") - } - - // not checking all contents of doc, see builder tests for those - - // get the package and its files, checking licenses for each, and - // confirming NOASSERTION for those that are skipped - pkg := doc.Packages[0] - if pkg == nil { - t.Fatalf("expected non-nil pkg, got nil") - } - if len(pkg.Files) != 5 { - t.Fatalf("expected len %d, got %d", 5, len(pkg.Files)) - } - - f := pkg.Files[0] - if f.FileName != "./dontscan.txt" { - t.Errorf("expected %v, got %v", "./dontscan.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[1] - if f.FileName != "./keep/keep.txt" { - t.Errorf("expected %v, got %v", "./keep/keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseConcluded) - } - - f = pkg.Files[2] - if f.FileName != "./keep.txt" { - t.Errorf("expected %v, got %v", "./keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[3] - if f.FileName != "./subdir/keep/dontscan.txt" { - t.Errorf("expected %v, got %v", "./subdir/keep/dontscan.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "NOASSERTION" { - t.Errorf("expected %s, got %s", "NOASSERTION", f.LicenseConcluded) - } - - f = pkg.Files[4] - if f.FileName != "./subdir/keep/keep.txt" { - t.Errorf("expected %v, got %v", "./subdir/keep/keep.txt", f.FileName) - } - if len(f.LicenseInfoInFiles) != 1 { - t.Errorf("expected len to be %d, got %d", 1, len(f.LicenseInfoInFiles)) - } - if f.LicenseInfoInFiles[0] != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseInfoInFiles[0]) - } - if f.LicenseConcluded != "MIT" { - t.Errorf("expected %s, got %s", "MIT", f.LicenseConcluded) - } -} - -func Test2_3SearcherFailsWithInvalidPath(t *testing.T) { - packageName := "project2" - dirRoot := "./oops/invalid" - config := &Config2_3{ - NamespacePrefix: "whatever", - } - - _, err := BuildIDsDocument2_3(packageName, dirRoot, config) + _, err := BuildIDsDocument(packageName, dirRoot, config) if err == nil { t.Fatalf("expected non-nil error, got nil") } diff --git a/json/json_v2_3_test.go b/json/json_v2_3_test.go deleted file mode 100644 index 2cefafcd..00000000 --- a/json/json_v2_3_test.go +++ /dev/null @@ -1,490 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_json - -import ( - "bytes" - "encoding/json" - "flag" - "fmt" - "os" - "testing" - - "github.com/google/go-cmp/cmp" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -var update = *flag.Bool("update-snapshots", false, "update the example snapshot") - -func TestLoad2_3(t *testing.T) { - fileName := "../examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json" - - if update { - data, err := json.MarshalIndent(want2_3, "", " ") - if err != nil { - t.Errorf("unable to serialize SPDX 2.3 example to JSON: %v", err) - } - err = os.WriteFile(fileName, data, 0644) - if err != nil { - t.Errorf("unable to write SPDX 2.3 example to JSON: %v", err) - } - } - - file, err := os.Open(fileName) - if err != nil { - panic(fmt.Errorf("error opening File: %s", err)) - } - - got, err := Load2_3(file) - if err != nil { - t.Errorf("json.parser.Load2_3() error = %v", err) - return - } - - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_3 - - if cmp.Equal(handwrittenExample, got) { - t.Errorf("Got incorrect struct after parsing JSON example") - return - } -} - -func TestWrite2_3(t *testing.T) { - w := &bytes.Buffer{} - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_3 - if err := Save2_3(&handwrittenExample, w); err != nil { - t.Errorf("Save2_3() error = %v", err.Error()) - return - } - - // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote - parsedDoc, err := Load2_3(bytes.NewReader(w.Bytes())) - if err != nil { - t.Errorf("failed to parse written document: %v", err.Error()) - return - } - - if cmp.Equal(handwrittenExample, parsedDoc) { - t.Errorf("Got incorrect struct after writing and re-parsing JSON example") - return - } -} - -// want is handwritten translation of the official example JSON SPDX v2.3 document into a Go struct. -// We expect that the result of parsing the official document should be this value. -// We expect that the result of writing this struct should match the official example document. -var want2_3 = v2_3.Document{ - DataLicense: "CC0-1.0", - SPDXVersion: "SPDX-2.3", - SPDXIdentifier: "SPDXRef-DOCUMENT", - DocumentName: "SPDX-Tools-v2.0", - DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", - CreationInfo: &v2_3.CreationInfo{ - LicenseListVersion: "3.9", - Creators: []common.Creator{ - {CreatorType: "Tool", Creator: "LicenseFind-1.0"}, - {CreatorType: "Organization", Creator: "ExampleCodeInspect ()"}, - {CreatorType: "Person", Creator: "Jane Doe ()"}, - }, - Created: "2010-01-29T18:30:22Z", - CreatorComment: "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.", - }, - DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.", - ExternalDocumentReferences: []v2_3.ExternalDocumentRef{ - { - DocumentRefID: "DocumentRef-spdx-tool-1.2", - URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301", - Checksum: common.Checksum{ - Algorithm: common.SHA1, - Value: "d6a770ba38583ed4bb4525bd96e50461655d2759", - }, - }, - }, - OtherLicenses: []*v2_3.OtherLicense{ - { - LicenseIdentifier: "LicenseRef-1", - ExtractedText: "/*\n * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-2", - ExtractedText: "This package includes the GRDDL parser developed by Hewlett Packard under the following license:\n� Copyright 2007 Hewlett-Packard Development Company, LP\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: \n\nRedistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. \nRedistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. \nThe name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. \nTHIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - }, - { - LicenseIdentifier: "LicenseRef-4", - ExtractedText: "/*\n * (c) Copyright 2009 University of Bristol\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-Beerware-4.2", - ExtractedText: "\"THE BEER-WARE LICENSE\" (Revision 42):\nphk@FreeBSD.ORG wrote this file. As long as you retain this notice you\ncan do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp", - LicenseComment: "The beerware license has a couple of other standard variants.", - LicenseName: "Beer-Ware License (Version 42)", - LicenseCrossReferences: []string{"http://people.freebsd.org/~phk/"}, - }, - { - LicenseIdentifier: "LicenseRef-3", - ExtractedText: "The CyberNeko Software License, Version 1.0\n\n \n(C) Copyright 2002-2005, Andy Clark. All rights reserved.\n \nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions\nare met:\n\n1. Redistributions of source code must retain the above copyright\n notice, this list of conditions and the following disclaimer. \n\n2. Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in\n the documentation and/or other materials provided with the\n distribution.\n\n3. The end-user documentation included with the redistribution,\n if any, must include the following acknowledgment: \n \"This product includes software developed by Andy Clark.\"\n Alternately, this acknowledgment may appear in the software itself,\n if and wherever such third-party acknowledgments normally appear.\n\n4. The names \"CyberNeko\" and \"NekoHTML\" must not be used to endorse\n or promote products derived from this software without prior \n written permission. For written permission, please contact \n andyc@cyberneko.net.\n\n5. Products derived from this software may not be called \"CyberNeko\",\n nor may \"CyberNeko\" appear in their name, without prior written\n permission of the author.\n\nTHIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\nOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS\nBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, \nOR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT \nOF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR \nBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \nWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE \nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, \nEVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - LicenseName: "CyberNeko License", - LicenseCrossReferences: []string{ - "http://people.apache.org/~andyc/neko/LICENSE", - "http://justasample.url.com", - }, - LicenseComment: "This is tye CyperNeko License", - }, - }, - Annotations: []*v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Jane Doe ()", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Document level annotation", - }, - { - Annotator: common.Annotator{ - Annotator: "Joe Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-02-10T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", - }, - { - Annotator: common.Annotator{ - Annotator: "Suzanne Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-03-13T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "Another example reviewer.", - }, - }, - Packages: []*v2_3.Package{ - { - PackageName: "glibc", - PackageSPDXIdentifier: "SPDXRef-Package", - PackageVersion: "2.11.1", - PackageFileName: "glibc-2.11.1.tar.gz", - PackageSupplier: &common.Supplier{ - Supplier: "Jane Doe (jane.doe@example.com)", - SupplierType: "Person", - }, - PackageOriginator: &common.Originator{ - Originator: "ExampleCodeInspect (contact@example.com)", - OriginatorType: "Organization", - }, - PackageDownloadLocation: "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz", - FilesAnalyzed: true, - PackageVerificationCode: &common.PackageVerificationCode{ - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - ExcludedFiles: []string{"./package.spdx"}, - }, - PackageChecksums: []common.Checksum{ - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - { - Algorithm: "SHA256", - Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd", - }, - }, - PackageHomePage: "http://ftp.gnu.org/gnu/glibc", - PackageSourceInfo: "uses glibc-2_11-branch from git://sourceware.org/git/glibc.git.", - PackageLicenseConcluded: "(LGPL-2.0-only OR LicenseRef-3)", - PackageLicenseInfoFromFiles: []string{ - "GPL-2.0-only", - "LicenseRef-2", - "LicenseRef-1", - }, - PackageLicenseDeclared: "(LGPL-2.0-only AND LicenseRef-3)", - PackageLicenseComments: "The license for this project changed with the release of version x.y. The version of the project included here post-dates the license change.", - PackageCopyrightText: "Copyright 2008-2010 John Smith", - PackageSummary: "GNU C library.", - PackageDescription: "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.", - PackageComment: "", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "SECURITY", - RefType: "cpe23Type", - Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", - }, - { - Category: "OTHER", - RefType: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge", - Locator: "acmecorp/acmenator/4.1.3-alpha", - ExternalRefComment: "This is the external ref for Acme", - }, - }, - PackageAttributionTexts: []string{ - "The GNU C Library is free software. See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed. License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually.", - }, - Files: nil, - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Package Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Package level annotation", - }, - }, - }, - { - PackageSPDXIdentifier: "SPDXRef-fromDoap-1", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "http://commons.apache.org/proper/commons-lang/", - PackageLicenseDeclared: "NOASSERTION", - PackageName: "Apache Commons Lang", - }, - { - PackageName: "Jena", - PackageSPDXIdentifier: "SPDXRef-fromDoap-0", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "PACKAGE-MANAGER", - RefType: "purl", - Locator: "pkg:maven/org.apache.jena/apache-jena@3.12.0", - }, - }, - FilesAnalyzed: false, - PackageHomePage: "http://www.openjena.org/", - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseDeclared: "NOASSERTION", - PackageVersion: "3.12.0", - }, - { - PackageSPDXIdentifier: "SPDXRef-Saxon", - PackageChecksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - }, - PackageCopyrightText: "Copyright Saxonica Ltd", - PackageDescription: "The Saxon package is a collection of tools for processing XML documents.", - PackageDownloadLocation: "https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download", - FilesAnalyzed: false, - PackageHomePage: "http://saxon.sourceforge.net/", - PackageLicenseComments: "Other versions available for a commercial license", - PackageLicenseConcluded: "MPL-1.0", - PackageLicenseDeclared: "MPL-1.0", - PackageName: "Saxon", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "8.8", - }, - { - PrimaryPackagePurpose: "CONTAINER", - PackageSPDXIdentifier: "SPDXRef-CentOS-7", - PackageCopyrightText: "NOASSERTION", - PackageDescription: "The CentOS container used to run the application.", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "https://www.centos.org/", - PackageName: "centos", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "centos7.9.2009", - BuiltDate: "2021-09-15T02:38:00Z", - ValidUntilDate: "2022-10-15T02:38:00Z", - ReleaseDate: "2021-10-15T02:38:00Z", - }, - }, - Files: []*v2_3.File{ - { - FileName: "./src/org/spdx/parser/DOAPProject.java", - FileSPDXIdentifier: "SPDXRef-DoapSource", - FileTypes: []string{ - "SOURCE", - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", - }, - }, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "Apache-2.0", - }, - FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.", - FileContributors: []string{ - "Protecode Inc.", - "SPDX Technical Team Members", - "Open Logic Inc.", - "Source Auditor Inc.", - "Black Duck Software In.c", - }, - }, - { - FileSPDXIdentifier: "SPDXRef-CommonsLangSrc", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "c2b4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file is used by Jena", - FileCopyrightText: "Copyright 2001-2011 The Apache Software Foundation", - FileContributors: []string{"Apache Software Foundation"}, - FileName: "./lib-source/commons-lang3-3.1-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{"Apache-2.0"}, - FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())", - }, - { - FileSPDXIdentifier: "SPDXRef-JenaLib", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "3ab4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file belongs to Jena", - FileCopyrightText: "(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP", - FileContributors: []string{"Apache Software Foundation", "Hewlett Packard Inc."}, - FileName: "./lib-source/jena-2.6.3-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseComments: "This license is used by Jena", - LicenseConcluded: "LicenseRef-1", - LicenseInfoInFiles: []string{"LicenseRef-1"}, - }, - { - FileSPDXIdentifier: "SPDXRef-File", - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "File Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "File level annotation", - }, - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - }, - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - }, - FileComment: "The concluded license was taken from the package level that the file was included in.\nThis information was found in the COPYING.txt file in the xyz directory.", - FileCopyrightText: "Copyright 2008-2010 John Smith", - FileContributors: []string{"The Regents of the University of California", "Modified by Paul Mundt lethal@linux-sh.org", "IBM Corporation"}, - FileName: "./package/foo.c", - FileTypes: []string{"SOURCE"}, - LicenseComments: "The concluded license was taken from the package level that the file was included in.", - LicenseConcluded: "(LGPL-2.0-only OR LicenseRef-2)", - LicenseInfoInFiles: []string{"GPL-2.0-only", "LicenseRef-2"}, - FileNotice: "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", - }, - }, - Snippets: []v2_3.Snippet{ - { - SnippetSPDXIdentifier: "SPDXRef-Snippet", - SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource", - Ranges: []common.SnippetRange{ - { - StartPointer: common.SnippetRangePointer{ - Offset: 310, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - Offset: 420, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - { - StartPointer: common.SnippetRangePointer{ - LineNumber: 5, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - LineNumber: 23, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - }, - SnippetLicenseConcluded: "GPL-2.0-only", - LicenseInfoInSnippet: []string{"GPL-2.0-only"}, - SnippetLicenseComments: "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.", - SnippetCopyrightText: "Copyright 2008-2010 John Smith", - SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.", - SnippetName: "from linux kernel", - }, - }, - Relationships: []*v2_3.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("spdx-tool-1.2", "ToolsElement"), - Relationship: "COPY_OF", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "File"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "JenaLib"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "Saxon"), - Relationship: "DYNAMIC_LINK", - }, - { - RefA: common.MakeDocElementID("", "CommonsLangSrc"), - RefB: common.MakeDocElementSpecial("NOASSERTION"), - Relationship: "GENERATED_FROM", - }, - { - RefA: common.MakeDocElementID("", "JenaLib"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "File"), - RefB: common.MakeDocElementID("", "fromDoap-0"), - Relationship: "GENERATED_FROM", - }, - }, - Reviews: []*v2_3.Review{ - { - Reviewer: "joe@example.com", - ReviewerType: "Person", - ReviewDate: "2021-11-03T05:43:21Z", - ReviewComment: "This is a review comment", - }, - }, -} diff --git a/json/parser.go b/json/parser.go deleted file mode 100644 index ee7915de..00000000 --- a/json/parser.go +++ /dev/null @@ -1,48 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_json - -import ( - "bytes" - "encoding/json" - "io" - - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -// Load2_2 takes in an io.Reader and returns an SPDX document. -func Load2_2(content io.Reader) (*v2_2.Document, error) { - // convert io.Reader to a slice of bytes and call the parser - buf := new(bytes.Buffer) - _, err := buf.ReadFrom(content) - if err != nil { - return nil, err - } - - var doc v2_2.Document - err = json.Unmarshal(buf.Bytes(), &doc) - if err != nil { - return nil, err - } - - return &doc, nil -} - -// Load2_3 takes in an io.Reader and returns an SPDX document. -func Load2_3(content io.Reader) (*v2_3.Document, error) { - // convert io.Reader to a slice of bytes and call the parser - buf := new(bytes.Buffer) - _, err := buf.ReadFrom(content) - if err != nil { - return nil, err - } - - var doc v2_3.Document - err = json.Unmarshal(buf.Bytes(), &doc) - if err != nil { - return nil, err - } - - return &doc, nil -} diff --git a/json/reader.go b/json/reader.go new file mode 100644 index 00000000..f1a0b989 --- /dev/null +++ b/json/reader.go @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package json + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +// Read takes an io.Reader and returns a fully-parsed current model SPDX Document +// or an error if any error is encountered. +func Read(content io.Reader) (*spdx.Document, error) { + doc := spdx.Document{} + err := ReadInto(content, &doc) + return &doc, err +} + +// ReadInto takes an io.Reader, reads in the SPDX document at the version provided +// and converts to the doc version +func ReadInto(content io.Reader, doc common.AnyDocument) error { + if !convert.IsPtr(doc) { + return fmt.Errorf("doc to read into must be a pointer") + } + + buf := new(bytes.Buffer) + _, err := buf.ReadFrom(content) + if err != nil { + return err + } + + var data interface{} + err = json.Unmarshal(buf.Bytes(), &data) + if err != nil { + return err + } + + val, ok := data.(map[string]interface{}) + if !ok { + return fmt.Errorf("not a valid SPDX JSON document") + } + + version, ok := val["spdxVersion"] + if !ok { + return fmt.Errorf("JSON document does not contain spdxVersion field") + } + + switch version { + case v2_1.Version: + var doc v2_1.Document + err = json.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + case v2_2.Version: + var doc v2_2.Document + err = json.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + case v2_3.Version: + var doc v2_3.Document + err = json.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + default: + return fmt.Errorf("unsupported SDPX version: %s", version) + } + + return convert.Document(data, doc) +} diff --git a/json/writer.go b/json/writer.go index f7da55b7..88563a5e 100644 --- a/json/writer.go +++ b/json/writer.go @@ -1,32 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package spdx_json +package json import ( "encoding/json" - "github.com/spdx/tools-golang/spdx/v2_3" "io" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/common" ) -// Save2_2 takes an SPDX Document (version 2.2) and an io.Writer, and writes the document to the writer in JSON format. -func Save2_2(doc *v2_2.Document, w io.Writer) error { - buf, err := json.Marshal(doc) - if err != nil { - return err - } - - _, err = w.Write(buf) - if err != nil { - return err - } - - return nil -} - -// Save2_3 takes an SPDX Document (version 2.3) and an io.Writer, and writes the document to the writer in JSON format. -func Save2_3(doc *v2_3.Document, w io.Writer) error { +// Write takes an SPDX Document and an io.Writer, and writes the document to the writer in JSON format. +func Write(doc common.AnyDocument, w io.Writer) error { buf, err := json.Marshal(doc) if err != nil { return err diff --git a/licensediff/licensediff.go b/licensediff/licensediff.go index ba8cd84c..542ec5d2 100644 --- a/licensediff/licensediff.go +++ b/licensediff/licensediff.go @@ -4,9 +4,7 @@ package licensediff 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" + "github.com/spdx/tools-golang/spdx" ) // LicensePair is a result set where we are talking about two license strings, @@ -16,65 +14,9 @@ type LicensePair struct { Second string } -// MakePairs2_1 essentially just consolidates all files and LicenseConcluded +// MakePairs essentially just consolidates all files and LicenseConcluded // strings into a single data structure. -func MakePairs2_1(p1 *v2_1.Package, p2 *v2_1.Package) (map[string]LicensePair, error) { - pairs := map[string]LicensePair{} - - // first, go through and add all files/licenses from p1 - for _, f := range p1.Files { - pair := LicensePair{First: f.LicenseConcluded, Second: ""} - pairs[f.FileName] = pair - } - - // now, go through all files/licenses from p2. If already - // present, add as .second; if not, create new pair - for _, f := range p2.Files { - firstLic := "" - existingPair, ok := pairs[f.FileName] - if ok { - // already present; update it - firstLic = existingPair.First - } - // now, update what's there, either way - pair := LicensePair{First: firstLic, Second: f.LicenseConcluded} - pairs[f.FileName] = pair - } - - return pairs, nil -} - -// MakePairs2_2 essentially just consolidates all files and LicenseConcluded -// strings into a single data structure. -func MakePairs2_2(p1 *v2_2.Package, p2 *v2_2.Package) (map[string]LicensePair, error) { - pairs := map[string]LicensePair{} - - // first, go through and add all files/licenses from p1 - for _, f := range p1.Files { - pair := LicensePair{First: f.LicenseConcluded, Second: ""} - pairs[f.FileName] = pair - } - - // now, go through all files/licenses from p2. If already - // present, add as .second; if not, create new pair - for _, f := range p2.Files { - firstLic := "" - existingPair, ok := pairs[f.FileName] - if ok { - // already present; update it - firstLic = existingPair.First - } - // now, update what's there, either way - pair := LicensePair{First: firstLic, Second: f.LicenseConcluded} - pairs[f.FileName] = pair - } - - return pairs, nil -} - -// MakePairs2_3 essentially just consolidates all files and LicenseConcluded -// strings into a single data structure. -func MakePairs2_3(p1 *v2_3.Package, p2 *v2_3.Package) (map[string]LicensePair, error) { +func MakePairs(p1 *spdx.Package, p2 *spdx.Package) (map[string]LicensePair, error) { pairs := map[string]LicensePair{} // first, go through and add all files/licenses from p1 diff --git a/licensediff/licensediff_test.go b/licensediff/licensediff_test.go index 226ad51d..5e6de63e 100644 --- a/licensediff/licensediff_test.go +++ b/licensediff/licensediff_test.go @@ -5,1057 +5,14 @@ package licensediff import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 License diff top-level function tests ===== -func Test2_1DifferCanCreateDiffPairs(t *testing.T) { +func TestDifferCanCreateDiffPairs(t *testing.T) { // create files to be used in diff // f1 will be identical in both - f1 := &v2_1.File{ - FileName: "/project/file1.txt", - FileSPDXIdentifier: common.ElementID("File561"), - Checksums: []common.Checksum{{Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", Algorithm: common.SHA1}}, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "LicenseRef-We-will-ignore-LicenseInfoInFiles", - }, - FileCopyrightText: "We'll ignore copyright values", - } - - // f2 will only appear in the first Package - f2 := &v2_1.File{ - FileName: "/project/file2.txt", - FileSPDXIdentifier: common.ElementID("File562"), - Checksums: []common.Checksum{{Value: "066c5139bd9a43d15812ec1a1755b08ccf199824", Algorithm: common.SHA1}}, - LicenseConcluded: "GPL-2.0-or-later", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f3 will only appear in the second Package - f3 := &v2_1.File{ - FileName: "/project/file3.txt", - FileSPDXIdentifier: common.ElementID("File563"), - Checksums: []common.Checksum{{Value: "bd0f4863b15fad2b79b35303af54fcb5baaf7c68", Algorithm: common.SHA1}}, - LicenseConcluded: "MPL-2.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f4_1 and f4_2 will appear in first and second, - // with same name, same hash and different license - f4_1 := &v2_1.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{Value: "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5", Algorithm: common.SHA1}}, - LicenseConcluded: "MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f4_2 := &v2_1.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{Value: "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5", Algorithm: common.SHA1}}, - LicenseConcluded: "Apache-2.0 AND MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f5_1 and f5_2 will appear in first and second, - // with same name, different hash and same license - f5_1 := &v2_1.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{Value: "ba226db943bbbf455da77afab6f16dbab156d000", Algorithm: common.SHA1}}, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f5_2 := &v2_1.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{Value: "b6e0ec7d085c5699b46f6f8d425413702652874d", Algorithm: common.SHA1}}, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f6_1 and f6_2 will appear in first and second, - // with same name, different hash and different license - f6_1 := &v2_1.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{Value: "ba226db943bbbf455da77afab6f16dbab156d000", Algorithm: common.SHA1}}, - LicenseConcluded: "CC0-1.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f6_2 := &v2_1.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{Value: "b6e0ec7d085c5699b46f6f8d425413702652874d", Algorithm: common.SHA1}}, - LicenseConcluded: "Unlicense", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // create Packages - p1 := &v2_1.Package{ - PackageName: "p1", - PackageSPDXIdentifier: common.ElementID("p1"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "abc123abc123"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_1.File{ - f1, - f2, - f4_1, - f5_1, - f6_1, - }, - } - p2 := &v2_1.Package{ - PackageName: "p2", - PackageSPDXIdentifier: common.ElementID("p2"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "def456def456"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_1.File{ - f1, - f3, - f4_2, - f5_2, - f6_2, - }, - } - - // run the diff between the two packages - diffMap, err := MakePairs2_1(p1, p2) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // check that the diff results are what we expect - // there should be 6 entries, one for each unique filename - if len(diffMap) != 6 { - t.Fatalf("Expected %d, got %d", 6, len(diffMap)) - } - - // check each filename is present, and check its pair - // pair 1 -- same in both - pair1, ok := diffMap["/project/file1.txt"] - if !ok { - t.Fatalf("Couldn't get pair1") - } - if pair1.First != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f1.LicenseConcluded, pair1.First) - } - if pair1.Second != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair1.Second) - } - - // pair 2 -- only in first - pair2, ok := diffMap["/project/file2.txt"] - if !ok { - t.Fatalf("Couldn't get pair2") - } - if pair2.First != f2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair2.First) - } - if pair2.Second != "" { - t.Errorf("Expected %s, got %s", "", pair2.Second) - } - - // pair 3 -- only in second - pair3, ok := diffMap["/project/file3.txt"] - if !ok { - t.Fatalf("Couldn't get pair3") - } - if pair3.First != "" { - t.Errorf("Expected %s, got %s", "", pair3.First) - } - if pair3.Second != f3.LicenseConcluded { - t.Errorf("Expected %s, got %s", f3.LicenseConcluded, pair3.Second) - } - - // pair 4 -- in both but different license - pair4, ok := diffMap["/project/file4.txt"] - if !ok { - t.Fatalf("Couldn't get pair4") - } - if pair4.First != f4_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, pair4.First) - } - if pair4.Second != f4_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, pair4.Second) - } - - // pair 5 -- in both but different hash, same license - pair5, ok := diffMap["/project/file5.txt"] - if !ok { - t.Fatalf("Couldn't get pair5") - } - if pair5.First != f5_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, pair5.First) - } - if pair5.Second != f5_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, pair5.Second) - } - - // pair 6 -- in both but different hash, different license - pair6, ok := diffMap["/project/file6.txt"] - if !ok { - t.Fatalf("Couldn't get pair6") - } - if pair6.First != f6_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, pair6.First) - } - if pair6.Second != f6_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, pair6.Second) - } -} - -func Test2_1DifferCanCreateDiffStructuredResults(t *testing.T) { - // create files to be used in diff - // f1 will be identical in both - f1 := &v2_1.File{ - FileName: "/project/file1.txt", - FileSPDXIdentifier: common.ElementID("File561"), - Checksums: []common.Checksum{{Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", Algorithm: common.SHA1}}, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "LicenseRef-We-will-ignore-LicenseInfoInFiles", - }, - FileCopyrightText: "We'll ignore copyright values", - } - - // f2 will only appear in the first Package - f2 := &v2_1.File{ - FileName: "/project/file2.txt", - FileSPDXIdentifier: common.ElementID("File562"), - Checksums: []common.Checksum{{Value: "066c5139bd9a43d15812ec1a1755b08ccf199824", Algorithm: common.SHA1}}, - LicenseConcluded: "GPL-2.0-or-later", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f3 will only appear in the second Package - f3 := &v2_1.File{ - FileName: "/project/file3.txt", - FileSPDXIdentifier: common.ElementID("File563"), - Checksums: []common.Checksum{{Value: "bd0f4863b15fad2b79b35303af54fcb5baaf7c68", Algorithm: common.SHA1}}, - LicenseConcluded: "MPL-2.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f4_1 and f4_2 will appear in first and second, - // with same name, same hash and different license - f4_1 := &v2_1.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{Value: "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5", Algorithm: common.SHA1}}, - LicenseConcluded: "MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f4_2 := &v2_1.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{Value: "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5", Algorithm: common.SHA1}}, - LicenseConcluded: "Apache-2.0 AND MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f5_1 and f5_2 will appear in first and second, - // with same name, different hash and same license - f5_1 := &v2_1.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{Value: "ba226db943bbbf455da77afab6f16dbab156d000", Algorithm: common.SHA1}}, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f5_2 := &v2_1.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{Value: "b6e0ec7d085c5699b46f6f8d425413702652874d", Algorithm: common.SHA1}}, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f6_1 and f6_2 will appear in first and second, - // with same name, different hash and different license - f6_1 := &v2_1.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{Value: "ba226db943bbbf455da77afab6f16dbab156d000", Algorithm: common.SHA1}}, - LicenseConcluded: "CC0-1.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f6_2 := &v2_1.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{Value: "b6e0ec7d085c5699b46f6f8d425413702652874d", Algorithm: common.SHA1}}, - LicenseConcluded: "Unlicense", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // create Packages - p1 := &v2_1.Package{ - PackageName: "p1", - PackageSPDXIdentifier: common.ElementID("p1"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "abc123abc123"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_1.File{ - f1, - f2, - f4_1, - f5_1, - f6_1, - }, - } - p2 := &v2_1.Package{ - PackageName: "p2", - PackageSPDXIdentifier: common.ElementID("p2"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "def456def456"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_1.File{ - f1, - f3, - f4_2, - f5_2, - f6_2, - }, - } - - // run the diff between the two packages - diffMap, err := MakePairs2_1(p1, p2) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // now, create the LicenseDiff structured results from the pairs - diffResults, err := MakeResults(diffMap) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // check that the diff results are the expected lengths - if len(diffResults.InBothChanged) != 2 { - t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothChanged)) - } - if len(diffResults.InBothSame) != 2 { - t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothSame)) - } - if len(diffResults.InFirstOnly) != 1 { - t.Fatalf("Expected %d, got %d", 1, len(diffResults.InFirstOnly)) - } - if len(diffResults.InSecondOnly) != 1 { - t.Fatalf("Expected %d, got %d", 1, len(diffResults.InSecondOnly)) - } - - // check each filename is present where it belongs, and check license(s) - - // in both and different license: f4 and f6 - // filename will map to a LicensePair - check4, ok := diffResults.InBothChanged["/project/file4.txt"] - if !ok { - t.Fatalf("Couldn't get check4") - } - if check4.First != f4_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, check4.First) - } - if check4.Second != f4_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, check4.Second) - } - check6, ok := diffResults.InBothChanged["/project/file6.txt"] - if !ok { - t.Fatalf("Couldn't get check6") - } - if check6.First != f6_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, check6.First) - } - if check6.Second != f6_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, check6.Second) - } - - // in both and same license: f1 and f5 - // filename will map to a string - check1, ok := diffResults.InBothSame["/project/file1.txt"] - if !ok { - t.Fatalf("Couldn't get check1") - } - if check1 != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f1.LicenseConcluded, check1) - } - check5, ok := diffResults.InBothSame["/project/file5.txt"] - if !ok { - t.Fatalf("Couldn't get check5") - } - if check5 != f5_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, check5) - } - if check5 != f5_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, check5) - } - - // in first only: f2 - // filename will map to a string - check2, ok := diffResults.InFirstOnly["/project/file2.txt"] - if !ok { - t.Fatalf("Couldn't get check2") - } - if check2 != f2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, check2) - } - - // in second only: f3 - // filename will map to a string - check3, ok := diffResults.InSecondOnly["/project/file3.txt"] - if !ok { - t.Fatalf("Couldn't get check3") - } - if check3 != f3.LicenseConcluded { - t.Errorf("Expected %s, got %s", f3.LicenseConcluded, check2) - } - -} - -// ===== 2.2 License diff top-level function tests ===== -func Test2_2DifferCanCreateDiffPairs(t *testing.T) { - // create files to be used in diff - // f1 will be identical in both - f1 := &v2_2.File{ - FileName: "/project/file1.txt", - FileSPDXIdentifier: common.ElementID("File561"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "LicenseRef-We-will-ignore-LicenseInfoInFiles", - }, - FileCopyrightText: "We'll ignore copyright values", - } - - // f2 will only appear in the first Package - f2 := &v2_2.File{ - FileName: "/project/file2.txt", - FileSPDXIdentifier: common.ElementID("File562"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "GPL-2.0-or-later", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f3 will only appear in the second Package - f3 := &v2_2.File{ - FileName: "/project/file3.txt", - FileSPDXIdentifier: common.ElementID("File563"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "MPL-2.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f4_1 and f4_2 will appear in first and second, - // with same name, same hash and different license - f4_1 := &v2_2.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f4_2 := &v2_2.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Apache-2.0 AND MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f5_1 and f5_2 will appear in first and second, - // with same name, different hash and same license - f5_1 := &v2_2.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f5_2 := &v2_2.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f6_1 and f6_2 will appear in first and second, - // with same name, different hash and different license - f6_1 := &v2_2.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "CC0-1.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f6_2 := &v2_2.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Unlicense", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // create Packages - p1 := &v2_2.Package{ - PackageName: "p1", - PackageSPDXIdentifier: common.ElementID("p1"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "abc123abc123"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_2.File{ - f1, - f2, - f4_1, - f5_1, - f6_1, - }, - } - p2 := &v2_2.Package{ - PackageName: "p2", - PackageSPDXIdentifier: common.ElementID("p2"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "def456def456"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_2.File{ - f1, - f3, - f4_2, - f5_2, - f6_2, - }, - } - - // run the diff between the two packages - diffMap, err := MakePairs2_2(p1, p2) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // check that the diff results are what we expect - // there should be 6 entries, one for each unique filename - if len(diffMap) != 6 { - t.Fatalf("Expected %d, got %d", 6, len(diffMap)) - } - - // check each filename is present, and check its pair - // pair 1 -- same in both - pair1, ok := diffMap["/project/file1.txt"] - if !ok { - t.Fatalf("Couldn't get pair1") - } - if pair1.First != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f1.LicenseConcluded, pair1.First) - } - if pair1.Second != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair1.Second) - } - - // pair 2 -- only in first - pair2, ok := diffMap["/project/file2.txt"] - if !ok { - t.Fatalf("Couldn't get pair2") - } - if pair2.First != f2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair2.First) - } - if pair2.Second != "" { - t.Errorf("Expected %s, got %s", "", pair2.Second) - } - - // pair 3 -- only in second - pair3, ok := diffMap["/project/file3.txt"] - if !ok { - t.Fatalf("Couldn't get pair3") - } - if pair3.First != "" { - t.Errorf("Expected %s, got %s", "", pair3.First) - } - if pair3.Second != f3.LicenseConcluded { - t.Errorf("Expected %s, got %s", f3.LicenseConcluded, pair3.Second) - } - - // pair 4 -- in both but different license - pair4, ok := diffMap["/project/file4.txt"] - if !ok { - t.Fatalf("Couldn't get pair4") - } - if pair4.First != f4_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, pair4.First) - } - if pair4.Second != f4_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, pair4.Second) - } - - // pair 5 -- in both but different hash, same license - pair5, ok := diffMap["/project/file5.txt"] - if !ok { - t.Fatalf("Couldn't get pair5") - } - if pair5.First != f5_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, pair5.First) - } - if pair5.Second != f5_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, pair5.Second) - } - - // pair 6 -- in both but different hash, different license - pair6, ok := diffMap["/project/file6.txt"] - if !ok { - t.Fatalf("Couldn't get pair6") - } - if pair6.First != f6_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, pair6.First) - } - if pair6.Second != f6_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, pair6.Second) - } -} - -func Test2_2DifferCanCreateDiffStructuredResults(t *testing.T) { - // create files to be used in diff - // f1 will be identical in both - f1 := &v2_2.File{ - FileName: "/project/file1.txt", - FileSPDXIdentifier: common.ElementID("File561"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "LicenseRef-We-will-ignore-LicenseInfoInFiles", - }, - FileCopyrightText: "We'll ignore copyright values", - } - - // f2 will only appear in the first Package - f2 := &v2_2.File{ - FileName: "/project/file2.txt", - FileSPDXIdentifier: common.ElementID("File562"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "GPL-2.0-or-later", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f3 will only appear in the second Package - f3 := &v2_2.File{ - FileName: "/project/file3.txt", - FileSPDXIdentifier: common.ElementID("File563"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "MPL-2.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f4_1 and f4_2 will appear in first and second, - // with same name, same hash and different license - f4_1 := &v2_2.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f4_2 := &v2_2.File{ - FileName: "/project/file4.txt", - FileSPDXIdentifier: common.ElementID("File564"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Apache-2.0 AND MIT", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f5_1 and f5_2 will appear in first and second, - // with same name, different hash and same license - f5_1 := &v2_2.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f5_2 := &v2_2.File{ - FileName: "/project/file5.txt", - FileSPDXIdentifier: common.ElementID("File565"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - - LicenseConcluded: "BSD-3-Clause", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // f6_1 and f6_2 will appear in first and second, - // with same name, different hash and different license - f6_1 := &v2_2.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "CC0-1.0", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - f6_2 := &v2_2.File{ - FileName: "/project/file6.txt", - FileSPDXIdentifier: common.ElementID("File566"), - Checksums: []common.Checksum{{ - Algorithm: common.SHA1, - Value: "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3", - }, - }, - LicenseConcluded: "Unlicense", - LicenseInfoInFiles: []string{ - "NOASSERTION", - }, - FileCopyrightText: "NOASSERTION", - } - - // create Packages - p1 := &v2_2.Package{ - PackageName: "p1", - PackageSPDXIdentifier: common.ElementID("p1"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "abc123abc123"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_2.File{ - f1, - f2, - f4_1, - f5_1, - f6_1, - }, - } - p2 := &v2_2.Package{ - PackageName: "p2", - PackageSPDXIdentifier: common.ElementID("p2"), - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: true, - IsFilesAnalyzedTagPresent: true, - // fake the verification code for present purposes - PackageVerificationCode: common.PackageVerificationCode{Value: "def456def456"}, - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseInfoFromFiles: []string{ - "NOASSERTION", - }, - PackageLicenseDeclared: "NOASSERTION", - PackageCopyrightText: "NOASSERTION", - Files: []*v2_2.File{ - f1, - f3, - f4_2, - f5_2, - f6_2, - }, - } - - // run the diff between the two packages - diffMap, err := MakePairs2_2(p1, p2) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // now, create the LicenseDiff structured results from the pairs - diffResults, err := MakeResults(diffMap) - if err != nil { - t.Fatalf("Expected nil error, got %v", err) - } - - // check that the diff results are the expected lengths - if len(diffResults.InBothChanged) != 2 { - t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothChanged)) - } - if len(diffResults.InBothSame) != 2 { - t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothSame)) - } - if len(diffResults.InFirstOnly) != 1 { - t.Fatalf("Expected %d, got %d", 1, len(diffResults.InFirstOnly)) - } - if len(diffResults.InSecondOnly) != 1 { - t.Fatalf("Expected %d, got %d", 1, len(diffResults.InSecondOnly)) - } - - // check each filename is present where it belongs, and check license(s) - - // in both and different license: f4 and f6 - // filename will map to a LicensePair - check4, ok := diffResults.InBothChanged["/project/file4.txt"] - if !ok { - t.Fatalf("Couldn't get check4") - } - if check4.First != f4_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, check4.First) - } - if check4.Second != f4_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, check4.Second) - } - check6, ok := diffResults.InBothChanged["/project/file6.txt"] - if !ok { - t.Fatalf("Couldn't get check6") - } - if check6.First != f6_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, check6.First) - } - if check6.Second != f6_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, check6.Second) - } - - // in both and same license: f1 and f5 - // filename will map to a string - check1, ok := diffResults.InBothSame["/project/file1.txt"] - if !ok { - t.Fatalf("Couldn't get check1") - } - if check1 != f1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f1.LicenseConcluded, check1) - } - check5, ok := diffResults.InBothSame["/project/file5.txt"] - if !ok { - t.Fatalf("Couldn't get check5") - } - if check5 != f5_1.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, check5) - } - if check5 != f5_2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, check5) - } - - // in first only: f2 - // filename will map to a string - check2, ok := diffResults.InFirstOnly["/project/file2.txt"] - if !ok { - t.Fatalf("Couldn't get check2") - } - if check2 != f2.LicenseConcluded { - t.Errorf("Expected %s, got %s", f2.LicenseConcluded, check2) - } - - // in second only: f3 - // filename will map to a string - check3, ok := diffResults.InSecondOnly["/project/file3.txt"] - if !ok { - t.Fatalf("Couldn't get check3") - } - if check3 != f3.LicenseConcluded { - t.Errorf("Expected %s, got %s", f3.LicenseConcluded, check2) - } - -} - -// ===== 2.3 License diff top-level function tests ===== -func Test2_3DifferCanCreateDiffPairs(t *testing.T) { - // create files to be used in diff - // f1 will be identical in both - f1 := &v2_3.File{ + f1 := &spdx.File{ FileName: "/project/file1.txt", FileSPDXIdentifier: common.ElementID("File561"), Checksums: []common.Checksum{{ @@ -1071,7 +28,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { } // f2 will only appear in the first Package - f2 := &v2_3.File{ + f2 := &spdx.File{ FileName: "/project/file2.txt", FileSPDXIdentifier: common.ElementID("File562"), Checksums: []common.Checksum{{ @@ -1087,7 +44,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { } // f3 will only appear in the second Package - f3 := &v2_3.File{ + f3 := &spdx.File{ FileName: "/project/file3.txt", FileSPDXIdentifier: common.ElementID("File563"), Checksums: []common.Checksum{{ @@ -1104,7 +61,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { // f4_1 and f4_2 will appear in first and second, // with same name, same hash and different license - f4_1 := &v2_3.File{ + f4_1 := &spdx.File{ FileName: "/project/file4.txt", FileSPDXIdentifier: common.ElementID("File564"), Checksums: []common.Checksum{{ @@ -1118,7 +75,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f4_2 := &v2_3.File{ + f4_2 := &spdx.File{ FileName: "/project/file4.txt", FileSPDXIdentifier: common.ElementID("File564"), Checksums: []common.Checksum{{ @@ -1135,7 +92,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { // f5_1 and f5_2 will appear in first and second, // with same name, different hash and same license - f5_1 := &v2_3.File{ + f5_1 := &spdx.File{ FileName: "/project/file5.txt", FileSPDXIdentifier: common.ElementID("File565"), Checksums: []common.Checksum{{ @@ -1149,7 +106,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f5_2 := &v2_3.File{ + f5_2 := &spdx.File{ FileName: "/project/file5.txt", FileSPDXIdentifier: common.ElementID("File565"), Checksums: []common.Checksum{{ @@ -1166,7 +123,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { // f6_1 and f6_2 will appear in first and second, // with same name, different hash and different license - f6_1 := &v2_3.File{ + f6_1 := &spdx.File{ FileName: "/project/file6.txt", FileSPDXIdentifier: common.ElementID("File566"), Checksums: []common.Checksum{{ @@ -1180,7 +137,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f6_2 := &v2_3.File{ + f6_2 := &spdx.File{ FileName: "/project/file6.txt", FileSPDXIdentifier: common.ElementID("File566"), Checksums: []common.Checksum{{ @@ -1196,7 +153,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { } // create Packages - p1 := &v2_3.Package{ + p1 := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "NOASSERTION", @@ -1210,7 +167,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { }, PackageLicenseDeclared: "NOASSERTION", PackageCopyrightText: "NOASSERTION", - Files: []*v2_3.File{ + Files: []*spdx.File{ f1, f2, f4_1, @@ -1218,7 +175,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { f6_1, }, } - p2 := &v2_3.Package{ + p2 := &spdx.Package{ PackageName: "p2", PackageSPDXIdentifier: common.ElementID("p2"), PackageDownloadLocation: "NOASSERTION", @@ -1232,7 +189,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { }, PackageLicenseDeclared: "NOASSERTION", PackageCopyrightText: "NOASSERTION", - Files: []*v2_3.File{ + Files: []*spdx.File{ f1, f3, f4_2, @@ -1242,7 +199,7 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { } // run the diff between the two packages - diffMap, err := MakePairs2_3(p1, p2) + diffMap, err := MakePairs(p1, p2) if err != nil { t.Fatalf("Expected nil error, got %v", err) } @@ -1327,10 +284,10 @@ func Test2_3DifferCanCreateDiffPairs(t *testing.T) { } } -func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { +func TestDifferCanCreateDiffStructuredResults(t *testing.T) { // create files to be used in diff // f1 will be identical in both - f1 := &v2_3.File{ + f1 := &spdx.File{ FileName: "/project/file1.txt", FileSPDXIdentifier: common.ElementID("File561"), Checksums: []common.Checksum{{ @@ -1346,7 +303,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { } // f2 will only appear in the first Package - f2 := &v2_3.File{ + f2 := &spdx.File{ FileName: "/project/file2.txt", FileSPDXIdentifier: common.ElementID("File562"), Checksums: []common.Checksum{{ @@ -1362,7 +319,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { } // f3 will only appear in the second Package - f3 := &v2_3.File{ + f3 := &spdx.File{ FileName: "/project/file3.txt", FileSPDXIdentifier: common.ElementID("File563"), Checksums: []common.Checksum{{ @@ -1379,7 +336,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { // f4_1 and f4_2 will appear in first and second, // with same name, same hash and different license - f4_1 := &v2_3.File{ + f4_1 := &spdx.File{ FileName: "/project/file4.txt", FileSPDXIdentifier: common.ElementID("File564"), Checksums: []common.Checksum{{ @@ -1393,7 +350,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f4_2 := &v2_3.File{ + f4_2 := &spdx.File{ FileName: "/project/file4.txt", FileSPDXIdentifier: common.ElementID("File564"), Checksums: []common.Checksum{{ @@ -1410,7 +367,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { // f5_1 and f5_2 will appear in first and second, // with same name, different hash and same license - f5_1 := &v2_3.File{ + f5_1 := &spdx.File{ FileName: "/project/file5.txt", FileSPDXIdentifier: common.ElementID("File565"), LicenseConcluded: "BSD-3-Clause", @@ -1419,7 +376,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f5_2 := &v2_3.File{ + f5_2 := &spdx.File{ FileName: "/project/file5.txt", FileSPDXIdentifier: common.ElementID("File565"), Checksums: []common.Checksum{{ @@ -1437,7 +394,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { // f6_1 and f6_2 will appear in first and second, // with same name, different hash and different license - f6_1 := &v2_3.File{ + f6_1 := &spdx.File{ FileName: "/project/file6.txt", FileSPDXIdentifier: common.ElementID("File566"), Checksums: []common.Checksum{{ @@ -1451,7 +408,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { }, FileCopyrightText: "NOASSERTION", } - f6_2 := &v2_3.File{ + f6_2 := &spdx.File{ FileName: "/project/file6.txt", FileSPDXIdentifier: common.ElementID("File566"), Checksums: []common.Checksum{{ @@ -1467,7 +424,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { } // create Packages - p1 := &v2_3.Package{ + p1 := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "NOASSERTION", @@ -1481,7 +438,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { }, PackageLicenseDeclared: "NOASSERTION", PackageCopyrightText: "NOASSERTION", - Files: []*v2_3.File{ + Files: []*spdx.File{ f1, f2, f4_1, @@ -1489,7 +446,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { f6_1, }, } - p2 := &v2_3.Package{ + p2 := &spdx.Package{ PackageName: "p2", PackageSPDXIdentifier: common.ElementID("p2"), PackageDownloadLocation: "NOASSERTION", @@ -1503,7 +460,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { }, PackageLicenseDeclared: "NOASSERTION", PackageCopyrightText: "NOASSERTION", - Files: []*v2_3.File{ + Files: []*spdx.File{ f1, f3, f4_2, @@ -1513,7 +470,7 @@ func Test2_3DifferCanCreateDiffStructuredResults(t *testing.T) { } // run the diff between the two packages - diffMap, err := MakePairs2_3(p1, p2) + diffMap, err := MakePairs(p1, p2) if err != nil { t.Fatalf("Expected nil error, got %v", err) } diff --git a/rdf/reader.go b/rdf/reader.go new file mode 100644 index 00000000..9bc3369e --- /dev/null +++ b/rdf/reader.go @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package rdf + +import ( + "errors" + "fmt" + "io" + + "github.com/spdx/gordf/rdfloader" + gordfParser "github.com/spdx/gordf/rdfloader/parser" + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + v2_2_reader "github.com/spdx/tools-golang/spdx/v2/v2_2/rdf/reader" + "github.com/spdx/tools-golang/spdx/v2/v2_3" + v2_3_reader "github.com/spdx/tools-golang/spdx/v2/v2_3/rdf/reader" +) + +// Read takes an io.Reader and returns a fully-parsed current model SPDX Document +// or an error if any error is encountered. +func Read(content io.Reader) (*spdx.Document, error) { + doc := spdx.Document{} + err := ReadInto(content, &doc) + return &doc, err +} + +// ReadInto takes an io.Reader, reads in the SPDX document at the version provided +// and converts to the doc version +func ReadInto(content io.Reader, doc common.AnyDocument) error { + if !convert.IsPtr(doc) { + return fmt.Errorf("doc to read into must be a pointer") + } + var rdfParserObj, err = rdfloader.LoadFromReaderObject(content) + if err != nil { + return err + } + + version, err := getSpdxVersion(rdfParserObj) + if err != nil { + return err + } + + var data interface{} + switch version { + case v2_2.Version: + data, err = v2_2_reader.LoadFromGoRDFParser(rdfParserObj) + case v2_3.Version: + data, err = v2_3_reader.LoadFromGoRDFParser(rdfParserObj) + default: + return fmt.Errorf("unsupported SPDX version: '%v'", version) + } + + if err != nil { + return err + } + + return convert.Document(data.(common.AnyDocument), doc) +} + +func getSpdxVersion(parser *gordfParser.Parser) (string, error) { + version := "" + for _, node := range parser.Triples { + if node.Predicate.ID == "http://spdx.org/rdf/terms#specVersion" { + version = node.Object.ID + break + } + } + if version == "" { + return "", errors.New("unable to determine version from RDF document") + } + return version, nil +} diff --git a/rdf/reader_test.go b/rdf/reader_test.go new file mode 100644 index 00000000..82449cdc --- /dev/null +++ b/rdf/reader_test.go @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package rdf + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/spdx/tools-golang/spdx" +) + +func Test_Read(t *testing.T) { + fileName := "../examples/sample-docs/rdf/SPDXRdfExample-v2.2.spdx.rdf" + + file, err := os.Open(fileName) + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + got, err := Read(file) + if err != nil { + t.Errorf("rdf.Read() error = %v", err) + return + } + + assert.IsType(t, &spdx.Document{}, got) +} diff --git a/rdfloader/rdfloader.go b/rdfloader/rdfloader.go deleted file mode 100644 index 4575799a..00000000 --- a/rdfloader/rdfloader.go +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package rdfloader - -import ( - "io" - - "github.com/spdx/gordf/rdfloader" - "github.com/spdx/tools-golang/rdfloader/parser2v2" - "github.com/spdx/tools-golang/rdfloader/parser2v3" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -// Takes in a file Reader and returns the pertaining spdx document -// or the error if any is encountered while setting the doc. -func Load2_2(content io.Reader) (*v2_2.Document, error) { - var rdfParserObj, err = rdfloader.LoadFromReaderObject(content) - if err != nil { - return nil, err - } - - doc, err := parser2v2.LoadFromGoRDFParser(rdfParserObj) - return doc, err -} - -// Takes in a file Reader and returns the pertaining spdx document -// or the error if any is encountered while setting the doc. -func Load2_3(content io.Reader) (*v2_3.Document, error) { - var rdfParserObj, err = rdfloader.LoadFromReaderObject(content) - if err != nil { - return nil, err - } - - doc, err := parser2v3.LoadFromGoRDFParser(rdfParserObj) - return doc, err -} diff --git a/reporter/reporter.go b/reporter/reporter.go index 0a492bc6..af883437 100644 --- a/reporter/reporter.go +++ b/reporter/reporter.go @@ -10,147 +10,17 @@ import ( "sort" "text/tabwriter" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" ) -// ===== 2.1 Reporter functions ===== - -// Generate2_1 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_1(pkg *v2_1.Package, w io.Writer) error { - if pkg.FilesAnalyzed == false { - return fmt.Errorf("Package FilesAnalyzed is false") - } - totalFound, totalNotFound, foundCounts := countLicenses2_1(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_1(pkg *v2_1.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 -} - -// ===== 2.2 Reporter functions ===== - -// Generate2_2 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_2(pkg *v2_2.Package, w io.Writer) error { - if pkg.FilesAnalyzed == false { - return fmt.Errorf("Package FilesAnalyzed is false") - } - totalFound, totalNotFound, foundCounts := countLicenses2_2(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_2(pkg *v2_2.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 -} - -// ===== 2.3 Reporter functions ===== - -// Generate2_3 takes a Package whose Files have been analyzed and an +// Generate 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 { +func Generate(pkg *spdx.Package, w io.Writer) error { if pkg.FilesAnalyzed == false { return fmt.Errorf("Package FilesAnalyzed is false") } - totalFound, totalNotFound, foundCounts := countLicenses2_3(pkg) + totalFound, totalNotFound, foundCounts := countLicenses(pkg) wr := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight) @@ -184,7 +54,7 @@ func Generate2_3(pkg *v2_3.Package, w io.Writer) error { return nil } -func countLicenses2_3(pkg *v2_3.Package) (int, int, map[string]int) { +func countLicenses(pkg *spdx.Package) (int, int, map[string]int) { if pkg == nil || pkg.Files == nil { return 0, 0, nil } diff --git a/reporter/reporter_test.go b/reporter/reporter_test.go index 7e6fc885..ee4ba941 100644 --- a/reporter/reporter_test.go +++ b/reporter/reporter_test.go @@ -6,16 +6,13 @@ import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" ) -// ===== 2.1 Reporter top-level function tests ===== -func Test2_1ReporterCanMakeReportFromPackage(t *testing.T) { - pkg := &v2_1.Package{ +func TestReporterCanMakeReportFromPackage(t *testing.T) { + pkg := &spdx.Package{ FilesAnalyzed: true, - Files: []*v2_1.File{ + Files: []*spdx.File{ {FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"}, {FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"}, {FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"}, @@ -43,7 +40,7 @@ func Test2_1ReporterCanMakeReportFromPackage(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := Generate2_1(pkg, &got) + err := Generate(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -55,262 +52,14 @@ func Test2_1ReporterCanMakeReportFromPackage(t *testing.T) { } } -func Test2_1ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) { - pkg := &v2_1.Package{ +func TestReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) { + pkg := &spdx.Package{ FilesAnalyzed: false, } // render as buffer of bytes var got bytes.Buffer - err := Generate2_1(pkg, &got) - if err == nil { - t.Errorf("Expected non-nil error, got nil") - } -} - -// ===== 2.1 Utility functions ===== - -func Test2_1CanGetCountsOfLicenses(t *testing.T) { - pkg := &v2_1.Package{ - FilesAnalyzed: true, - Files: []*v2_1.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_1(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_1NilPackageReturnsZeroCountsOfLicenses(t *testing.T) { - totalFound, totalNotFound, foundCounts := countLicenses2_1(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_1.Package{} - totalFound, totalNotFound, foundCounts = countLicenses2_1(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)) - } -} - -// ===== 2.2 Reporter top-level function tests ===== -func Test2_2ReporterCanMakeReportFromPackage(t *testing.T) { - pkg := &v2_2.Package{ - FilesAnalyzed: true, - Files: []*v2_2.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_2(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_2ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) { - pkg := &v2_2.Package{ - FilesAnalyzed: false, - } - - // render as buffer of bytes - var got bytes.Buffer - err := Generate2_2(pkg, &got) - if err == nil { - t.Errorf("Expected non-nil error, got nil") - } -} - -// ===== 2.2 Utility functions ===== - -func Test2_2CanGetCountsOfLicenses(t *testing.T) { - pkg := &v2_2.Package{ - FilesAnalyzed: true, - Files: []*v2_2.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_2(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_2NilPackageReturnsZeroCountsOfLicenses(t *testing.T) { - totalFound, totalNotFound, foundCounts := countLicenses2_2(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_2.Package{} - totalFound, totalNotFound, foundCounts = countLicenses2_2(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)) - } -} - -// ===== 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) + err := Generate(pkg, &got) if err == nil { t.Errorf("Expected non-nil error, got nil") } @@ -318,10 +67,10 @@ func Test2_3ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) { // ===== 2.3 Utility functions ===== -func Test2_3CanGetCountsOfLicenses(t *testing.T) { - pkg := &v2_3.Package{ +func TestCanGetCountsOfLicenses(t *testing.T) { + pkg := &spdx.Package{ FilesAnalyzed: true, - Files: []*v2_3.File{ + Files: []*spdx.File{ {FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"}, {FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"}, {FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"}, @@ -337,7 +86,7 @@ func Test2_3CanGetCountsOfLicenses(t *testing.T) { }, } - totalFound, totalNotFound, foundCounts := countLicenses2_3(pkg) + totalFound, totalNotFound, foundCounts := countLicenses(pkg) if totalFound != 9 { t.Errorf("expected %v, got %v", 9, totalFound) } @@ -358,8 +107,8 @@ func Test2_3CanGetCountsOfLicenses(t *testing.T) { } } -func Test2_3NilPackageReturnsZeroCountsOfLicenses(t *testing.T) { - totalFound, totalNotFound, foundCounts := countLicenses2_3(nil) +func TestNilPackageReturnsZeroCountsOfLicenses(t *testing.T) { + totalFound, totalNotFound, foundCounts := countLicenses(nil) if totalFound != 0 { t.Errorf("expected %v, got %v", 0, totalFound) } @@ -370,8 +119,8 @@ func Test2_3NilPackageReturnsZeroCountsOfLicenses(t *testing.T) { t.Fatalf("expected %v, got %v", 0, len(foundCounts)) } - pkg := &v2_3.Package{} - totalFound, totalNotFound, foundCounts = countLicenses2_3(pkg) + pkg := &spdx.Package{} + totalFound, totalNotFound, foundCounts = countLicenses(pkg) if totalFound != 0 { t.Errorf("expected %v, got %v", 0, totalFound) } diff --git a/spdx/common/types.go b/spdx/common/types.go new file mode 100644 index 00000000..059d62f2 --- /dev/null +++ b/spdx/common/types.go @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package common + +// AnyDocument a placeholder for allowing any SPDX document to be used in function args +type AnyDocument interface{} diff --git a/spdx/model.go b/spdx/model.go new file mode 100644 index 00000000..b8147ef4 --- /dev/null +++ b/spdx/model.go @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Package spdx contains references to the latest spdx version +package spdx + +import ( + "github.com/spdx/tools-golang/spdx/v2/common" + latest "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +const ( + Version = latest.Version + DataLicense = latest.DataLicense +) + +type ( + Annotation = latest.Annotation + ArtifactOfProject = latest.ArtifactOfProject + CreationInfo = latest.CreationInfo + Document = latest.Document + ExternalDocumentRef = latest.ExternalDocumentRef + File = latest.File + OtherLicense = latest.OtherLicense + Package = latest.Package + PackageExternalReference = latest.PackageExternalReference + Relationship = latest.Relationship + Review = latest.Review + Snippet = latest.Snippet +) + +type ( + Annotator = common.Annotator + Checksum = common.Checksum + ChecksumAlgorithm = common.ChecksumAlgorithm + Creator = common.Creator + DocElementID = common.DocElementID + ElementID = common.ElementID + Originator = common.Originator + PackageVerificationCode = common.PackageVerificationCode + SnippetRange = common.SnippetRange + SnippetRangePointer = common.SnippetRangePointer + Supplier = common.Supplier +) + +const ( + SHA224 = common.SHA224 + SHA1 = common.SHA1 + SHA256 = common.SHA256 + SHA384 = common.SHA384 + SHA512 = common.SHA512 + MD2 = common.MD2 + MD4 = common.MD4 + MD5 = common.MD5 + MD6 = common.MD6 + SHA3_256 = common.SHA3_256 + SHA3_384 = common.SHA3_384 + SHA3_512 = common.SHA3_512 + BLAKE2b_256 = common.BLAKE2b_256 + BLAKE2b_384 = common.BLAKE2b_384 + BLAKE2b_512 = common.BLAKE2b_512 + BLAKE3 = common.BLAKE3 + ADLER32 = common.ADLER32 +) + +const ( + // F.2 Security types + SecurityCPE23Type = common.TypeSecurityCPE23Type + SecurityCPE22Type = common.TypeSecurityCPE22Type + SecurityAdvisory = common.TypeSecurityAdvisory + SecurityFix = common.TypeSecurityFix + SecurityUrl = common.TypeSecurityUrl + SecuritySwid = common.TypeSecuritySwid + + // F.3 Package-Manager types + PackageManagerMavenCentral = common.TypePackageManagerMavenCentral + PackageManagerNpm = common.TypePackageManagerNpm + PackageManagerNuGet = common.TypePackageManagerNuGet + PackageManagerBower = common.TypePackageManagerBower + PackageManagerPURL = common.TypePackageManagerPURL + + // 11.1 Relationship field types + RelationshipDescribes = common.TypeRelationshipDescribe + RelationshipDescribedBy = common.TypeRelationshipDescribeBy + RelationshipContains = common.TypeRelationshipContains + RelationshipContainedBy = common.TypeRelationshipContainedBy + RelationshipDependsOn = common.TypeRelationshipDependsOn + RelationshipDependencyOf = common.TypeRelationshipDependencyOf + RelationshipBuildDependencyOf = common.TypeRelationshipBuildDependencyOf + RelationshipDevDependencyOf = common.TypeRelationshipDevDependencyOf + RelationshipOptionalDependencyOf = common.TypeRelationshipOptionalDependencyOf + RelationshipProvidedDependencyOf = common.TypeRelationshipProvidedDependencyOf + RelationshipTestDependencyOf = common.TypeRelationshipTestDependencyOf + RelationshipRuntimeDependencyOf = common.TypeRelationshipRuntimeDependencyOf + RelationshipExampleOf = common.TypeRelationshipExampleOf + RelationshipGenerates = common.TypeRelationshipGenerates + RelationshipGeneratedFrom = common.TypeRelationshipGeneratedFrom + RelationshipAncestorOf = common.TypeRelationshipAncestorOf + RelationshipDescendantOf = common.TypeRelationshipDescendantOf + RelationshipVariantOf = common.TypeRelationshipVariantOf + RelationshipDistributionArtifact = common.TypeRelationshipDistributionArtifact + RelationshipPatchFor = common.TypeRelationshipPatchFor + RelationshipPatchApplied = common.TypeRelationshipPatchApplied + RelationshipCopyOf = common.TypeRelationshipCopyOf + RelationshipFileAdded = common.TypeRelationshipFileAdded + RelationshipFileDeleted = common.TypeRelationshipFileDeleted + RelationshipFileModified = common.TypeRelationshipFileModified + RelationshipExpandedFromArchive = common.TypeRelationshipExpandedFromArchive + RelationshipDynamicLink = common.TypeRelationshipDynamicLink + RelationshipStaticLink = common.TypeRelationshipStaticLink + RelationshipDataFileOf = common.TypeRelationshipDataFileOf + RelationshipTestCaseOf = common.TypeRelationshipTestCaseOf + RelationshipBuildToolOf = common.TypeRelationshipBuildToolOf + RelationshipDevToolOf = common.TypeRelationshipDevToolOf + RelationshipTestOf = common.TypeRelationshipTestOf + RelationshipTestToolOf = common.TypeRelationshipTestToolOf + RelationshipDocumentationOf = common.TypeRelationshipDocumentationOf + RelationshipOptionalComponentOf = common.TypeRelationshipOptionalComponentOf + RelationshipMetafileOf = common.TypeRelationshipMetafileOf + RelationshipPackageOf = common.TypeRelationshipPackageOf + RelationshipAmends = common.TypeRelationshipAmends + RelationshipPrerequisiteFor = common.TypeRelationshipPrerequisiteFor + RelationshipHasPrerequisite = common.TypeRelationshipHasPrerequisite + RelationshipRequirementDescriptionFor = common.TypeRelationshipRequirementDescriptionFor + RelationshipSpecificationFor = common.TypeRelationshipSpecificationFor + RelationshipOther = common.TypeRelationshipOther +) diff --git a/spdx/common/annotation.go b/spdx/v2/common/annotation.go similarity index 100% rename from spdx/common/annotation.go rename to spdx/v2/common/annotation.go diff --git a/spdx/common/checksum.go b/spdx/v2/common/checksum.go similarity index 91% rename from spdx/common/checksum.go rename to spdx/v2/common/checksum.go index aa2ae52f..d4969ef8 100644 --- a/spdx/common/checksum.go +++ b/spdx/v2/common/checksum.go @@ -5,7 +5,7 @@ package common // ChecksumAlgorithm represents the algorithm used to generate the file checksum in the Checksum struct. type ChecksumAlgorithm string -// The checksum algorithms mentioned in the spdxv2.2.0 https://spdx.github.io/spdx-spec/4-file-information/#44-file-checksum +// The checksum algorithms mentioned in the spec https://spdx.github.io/spdx-spec/4-file-information/#44-file-checksum const ( SHA224 ChecksumAlgorithm = "SHA224" SHA1 ChecksumAlgorithm = "SHA1" diff --git a/spdx/common/creation_info.go b/spdx/v2/common/creation_info.go similarity index 100% rename from spdx/common/creation_info.go rename to spdx/v2/common/creation_info.go diff --git a/spdx/common/external.go b/spdx/v2/common/external.go similarity index 100% rename from spdx/common/external.go rename to spdx/v2/common/external.go diff --git a/spdx/common/identifier.go b/spdx/v2/common/identifier.go similarity index 100% rename from spdx/common/identifier.go rename to spdx/v2/common/identifier.go diff --git a/spdx/common/identifier_test.go b/spdx/v2/common/identifier_test.go similarity index 99% rename from spdx/common/identifier_test.go rename to spdx/v2/common/identifier_test.go index 8df2ea30..0fbb52e0 100644 --- a/spdx/common/identifier_test.go +++ b/spdx/v2/common/identifier_test.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package common import ( diff --git a/spdx/common/package.go b/spdx/v2/common/package.go similarity index 100% rename from spdx/common/package.go rename to spdx/v2/common/package.go diff --git a/spdx/common/snippet.go b/spdx/v2/common/snippet.go similarity index 100% rename from spdx/common/snippet.go rename to spdx/v2/common/snippet.go diff --git a/spdx/v2_1/annotation.go b/spdx/v2/v2_1/annotation.go similarity index 94% rename from spdx/v2_1/annotation.go rename to spdx/v2/v2_1/annotation.go index 45fcd13f..c80f64cf 100644 --- a/spdx/v2_1/annotation.go +++ b/spdx/v2/v2_1/annotation.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Annotation is an Annotation section of an SPDX Document for version 2.1 of the spec. type Annotation struct { diff --git a/spdx/v2_1/creation_info.go b/spdx/v2/v2_1/creation_info.go similarity index 92% rename from spdx/v2_1/creation_info.go rename to spdx/v2/v2_1/creation_info.go index 0e1bd87b..8b6ca182 100644 --- a/spdx/v2_1/creation_info.go +++ b/spdx/v2/v2_1/creation_info.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // CreationInfo is a Document Creation Information section of an // SPDX Document for version 2.1 of the spec. diff --git a/spdx/v2_1/document.go b/spdx/v2/v2_1/document.go similarity index 88% rename from spdx/v2_1/document.go rename to spdx/v2/v2_1/document.go index 81738fff..60a27c44 100644 --- a/spdx/v2_1/document.go +++ b/spdx/v2/v2_1/document.go @@ -3,7 +3,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/anchore/go-struct-converter" + + "github.com/spdx/tools-golang/spdx/v2/common" +) + +const Version = "SPDX-2.1" +const DataLicense = "CC0-1.0" // ExternalDocumentRef is a reference to an external SPDX document // as defined in section 2.6 for version 2.1 of the spec. @@ -63,3 +70,10 @@ type Document struct { // DEPRECATED in version 2.0 of spec Reviews []*Review `json:"-"` } + +func (d *Document) ConvertFrom(_ interface{}) error { + d.SPDXVersion = Version + return nil +} + +var _ converter.ConvertFrom = (*Document)(nil) diff --git a/spdx/v2_1/file.go b/spdx/v2/v2_1/file.go similarity index 98% rename from spdx/v2_1/file.go rename to spdx/v2/v2_1/file.go index 23738876..50bdcf1a 100644 --- a/spdx/v2_1/file.go +++ b/spdx/v2/v2_1/file.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // File is a File section of an SPDX Document for version 2.1 of the spec. type File struct { diff --git a/spdx/v2_1/other_license.go b/spdx/v2/v2_1/other_license.go similarity index 100% rename from spdx/v2_1/other_license.go rename to spdx/v2/v2_1/other_license.go diff --git a/spdx/v2_1/package.go b/spdx/v2/v2_1/package.go similarity index 98% rename from spdx/v2_1/package.go rename to spdx/v2/v2_1/package.go index 6b0d7a5d..85a4c4bc 100644 --- a/spdx/v2_1/package.go +++ b/spdx/v2/v2_1/package.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Package is a Package section of an SPDX Document for version 2.1 of the spec. type Package struct { diff --git a/spdx/v2_1/relationship.go b/spdx/v2/v2_1/relationship.go similarity index 92% rename from spdx/v2_1/relationship.go rename to spdx/v2/v2_1/relationship.go index 006e23fd..827927ae 100644 --- a/spdx/v2_1/relationship.go +++ b/spdx/v2/v2_1/relationship.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Relationship is a Relationship section of an SPDX Document for // version 2.1 of the spec. diff --git a/spdx/v2_1/review.go b/spdx/v2/v2_1/review.go similarity index 100% rename from spdx/v2_1/review.go rename to spdx/v2/v2_1/review.go diff --git a/spdx/v2_1/snippet.go b/spdx/v2/v2_1/snippet.go similarity index 96% rename from spdx/v2_1/snippet.go rename to spdx/v2/v2_1/snippet.go index e4d2f593..9b94fd8d 100644 --- a/spdx/v2_1/snippet.go +++ b/spdx/v2/v2_1/snippet.go @@ -2,7 +2,9 @@ package v2_1 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Snippet is a Snippet section of an SPDX Document for version 2.1 of the spec. type Snippet struct { diff --git a/tvloader/parser2v3/parse_annotation.go b/spdx/v2/v2_1/tagvalue/reader/parse_annotation.go similarity index 90% rename from tvloader/parser2v3/parse_annotation.go rename to spdx/v2/v2_1/tagvalue/reader/parse_annotation.go index 37861baa..0d7e251d 100644 --- a/tvloader/parser2v3/parse_annotation.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_annotation.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" ) -func (parser *tvParser2_3) parsePairForAnnotation2_3(tag string, value string) error { +func (parser *tvParser) parsePairForAnnotation(tag string, value string) error { if parser.ann == nil { return fmt.Errorf("no annotation struct created in parser ann pointer") } diff --git a/spdx/v2/v2_1/tagvalue/reader/parse_annotation_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_annotation_test.go new file mode 100644 index 00000000..f0de5622 --- /dev/null +++ b/spdx/v2/v2_1/tagvalue/reader/parse_annotation_test.go @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" +) + +// ===== Annotation section tests ===== +func TestParserFailsIfAnnotationNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForAnnotation("Annotator", "Person: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected error when calling parsePairFromAnnotation without setting ann pointer") + } +} + +func TestParserFailsIfAnnotationTagUnknown(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // parse invalid tag, using parsePairForAnnotation( + err = parser.parsePairForAnnotation("blah", "oops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("AnnotationDate", "2018-09-15T17:25:00Z") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationDate without Annotator first") + } + err = parser.parsePair("AnnotationType", "REVIEW") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationType without Annotator first") + } + err = parser.parsePair("SPDXREF", "SPDXRef-45") + if err == nil { + t.Errorf("expected error when calling parsePair for SPDXREF without Annotator first") + } + err = parser.parsePair("AnnotationComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationComment without Annotator first") + } +} + +func TestParserCanParseAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Annotator without email address + err := parser.parsePair("Annotator", "Person: John Doe") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.Annotator.Annotator != "John Doe" { + t.Errorf("got %v for Annotator, expected John Doe", parser.ann.Annotator) + } + if parser.ann.Annotator.AnnotatorType != "Person" { + t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) + } + + // Annotation Date + dt := "2018-09-15T17:32:00Z" + err = parser.parsePair("AnnotationDate", dt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationDate != dt { + t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) + } + + // Annotation type + aType := "REVIEW" + err = parser.parsePair("AnnotationType", aType) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationType != aType { + t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) + } + + // SPDX Identifier Reference + ref := "SPDXRef-30" + err = parser.parsePair("SPDXREF", ref) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + deID := parser.ann.AnnotationSPDXIdentifier + if deID.DocumentRefID != "" || deID.ElementRefID != "30" { + t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") + } + + // Annotation Comment + cmt := "this is a comment" + err = parser.parsePair("AnnotationComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationComment != cmt { + t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) + } +} + +func TestParserFailsIfAnnotatorInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotatorTypeInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "Human: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationRefInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + err = parser.parsePair("SPDXREF", "blah:other") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v1/parse_creation_info.go b/spdx/v2/v2_1/tagvalue/reader/parse_creation_info.go similarity index 80% rename from tvloader/parser2v1/parse_creation_info.go rename to spdx/v2/v2_1/tagvalue/reader/parse_creation_info.go index d7ca6876..14c315c0 100644 --- a/tvloader/parser2v1/parse_creation_info.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_creation_info.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromCreationInfo2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromCreationInfo(tag string, value string) error { // fail if not in Creation Info parser state - if parser.st != psCreationInfo2_1 { - return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo2_1", parser.st) + if parser.st != psCreationInfo { + return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo", parser.st) } // create an SPDX Creation Info data struct if we don't have one already @@ -51,49 +51,49 @@ func (parser *tvParser2_1) parsePairFromCreationInfo2_1(tag string, value string // this may be a null case: can we ever have a "last file" in // the "creation info" state? should go on to "file" state // even when parsing unpackaged files. - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_1 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.st = psPackage2_1 + parser.st = psPackage parser.pkg = &v2_1.Package{ FilesAnalyzed: true, IsFilesAnalyzedTagPresent: false, } - return parser.parsePairFromPackage2_1(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to _unpackaged_ file section case "FileName": // leave pkg as nil, so that packages will be placed in Files - parser.st = psFile2_1 + parser.st = psFile parser.pkg = nil - return parser.parsePairFromFile2_1(tag, value) + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_1 - return parser.parsePairFromOtherLicense2_1(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_1 - return parser.parsePairFromReview2_1(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) // for relationship tags, pass along but don't change state case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in CreationInfo section", tag) } diff --git a/tvloader/parser2v1/parse_creation_info_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_creation_info_test.go similarity index 61% rename from tvloader/parser2v1/parse_creation_info_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_creation_info_test.go index 867491af..94ab57f3 100644 --- a/tvloader/parser2v1/parse_creation_info_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_creation_info_test.go @@ -1,26 +1,26 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser creation info state change tests ===== -func TestParser2_1CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } pkgName := "testPkg" - err := parser.parsePair2_1("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } // and a package should be created if parser.pkg == nil { @@ -44,18 +44,18 @@ func TestParser2_1CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_1CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } // and current package should be nil, meaning Files are placed in the // Files map instead of in a Package @@ -64,138 +64,138 @@ func TestParser2_1CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing. } } -func TestParser2_1CIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } } -func TestParser2_1CIMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } } -func TestParser2_1CIStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_1CIStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this spdx file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this spdx file") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_1) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_1FailsParsingCreationInfoWithInvalidState(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psPackage2_1, +func TestParserFailsParsingCreationInfoWithInvalidState(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psPackage, } - err := parser.parsePairFromCreationInfo2_1("SPDXVersion", "SPDX-2.1") + err := parser.parsePairFromCreationInfo("SPDXVersion", "SPDX-2.1") if err == nil { t.Errorf("expected non-nil error, got nil") } } // ===== Creation Info section tests ===== -func TestParser2_1HasCreationInfoAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserHasCreationInfoAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_1("LicenseListVersion", "3.9") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "3.9") if err != nil { - t.Errorf("got error when calling parsePairFromCreationInfo2_1: %v", err) + t.Errorf("got error when calling parsePairFromCreationInfo: %v", err) } if parser.doc.CreationInfo == nil { t.Errorf("doc.CreationInfo is still nil after parsing first pair") } } -func TestParser2_1CanParseCreationInfoTags(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCanParseCreationInfoTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } // License List Version - err := parser.parsePairFromCreationInfo2_1("LicenseListVersion", "2.2") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "2.2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -208,11 +208,11 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { "Person: Person A", "Person: Person B", } - err = parser.parsePairFromCreationInfo2_1("Creator", refPersons[0]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_1("Creator", refPersons[1]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -227,11 +227,11 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { "Organization: Organization A", "Organization: Organization B", } - err = parser.parsePairFromCreationInfo2_1("Creator", refOrgs[0]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_1("Creator", refOrgs[1]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -246,11 +246,11 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { "Tool: Tool A", "Tool: Tool B", } - err = parser.parsePairFromCreationInfo2_1("Creator", refTools[0]) + err = parser.parsePairFromCreationInfo("Creator", refTools[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_1("Creator", refTools[1]) + err = parser.parsePairFromCreationInfo("Creator", refTools[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -261,7 +261,7 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { } // Created date - err = parser.parsePairFromCreationInfo2_1("Created", "2018-09-10T11:46:00Z") + err = parser.parsePairFromCreationInfo("Created", "2018-09-10T11:46:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -270,7 +270,7 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { } // Creator Comment - err = parser.parsePairFromCreationInfo2_1("CreatorComment", "Blah whatever") + err = parser.parsePairFromCreationInfo("CreatorComment", "Blah whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -279,56 +279,56 @@ func TestParser2_1CanParseCreationInfoTags(t *testing.T) { } } -func TestParser2_1InvalidCreatorTagsFail(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserInvalidCreatorTagsFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_1("Creator", "blah: somebody") + err := parser.parsePairFromCreationInfo("Creator", "blah: somebody") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } - err = parser.parsePairFromCreationInfo2_1("Creator", "Tool with no colons") + err = parser.parsePairFromCreationInfo("Creator", "Tool with no colons") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } } -func TestParser2_1CreatorTagWithMultipleColonsPasses(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCreatorTagWithMultipleColonsPasses(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_1("Creator", "Tool: tool1:2:3") + err := parser.parsePairFromCreationInfo("Creator", "Tool: tool1:2:3") if err != nil { t.Errorf("unexpected error from parsing valid Creator format") } } -func TestParser2_1CIUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCIUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_1("blah", "something") + err := parser.parsePairFromCreationInfo("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_1CICreatesRelationship(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCICreatesRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -338,15 +338,15 @@ func TestParser2_1CICreatesRelationship(t *testing.T) { } } -func TestParser2_1CICreatesAnnotation(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, +func TestParserCICreatesAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") diff --git a/tvloader/parser2v1/parse_file.go b/spdx/v2/v2_1/tagvalue/reader/parse_file.go similarity index 81% rename from tvloader/parser2v1/parse_file.go rename to spdx/v2/v2_1/tagvalue/reader/parse_file.go index 8ecc2803..46f494c8 100644 --- a/tvloader/parser2v1/parse_file.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_file.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromFile2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromFile(tag string, value string) error { // expire fileAOP for anything other than an AOPHomePage or AOPURI // (we'll actually handle the HomePage and URI further below) if tag != "ArtifactOfProjectHomePage" && tag != "ArtifactOfProjectURI" { @@ -20,7 +20,7 @@ func (parser *tvParser2_1) parsePairFromFile2_1(tag string, value string) error // tag for creating new file section case "FileName": // check if the previous file contained a spdxId or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_1 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.file = &v2_1.File{} @@ -28,20 +28,20 @@ func (parser *tvParser2_1) parsePairFromFile2_1(tag string, value string) error // tag for creating new package section and going back to parsing Package case "PackageName": // check if the previous file contained a spdxId or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_1 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.st = psPackage2_1 + parser.st = psPackage parser.file = nil - return parser.parsePairFromPackage2_1(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to snippet section case "SnippetSPDXID": - parser.st = psSnippet2_1 - return parser.parsePairFromSnippet2_1(tag, value) + parser.st = psSnippet + return parser.parsePairFromSnippet(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_1 - return parser.parsePairFromOtherLicense2_1(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for file data case "SPDXID": eID, err := extractElementID(value) @@ -111,26 +111,26 @@ func (parser *tvParser2_1) parsePairFromFile2_1(tag string, value string) error case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_1 - return parser.parsePairFromReview2_1(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in File section", tag) } diff --git a/tvloader/parser2v1/parse_file_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_file_test.go similarity index 72% rename from tvloader/parser2v1/parse_file_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_file_test.go index 33f4ef22..e1fa0f21 100644 --- a/tvloader/parser2v1/parse_file_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_file_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser file section state change tests ===== -func TestParser2_1FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { +func TestParserFileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // create the first file fileOldName := "f1.txt" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: fileOldName, FileSPDXIdentifier: "f1"}, } @@ -35,13 +35,13 @@ func TestParser2_1FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // now add a new file fileName := "f2.txt" - err := parser.parsePair2_1("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_1 { - t.Errorf("expected state to be %v, got %v", psFile2_1, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and a file should be created if parser.file == nil { @@ -64,9 +64,9 @@ func TestParser2_1FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } // now parse an SPDX identifier tag - err = parser.parsePair2_1("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // and the Package's Files should now be of size 2 and have this new one if len(parser.pkg.Files) != 2 { @@ -86,22 +86,22 @@ func TestParser2_1FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_1FileAddsToPackageOrUnpackagedFiles(t *testing.T) { +func TestParserFileAddsToPackageOrUnpackagedFiles(t *testing.T) { // start with no packages - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{}, - st: psCreationInfo2_1, + st: psCreationInfo, } // add a file and SPDX identifier fileName := "f2.txt" - err := parser.parsePair2_1("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_1("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } fileOld := parser.file // should have been added to Files @@ -112,21 +112,21 @@ func TestParser2_1FileAddsToPackageOrUnpackagedFiles(t *testing.T) { t.Errorf("expected file %v in Files[f2ID], got %v", fileOld, parser.doc.Files[0]) } // now create a package and a new file - err = parser.parsePair2_1("PackageName", "package1") + err = parser.parsePair("PackageName", "package1") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_1("SPDXID", "SPDXRef-pkg1") + err = parser.parsePair("SPDXID", "SPDXRef-pkg1") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_1("FileName", "f3.txt") + err = parser.parsePair("FileName", "f3.txt") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_1("SPDXID", "SPDXRef-f3ID") + err = parser.parsePair("SPDXID", "SPDXRef-f3ID") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // Files should still be size 1 and have old file only if len(parser.doc.Files) != 1 { @@ -144,14 +144,14 @@ func TestParser2_1FileAddsToPackageOrUnpackagedFiles(t *testing.T) { } } -func TestParser2_1FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserFileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first file and package p1Name := "package1" f1Name := "f1.txt" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: p1Name, PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: f1Name, FileSPDXIdentifier: "f1"}, } @@ -162,13 +162,13 @@ func TestParser2_1FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // now add a new package p2Name := "package2" - err := parser.parsePair2_1("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -216,10 +216,10 @@ func TestParser2_1FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_1FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -227,13 +227,13 @@ func TestParser2_1FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) fileCurrent := parser.file - err := parser.parsePair2_1("SnippetSPDXID", "SPDXRef-Test1") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-Test1") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_1 { - t.Errorf("expected state to be %v, got %v", psSnippet2_1, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current file should remain what it was if parser.file != fileCurrent { @@ -241,135 +241,135 @@ func TestParser2_1FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { } } -func TestParser2_1FileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_1FileMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_1FileStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psFile2_1 { - t.Errorf("expected state to be %v, got %v", psFile2_1, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psFile2_1 { - t.Errorf("expected state to be %v, got %v", psFile2_1, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } } -func TestParser2_1FileStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_1) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } } // ===== File data section tests ===== -func TestParser2_1CanParseFileTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseFileTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -383,7 +383,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } // File SPDX Identifier - err = parser.parsePairFromFile2_1("SPDXID", "SPDXRef-f1") + err = parser.parsePairFromFile("SPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -404,7 +404,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { "DOCUMENTATION", } for _, ty := range fileTypes { - err = parser.parsePairFromFile2_1("FileType", ty) + err = parser.parsePairFromFile("FileType", ty) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -432,15 +432,15 @@ func TestParser2_1CanParseFileTags(t *testing.T) { sumSha256 := "SHA256: 11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd" codeMd5 := "624c1abb3664f4b35547e7c73864ad24" sumMd5 := "MD5: 624c1abb3664f4b35547e7c73864ad24" - err = parser.parsePairFromFile2_1("FileChecksum", sumSha1) + err = parser.parsePairFromFile("FileChecksum", sumSha1) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("FileChecksum", sumSha256) + err = parser.parsePairFromFile("FileChecksum", sumSha256) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("FileChecksum", sumMd5) + err = parser.parsePairFromFile("FileChecksum", sumMd5) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -461,7 +461,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } } // Concluded License - err = parser.parsePairFromFile2_1("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromFile("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -476,7 +476,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromFile2_1("LicenseInfoInFile", lic) + err = parser.parsePairFromFile("LicenseInfoInFile", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -498,7 +498,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } // Comments on License - err = parser.parsePairFromFile2_1("LicenseComments", "this is a comment") + err = parser.parsePairFromFile("LicenseComments", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -507,7 +507,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } // Copyright Text - err = parser.parsePairFromFile2_1("FileCopyrightText", "copyright (c) me") + err = parser.parsePairFromFile("FileCopyrightText", "copyright (c) me") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -517,38 +517,38 @@ func TestParser2_1CanParseFileTags(t *testing.T) { // Artifact of Projects: Name, HomePage and URI // Artifact set 1 - err = parser.parsePairFromFile2_1("ArtifactOfProjectName", "project1") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 2 -- just name - err = parser.parsePairFromFile2_1("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 3 -- just name and home page - err = parser.parsePairFromFile2_1("ArtifactOfProjectName", "project3") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project3") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("ArtifactOfProjectHomePage", "http://example.com/3/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/3/") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 4 -- just name and URI - err = parser.parsePairFromFile2_1("ArtifactOfProjectName", "project4") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project4") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_1("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -602,7 +602,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } // File Comment - err = parser.parsePairFromFile2_1("FileComment", "this is a comment") + err = parser.parsePairFromFile("FileComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -611,7 +611,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } // File Notice - err = parser.parsePairFromFile2_1("FileNotice", "this is a Notice") + err = parser.parsePairFromFile("FileNotice", "this is a Notice") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -625,7 +625,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { "EvilCorp", } for _, contrib := range contribs { - err = parser.parsePairFromFile2_1("FileContributor", contrib) + err = parser.parsePairFromFile("FileContributor", contrib) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -652,7 +652,7 @@ func TestParser2_1CanParseFileTags(t *testing.T) { "g.txt", } for _, dep := range deps { - err = parser.parsePairFromFile2_1("FileDependency", dep) + err = parser.parsePairFromFile("FileDependency", dep) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -675,19 +675,19 @@ func TestParser2_1CanParseFileTags(t *testing.T) { } -func TestParser2_1FileCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -697,19 +697,19 @@ func TestParser2_1FileCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_1FileCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -719,33 +719,33 @@ func TestParser2_1FileCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_1FileUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ +func TestParserFileUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_1("blah", "something") + err := parser.parsePairFromFile("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } func TestFileAOPPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_1("ArtifactOfProjectName", "project1") + err := parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -755,7 +755,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { curPtr := parser.fileAOP // now, a home page; pointer should stay - err = parser.parsePairFromFile2_1("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -764,7 +764,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // a URI; pointer should stay - err = parser.parsePairFromFile2_1("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -774,7 +774,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { // now, another artifact name; pointer should change but be non-nil // now, a home page; pointer should stay - err = parser.parsePairFromFile2_1("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -783,7 +783,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // finally, an unrelated tag; pointer should go away - err = parser.parsePairFromFile2_1("FileComment", "whatever") + err = parser.parsePairFromFile("FileComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -792,115 +792,115 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_1FailsIfInvalidSPDXIDInFileSection(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidSPDXIDInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid SPDX Identifier - err = parser.parsePairFromFile2_1("SPDXID", "whoops") + err = parser.parsePairFromFile("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfInvalidChecksumFormatInFileSection(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidChecksumFormatInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid format for checksum line, missing colon - err = parser.parsePairFromFile2_1("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfUnknownChecksumTypeInFileSection(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfUnknownChecksumTypeInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // unknown checksum type - err = parser.parsePairFromFile2_1("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_1("ArtifactOfProjectHomePage", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfArtifactURIBeforeArtifactName(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfArtifactURIBeforeArtifactName(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_1("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_1("ArtifactOfProjectURI", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FilesWithoutSpdxIdThrowError(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowError(t *testing.T) { // case 1: The previous file (packaged or unpackaged) does not contain spdxID - parser1 := tvParser2_1{ + parser1 := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, file: &v2_1.File{FileName: "FileName"}, } - err := parser1.parsePair2_1("FileName", "f2") + err := parser1.parsePair("FileName", "f2") if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -909,12 +909,12 @@ func TestParser2_1FilesWithoutSpdxIdThrowError(t *testing.T) { // Last unpackaged file before a snippet starts sid1 := common.ElementID("s1") fileName := "f2.txt" - parser2 := tvParser2_1{ + parser2 := tvParser{ doc: &v2_1.Document{}, - st: psCreationInfo2_1, + st: psCreationInfo, file: &v2_1.File{FileName: fileName}, } - err = parser2.parsePair2_1("SnippetSPDXID", string(sid1)) + err = parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -922,16 +922,16 @@ func TestParser2_1FilesWithoutSpdxIdThrowError(t *testing.T) { // case 3 : Invalid File without snippets // Last unpackaged file before a package starts // Last file of a package and New package starts - parser3 := tvParser2_1{ + parser3 := tvParser{ doc: &v2_1.Document{}, - st: psCreationInfo2_1, + st: psCreationInfo, } fileName = "f3.txt" - err = parser3.parsePair2_1("FileName", fileName) + err = parser3.parsePair("FileName", fileName) if err != nil { t.Errorf("%s", err) } - err = parser3.parsePair2_1("PackageName", "p2") + err = parser3.parsePair("PackageName", "p2") if err == nil { t.Errorf("files withoutSpdx Identifier getting accepted") } diff --git a/tvloader/parser2v1/parse_other_license.go b/spdx/v2/v2_1/tagvalue/reader/parse_other_license.go similarity index 68% rename from tvloader/parser2v1/parse_other_license.go rename to spdx/v2/v2_1/tagvalue/reader/parse_other_license.go index a2c3d00c..3ebeb496 100644 --- a/tvloader/parser2v1/parse_other_license.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_other_license.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromOtherLicense2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromOtherLicense(tag string, value string) error { switch tag { // tag for creating new other license section case "LicenseID": @@ -27,26 +27,26 @@ func (parser *tvParser2_1) parsePairFromOtherLicense2_1(tag string, value string case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_1 - return parser.parsePairFromReview2_1(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in OtherLicense section", tag) } diff --git a/tvloader/parser2v1/parse_other_license_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_other_license_test.go similarity index 70% rename from tvloader/parser2v1/parse_other_license_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_other_license_test.go index b1b74715..0d1e3e74 100644 --- a/tvloader/parser2v1/parse_other_license_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_other_license_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser other license section state change tests ===== -func TestParser2_1OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { +func TestParserOLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { // create the first other license olid1 := "LicenseRef-Lic11" olname1 := "License 11" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -39,26 +39,26 @@ func TestParser2_1OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) // now add a new other license olid2 := "LicenseRef-22" olname2 := "License 22" - err := parser.parsePair2_1("LicenseID", olid2) + err := parser.parsePair("LicenseID", olid2) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and an other license should be created if parser.otherLic == nil { t.Fatalf("parser didn't create new other license") } // also parse the new license's name - err = parser.parsePair2_1("LicenseName", olname2) + err = parser.parsePair("LicenseName", olname2) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still be correct - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the other license name should be as expected if parser.otherLic.LicenseName != olname2 { @@ -88,10 +88,10 @@ func TestParser2_1OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) } } -func TestParser2_1OLMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserOLMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -99,19 +99,19 @@ func TestParser2_1OLMovesToReviewAfterParsingReviewerTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_1("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_1OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserOtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -123,13 +123,13 @@ func TestParser2_1OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -140,20 +140,20 @@ func TestParser2_1OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_1OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserOtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -165,44 +165,44 @@ func TestParser2_1OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_1) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } // and the annotation should be in the Document's Annotations @@ -214,10 +214,10 @@ func TestParser2_1OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_1OLFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserOLFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -230,25 +230,25 @@ func TestParser2_1OLFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // can't go back to old sections - err := parser.parsePair2_1("SPDXVersion", "SPDX-2.1") + err := parser.parsePair("SPDXVersion", "SPDX-2.1") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_1("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_1("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Other License data section tests ===== -func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseOtherLicenseTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -257,7 +257,7 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // License Identifier - err := parser.parsePairFromOtherLicense2_1("LicenseID", "LicenseRef-Lic11") + err := parser.parsePairFromOtherLicense("LicenseID", "LicenseRef-Lic11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -266,7 +266,7 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { } // Extracted Text - err = parser.parsePairFromOtherLicense2_1("ExtractedText", "You are permitted to do anything with the software, hooray!") + err = parser.parsePairFromOtherLicense("ExtractedText", "You are permitted to do anything with the software, hooray!") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -275,7 +275,7 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { } // License Name - err = parser.parsePairFromOtherLicense2_1("LicenseName", "License 11") + err = parser.parsePairFromOtherLicense("LicenseName", "License 11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -290,7 +290,7 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { "https://example.com/3", } for _, cr := range crossRefs { - err = parser.parsePairFromOtherLicense2_1("LicenseCrossReference", cr) + err = parser.parsePairFromOtherLicense("LicenseCrossReference", cr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -312,7 +312,7 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { } // License Comment - err = parser.parsePairFromOtherLicense2_1("LicenseComment", "this is a comment") + err = parser.parsePairFromOtherLicense("LicenseComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -321,10 +321,10 @@ func TestParser2_1CanParseOtherLicenseTags(t *testing.T) { } } -func TestParser2_1OLUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ +func TestParserOLUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psOtherLicense2_1, + st: psOtherLicense, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -332,7 +332,7 @@ func TestParser2_1OLUnknownTagFails(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePairFromOtherLicense2_1("blah", "something") + err := parser.parsePairFromOtherLicense("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v1/parse_package.go b/spdx/v2/v2_1/tagvalue/reader/parse_package.go similarity index 88% rename from tvloader/parser2v1/parse_package.go rename to spdx/v2/v2_1/tagvalue/reader/parse_package.go index 7a3e2a82..4d4c4918 100644 --- a/tvloader/parser2v1/parse_package.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_package.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromPackage2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromPackage(tag string, value string) error { // expire pkgExtRef for anything other than a comment // (we'll actually handle the comment further below) if tag != "ExternalRefComment" { @@ -22,7 +22,7 @@ func (parser *tvParser2_1) parsePairFromPackage2_1(tag string, value string) err // if package already has a name, create and go on to a new package if parser.pkg == nil || parser.pkg.PackageName != "" { // check if the previous package contained an spdxId or not - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_1 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } parser.pkg = &v2_1.Package{ @@ -33,12 +33,12 @@ func (parser *tvParser2_1) parsePairFromPackage2_1(tag string, value string) err parser.pkg.PackageName = value // tag for going on to file section case "FileName": - parser.st = psFile2_1 - return parser.parsePairFromFile2_1(tag, value) + parser.st = psFile + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_1 - return parser.parsePairFromOtherLicense2_1(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) case "SPDXID": eID, err := extractElementID(value) if err != nil { @@ -154,26 +154,26 @@ func (parser *tvParser2_1) parsePairFromPackage2_1(tag string, value string) err case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_1 - return parser.parsePairFromReview2_1(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Package section", tag) } diff --git a/tvloader/parser2v1/parse_package_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_package_test.go similarity index 72% rename from tvloader/parser2v1/parse_package_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_package_test.go index 052f5355..7b28a64f 100644 --- a/tvloader/parser2v1/parse_package_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_package_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser package section state change tests ===== -func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first package pkgOldName := "p1" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: pkgOldName, PackageSPDXIdentifier: "p1"}, } pkgOld := parser.pkg @@ -23,13 +23,13 @@ func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package pkgName := "p2" - err := parser.parsePair2_1("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -59,12 +59,12 @@ func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { // pkg is nil, so that Files appearing before the first PackageName tag // are added to Files instead of Packages - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psFile2_1, + st: psFile, pkg: nil, } // the Document's Packages should be empty @@ -74,13 +74,13 @@ func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack // now add a new package pkgName := "p2" - err := parser.parsePair2_1("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -104,22 +104,22 @@ func TestParser2_1PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack } } -func TestParser2_1PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageMovesToFileAfterParsingFileNameTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) pkgCurrent := parser.pkg - err := parser.parsePair2_1("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_1 { - t.Errorf("expected state to be %v, got %v", psFile2_1, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and current package should remain what it was if parser.pkg != pkgCurrent { @@ -127,121 +127,121 @@ func TestParser2_1PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_1PackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_1PackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_1PackageStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } } -func TestParser2_1PackageStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this package") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this package") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_1) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } } // ===== Package data section tests ===== -func TestParser2_1CanParsePackageTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } @@ -251,7 +251,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -264,7 +264,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package SPDX Identifier - err = parser.parsePairFromPackage2_1("SPDXID", "SPDXRef-p1") + err = parser.parsePairFromPackage("SPDXID", "SPDXRef-p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -281,7 +281,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Version - err = parser.parsePairFromPackage2_1("PackageVersion", "2.1.1") + err = parser.parsePairFromPackage("PackageVersion", "2.1.1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -290,7 +290,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package File Name - err = parser.parsePairFromPackage2_1("PackageFileName", "p1-2.1.1.tar.gz") + err = parser.parsePairFromPackage("PackageFileName", "p1-2.1.1.tar.gz") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -305,7 +305,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { // SKIP -- separate tests for subvalues below // Package Download Location - err = parser.parsePairFromPackage2_1("PackageDownloadLocation", "https://example.com/whatever") + err = parser.parsePairFromPackage("PackageDownloadLocation", "https://example.com/whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -314,7 +314,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Files Analyzed - err = parser.parsePairFromPackage2_1("FilesAnalyzed", "false") + err = parser.parsePairFromPackage("FilesAnalyzed", "false") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -335,15 +335,15 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { sumSha256 := "SHA256: 11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd" codeMd5 := "624c1abb3664f4b35547e7c73864ad24" sumMd5 := "MD5: 624c1abb3664f4b35547e7c73864ad24" - err = parser.parsePairFromPackage2_1("PackageChecksum", sumSha1) + err = parser.parsePairFromPackage("PackageChecksum", sumSha1) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromPackage2_1("PackageChecksum", sumSha256) + err = parser.parsePairFromPackage("PackageChecksum", sumSha256) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromPackage2_1("PackageChecksum", sumMd5) + err = parser.parsePairFromPackage("PackageChecksum", sumMd5) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -366,7 +366,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Home Page - err = parser.parsePairFromPackage2_1("PackageHomePage", "https://example.com/whatever2") + err = parser.parsePairFromPackage("PackageHomePage", "https://example.com/whatever2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -375,7 +375,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Source Info - err = parser.parsePairFromPackage2_1("PackageSourceInfo", "random comment") + err = parser.parsePairFromPackage("PackageSourceInfo", "random comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -384,7 +384,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package License Concluded - err = parser.parsePairFromPackage2_1("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -399,7 +399,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromPackage2_1("PackageLicenseInfoFromFiles", lic) + err = parser.parsePairFromPackage("PackageLicenseInfoFromFiles", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -421,7 +421,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package License Declared - err = parser.parsePairFromPackage2_1("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -430,7 +430,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package License Comments - err = parser.parsePairFromPackage2_1("PackageLicenseComments", "this is a license comment") + err = parser.parsePairFromPackage("PackageLicenseComments", "this is a license comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -439,7 +439,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Copyright Text - err = parser.parsePairFromPackage2_1("PackageCopyrightText", "Copyright (c) me myself and i") + err = parser.parsePairFromPackage("PackageCopyrightText", "Copyright (c) me myself and i") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -448,7 +448,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Summary - err = parser.parsePairFromPackage2_1("PackageSummary", "i wrote this package") + err = parser.parsePairFromPackage("PackageSummary", "i wrote this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -457,7 +457,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Description - err = parser.parsePairFromPackage2_1("PackageDescription", "i wrote this package a lot") + err = parser.parsePairFromPackage("PackageDescription", "i wrote this package a lot") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -466,7 +466,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } // Package Comment - err = parser.parsePairFromPackage2_1("PackageComment", "i scanned this package") + err = parser.parsePairFromPackage("PackageComment", "i scanned this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -485,7 +485,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { ref2Type := "LocationRef-acmeforge" ref2Locator := "acmecorp/acmenator/4.1.3alpha" ref2Comment := "this is comment #2" - err = parser.parsePairFromPackage2_1("ExternalRef", ref1) + err = parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -501,11 +501,11 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[0] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[0], got no match") } - err = parser.parsePairFromPackage2_1("ExternalRefComment", ref1Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref1Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromPackage2_1("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -521,7 +521,7 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[1] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[1], got no match") } - err = parser.parsePairFromPackage2_1("ExternalRefComment", ref2Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref2Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -555,16 +555,16 @@ func TestParser2_1CanParsePackageTags(t *testing.T) { } -func TestParser2_1CanParsePackageSupplierPersonTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageSupplierPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Person - err := parser.parsePairFromPackage2_1("PackageSupplier", "Person: John Doe") + err := parser.parsePairFromPackage("PackageSupplier", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -573,16 +573,16 @@ func TestParser2_1CanParsePackageSupplierPersonTag(t *testing.T) { } } -func TestParser2_1CanParsePackageSupplierOrganizationTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageSupplierOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Organization - err := parser.parsePairFromPackage2_1("PackageSupplier", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageSupplier", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -591,16 +591,16 @@ func TestParser2_1CanParsePackageSupplierOrganizationTag(t *testing.T) { } } -func TestParser2_1CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageSupplierNOASSERTIONTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: NOASSERTION - err := parser.parsePairFromPackage2_1("PackageSupplier", "NOASSERTION") + err := parser.parsePairFromPackage("PackageSupplier", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -609,16 +609,16 @@ func TestParser2_1CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { } } -func TestParser2_1CanParsePackageOriginatorPersonTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageOriginatorPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Person - err := parser.parsePairFromPackage2_1("PackageOriginator", "Person: John Doe") + err := parser.parsePairFromPackage("PackageOriginator", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -627,16 +627,16 @@ func TestParser2_1CanParsePackageOriginatorPersonTag(t *testing.T) { } } -func TestParser2_1CanParsePackageOriginatorOrganizationTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageOriginatorOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Organization - err := parser.parsePairFromPackage2_1("PackageOriginator", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageOriginator", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -645,16 +645,16 @@ func TestParser2_1CanParsePackageOriginatorOrganizationTag(t *testing.T) { } } -func TestParser2_1CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: NOASSERTION - err := parser.parsePairFromPackage2_1("PackageOriginator", "NOASSERTION") + err := parser.parsePairFromPackage("PackageOriginator", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -663,10 +663,10 @@ func TestParser2_1CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { } } -func TestParser2_1CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) @@ -675,7 +675,7 @@ func TestParser2_1CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { code := "d6a770ba38583ed4bb4525bd96e50461655d2758" fileName := "./package.spdx" fullCodeValue := "d6a770ba38583ed4bb4525bd96e50461655d2758 (excludes: ./package.spdx)" - err := parser.parsePairFromPackage2_1("PackageVerificationCode", fullCodeValue) + err := parser.parsePairFromPackage("PackageVerificationCode", fullCodeValue) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -688,17 +688,17 @@ func TestParser2_1CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { } -func TestParser2_1CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Verification Code without excludes parenthetical code := "d6a770ba38583ed4bb4525bd96e50461655d2758" - err := parser.parsePairFromPackage2_1("PackageVerificationCode", code) + err := parser.parsePairFromPackage("PackageVerificationCode", code) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -712,15 +712,15 @@ func TestParser2_1CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T } func TestPackageExternalRefPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) ref1 := "SECURITY cpe23Type cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*" - err := parser.parsePairFromPackage2_1("ExternalRef", ref1) + err := parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -729,7 +729,7 @@ func TestPackageExternalRefPointerChangesAfterTags(t *testing.T) { } // now, a comment; pointer should go away - err = parser.parsePairFromPackage2_1("ExternalRefComment", "whatever") + err = parser.parsePairFromPackage("ExternalRefComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -738,7 +738,7 @@ func TestPackageExternalRefPointerChangesAfterTags(t *testing.T) { } ref2 := "Other LocationRef-something https://example.com/whatever" - err = parser.parsePairFromPackage2_1("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -747,7 +747,7 @@ func TestPackageExternalRefPointerChangesAfterTags(t *testing.T) { } // and some other random tag makes the pointer go away too - err = parser.parsePairFromPackage2_1("PackageSummary", "whatever") + err = parser.parsePairFromPackage("PackageSummary", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -756,17 +756,17 @@ func TestPackageExternalRefPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_1PackageCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -776,17 +776,17 @@ func TestParser2_1PackageCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_1PackageCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -796,129 +796,129 @@ func TestParser2_1PackageCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_1PackageUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ +func TestParserPackageUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePairFromPackage2_1("blah", "something") + err := parser.parsePairFromPackage("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_1FailsIfInvalidSPDXIDInPackageSection(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidSPDXIDInPackageSection(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid ID format - err = parser.parsePairFromPackage2_1("SPDXID", "whoops") + err = parser.parsePairFromPackage("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfInvalidPackageSupplierFormat(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidPackageSupplierFormat(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier format - err = parser.parsePairFromPackage2_1("PackageSupplier", "whoops") + err = parser.parsePairFromPackage("PackageSupplier", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfUnknownPackageSupplierType(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfUnknownPackageSupplierType(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier type - err = parser.parsePairFromPackage2_1("PackageSupplier", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageSupplier", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfInvalidPackageOriginatorFormat(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidPackageOriginatorFormat(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator format - err = parser.parsePairFromPackage2_1("PackageOriginator", "whoops") + err = parser.parsePairFromPackage("PackageOriginator", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfUnknownPackageOriginatorType(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfUnknownPackageOriginatorType(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator type - err = parser.parsePairFromPackage2_1("PackageOriginator", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageOriginator", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1SetsFilesAnalyzedTagsCorrectly(t *testing.T) { - parser := tvParser2_1{ +func TestParserSetsFilesAnalyzedTagsCorrectly(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // set tag - err = parser.parsePairFromPackage2_1("FilesAnalyzed", "true") + err = parser.parsePairFromPackage("FilesAnalyzed", "true") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -930,77 +930,77 @@ func TestParser2_1SetsFilesAnalyzedTagsCorrectly(t *testing.T) { } } -func TestParser2_1FailsIfInvalidPackageChecksumFormat(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidPackageChecksumFormat(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum format - err = parser.parsePairFromPackage2_1("PackageChecksum", "whoops") + err = parser.parsePairFromPackage("PackageChecksum", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfInvalidPackageChecksumType(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidPackageChecksumType(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum type - err = parser.parsePairFromPackage2_1("PackageChecksum", "whoops: blah") + err = parser.parsePairFromPackage("PackageChecksum", "whoops: blah") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfInvalidExternalRefFormat(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfInvalidExternalRefFormat(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid external ref format - err = parser.parsePairFromPackage2_1("ExternalRef", "whoops") + err = parser.parsePairFromPackage("ExternalRef", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_1("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // external ref comment before external ref - err = parser.parsePairFromPackage2_1("ExternalRefComment", "whoops") + err = parser.parsePairFromPackage("ExternalRefComment", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } @@ -1071,12 +1071,12 @@ func TestFailsPackageExternalRefWithInvalidFormat(t *testing.T) { } } -func TestParser2_1PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { +func TestParserPackageWithoutSpdxIdentifierThrowsError(t *testing.T) { // More than one package, the previous package doesn't contain the SPDXID pkgOldName := "p1" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psPackage2_1, + st: psPackage, pkg: &v2_1.Package{PackageName: pkgOldName}, } pkgOld := parser.pkg @@ -1090,7 +1090,7 @@ func TestParser2_1PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { } pkgName := "p2" - err := parser.parsePair2_1("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err == nil { t.Errorf("package without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v1/parse_relationship.go b/spdx/v2/v2_1/tagvalue/reader/parse_relationship.go similarity index 90% rename from tvloader/parser2v1/parse_relationship.go rename to spdx/v2/v2_1/tagvalue/reader/parse_relationship.go index 2b9b8ccb..e2cc5bba 100644 --- a/tvloader/parser2v1/parse_relationship.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_relationship.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" "strings" ) -func (parser *tvParser2_1) parsePairForRelationship2_1(tag string, value string) error { +func (parser *tvParser) parsePairForRelationship(tag string, value string) error { if parser.rln == nil { return fmt.Errorf("no relationship struct created in parser rln pointer") } diff --git a/spdx/v2/v2_1/tagvalue/reader/parse_relationship_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_relationship_test.go new file mode 100644 index 00000000..801ab105 --- /dev/null +++ b/spdx/v2/v2_1/tagvalue/reader/parse_relationship_test.go @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" +) + +// ===== Relationship section tests ===== +func TestParserFailsIfRelationshipNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForRelationship("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") + if err == nil { + t.Errorf("expected error when calling parsePairFromRelationship without setting rln pointer") + } +} + +func TestParserFailsIfRelationshipCommentWithoutRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("RelationshipComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for RelationshipComment without Relationship first") + } +} + +func TestParserCanParseRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { + t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) + } + if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { + t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) + } + if parser.rln.Relationship != "CONTAINS" { + t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) + } + + // Relationship Comment + cmt := "this is a comment" + err = parser.parsePair("RelationshipComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RelationshipComment != cmt { + t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) + } +} + +func TestParserInvalidRelationshipTagsNoValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // no items + parser.rln = nil + err := parser.parsePair("Relationship", "") + if err == nil { + t.Errorf("expected error for empty items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsOneValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // one item + parser.rln = nil + err := parser.parsePair("Relationship", "DESCRIBES") + if err == nil { + t.Errorf("expected error for only one item in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsTwoValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // two items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-DOCUMENT DESCRIBES") + if err == nil { + t.Errorf("expected error for only two items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // three items but with interspersed additional whitespace + parser.rln = nil + err := parser.parsePair("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") + if err != nil { + t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) + } +} + +func TestParserInvalidRelationshipTagsFourValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") + if err == nil { + t.Errorf("expected error for more than three items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsInvalidRefIDs(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } + + parser.rln = nil + err = parser.parsePair("Relationship", "a DESCRIBES SPDXRef-b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } +} + +func TestParserFailsToParseUnknownTagInRelationshipSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // invalid tag + err = parser.parsePairForRelationship("blah", "whoops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v1/parse_review.go b/spdx/v2/v2_1/tagvalue/reader/parse_review.go similarity index 72% rename from tvloader/parser2v1/parse_review.go rename to spdx/v2/v2_1/tagvalue/reader/parse_review.go index b241a223..57ffa004 100644 --- a/tvloader/parser2v1/parse_review.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_review.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromReview2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromReview(tag string, value string) error { switch tag { // tag for creating new review section case "Reviewer": @@ -39,22 +39,22 @@ func (parser *tvParser2_1) parsePairFromReview2_1(tag string, value string) erro case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in Review section", tag) } diff --git a/tvloader/parser2v1/parse_review_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_review_test.go similarity index 74% rename from tvloader/parser2v1/parse_review_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_review_test.go index c3cea2dd..0a4c23cd 100644 --- a/tvloader/parser2v1/parse_review_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_review_test.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser review section state change tests ===== -func TestParser2_1ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { +func TestParserReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // create the first review rev1 := "John Doe" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -45,13 +45,13 @@ func TestParser2_1ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // now add a new review rev2 := "Steve" rp2 := "Person: Steve" - err := parser.parsePair2_1("Reviewer", rp2) + err := parser.parsePair("Reviewer", rp2) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and a review should be created if parser.rev == nil { @@ -80,10 +80,10 @@ func TestParser2_1ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { } -func TestParser2_1ReviewStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserReviewStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -100,13 +100,13 @@ func TestParser2_1ReviewStaysAfterParsingRelationshipTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -117,20 +117,20 @@ func TestParser2_1ReviewStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_1ReviewStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserReviewStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -147,44 +147,44 @@ func TestParser2_1ReviewStaysAfterParsingAnnotationTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_1) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } // and the annotation should be in the Document's Annotations @@ -196,10 +196,10 @@ func TestParser2_1ReviewStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_1ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserReviewFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -214,29 +214,29 @@ func TestParser2_1ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // can't go back to old sections - err := parser.parsePair2_1("SPDXVersion", "SPDX-2.1") + err := parser.parsePair("SPDXVersion", "SPDX-2.1") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_1("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_1("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_1("LicenseID", "LicenseRef-Lic22") + err = parser.parsePair("LicenseID", "LicenseRef-Lic22") if err == nil { - t.Errorf("expected error when calling parsePair2_1, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Review data section tests ===== -func TestParser2_1CanParseReviewTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseReviewTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -254,7 +254,7 @@ func TestParser2_1CanParseReviewTags(t *testing.T) { // handled in subsequent subtests // Review Date (DEPRECATED) - err := parser.parsePairFromReview2_1("ReviewDate", "2018-09-23T08:30:00Z") + err := parser.parsePairFromReview("ReviewDate", "2018-09-23T08:30:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -263,7 +263,7 @@ func TestParser2_1CanParseReviewTags(t *testing.T) { } // Review Comment (DEPRECATED) - err = parser.parsePairFromReview2_1("ReviewComment", "this is a comment") + err = parser.parsePairFromReview("ReviewComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -272,10 +272,10 @@ func TestParser2_1CanParseReviewTags(t *testing.T) { } } -func TestParser2_1CanParseReviewerPersonTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseReviewerPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -290,7 +290,7 @@ func TestParser2_1CanParseReviewerPersonTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Person - err := parser.parsePairFromReview2_1("Reviewer", "Person: John Doe") + err := parser.parsePairFromReview("Reviewer", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -302,10 +302,10 @@ func TestParser2_1CanParseReviewerPersonTag(t *testing.T) { } } -func TestParser2_1CanParseReviewerOrganizationTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseReviewerOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -320,7 +320,7 @@ func TestParser2_1CanParseReviewerOrganizationTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Organization - err := parser.parsePairFromReview2_1("Reviewer", "Organization: John Doe, Inc.") + err := parser.parsePairFromReview("Reviewer", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -332,10 +332,10 @@ func TestParser2_1CanParseReviewerOrganizationTag(t *testing.T) { } } -func TestParser2_1CanParseReviewerToolTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseReviewerToolTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -350,7 +350,7 @@ func TestParser2_1CanParseReviewerToolTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Tool - err := parser.parsePairFromReview2_1("Reviewer", "Tool: scannertool - 1.2.12") + err := parser.parsePairFromReview("Reviewer", "Tool: scannertool - 1.2.12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -362,38 +362,38 @@ func TestParser2_1CanParseReviewerToolTag(t *testing.T) { } } -func TestParser2_1FailsIfReviewerInvalidFormat(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfReviewerInvalidFormat(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, rev: &v2_1.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_1("Reviewer", "oops") + err := parser.parsePairFromReview("Reviewer", "oops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfReviewerUnknownType(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsIfReviewerUnknownType(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, rev: &v2_1.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_1("Reviewer", "whoops: John Doe") + err := parser.parsePairFromReview("Reviewer", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1ReviewUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ +func TestParserReviewUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psReview2_1, + st: psReview, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_1.OtherLicense{ @@ -407,7 +407,7 @@ func TestParser2_1ReviewUnknownTagFails(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_1("blah", "something") + err := parser.parsePairFromReview("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v1/parse_snippet.go b/spdx/v2/v2_1/tagvalue/reader/parse_snippet.go similarity index 80% rename from tvloader/parser2v1/parse_snippet.go rename to spdx/v2/v2_1/tagvalue/reader/parse_snippet.go index ad4c74f0..77b9926a 100644 --- a/tvloader/parser2v1/parse_snippet.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_snippet.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" "strconv" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func (parser *tvParser2_1) parsePairFromSnippet2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromSnippet(tag string, value string) error { switch tag { // tag for creating new snippet section case "SnippetSPDXID": // check here whether the previous file contained an SPDX ID or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_1 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.snippet = &v2_1.Snippet{} @@ -33,19 +33,19 @@ func (parser *tvParser2_1) parsePairFromSnippet2_1(tag string, value string) err parser.snippet.SnippetSPDXIdentifier = eID // tag for creating new file section and going back to parsing File case "FileName": - parser.st = psFile2_1 + parser.st = psFile parser.snippet = nil - return parser.parsePairFromFile2_1(tag, value) + return parser.parsePairFromFile(tag, value) // tag for creating new package section and going back to parsing Package case "PackageName": - parser.st = psPackage2_1 + parser.st = psPackage parser.file = nil parser.snippet = nil - return parser.parsePairFromPackage2_1(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_1 - return parser.parsePairFromOtherLicense2_1(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for snippet data case "SnippetFromFileSPDXID": deID, err := extractDocElementID(value) @@ -107,26 +107,26 @@ func (parser *tvParser2_1) parsePairFromSnippet2_1(tag string, value string) err case "Relationship": parser.rln = &v2_1.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_1(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_1.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_1(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_1 - return parser.parsePairFromReview2_1(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Snippet section", tag) } diff --git a/tvloader/parser2v1/parse_snippet_test.go b/spdx/v2/v2_1/tagvalue/reader/parse_snippet_test.go similarity index 75% rename from tvloader/parser2v1/parse_snippet_test.go rename to spdx/v2/v2_1/tagvalue/reader/parse_snippet_test.go index aa42c3c3..9f046942 100644 --- a/tvloader/parser2v1/parse_snippet_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parse_snippet_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Parser snippet section state change tests ===== -func TestParser2_1SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { +func TestParserSnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { // create the first snippet sid1 := common.ElementID("s1") - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: sid1}, @@ -36,13 +36,13 @@ func TestParser2_1SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } // now add a new snippet - err := parser.parsePair2_1("SnippetSPDXID", "SPDXRef-s2") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-s2") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_1 { - t.Errorf("expected state to be %v, got %v", psSnippet2_1, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and a snippet should be created if parser.snippet == nil { @@ -70,10 +70,10 @@ func TestParser2_1SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } } -func TestParser2_1SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -86,13 +86,13 @@ func TestParser2_1SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package p2Name := "package2" - err := parser.parsePair2_1("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_1 { - t.Errorf("expected state to be %v, got %v", psPackage2_1, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -144,11 +144,11 @@ func TestParser2_1SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_1SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { +func TestParserSnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { f1Name := "f1.txt" - parser := tvParser2_1{ + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -160,13 +160,13 @@ func TestParser2_1SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { parser.file.Snippets["s1"] = parser.snippet f2Name := "f2.txt" - err := parser.parsePair2_1("FileName", f2Name) + err := parser.parsePair("FileName", f2Name) if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_1 { - t.Errorf("expected state to be %v, got %v", psSnippet2_1, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current package should remain what it was if parser.pkg != p1 { @@ -197,10 +197,10 @@ func TestParser2_1SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_1SnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -209,19 +209,19 @@ func TestParser2_1SnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing. parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_1("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_1 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_1, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_1SnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -230,19 +230,19 @@ func TestParser2_1SnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_1("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_1 { - t.Errorf("expected state to be %v, got %v", psReview2_1, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_1SnippetStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -251,13 +251,13 @@ func TestParser2_1SnippetStaysAfterParsingRelationshipTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_1("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psSnippet2_1 { - t.Errorf("expected state to be %v, got %v", psSnippet2_1, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -268,20 +268,20 @@ func TestParser2_1SnippetStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_1("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psSnippet2_1 { - t.Errorf("expected state to be %v, got %v", psSnippet2_1, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } } -func TestParser2_1SnippetStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -290,44 +290,44 @@ func TestParser2_1SnippetStaysAfterParsingAnnotationTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_1("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_1) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_1("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_1) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_1("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_1) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_1) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_1("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_1 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_1) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } // and the annotation should be in the Document's Annotations @@ -340,10 +340,10 @@ func TestParser2_1SnippetStaysAfterParsingAnnotationTags(t *testing.T) { } // ===== Snippet data section tests ===== -func TestParser2_1CanParseSnippetTags(t *testing.T) { - parser := tvParser2_1{ +func TestParserCanParseSnippetTags(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{}, @@ -352,7 +352,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_1("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -361,7 +361,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet from File SPDX Identifier - err = parser.parsePairFromSnippet2_1("SnippetFromFileSPDXID", "SPDXRef-f1") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -371,7 +371,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Byte Range - err = parser.parsePairFromSnippet2_1("SnippetByteRange", "20:320") + err = parser.parsePairFromSnippet("SnippetByteRange", "20:320") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -383,7 +383,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Line Range - err = parser.parsePairFromSnippet2_1("SnippetLineRange", "5:12") + err = parser.parsePairFromSnippet("SnippetLineRange", "5:12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -395,7 +395,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Concluded License - err = parser.parsePairFromSnippet2_1("SnippetLicenseConcluded", "BSD-3-Clause") + err = parser.parsePairFromSnippet("SnippetLicenseConcluded", "BSD-3-Clause") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -410,7 +410,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromSnippet2_1("LicenseInfoInSnippet", lic) + err = parser.parsePairFromSnippet("LicenseInfoInSnippet", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -432,7 +432,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Comments on License - err = parser.parsePairFromSnippet2_1("SnippetLicenseComments", "this is a comment about the licenses") + err = parser.parsePairFromSnippet("SnippetLicenseComments", "this is a comment about the licenses") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -441,7 +441,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Copyright Text - err = parser.parsePairFromSnippet2_1("SnippetCopyrightText", "copyright (c) John Doe and friends") + err = parser.parsePairFromSnippet("SnippetCopyrightText", "copyright (c) John Doe and friends") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -450,7 +450,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Comment - err = parser.parsePairFromSnippet2_1("SnippetComment", "this is a comment about the snippet") + err = parser.parsePairFromSnippet("SnippetComment", "this is a comment about the snippet") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -459,7 +459,7 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } // Snippet Name - err = parser.parsePairFromSnippet2_1("SnippetName", "from some other package called abc") + err = parser.parsePairFromSnippet("SnippetName", "from some other package called abc") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -468,10 +468,10 @@ func TestParser2_1CanParseSnippetTags(t *testing.T) { } } -func TestParser2_1SnippetUnknownTagFails(t *testing.T) { - parser := tvParser2_1{ +func TestParserSnippetUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -479,16 +479,16 @@ func TestParser2_1SnippetUnknownTagFails(t *testing.T) { parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromSnippet2_1("blah", "something") + err := parser.parsePairFromSnippet("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_1FailsForInvalidSnippetSPDXID(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsForInvalidSnippetSPDXID(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{}, @@ -497,16 +497,16 @@ func TestParser2_1FailsForInvalidSnippetSPDXID(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // invalid Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_1("SnippetSPDXID", "whoops") + err := parser.parsePairFromSnippet("SnippetSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsForInvalidSnippetFromFileSPDXID(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsForInvalidSnippetFromFileSPDXID(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{}, @@ -515,21 +515,21 @@ func TestParser2_1FailsForInvalidSnippetFromFileSPDXID(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_1("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid From File identifier - err = parser.parsePairFromSnippet2_1("SnippetFromFileSPDXID", "whoops") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsForInvalidSnippetByteValues(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsForInvalidSnippetByteValues(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{}, @@ -538,29 +538,29 @@ func TestParser2_1FailsForInvalidSnippetByteValues(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_1("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_1("SnippetByteRange", "200 210") + err = parser.parsePairFromSnippet("SnippetByteRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_1("SnippetByteRange", "a:210") + err = parser.parsePairFromSnippet("SnippetByteRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_1("SnippetByteRange", "200:a") + err = parser.parsePairFromSnippet("SnippetByteRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsForInvalidSnippetLineValues(t *testing.T) { - parser := tvParser2_1{ +func TestParserFailsForInvalidSnippetLineValues(t *testing.T) { + parser := tvParser{ doc: &v2_1.Document{Packages: []*v2_1.Package{}}, - st: psSnippet2_1, + st: psSnippet, pkg: &v2_1.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_1.File{}}, file: &v2_1.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_1.Snippet{}}, snippet: &v2_1.Snippet{}, @@ -569,37 +569,37 @@ func TestParser2_1FailsForInvalidSnippetLineValues(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_1("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_1("SnippetLineRange", "200 210") + err = parser.parsePairFromSnippet("SnippetLineRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_1("SnippetLineRange", "a:210") + err = parser.parsePairFromSnippet("SnippetLineRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_1("SnippetLineRange", "200:a") + err = parser.parsePairFromSnippet("SnippetLineRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { // Invalid file with snippet // Last unpackaged file before the snippet starts // Last file of a package and new snippet starts fileName := "f2.txt" sid1 := common.ElementID("s1") - parser2 := tvParser2_1{ + parser2 := tvParser{ doc: &v2_1.Document{}, - st: psCreationInfo2_1, + st: psCreationInfo, file: &v2_1.File{FileName: fileName}, } - err := parser2.parsePair2_1("SnippetSPDXID", string(sid1)) + err := parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("files without SPDX Identifiers getting accepted") } diff --git a/tvloader/parser2v1/parser.go b/spdx/v2/v2_1/tagvalue/reader/parser.go similarity index 59% rename from tvloader/parser2v1/parser.go rename to spdx/v2/v2_1/tagvalue/reader/parser.go index c083fc9b..744fb16d 100644 --- a/tvloader/parser2v1/parser.go +++ b/spdx/v2/v2_1/tagvalue/reader/parser.go @@ -1,67 +1,67 @@ // Package parser2v1 contains functions to read, load and parse // SPDX tag-value files. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/tvloader/reader" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ParseTagValues takes a list of (tag, value) pairs, parses it and returns // a pointer to a parsed SPDX Document. -func ParseTagValues(tvs []reader.TagValuePair) (*v2_1.Document, error) { - parser := tvParser2_1{} +func ParseTagValues(tvs []reader.TagValuePair) (*spdx.Document, error) { + parser := tvParser{} for _, tv := range tvs { - err := parser.parsePair2_1(tv.Tag, tv.Value) + err := parser.parsePair(tv.Tag, tv.Value) if err != nil { return nil, err } } - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_1 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_1 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } return parser.doc, nil } -func (parser *tvParser2_1) parsePair2_1(tag string, value string) error { +func (parser *tvParser) parsePair(tag string, value string) error { switch parser.st { - case psStart2_1: - return parser.parsePairFromStart2_1(tag, value) - case psCreationInfo2_1: - return parser.parsePairFromCreationInfo2_1(tag, value) - case psPackage2_1: - return parser.parsePairFromPackage2_1(tag, value) - case psFile2_1: - return parser.parsePairFromFile2_1(tag, value) - case psSnippet2_1: - return parser.parsePairFromSnippet2_1(tag, value) - case psOtherLicense2_1: - return parser.parsePairFromOtherLicense2_1(tag, value) - case psReview2_1: - return parser.parsePairFromReview2_1(tag, value) + case psStart: + return parser.parsePairFromStart(tag, value) + case psCreationInfo: + return parser.parsePairFromCreationInfo(tag, value) + case psPackage: + return parser.parsePairFromPackage(tag, value) + case psFile: + return parser.parsePairFromFile(tag, value) + case psSnippet: + return parser.parsePairFromSnippet(tag, value) + case psOtherLicense: + return parser.parsePairFromOtherLicense(tag, value) + case psReview: + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("parser state %v not recognized when parsing (%s, %s)", parser.st, tag, value) } } -func (parser *tvParser2_1) parsePairFromStart2_1(tag string, value string) error { +func (parser *tvParser) parsePairFromStart(tag string, value string) error { // fail if not in Start parser state - if parser.st != psStart2_1 { - return fmt.Errorf("got invalid state %v in parsePairFromStart2_1", parser.st) + if parser.st != psStart { + return fmt.Errorf("got invalid state %v in parsePairFromStart", parser.st) } // create an SPDX Document data struct if we don't have one already if parser.doc == nil { - parser.doc = &v2_1.Document{ - ExternalDocumentReferences: []v2_1.ExternalDocumentRef{}, + parser.doc = &spdx.Document{ + ExternalDocumentReferences: []spdx.ExternalDocumentRef{}, } } @@ -85,7 +85,7 @@ func (parser *tvParser2_1) parsePairFromStart2_1(tag string, value string) error if err != nil { return err } - edr := v2_1.ExternalDocumentRef{ + edr := spdx.ExternalDocumentRef{ DocumentRefID: documentRefID, URI: uri, Checksum: common.Checksum{Algorithm: common.ChecksumAlgorithm(alg), Value: checksum}, @@ -95,8 +95,8 @@ func (parser *tvParser2_1) parsePairFromStart2_1(tag string, value string) error parser.doc.DocumentComment = value default: // move to Creation Info parser state - parser.st = psCreationInfo2_1 - return parser.parsePairFromCreationInfo2_1(tag, value) + parser.st = psCreationInfo + return parser.parsePairFromCreationInfo(tag, value) } return nil diff --git a/tvloader/parser2v1/parser_test.go b/spdx/v2/v2_1/tagvalue/reader/parser_test.go similarity index 66% rename from tvloader/parser2v1/parser_test.go rename to spdx/v2/v2_1/tagvalue/reader/parser_test.go index e8954455..f444122f 100644 --- a/tvloader/parser2v1/parser_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/parser_test.go @@ -1,20 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/tvloader/reader" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ===== Parser exported entry point tests ===== -func TestParser2_1CanParseTagValues(t *testing.T) { +func TestParserCanParseTagValues(t *testing.T) { var tvPairs []reader.TagValuePair // create some pairs tvPair1 := reader.TagValuePair{Tag: "SPDXVersion", Value: "SPDX-2.1"} tvPairs = append(tvPairs, tvPair1) - tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: "CC0-1.0"} + tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: spdx.DataLicense} tvPairs = append(tvPairs, tvPair2) tvPair3 := reader.TagValuePair{Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"} tvPairs = append(tvPairs, tvPair3) @@ -27,7 +28,7 @@ func TestParser2_1CanParseTagValues(t *testing.T) { if doc.SPDXVersion != "SPDX-2.1" { t.Errorf("expected SPDXVersion to be SPDX-2.1, got %v", doc.SPDXVersion) } - if doc.DataLicense != "CC0-1.0" { + if doc.DataLicense != spdx.DataLicense { t.Errorf("expected DataLicense to be CC0-1.0, got %v", doc.DataLicense) } if doc.SPDXIdentifier != "DOCUMENT" { @@ -37,9 +38,9 @@ func TestParser2_1CanParseTagValues(t *testing.T) { } // ===== Parser initialization tests ===== -func TestParser2_1InitCreatesResetStatus(t *testing.T) { - parser := tvParser2_1{} - if parser.st != psStart2_1 { +func TestParserInitCreatesResetStatus(t *testing.T) { + parser := tvParser{} + if parser.st != psStart { t.Errorf("parser did not begin in start state") } if parser.doc != nil { @@ -47,32 +48,32 @@ func TestParser2_1InitCreatesResetStatus(t *testing.T) { } } -func TestParser2_1HasDocumentAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_1{} - err := parser.parsePair2_1("SPDXVersion", "SPDX-2.1") +func TestParserHasDocumentAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{} + err := parser.parsePair("SPDXVersion", "SPDX-2.1") if err != nil { - t.Errorf("got error when calling parsePair2_1: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.doc == nil { t.Errorf("doc is still nil after parsing first pair") } } -func TestParser2_1StartFailsToParseIfInInvalidState(t *testing.T) { - parser := tvParser2_1{st: psReview2_1} - err := parser.parsePairFromStart2_1("SPDXVersion", "SPDX-2.1") +func TestParserStartFailsToParseIfInInvalidState(t *testing.T) { + parser := tvParser{st: psReview} + err := parser.parsePairFromStart("SPDXVersion", "SPDX-2.1") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: checks the last file // Last unpackaged file no packages in doc // Last file of last package in the doc tvPairs := []reader.TagValuePair{ {Tag: "SPDXVersion", Value: "SPDX-2.1"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "FileName", Value: "f1"}, } @@ -82,11 +83,11 @@ func TestParser2_1FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { } } -func TestParser2_1PackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserPackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: Checks the last package tvPairs := []reader.TagValuePair{ {Tag: "SPDXVersion", Value: "SPDX-2.1"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "PackageName", Value: "p1"}, } diff --git a/tvloader/parser2v1/types.go b/spdx/v2/v2_1/tagvalue/reader/types.go similarity index 71% rename from tvloader/parser2v1/types.go rename to spdx/v2/v2_1/tagvalue/reader/types.go index 8f3cf238..fc3afe3e 100644 --- a/tvloader/parser2v1/types.go +++ b/spdx/v2/v2_1/tagvalue/reader/types.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -type tvParser2_1 struct { +type tvParser struct { // document into which data is being parsed doc *v2_1.Document // current parser state - st tvParserState2_1 + st tvParserState // current SPDX item being filled in, if any pkg *v2_1.Package @@ -29,29 +29,29 @@ type tvParser2_1 struct { } // parser state (SPDX document version 2.1) -type tvParserState2_1 int +type tvParserState int const ( // at beginning of document - psStart2_1 tvParserState2_1 = iota + psStart tvParserState = iota // in document creation info section - psCreationInfo2_1 + psCreationInfo // in package data section - psPackage2_1 + psPackage // in file data section (including "unpackaged" files) - psFile2_1 + psFile // in snippet data section (including "unpackaged" files) - psSnippet2_1 + psSnippet // in other license section - psOtherLicense2_1 + psOtherLicense // in review section - psReview2_1 + psReview ) -const nullSpdxElementId2_1 = common.ElementID("") +const nullSpdxElementId = common.ElementID("") diff --git a/tvloader/parser2v1/util.go b/spdx/v2/v2_1/tagvalue/reader/util.go similarity index 98% rename from tvloader/parser2v1/util.go rename to spdx/v2/v2_1/tagvalue/reader/util.go index 7cac43d6..817660b8 100644 --- a/tvloader/parser2v1/util.go +++ b/spdx/v2/v2_1/tagvalue/reader/util.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // used to extract key / value from embedded substrings diff --git a/tvloader/parser2v1/util_test.go b/spdx/v2/v2_1/tagvalue/reader/util_test.go similarity index 98% rename from tvloader/parser2v1/util_test.go rename to spdx/v2/v2_1/tagvalue/reader/util_test.go index 349d84fd..6043bf9f 100644 --- a/tvloader/parser2v1/util_test.go +++ b/spdx/v2/v2_1/tagvalue/reader/util_test.go @@ -1,10 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== Helper function tests ===== diff --git a/tvsaver/saver2v3/save_annotation.go b/spdx/v2/v2_1/tagvalue/writer/save_annotation.go similarity index 80% rename from tvsaver/saver2v3/save_annotation.go rename to spdx/v2/v2_1/tagvalue/writer/save_annotation.go index 6c4f1aa2..52bd60d9 100644 --- a/tvsaver/saver2v3/save_annotation.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_annotation.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderAnnotation2_3(ann *v2_3.Annotation, w io.Writer) error { +func renderAnnotation(ann *spdx.Annotation, w io.Writer) error { if ann.Annotator.Annotator != "" && ann.Annotator.AnnotatorType != "" { fmt.Fprintf(w, "Annotator: %s: %s\n", ann.Annotator.AnnotatorType, ann.Annotator.Annotator) } diff --git a/tvsaver/saver2v2/save_annotation_test.go b/spdx/v2/v2_1/tagvalue/writer/save_annotation_test.go similarity index 85% rename from tvsaver/saver2v2/save_annotation_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_annotation_test.go index e8d1741a..6bf6c05f 100644 --- a/tvsaver/saver2v2/save_annotation_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_annotation_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Annotation section Saver tests ===== -func TestSaver2_2AnnotationSavesTextForPerson(t *testing.T) { - ann := &v2_2.Annotation{ +func TestSaverAnnotationSavesTextForPerson(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Person", Annotator: "John Doe"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -31,7 +31,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_2(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -43,8 +43,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_2AnnotationSavesTextForOrganization(t *testing.T) { - ann := &v2_2.Annotation{ +func TestSaverAnnotationSavesTextForOrganization(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Organization", Annotator: "John Doe, Inc."}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -63,7 +63,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_2(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -75,8 +75,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_2AnnotationSavesTextForTool(t *testing.T) { - ann := &v2_2.Annotation{ +func TestSaverAnnotationSavesTextForTool(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Tool", Annotator: "magictool-1.1"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -95,7 +95,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_2(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_creation_info.go b/spdx/v2/v2_1/tagvalue/writer/save_creation_info.go similarity index 81% rename from tvsaver/saver2v1/save_creation_info.go rename to spdx/v2/v2_1/tagvalue/writer/save_creation_info.go index e4b5992c..143ba784 100644 --- a/tvsaver/saver2v1/save_creation_info.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_creation_info.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderCreationInfo2_1(ci *v2_1.CreationInfo, w io.Writer) error { +func renderCreationInfo(ci *spdx.CreationInfo, w io.Writer) error { if ci.LicenseListVersion != "" { fmt.Fprintf(w, "LicenseListVersion: %s\n", ci.LicenseListVersion) } diff --git a/tvsaver/saver2v1/save_creation_info_test.go b/spdx/v2/v2_1/tagvalue/writer/save_creation_info_test.go similarity index 86% rename from tvsaver/saver2v1/save_creation_info_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_creation_info_test.go index 52257359..33185930 100644 --- a/tvsaver/saver2v1/save_creation_info_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_creation_info_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Creation Info section Saver tests ===== -func TestSaver2_1CISavesText(t *testing.T) { - ci := &v2_1.CreationInfo{ +func TestSaverCISavesText(t *testing.T) { + ci := &spdx.CreationInfo{ LicenseListVersion: "2.0", Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, @@ -43,7 +43,7 @@ CreatorComment: this is a creator comment // render as buffer of bytes var got bytes.Buffer - err := renderCreationInfo2_1(ci, &got) + err := renderCreationInfo(ci, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -55,9 +55,9 @@ CreatorComment: this is a creator comment } } -func TestSaver2_1CIOmitsOptionalFieldsIfEmpty(t *testing.T) { +func TestSaverCIOmitsOptionalFieldsIfEmpty(t *testing.T) { // --- need at least one creator; do first for Persons --- - ci1 := &v2_1.CreationInfo{ + ci1 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -72,7 +72,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got1 bytes.Buffer - err := renderCreationInfo2_1(ci1, &got1) + err := renderCreationInfo(ci1, &got1) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -84,7 +84,7 @@ Created: 2018-10-10T06:20:00Z } // --- need at least one creator; now switch to organization --- - ci2 := &v2_1.CreationInfo{ + ci2 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe, Inc.", CreatorType: "Organization"}, }, @@ -99,7 +99,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got2 bytes.Buffer - err = renderCreationInfo2_1(ci2, &got2) + err = renderCreationInfo(ci2, &got2) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_document.go b/spdx/v2/v2_1/tagvalue/writer/save_document.go similarity index 85% rename from tvsaver/saver2v1/save_document.go rename to spdx/v2/v2_1/tagvalue/writer/save_document.go index 1db4bf6d..f5ef79fd 100644 --- a/tvsaver/saver2v1/save_document.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_document.go @@ -2,22 +2,22 @@ // formatted version of an in-memory SPDX document and its sections // (version 2.1). // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -// RenderDocument2_1 is the main entry point to take an SPDX in-memory +// RenderDocument is the main entry point to take an SPDX in-memory // Document (version 2.1), and render it to the received io.Writer. // It is only exported in order to be available to the tvsaver package, // and typically does not need to be called by client code. -func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { +func RenderDocument(doc *spdx.Document, w io.Writer) error { if doc.CreationInfo == nil { return fmt.Errorf("Document had nil CreationInfo section") } @@ -49,7 +49,7 @@ func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { fmt.Fprintf(w, "DocumentComment: %s\n", textify(doc.DocumentComment)) } - renderCreationInfo2_1(doc.CreationInfo, w) + renderCreationInfo(doc.CreationInfo, w) if len(doc.Files) > 0 { fmt.Fprintf(w, "##### Unpackaged files\n\n") @@ -57,7 +57,7 @@ func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { return doc.Files[i].FileSPDXIdentifier < doc.Files[j].FileSPDXIdentifier }) for _, fi := range doc.Files { - renderFile2_1(fi, w) + renderFile(fi, w) } } @@ -67,20 +67,20 @@ func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { }) for _, pkg := range doc.Packages { fmt.Fprintf(w, "##### Package: %s\n\n", pkg.PackageName) - renderPackage2_1(pkg, w) + renderPackage(pkg, w) } if len(doc.OtherLicenses) > 0 { fmt.Fprintf(w, "##### Other Licenses\n\n") for _, ol := range doc.OtherLicenses { - renderOtherLicense2_1(ol, w) + renderOtherLicense(ol, w) } } if len(doc.Relationships) > 0 { fmt.Fprintf(w, "##### Relationships\n\n") for _, rln := range doc.Relationships { - renderRelationship2_1(rln, w) + renderRelationship(rln, w) } fmt.Fprintf(w, "\n") } @@ -88,7 +88,7 @@ func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { if len(doc.Annotations) > 0 { fmt.Fprintf(w, "##### Annotations\n\n") for _, ann := range doc.Annotations { - renderAnnotation2_1(ann, w) + renderAnnotation(ann, w) fmt.Fprintf(w, "\n") } } @@ -96,7 +96,7 @@ func RenderDocument2_1(doc *v2_1.Document, w io.Writer) error { if len(doc.Reviews) > 0 { fmt.Fprintf(w, "##### Reviews\n\n") for _, rev := range doc.Reviews { - renderReview2_1(rev, w) + renderReview(rev, w) } } diff --git a/tvsaver/saver2v1/save_document_test.go b/spdx/v2/v2_1/tagvalue/writer/save_document_test.go similarity index 89% rename from tvsaver/saver2v1/save_document_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_document_test.go index 1447c07a..2ad24179 100644 --- a/tvsaver/saver2v1/save_document_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_document_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== entire Document Saver tests ===== -func TestSaver2_1DocumentSavesText(t *testing.T) { +func TestSaverDocumentSavesText(t *testing.T) { // Creation Info section - ci := &v2_1.CreationInfo{ + ci := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -22,7 +22,7 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { } // unpackaged files - f1 := &v2_1.File{ + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983c", Algorithm: common.SHA1}}, @@ -31,7 +31,7 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_1.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983d", Algorithm: common.SHA1}}, @@ -40,13 +40,13 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - unFiles := []*v2_1.File{ + unFiles := []*spdx.File{ f1, f2, } // Package 1: packaged files with snippets - sn1 := &v2_1.Snippet{ + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet19", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -54,7 +54,7 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_1.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet20", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -62,7 +62,7 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - f3 := &v2_1.File{ + f3 := &spdx.File{ FileName: "/tmp/file-with-snippets.txt", FileSPDXIdentifier: common.ElementID("FileHasSnippets"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983e", Algorithm: common.SHA1}}, @@ -73,13 +73,13 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { "WTFPL", }, FileCopyrightText: "Copyright (c) Jane Doe", - Snippets: map[common.ElementID]*v2_1.Snippet{ + Snippets: map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, }, } - f4 := &v2_1.File{ + f4 := &spdx.File{ FileName: "/tmp/another-file.txt", FileSPDXIdentifier: common.ElementID("FileAnother"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983f", Algorithm: common.SHA1}}, @@ -88,7 +88,7 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe LLC", } - pkgWith := &v2_1.Package{ + pkgWith := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -104,14 +104,14 @@ func TestSaver2_1DocumentSavesText(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_1.File{ + Files: []*spdx.File{ f3, f4, }, } // Other Licenses 1 and 2 - ol1 := &v2_1.OtherLicense{ + ol1 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -119,33 +119,33 @@ blah blah blah blah`, LicenseName: "License 1", } - ol2 := &v2_1.OtherLicense{ + ol2 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-2", ExtractedText: `License 2 text - this is a license that does some stuff`, LicenseName: "License 2", } // Relationships - rln1 := &v2_1.Relationship{ + rln1 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), Relationship: "DESCRIBES", } - rln2 := &v2_1.Relationship{ + rln2 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1231"), Relationship: "DESCRIBES", } - rln3 := &v2_1.Relationship{ + rln3 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1232"), Relationship: "DESCRIBES", } // Annotations - ann1 := &v2_1.Annotation{ + ann1 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe", AnnotatorType: "Person"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -154,7 +154,7 @@ blah blah blah blah`, AnnotationComment: "This is an annotation about the SPDX document", } - ann2 := &v2_1.Annotation{ + ann2 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe, Inc.", AnnotatorType: "Organization"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -164,12 +164,12 @@ blah blah blah blah`, } // Reviews - rev1 := &v2_1.Review{ + rev1 := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", } - rev2 := &v2_1.Review{ + rev2 := &spdx.Review{ Reviewer: "Jane Doe LLC", ReviewerType: "Organization", ReviewDate: "2018-10-14T10:28:00Z", @@ -177,31 +177,31 @@ blah blah blah blah`, } // now, build the document - doc := &v2_1.Document{ + doc := &spdx.Document{ SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), DocumentName: "spdx-go-0.0.1.abcdef", DocumentNamespace: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1.abcdef.whatever", CreationInfo: ci, - Packages: []*v2_1.Package{ + Packages: []*spdx.Package{ pkgWith, }, Files: unFiles, - OtherLicenses: []*v2_1.OtherLicense{ + OtherLicenses: []*spdx.OtherLicense{ ol1, ol2, }, - Relationships: []*v2_1.Relationship{ + Relationships: []*spdx.Relationship{ rln1, rln2, rln3, }, - Annotations: []*v2_1.Annotation{ + Annotations: []*spdx.Annotation{ ann1, ann2, }, - Reviews: []*v2_1.Review{ + Reviews: []*spdx.Review{ rev1, rev2, }, @@ -319,7 +319,7 @@ ReviewComment: I have reviewed this SPDX document and it is awesome // render as buffer of bytes var got bytes.Buffer - err := RenderDocument2_1(doc, &got) + err := RenderDocument(doc, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -332,11 +332,11 @@ ReviewComment: I have reviewed this SPDX document and it is awesome } -func TestSaver2_1DocumentReturnsErrorIfNilCreationInfo(t *testing.T) { - doc := &v2_1.Document{} +func TestSaverDocumentReturnsErrorIfNilCreationInfo(t *testing.T) { + doc := &spdx.Document{} var got bytes.Buffer - err := RenderDocument2_1(doc, &got) + err := RenderDocument(doc, &got) if err == nil { t.Errorf("Expected error, got nil") } diff --git a/tvsaver/saver2v1/save_file.go b/spdx/v2/v2_1/tagvalue/writer/save_file.go similarity index 90% rename from tvsaver/saver2v1/save_file.go rename to spdx/v2/v2_1/tagvalue/writer/save_file.go index 3cb90277..5dc71162 100644 --- a/tvsaver/saver2v1/save_file.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_file.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderFile2_1(f *v2_1.File, w io.Writer) error { +func renderFile(f *spdx.File, w io.Writer) error { if f.FileName != "" { fmt.Fprintf(w, "FileName: %s\n", f.FileName) } @@ -70,7 +70,7 @@ func renderFile2_1(f *v2_1.File, w io.Writer) error { sort.Strings(snippetKeys) for _, sID := range snippetKeys { s := f.Snippets[common.ElementID(sID)] - renderSnippet2_1(s, w) + renderSnippet(s, w) } return nil diff --git a/tvsaver/saver2v1/save_file_test.go b/spdx/v2/v2_1/tagvalue/writer/save_file_test.go similarity index 90% rename from tvsaver/saver2v1/save_file_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_file_test.go index ba1d82b3..7759b031 100644 --- a/tvsaver/saver2v1/save_file_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_file_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== File section Saver tests ===== -func TestSaver2_1FileSavesText(t *testing.T) { - f := &v2_1.File{ +func TestSaverFileSavesText(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), FileTypes: []string{ @@ -31,20 +31,20 @@ func TestSaver2_1FileSavesText(t *testing.T) { }, LicenseComments: "this is a license comment(s)", FileCopyrightText: "Copyright (c) Jane Doe", - ArtifactOfProjects: []*v2_1.ArtifactOfProject{ - &v2_1.ArtifactOfProject{ + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project1", HomePage: "http://example.com/1/", URI: "http://example.com/1/uri.whatever", }, - &v2_1.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project2", }, - &v2_1.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project3", HomePage: "http://example.com/3/", }, - &v2_1.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project4", URI: "http://example.com/4/uri.whatever", }, @@ -93,7 +93,7 @@ FileDependency: g.txt // render as buffer of bytes var got bytes.Buffer - err := renderFile2_1(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -105,8 +105,8 @@ FileDependency: g.txt } } -func TestSaver2_1FileSavesSnippetsAlso(t *testing.T) { - sn1 := &v2_1.Snippet{ +func TestSaverFileSavesSnippetsAlso(t *testing.T) { + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet19"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -114,7 +114,7 @@ func TestSaver2_1FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_1.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet20"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -122,12 +122,12 @@ func TestSaver2_1FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - sns := map[common.ElementID]*v2_1.Snippet{ + sns := map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, } - f := &v2_1.File{ + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -165,7 +165,7 @@ SnippetCopyrightText: NOASSERTION // render as buffer of bytes var got bytes.Buffer - err := renderFile2_1(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -177,8 +177,8 @@ SnippetCopyrightText: NOASSERTION } } -func TestSaver2_1FileOmitsOptionalFieldsIfEmpty(t *testing.T) { - f := &v2_1.File{ +func TestSaverFileOmitsOptionalFieldsIfEmpty(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -203,7 +203,7 @@ FileCopyrightText: Copyright (c) Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_1(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -215,8 +215,8 @@ FileCopyrightText: Copyright (c) Jane Doe } } -func TestSaver2_1FileWrapsCopyrightMultiLine(t *testing.T) { - f := &v2_1.File{ +func TestSaverFileWrapsCopyrightMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -243,7 +243,7 @@ Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_1(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -255,8 +255,8 @@ Copyright (c) John Doe } } -func TestSaver2_1FileWrapsCommentsAndNoticesMultiLine(t *testing.T) { - f := &v2_1.File{ +func TestSaverFileWrapsCommentsAndNoticesMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -293,7 +293,7 @@ under either Apache-2.0 or Apache-1.1. // render as buffer of bytes var got bytes.Buffer - err := renderFile2_1(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_other_license.go b/spdx/v2/v2_1/tagvalue/writer/save_other_license.go similarity index 81% rename from tvsaver/saver2v3/save_other_license.go rename to spdx/v2/v2_1/tagvalue/writer/save_other_license.go index 93511083..29bcb27c 100644 --- a/tvsaver/saver2v3/save_other_license.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_other_license.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderOtherLicense2_3(ol *v2_3.OtherLicense, w io.Writer) error { +func renderOtherLicense(ol *spdx.OtherLicense, w io.Writer) error { if ol.LicenseIdentifier != "" { fmt.Fprintf(w, "LicenseID: %s\n", ol.LicenseIdentifier) } diff --git a/tvsaver/saver2v1/save_other_license_test.go b/spdx/v2/v2_1/tagvalue/writer/save_other_license_test.go similarity index 84% rename from tvsaver/saver2v1/save_other_license_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_other_license_test.go index 46ef82cd..c172ec2c 100644 --- a/tvsaver/saver2v1/save_other_license_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_other_license_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Other License section Saver tests ===== -func TestSaver2_1OtherLicenseSavesText(t *testing.T) { - ol := &v2_1.OtherLicense{ +func TestSaverOtherLicenseSavesText(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -38,7 +38,7 @@ LicenseComment: this is a license comment // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_1(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -50,8 +50,8 @@ LicenseComment: this is a license comment } } -func TestSaver2_1OtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { - ol := &v2_1.OtherLicense{ +func TestSaverOtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -70,7 +70,7 @@ LicenseName: License 1 // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_1(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_package.go b/spdx/v2/v2_1/tagvalue/writer/save_package.go similarity index 95% rename from tvsaver/saver2v1/save_package.go rename to spdx/v2/v2_1/tagvalue/writer/save_package.go index 762876f2..56f94418 100644 --- a/tvsaver/saver2v1/save_package.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_package.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" @@ -8,11 +8,11 @@ import ( "sort" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderPackage2_1(pkg *v2_1.Package, w io.Writer) error { +func renderPackage(pkg *spdx.Package, w io.Writer) error { if pkg.PackageName != "" { fmt.Fprintf(w, "PackageName: %s\n", pkg.PackageName) } @@ -107,7 +107,7 @@ func renderPackage2_1(pkg *v2_1.Package, w io.Writer) error { return pkg.Files[i].FileSPDXIdentifier < pkg.Files[j].FileSPDXIdentifier }) for _, fi := range pkg.Files { - renderFile2_1(fi, w) + renderFile(fi, w) } return nil diff --git a/tvsaver/saver2v1/save_package_test.go b/spdx/v2/v2_1/tagvalue/writer/save_package_test.go similarity index 93% rename from tvsaver/saver2v1/save_package_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_package_test.go index 4939b3fe..9f5dbe17 100644 --- a/tvsaver/saver2v1/save_package_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_package_test.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Package section Saver tests ===== -func TestSaver2_1PackageSavesTextCombo1(t *testing.T) { +func TestSaverPackageSavesTextCombo1(t *testing.T) { // include package external refs // test Supplier:Organization, Originator:Person // FilesAnalyzed true, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile has string // NOTE, this is an entirely made up CPE and the format is likely invalid - per1 := &v2_1.PackageExternalReference{ + per1 := &spdx.PackageExternalReference{ Category: "SECURITY", RefType: "cpe22Type", Locator: "cpe:/a:john_doe_inc:p1:0.1.0", @@ -26,7 +26,7 @@ func TestSaver2_1PackageSavesTextCombo1(t *testing.T) { } // NOTE, this is an entirely made up NPM - per2 := &v2_1.PackageExternalReference{ + per2 := &spdx.PackageExternalReference{ Category: "PACKAGE-MANAGER", RefType: "npm", Locator: "p1@0.1.0", @@ -34,14 +34,14 @@ func TestSaver2_1PackageSavesTextCombo1(t *testing.T) { multi-line external ref comment`, } - per3 := &v2_1.PackageExternalReference{ + per3 := &spdx.PackageExternalReference{ Category: "OTHER", RefType: "anything", Locator: "anything-without-spaces-can-go-here", // no ExternalRefComment for this one } - pkg := &v2_1.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -83,7 +83,7 @@ multi-line external ref comment`, PackageSummary: "this is a summary comment", PackageDescription: "this is a description comment", PackageComment: "this is a comment comment", - PackageExternalReferences: []*v2_1.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ per1, per2, per3, @@ -126,7 +126,7 @@ ExternalRef: OTHER anything anything-without-spaces-can-go-here // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -138,13 +138,13 @@ ExternalRef: OTHER anything anything-without-spaces-can-go-here } } -func TestSaver2_1PackageSavesTextCombo2(t *testing.T) { +func TestSaverPackageSavesTextCombo2(t *testing.T) { // no package external refs // test Supplier:NOASSERTION, Originator:Organization // FilesAnalyzed true, IsFilesAnalyzedTagPresent false // PackageVerificationCodeExcludedFile is empty - pkg := &v2_1.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -214,7 +214,7 @@ PackageComment: this is a comment comment // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -226,13 +226,13 @@ PackageComment: this is a comment comment } } -func TestSaver2_1PackageSavesTextCombo3(t *testing.T) { +func TestSaverPackageSavesTextCombo3(t *testing.T) { // no package external refs // test Supplier:Person, Originator:NOASSERTION // FilesAnalyzed false, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile is empty - pkg := &v2_1.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -303,7 +303,7 @@ PackageComment: this is a comment comment // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -315,8 +315,8 @@ PackageComment: this is a comment comment } } -func TestSaver2_1PackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { - pkg := &v2_1.Package{ +func TestSaverPackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -349,7 +349,7 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -361,8 +361,8 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. } } -func TestSaver2_1PackageSavesFilesIfPresent(t *testing.T) { - f1 := &v2_1.File{ +func TestSaverPackageSavesFilesIfPresent(t *testing.T) { + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{ @@ -376,7 +376,7 @@ func TestSaver2_1PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_1.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{ @@ -390,7 +390,7 @@ func TestSaver2_1PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - pkg := &v2_1.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -408,7 +408,7 @@ func TestSaver2_1PackageSavesFilesIfPresent(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_1.File{ + Files: []*spdx.File{ f1, f2, }, @@ -441,7 +441,7 @@ FileCopyrightText: Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -453,8 +453,8 @@ FileCopyrightText: Copyright (c) John Doe } } -func TestSaver2_1PackageWrapsCopyrightMultiLine(t *testing.T) { - pkg := &v2_1.Package{ +func TestSaverPackageWrapsCopyrightMultiLine(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -485,7 +485,7 @@ Copyright Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_1(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_relationship.go b/spdx/v2/v2_1/tagvalue/writer/save_relationship.go similarity index 72% rename from tvsaver/saver2v2/save_relationship.go rename to spdx/v2/v2_1/tagvalue/writer/save_relationship.go index a9621200..2e00db69 100644 --- a/tvsaver/saver2v2/save_relationship.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_relationship.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderRelationship2_2(rln *v2_2.Relationship, w io.Writer) error { +func renderRelationship(rln *spdx.Relationship, w io.Writer) error { rlnAStr := common.RenderDocElementID(rln.RefA) rlnBStr := common.RenderDocElementID(rln.RefB) if rlnAStr != "SPDXRef-" && rlnBStr != "SPDXRef-" && rln.Relationship != "" { diff --git a/tvsaver/saver2v1/save_relationship_test.go b/spdx/v2/v2_1/tagvalue/writer/save_relationship_test.go similarity index 80% rename from tvsaver/saver2v1/save_relationship_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_relationship_test.go index 886670b4..38f210ec 100644 --- a/tvsaver/saver2v1/save_relationship_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_relationship_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Relationship section Saver tests ===== -func TestSaver2_1RelationshipSavesText(t *testing.T) { - rln := &v2_1.Relationship{ +func TestSaverRelationshipSavesText(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -27,7 +27,7 @@ RelationshipComment: this is a comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_1(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ RelationshipComment: this is a comment } } -func TestSaver2_1RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { - rln := &v2_1.Relationship{ +func TestSaverRelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -52,7 +52,7 @@ func TestSaver2_1RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_1(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -64,8 +64,8 @@ func TestSaver2_1RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { } } -func TestSaver2_1RelationshipWrapsCommentMultiLine(t *testing.T) { - rln := &v2_1.Relationship{ +func TestSaverRelationshipWrapsCommentMultiLine(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -82,7 +82,7 @@ multi-line comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_1(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_review.go b/spdx/v2/v2_1/tagvalue/writer/save_review.go similarity index 78% rename from tvsaver/saver2v3/save_review.go rename to spdx/v2/v2_1/tagvalue/writer/save_review.go index 72bac113..89db681b 100644 --- a/tvsaver/saver2v3/save_review.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_review.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderReview2_3(rev *v2_3.Review, w io.Writer) error { +func renderReview(rev *spdx.Review, w io.Writer) error { if rev.Reviewer != "" && rev.ReviewerType != "" { fmt.Fprintf(w, "Reviewer: %s: %s\n", rev.ReviewerType, rev.Reviewer) } diff --git a/tvsaver/saver2v2/save_review_test.go b/spdx/v2/v2_1/tagvalue/writer/save_review_test.go similarity index 82% rename from tvsaver/saver2v2/save_review_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_review_test.go index 961daa12..655d8cc8 100644 --- a/tvsaver/saver2v2/save_review_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_review_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Review section Saver tests ===== -func TestSaver2_2ReviewSavesText(t *testing.T) { - rev := &v2_2.Review{ +func TestSaverReviewSavesText(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -27,7 +27,7 @@ ReviewComment: this is a review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_2(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ ReviewComment: this is a review comment } } -func TestSaver2_2ReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { - rev := &v2_2.Review{ +func TestSaverReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -54,7 +54,7 @@ ReviewDate: 2018-10-14T10:28:00Z // render as buffer of bytes var got bytes.Buffer - err := renderReview2_2(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -66,8 +66,8 @@ ReviewDate: 2018-10-14T10:28:00Z } } -func TestSaver2_2ReviewWrapsMultiLine(t *testing.T) { - rev := &v2_2.Review{ +func TestSaverReviewWrapsMultiLine(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -85,7 +85,7 @@ multi-line review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_2(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_snippet.go b/spdx/v2/v2_1/tagvalue/writer/save_snippet.go similarity index 90% rename from tvsaver/saver2v1/save_snippet.go rename to spdx/v2/v2_1/tagvalue/writer/save_snippet.go index 19c93230..afb5b80f 100644 --- a/tvsaver/saver2v1/save_snippet.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_snippet.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) -func renderSnippet2_1(sn *v2_1.Snippet, w io.Writer) error { +func renderSnippet(sn *spdx.Snippet, w io.Writer) error { if sn.SnippetSPDXIdentifier != "" { fmt.Fprintf(w, "SnippetSPDXID: %s\n", common.RenderElementID(sn.SnippetSPDXIdentifier)) } diff --git a/tvsaver/saver2v1/save_snippet_test.go b/spdx/v2/v2_1/tagvalue/writer/save_snippet_test.go similarity index 89% rename from tvsaver/saver2v1/save_snippet_test.go rename to spdx/v2/v2_1/tagvalue/writer/save_snippet_test.go index 3c9036a0..b65d422f 100644 --- a/tvsaver/saver2v1/save_snippet_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/save_snippet_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_1" ) // ===== Snippet section Saver tests ===== -func TestSaver2_1SnippetSavesText(t *testing.T) { - sn := &v2_1.Snippet{ +func TestSaverSnippetSavesText(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -53,7 +53,7 @@ SnippetName: from John's program // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_1(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -65,8 +65,8 @@ SnippetName: from John's program } } -func TestSaver2_1SnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { - sn := &v2_1.Snippet{ +func TestSaverSnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -90,7 +90,7 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_1(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -102,8 +102,8 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 } } -func TestSaver2_1SnippetWrapsCopyrightMultiline(t *testing.T) { - sn := &v2_1.Snippet{ +func TestSaverSnippetWrapsCopyrightMultiline(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -129,7 +129,7 @@ Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_1(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/util.go b/spdx/v2/v2_1/tagvalue/writer/util.go similarity index 92% rename from tvsaver/saver2v2/util.go rename to spdx/v2/v2_1/tagvalue/writer/util.go index 988d2be6..5298ee0a 100644 --- a/tvsaver/saver2v2/util.go +++ b/spdx/v2/v2_1/tagvalue/writer/util.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" diff --git a/tvsaver/saver2v3/util_test.go b/spdx/v2/v2_1/tagvalue/writer/util_test.go similarity index 97% rename from tvsaver/saver2v3/util_test.go rename to spdx/v2/v2_1/tagvalue/writer/util_test.go index b4b7553b..94a20c86 100644 --- a/tvsaver/saver2v3/util_test.go +++ b/spdx/v2/v2_1/tagvalue/writer/util_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "testing" diff --git a/spdx/v2_2/annotation.go b/spdx/v2/v2_2/annotation.go similarity index 94% rename from spdx/v2_2/annotation.go rename to spdx/v2/v2_2/annotation.go index 35eddc61..3d76d4b7 100644 --- a/spdx/v2_2/annotation.go +++ b/spdx/v2/v2_2/annotation.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Annotation is an Annotation section of an SPDX Document for version 2.2 of the spec. type Annotation struct { diff --git a/spdx/v2_2/creation_info.go b/spdx/v2/v2_2/creation_info.go similarity index 92% rename from spdx/v2_2/creation_info.go rename to spdx/v2/v2_2/creation_info.go index 70e611f7..8496462e 100644 --- a/spdx/v2_2/creation_info.go +++ b/spdx/v2/v2_2/creation_info.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // CreationInfo is a Document Creation Information section of an // SPDX Document for version 2.2 of the spec. diff --git a/spdx/v2_2/document.go b/spdx/v2/v2_2/document.go similarity index 88% rename from spdx/v2_2/document.go rename to spdx/v2/v2_2/document.go index 31ac08b6..6e8b2f4c 100644 --- a/spdx/v2_2/document.go +++ b/spdx/v2/v2_2/document.go @@ -3,7 +3,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/anchore/go-struct-converter" + + "github.com/spdx/tools-golang/spdx/v2/common" +) + +const Version = "SPDX-2.2" +const DataLicense = "CC0-1.0" // ExternalDocumentRef is a reference to an external SPDX document // as defined in section 6.6 for version 2.2 of the spec. @@ -63,3 +70,10 @@ type Document struct { // DEPRECATED in version 2.0 of spec Reviews []*Review `json:"-"` } + +func (d *Document) ConvertFrom(_ interface{}) error { + d.SPDXVersion = Version + return nil +} + +var _ converter.ConvertFrom = (*Document)(nil) diff --git a/json/json_v2_2_test.go b/spdx/v2/v2_2/example/example.go similarity index 88% rename from json/json_v2_2_test.go rename to spdx/v2/v2_2/example/example.go index 01400ecd..1ddd5435 100644 --- a/json/json_v2_2_test.go +++ b/spdx/v2/v2_2/example/example.go @@ -1,72 +1,34 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_json +package example import ( - "bytes" "fmt" - "os" - "testing" - "github.com/google/go-cmp/cmp" + converter "github.com/anchore/go-struct-converter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func TestLoad2_2(t *testing.T) { - file, err := os.Open("../examples/sample-docs/json/SPDXJSONExample-v2.2.spdx.json") - if err != nil { - panic(fmt.Errorf("error opening File: %s", err)) - } - - got, err := Load2_2(file) +// Copy provides a deep copy of the example +func Copy() spdx.Document { + out := spdx.Document{} + err := converter.Convert(example, &out) if err != nil { - t.Errorf("json.parser.Load2_2() error = %v", err) - return - } - - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_2 - - if cmp.Equal(handwrittenExample, got) { - t.Errorf("Got incorrect struct after parsing JSON example") - return - } -} - -func TestWrite2_2(t *testing.T) { - w := &bytes.Buffer{} - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_2 - if err := Save2_2(&handwrittenExample, w); err != nil { - t.Errorf("Save2_2() error = %v", err.Error()) - return - } - - // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote - parsedDoc, err := Load2_2(bytes.NewReader(w.Bytes())) - if err != nil { - t.Errorf("failed to parse written document: %v", err.Error()) - return - } - - if cmp.Equal(handwrittenExample, parsedDoc) { - t.Errorf("Got incorrect struct after writing and re-parsing JSON example") - return + panic(fmt.Errorf("unable to convert example doc: %w", err)) } + return out } -// want is handwritten translation of the official example JSON SPDX v2.2 document into a Go struct. +// Example is handwritten translation of an official example SPDX document into a Go struct. // We expect that the result of parsing the official document should be this value. // We expect that the result of writing this struct should match the official example document. -var want2_2 = v2_2.Document{ - DataLicense: "CC0-1.0", - SPDXVersion: "SPDX-2.2", - SPDXIdentifier: "SPDXRef-DOCUMENT", +var example = spdx.Document{ + DataLicense: spdx.DataLicense, + SPDXVersion: spdx.Version, + SPDXIdentifier: "DOCUMENT", DocumentName: "SPDX-Tools-v2.0", DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", - CreationInfo: &v2_2.CreationInfo{ + CreationInfo: &spdx.CreationInfo{ LicenseListVersion: "3.9", Creators: []common.Creator{ {CreatorType: "Tool", Creator: "LicenseFind-1.0"}, @@ -77,7 +39,7 @@ var want2_2 = v2_2.Document{ CreatorComment: "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.", }, DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.", - ExternalDocumentReferences: []v2_2.ExternalDocumentRef{ + ExternalDocumentReferences: []spdx.ExternalDocumentRef{ { DocumentRefID: "DocumentRef-spdx-tool-1.2", URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301", @@ -87,7 +49,7 @@ var want2_2 = v2_2.Document{ }, }, }, - OtherLicenses: []*v2_2.OtherLicense{ + OtherLicenses: []*spdx.OtherLicense{ { LicenseIdentifier: "LicenseRef-1", ExtractedText: "/*\n * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", @@ -118,7 +80,7 @@ var want2_2 = v2_2.Document{ LicenseComment: "This is tye CyperNeko License", }, }, - Annotations: []*v2_2.Annotation{ + Annotations: []*spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "Jane Doe ()", @@ -147,10 +109,10 @@ var want2_2 = v2_2.Document{ AnnotationComment: "Another example reviewer.", }, }, - Packages: []*v2_2.Package{ + Packages: []*spdx.Package{ { PackageName: "glibc", - PackageSPDXIdentifier: "SPDXRef-Package", + PackageSPDXIdentifier: "Package", PackageVersion: "2.11.1", PackageFileName: "glibc-2.11.1.tar.gz", PackageSupplier: &common.Supplier{ @@ -195,7 +157,7 @@ var want2_2 = v2_2.Document{ PackageSummary: "GNU C library.", PackageDescription: "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.", PackageComment: "", - PackageExternalReferences: []*v2_2.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ { Category: "SECURITY", RefType: "cpe23Type", @@ -212,7 +174,7 @@ var want2_2 = v2_2.Document{ "The GNU C Library is free software. See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed. License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually.", }, Files: nil, - Annotations: []v2_2.Annotation{ + Annotations: []spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "Package Commenter", @@ -225,7 +187,7 @@ var want2_2 = v2_2.Document{ }, }, { - PackageSPDXIdentifier: "SPDXRef-fromDoap-1", + PackageSPDXIdentifier: "fromDoap-1", PackageCopyrightText: "NOASSERTION", PackageDownloadLocation: "NOASSERTION", FilesAnalyzed: false, @@ -236,10 +198,10 @@ var want2_2 = v2_2.Document{ }, { PackageName: "Jena", - PackageSPDXIdentifier: "SPDXRef-fromDoap-0", + PackageSPDXIdentifier: "fromDoap-0", PackageCopyrightText: "NOASSERTION", PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz", - PackageExternalReferences: []*v2_2.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ { Category: "PACKAGE_MANAGER", RefType: "purl", @@ -253,7 +215,7 @@ var want2_2 = v2_2.Document{ PackageVersion: "3.12.0", }, { - PackageSPDXIdentifier: "SPDXRef-Saxon", + PackageSPDXIdentifier: "Saxon", PackageChecksums: []common.Checksum{ { Algorithm: "SHA1", @@ -273,10 +235,10 @@ var want2_2 = v2_2.Document{ PackageVersion: "8.8", }, }, - Files: []*v2_2.File{ + Files: []*spdx.File{ { FileName: "./src/org/spdx/parser/DOAPProject.java", - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", FileTypes: []string{ "SOURCE", }, @@ -300,7 +262,7 @@ var want2_2 = v2_2.Document{ }, }, { - FileSPDXIdentifier: "SPDXRef-CommonsLangSrc", + FileSPDXIdentifier: "CommonsLangSrc", Checksums: []common.Checksum{ { Algorithm: "SHA1", @@ -317,7 +279,7 @@ var want2_2 = v2_2.Document{ FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())", }, { - FileSPDXIdentifier: "SPDXRef-JenaLib", + FileSPDXIdentifier: "JenaLib", Checksums: []common.Checksum{ { Algorithm: "SHA1", @@ -334,8 +296,8 @@ var want2_2 = v2_2.Document{ LicenseInfoInFiles: []string{"LicenseRef-1"}, }, { - FileSPDXIdentifier: "SPDXRef-File", - Annotations: []v2_2.Annotation{ + FileSPDXIdentifier: "File", + Annotations: []spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "File Commenter", @@ -367,29 +329,29 @@ var want2_2 = v2_2.Document{ FileNotice: "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", }, }, - Snippets: []v2_2.Snippet{ + Snippets: []spdx.Snippet{ { - SnippetSPDXIdentifier: "SPDXRef-Snippet", - SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource", + SnippetSPDXIdentifier: "Snippet", + SnippetFromFileSPDXIdentifier: "DoapSource", Ranges: []common.SnippetRange{ { StartPointer: common.SnippetRangePointer{ Offset: 310, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, EndPointer: common.SnippetRangePointer{ Offset: 420, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, }, { StartPointer: common.SnippetRangePointer{ LineNumber: 5, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, EndPointer: common.SnippetRangePointer{ LineNumber: 23, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, }, }, @@ -401,7 +363,7 @@ var want2_2 = v2_2.Document{ SnippetName: "from linux kernel", }, }, - Relationships: []*v2_2.Relationship{ + Relationships: []*spdx.Relationship{ { RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "Package"), diff --git a/spdx/v2_2/file.go b/spdx/v2/v2_2/file.go similarity index 98% rename from spdx/v2_2/file.go rename to spdx/v2/v2_2/file.go index 150e79f0..14333949 100644 --- a/spdx/v2_2/file.go +++ b/spdx/v2/v2_2/file.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // File is a File section of an SPDX Document for version 2.2 of the spec. type File struct { diff --git a/spdx/v2/v2_2/json/json_test.go b/spdx/v2/v2_2/json/json_test.go new file mode 100644 index 00000000..b24fe4b0 --- /dev/null +++ b/spdx/v2/v2_2/json/json_test.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package json + +import ( + "bytes" + "fmt" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + + "github.com/spdx/tools-golang/json" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2/example" +) + +func TestLoad(t *testing.T) { + want := example.Copy() + + file, err := os.Open("../../../../examples/sample-docs/json/SPDXJSONExample-v2.2.spdx.json") + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + var got v2_2.Document + err = json.ReadInto(file, &got) + if err != nil { + t.Errorf("json.parser.Load() error = %v", err) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} + +func Test_Write(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + + if err := json.Write(&want, w); err != nil { + t.Errorf("Write() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote + var got v2_2.Document + err := json.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got)) + return + } +} diff --git a/spdx/v2_2/other_license.go b/spdx/v2/v2_2/other_license.go similarity index 100% rename from spdx/v2_2/other_license.go rename to spdx/v2/v2_2/other_license.go diff --git a/spdx/v2_2/package.go b/spdx/v2/v2_2/package.go similarity index 97% rename from spdx/v2_2/package.go rename to spdx/v2/v2_2/package.go index 2d99e045..17daca91 100644 --- a/spdx/v2_2/package.go +++ b/spdx/v2/v2_2/package.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Package is a Package section of an SPDX Document for version 2.2 of the spec. type Package struct { @@ -101,7 +103,7 @@ type Package struct { // 7.22: Package External Reference Comment // Cardinality: conditional (optional, one) for each External Reference - // contained within PackageExternalReference2_1 struct, if present + // contained within PackageExternalReference struct, if present // 7.23: Package Attribution Text // Cardinality: optional, one or many diff --git a/spdx/v2/v2_2/rdf/reader.go b/spdx/v2/v2_2/rdf/reader.go new file mode 100644 index 00000000..747e5b22 --- /dev/null +++ b/spdx/v2/v2_2/rdf/reader.go @@ -0,0 +1,21 @@ +package rdf + +import ( + "io" + + "github.com/spdx/gordf/rdfloader" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2/rdf/reader" +) + +// Takes in a file Reader and returns the pertaining spdx document +// or the error if any is encountered while setting the doc. +func Read(content io.Reader) (*v2_2.Document, error) { + var rdfParserObj, err = rdfloader.LoadFromReaderObject(content) + if err != nil { + return nil, err + } + + doc, err := reader.LoadFromGoRDFParser(rdfParserObj) + return doc, err +} diff --git a/rdfloader/parser2v2/constants.go b/spdx/v2/v2_2/rdf/reader/constants.go similarity index 99% rename from rdfloader/parser2v2/constants.go rename to spdx/v2/v2_2/rdf/reader/constants.go index 71a3e33a..f68fdd9c 100644 --- a/rdfloader/parser2v2/constants.go +++ b/spdx/v2/v2_2/rdf/reader/constants.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import "github.com/spdx/gordf/rdfloader/parser" diff --git a/rdfloader/parser2v2/license_utils.go b/spdx/v2/v2_2/rdf/reader/license_utils.go similarity index 98% rename from rdfloader/parser2v2/license_utils.go rename to spdx/v2/v2_2/rdf/reader/license_utils.go index 41e8870d..bdf2ba93 100644 --- a/rdfloader/parser2v2/license_utils.go +++ b/spdx/v2/v2_2/rdf/reader/license_utils.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) /* util methods for licenses and checksums below:*/ diff --git a/rdfloader/parser2v2/license_utils_test.go b/spdx/v2/v2_2/rdf/reader/license_utils_test.go similarity index 99% rename from rdfloader/parser2v2/license_utils_test.go rename to spdx/v2/v2_2/rdf/reader/license_utils_test.go index 7e20b5cd..f65e81ab 100644 --- a/rdfloader/parser2v2/license_utils_test.go +++ b/spdx/v2/v2_2/rdf/reader/license_utils_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" diff --git a/rdfloader/parser2v2/parse_annotation.go b/spdx/v2/v2_2/rdf/reader/parse_annotation.go similarity index 97% rename from rdfloader/parser2v2/parse_annotation.go rename to spdx/v2/v2_2/rdf/reader/parse_annotation.go index a5801b0b..bbbfc62c 100644 --- a/rdfloader/parser2v2/parse_annotation.go +++ b/spdx/v2/v2_2/rdf/reader/parse_annotation.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "errors" "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // creates a new instance of annotation and sets the annotation attributes diff --git a/rdfloader/parser2v2/parse_annotation_test.go b/spdx/v2/v2_2/rdf/reader/parse_annotation_test.go similarity index 99% rename from rdfloader/parser2v2/parse_annotation_test.go rename to spdx/v2/v2_2/rdf/reader/parse_annotation_test.go index 114fc5de..cb1099c5 100644 --- a/rdfloader/parser2v2/parse_annotation_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_annotation_test.go @@ -1,11 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func Test_setAnnotatorFromString(t *testing.T) { diff --git a/rdfloader/parser2v2/parse_creation_info.go b/spdx/v2/v2_2/rdf/reader/parse_creation_info.go similarity index 93% rename from rdfloader/parser2v2/parse_creation_info.go rename to spdx/v2/v2_2/rdf/reader/parse_creation_info.go index b58149d0..c0e7fd0d 100644 --- a/rdfloader/parser2v2/parse_creation_info.go +++ b/spdx/v2/v2_2/rdf/reader/parse_creation_info.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // Cardinality: Mandatory, one. diff --git a/rdfloader/parser2v2/parse_creation_info_test.go b/spdx/v2/v2_2/rdf/reader/parse_creation_info_test.go similarity index 89% rename from rdfloader/parser2v2/parse_creation_info_test.go rename to spdx/v2/v2_2/rdf/reader/parse_creation_info_test.go index 81fceaee..a39755dc 100644 --- a/rdfloader/parser2v2/parse_creation_info_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_creation_info_test.go @@ -1,31 +1,31 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func Test_setCreator(t *testing.T) { // TestCase 1: invalid creator (empty) input := "" - err := setCreator(input, &v2_2.CreationInfo{}) + err := setCreator(input, &spdx.CreationInfo{}) if err == nil { t.Errorf("shoud've raised an error due to invalid input") } // TestCase 2: invalid entity type input = "Company: some company" - err = setCreator(input, &v2_2.CreationInfo{}) + err = setCreator(input, &spdx.CreationInfo{}) if err == nil { t.Errorf("shoud've raised an error due to unknown entity type") } // TestCase 3: valid input input = "Person: Jane Doe" - ci := &v2_2.CreationInfo{} + ci := &spdx.CreationInfo{} err = setCreator(input, ci) if err != nil { t.Errorf("error parsing a valid input: %v", err) @@ -49,7 +49,7 @@ func Test_rdfParser2_2_parseCreationInfoFromNode(t *testing.T) { `) ciNode := parser.gordfParserObj.Triples[0].Subject - err := parser.parseCreationInfoFromNode(&v2_2.CreationInfo{}, ciNode) + err := parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode) if err == nil { t.Errorf("invalid creator must raise an error") } @@ -66,7 +66,7 @@ func Test_rdfParser2_2_parseCreationInfoFromNode(t *testing.T) { `) ciNode = parser.gordfParserObj.Triples[0].Subject - err = parser.parseCreationInfoFromNode(&v2_2.CreationInfo{}, ciNode) + err = parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode) if err == nil { t.Errorf("unknown predicate must raise an error") } @@ -81,7 +81,7 @@ func Test_rdfParser2_2_parseCreationInfoFromNode(t *testing.T) { `) ciNode = parser.gordfParserObj.Triples[0].Subject - ci := &v2_2.CreationInfo{} + ci := &spdx.CreationInfo{} err = parser.parseCreationInfoFromNode(ci, ciNode) if err != nil { t.Errorf("unexpected error: %v", err) diff --git a/rdfloader/parser2v2/parse_file.go b/spdx/v2/v2_2/rdf/reader/parse_file.go similarity index 98% rename from rdfloader/parser2v2/parse_file.go rename to spdx/v2/v2_2/rdf/reader/parse_file.go index d5a19ef0..ffc7645a 100644 --- a/rdfloader/parser2v2/parse_file.go +++ b/spdx/v2/v2_2/rdf/reader/parse_file.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // returns a file instance and the error if any encountered. diff --git a/rdfloader/parser2v2/parse_file_test.go b/spdx/v2/v2_2/rdf/reader/parse_file_test.go similarity index 99% rename from rdfloader/parser2v2/parse_file_test.go rename to spdx/v2/v2_2/rdf/reader/parse_file_test.go index 0383cd97..cab0d2fb 100644 --- a/rdfloader/parser2v2/parse_file_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_file_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "bufio" @@ -10,8 +10,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" rdfloader2 "github.com/spdx/gordf/rdfloader/xmlreader" gordfWriter "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // content is the tags within the rdf:RDF tag diff --git a/rdfloader/parser2v2/parse_license.go b/spdx/v2/v2_2/rdf/reader/parse_license.go similarity index 99% rename from rdfloader/parser2v2/parse_license.go rename to spdx/v2/v2_2/rdf/reader/parse_license.go index 71a1e4a2..0f80ed25 100644 --- a/rdfloader/parser2v2/parse_license.go +++ b/spdx/v2/v2_2/rdf/reader/parse_license.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "errors" diff --git a/rdfloader/parser2v2/parse_license_test.go b/spdx/v2/v2_2/rdf/reader/parse_license_test.go similarity index 99% rename from rdfloader/parser2v2/parse_license_test.go rename to spdx/v2/v2_2/rdf/reader/parse_license_test.go index e2c684dc..969112bf 100644 --- a/rdfloader/parser2v2/parse_license_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_license_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" diff --git a/rdfloader/parser2v2/parse_other_license_info.go b/spdx/v2/v2_2/rdf/reader/parse_other_license_info.go similarity index 95% rename from rdfloader/parser2v2/parse_other_license_info.go rename to spdx/v2/v2_2/rdf/reader/parse_other_license_info.go index 97dcf0c5..8612bd11 100644 --- a/rdfloader/parser2v2/parse_other_license_info.go +++ b/spdx/v2/v2_2/rdf/reader/parse_other_license_info.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func (parser *rdfParser2_2) getExtractedLicensingInfoFromNode(node *gordfParser.Node) (lic ExtractedLicensingInfo, err error) { diff --git a/rdfloader/parser2v2/parse_other_license_info_test.go b/spdx/v2/v2_2/rdf/reader/parse_other_license_info_test.go similarity index 99% rename from rdfloader/parser2v2/parse_other_license_info_test.go rename to spdx/v2/v2_2/rdf/reader/parse_other_license_info_test.go index cd7ad321..0daf519b 100644 --- a/rdfloader/parser2v2/parse_other_license_info_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_other_license_info_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" diff --git a/rdfloader/parser2v2/parse_package.go b/spdx/v2/v2_2/rdf/reader/parse_package.go similarity index 99% rename from rdfloader/parser2v2/parse_package.go rename to spdx/v2/v2_2/rdf/reader/parse_package.go index e6d9f60a..bb4a3310 100644 --- a/rdfloader/parser2v2/parse_package.go +++ b/spdx/v2/v2_2/rdf/reader/parse_package.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func (parser *rdfParser2_2) getPackageFromNode(packageNode *gordfParser.Node) (pkg *v2_2.Package, err error) { diff --git a/rdfloader/parser2v2/parse_package_test.go b/spdx/v2/v2_2/rdf/reader/parse_package_test.go similarity index 99% rename from rdfloader/parser2v2/parse_package_test.go rename to spdx/v2/v2_2/rdf/reader/parse_package_test.go index f458984d..8c0f26a6 100644 --- a/rdfloader/parser2v2/parse_package_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_package_test.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func Test_setPackageSupplier(t *testing.T) { diff --git a/rdfloader/parser2v2/parse_relationship.go b/spdx/v2/v2_2/rdf/reader/parse_relationship.go similarity index 97% rename from rdfloader/parser2v2/parse_relationship.go rename to spdx/v2/v2_2/rdf/reader/parse_relationship.go index 49f10257..19469337 100644 --- a/rdfloader/parser2v2/parse_relationship.go +++ b/spdx/v2/v2_2/rdf/reader/parse_relationship.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" @@ -8,8 +8,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // parsing the relationship that exists in the rdf document. diff --git a/rdfloader/parser2v2/parse_relationship_test.go b/spdx/v2/v2_2/rdf/reader/parse_relationship_test.go similarity index 99% rename from rdfloader/parser2v2/parse_relationship_test.go rename to spdx/v2/v2_2/rdf/reader/parse_relationship_test.go index fd66d148..e0a6b4eb 100644 --- a/rdfloader/parser2v2/parse_relationship_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_relationship_test.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" "testing" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func Test_getReferenceFromURI(t *testing.T) { diff --git a/rdfloader/parser2v2/parse_review.go b/spdx/v2/v2_2/rdf/reader/parse_review.go similarity index 94% rename from rdfloader/parser2v2/parse_review.go rename to spdx/v2/v2_2/rdf/reader/parse_review.go index 40c87d33..4f72353a 100644 --- a/rdfloader/parser2v2/parse_review.go +++ b/spdx/v2/v2_2/rdf/reader/parse_review.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func (parser *rdfParser2_2) setReviewFromNode(reviewedNode *gordfParser.Node) error { diff --git a/rdfloader/parser2v2/parse_review_test.go b/spdx/v2/v2_2/rdf/reader/parse_review_test.go similarity index 99% rename from rdfloader/parser2v2/parse_review_test.go rename to spdx/v2/v2_2/rdf/reader/parse_review_test.go index d62b7b4e..1651469d 100644 --- a/rdfloader/parser2v2/parse_review_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_review_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" diff --git a/rdfloader/parser2v2/parse_snippet_info.go b/spdx/v2/v2_2/rdf/reader/parse_snippet_info.go similarity index 98% rename from rdfloader/parser2v2/parse_snippet_info.go rename to spdx/v2/v2_2/rdf/reader/parse_snippet_info.go index 5e80944d..6b920e7d 100644 --- a/rdfloader/parser2v2/parse_snippet_info.go +++ b/spdx/v2/v2_2/rdf/reader/parse_snippet_info.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" @@ -8,8 +8,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // Snippet Information diff --git a/rdfloader/parser2v2/parse_snippet_info_test.go b/spdx/v2/v2_2/rdf/reader/parse_snippet_info_test.go similarity index 99% rename from rdfloader/parser2v2/parse_snippet_info_test.go rename to spdx/v2/v2_2/rdf/reader/parse_snippet_info_test.go index df0bb70e..f24bcc71 100644 --- a/rdfloader/parser2v2/parse_snippet_info_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_snippet_info_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func Test_rdfParser2_2_getSnippetInformationFromTriple2_2(t *testing.T) { diff --git a/rdfloader/parser2v2/parse_spdx_document.go b/spdx/v2/v2_2/rdf/reader/parse_spdx_document.go similarity index 97% rename from rdfloader/parser2v2/parse_spdx_document.go rename to spdx/v2/v2_2/rdf/reader/parse_spdx_document.go index 481e8ccb..7f16cc28 100644 --- a/rdfloader/parser2v2/parse_spdx_document.go +++ b/spdx/v2/v2_2/rdf/reader/parse_spdx_document.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) func (parser *rdfParser2_2) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) (err error) { diff --git a/rdfloader/parser2v2/parse_spdx_document_test.go b/spdx/v2/v2_2/rdf/reader/parse_spdx_document_test.go similarity index 99% rename from rdfloader/parser2v2/parse_spdx_document_test.go rename to spdx/v2/v2_2/rdf/reader/parse_spdx_document_test.go index 9d22faad..251105d9 100644 --- a/rdfloader/parser2v2/parse_spdx_document_test.go +++ b/spdx/v2/v2_2/rdf/reader/parse_spdx_document_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" diff --git a/rdfloader/parser2v2/parser.go b/spdx/v2/v2_2/rdf/reader/parser.go similarity index 98% rename from rdfloader/parser2v2/parser.go rename to spdx/v2/v2_2/rdf/reader/parser.go index 4b3b62c1..2c614910 100644 --- a/rdfloader/parser2v2/parser.go +++ b/spdx/v2/v2_2/rdf/reader/parser.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "errors" @@ -8,8 +8,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" gordfWriter "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // returns a new instance of rdfParser2_2 given the gordf object and nodeToTriples mapping diff --git a/rdfloader/parser2v2/parser_test.go b/spdx/v2/v2_2/rdf/reader/parser_test.go similarity index 99% rename from rdfloader/parser2v2/parser_test.go rename to spdx/v2/v2_2/rdf/reader/parser_test.go index 11b2da6b..d008ed39 100644 --- a/rdfloader/parser2v2/parser_test.go +++ b/spdx/v2/v2_2/rdf/reader/parser_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" diff --git a/rdfloader/parser2v2/types.go b/spdx/v2/v2_2/rdf/reader/types.go similarity index 96% rename from rdfloader/parser2v2/types.go rename to spdx/v2/v2_2/rdf/reader/types.go index dbb50d5d..2e2d7b01 100644 --- a/rdfloader/parser2v2/types.go +++ b/spdx/v2/v2_2/rdf/reader/types.go @@ -1,11 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later // copied from tvloader/parser2v2/types.go -package parser2v2 +package reader import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) type rdfParser2_2 struct { diff --git a/rdfloader/parser2v2/utils.go b/spdx/v2/v2_2/rdf/reader/utils.go similarity index 98% rename from rdfloader/parser2v2/utils.go rename to spdx/v2/v2_2/rdf/reader/utils.go index 9f51a3c6..aa68dfa7 100644 --- a/rdfloader/parser2v2/utils.go +++ b/spdx/v2/v2_2/rdf/reader/utils.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "errors" @@ -10,7 +10,7 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" urilib "github.com/spdx/gordf/uri" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // a uri is of type baseURI#fragment or baseFragment/subFragment diff --git a/rdfloader/parser2v2/utils_test.go b/spdx/v2/v2_2/rdf/reader/utils_test.go similarity index 99% rename from rdfloader/parser2v2/utils_test.go rename to spdx/v2/v2_2/rdf/reader/utils_test.go index c0cc5746..9d37a154 100644 --- a/rdfloader/parser2v2/utils_test.go +++ b/spdx/v2/v2_2/rdf/reader/utils_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "reflect" "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) func Test_getLastPartOfURI(t *testing.T) { diff --git a/spdx/v2/v2_2/rdf/reader_test.go b/spdx/v2/v2_2/rdf/reader_test.go new file mode 100644 index 00000000..45d73710 --- /dev/null +++ b/spdx/v2/v2_2/rdf/reader_test.go @@ -0,0 +1,32 @@ +package rdf + +import ( + "io" + "strings" + "testing" +) + +func Test_Read(t *testing.T) { + var reader io.Reader + var err error + + // TestCase 1: invalid rdf/xml must raise an error + reader = strings.NewReader("") + _, err = Read(reader) + if err == nil { + t.Errorf("expected an EOF error reading an empty file, got %v", err) + } + + // TestCase 2: Valid rdf/xml but invalid spdx document must raise an error + reader = strings.NewReader(` + + + `) + _, err = Read(reader) + if err == nil { + t.Errorf("expected an error due to no SpdxDocument Node in the document") + } +} diff --git a/spdx/v2_2/relationship.go b/spdx/v2/v2_2/relationship.go similarity index 92% rename from spdx/v2_2/relationship.go rename to spdx/v2/v2_2/relationship.go index a93baa71..47df3337 100644 --- a/spdx/v2_2/relationship.go +++ b/spdx/v2/v2_2/relationship.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Relationship is a Relationship section of an SPDX Document for // version 2.2 of the spec. diff --git a/spdx/v2_2/review.go b/spdx/v2/v2_2/review.go similarity index 100% rename from spdx/v2_2/review.go rename to spdx/v2/v2_2/review.go diff --git a/spdx/v2_2/snippet.go b/spdx/v2/v2_2/snippet.go similarity index 96% rename from spdx/v2_2/snippet.go rename to spdx/v2/v2_2/snippet.go index 61045f1e..473c5a11 100644 --- a/spdx/v2_2/snippet.go +++ b/spdx/v2/v2_2/snippet.go @@ -2,7 +2,9 @@ package v2_2 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) // Snippet is a Snippet section of an SPDX Document for version 2.2 of the spec. type Snippet struct { diff --git a/tvloader/parser2v1/parse_annotation.go b/spdx/v2/v2_2/tagvalue/reader/parse_annotation.go similarity index 90% rename from tvloader/parser2v1/parse_annotation.go rename to spdx/v2/v2_2/tagvalue/reader/parse_annotation.go index ca2e8504..0d7e251d 100644 --- a/tvloader/parser2v1/parse_annotation.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_annotation.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 +package reader import ( "fmt" ) -func (parser *tvParser2_1) parsePairForAnnotation2_1(tag string, value string) error { +func (parser *tvParser) parsePairForAnnotation(tag string, value string) error { if parser.ann == nil { return fmt.Errorf("no annotation struct created in parser ann pointer") } diff --git a/spdx/v2/v2_2/tagvalue/reader/parse_annotation_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_annotation_test.go new file mode 100644 index 00000000..082b438a --- /dev/null +++ b/spdx/v2/v2_2/tagvalue/reader/parse_annotation_test.go @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" +) + +// ===== Annotation section tests ===== +func TestParserFailsIfAnnotationNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForAnnotation("Annotator", "Person: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected error when calling parsePairFromAnnotation without setting ann pointer") + } +} + +func TestParserFailsIfAnnotationTagUnknown(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // parse invalid tag, using parsePairForAnnotation( + err = parser.parsePairForAnnotation("blah", "oops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("AnnotationDate", "2018-09-15T17:25:00Z") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationDate without Annotator first") + } + err = parser.parsePair("AnnotationType", "REVIEW") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationType without Annotator first") + } + err = parser.parsePair("SPDXREF", "SPDXRef-45") + if err == nil { + t.Errorf("expected error when calling parsePair for SPDXREF without Annotator first") + } + err = parser.parsePair("AnnotationComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationComment without Annotator first") + } +} + +func TestParserCanParseAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Annotator without email address + err := parser.parsePair("Annotator", "Person: John Doe") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.Annotator.Annotator != "John Doe" { + t.Errorf("got %+v for Annotator, expected John Doe", parser.ann.Annotator.Annotator) + } + if parser.ann.Annotator.AnnotatorType != "Person" { + t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) + } + + // Annotation Date + dt := "2018-09-15T17:32:00Z" + err = parser.parsePair("AnnotationDate", dt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationDate != dt { + t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) + } + + // Annotation type + aType := "REVIEW" + err = parser.parsePair("AnnotationType", aType) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationType != aType { + t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) + } + + // SPDX Identifier Reference + ref := "SPDXRef-30" + err = parser.parsePair("SPDXREF", ref) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + deID := parser.ann.AnnotationSPDXIdentifier + if deID.DocumentRefID != "" || deID.ElementRefID != "30" { + t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") + } + + // Annotation Comment + cmt := "this is a comment" + err = parser.parsePair("AnnotationComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationComment != cmt { + t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) + } +} + +func TestParserFailsIfAnnotatorInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotatorTypeInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "Human: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationRefInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + err = parser.parsePair("SPDXREF", "blah:other") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v2/parse_creation_info.go b/spdx/v2/v2_2/tagvalue/reader/parse_creation_info.go similarity index 80% rename from tvloader/parser2v2/parse_creation_info.go rename to spdx/v2/v2_2/tagvalue/reader/parse_creation_info.go index da258bdf..77e767e4 100644 --- a/tvloader/parser2v2/parse_creation_info.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_creation_info.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromCreationInfo2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromCreationInfo(tag string, value string) error { // fail if not in Creation Info parser state - if parser.st != psCreationInfo2_2 { - return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo2_2", parser.st) + if parser.st != psCreationInfo { + return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo", parser.st) } // create an SPDX Creation Info data struct if we don't have one already @@ -51,49 +51,49 @@ func (parser *tvParser2_2) parsePairFromCreationInfo2_2(tag string, value string // this may be a null case: can we ever have a "last file" in // the "creation info" state? should go on to "file" state // even when parsing unpackaged files. - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_2 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.st = psPackage2_2 + parser.st = psPackage parser.pkg = &v2_2.Package{ FilesAnalyzed: true, IsFilesAnalyzedTagPresent: false, } - return parser.parsePairFromPackage2_2(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to _unpackaged_ file section case "FileName": // leave pkg as nil, so that packages will be placed in Files - parser.st = psFile2_2 + parser.st = psFile parser.pkg = nil - return parser.parsePairFromFile2_2(tag, value) + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_2 - return parser.parsePairFromOtherLicense2_2(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_2 - return parser.parsePairFromReview2_2(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) // for relationship tags, pass along but don't change state case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in CreationInfo section", tag) } diff --git a/tvloader/parser2v2/parse_creation_info_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_creation_info_test.go similarity index 61% rename from tvloader/parser2v2/parse_creation_info_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_creation_info_test.go index dcf60986..ba7fdc51 100644 --- a/tvloader/parser2v2/parse_creation_info_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_creation_info_test.go @@ -1,26 +1,26 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser creation info state change tests ===== -func TestParser2_2CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } pkgName := "testPkg" - err := parser.parsePair2_2("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } // and a package should be created if parser.pkg == nil { @@ -44,18 +44,18 @@ func TestParser2_2CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_2CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } // and current package should be nil, meaning Files are placed in the // Files map instead of in a Package @@ -64,138 +64,138 @@ func TestParser2_2CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing. } } -func TestParser2_2CIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } } -func TestParser2_2CIMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } } -func TestParser2_2CIStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_2CIStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this spdx file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this spdx file") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_2) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_2FailsParsingCreationInfoWithInvalidState(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psPackage2_2, +func TestParserFailsParsingCreationInfoWithInvalidState(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psPackage, } - err := parser.parsePairFromCreationInfo2_2("SPDXVersion", "SPDX-2.2") + err := parser.parsePairFromCreationInfo("SPDXVersion", "SPDX-2.2") if err == nil { t.Errorf("expected non-nil error, got nil") } } // ===== Creation Info section tests ===== -func TestParser2_2HasCreationInfoAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserHasCreationInfoAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_2("LicenseListVersion", "3.9") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "3.9") if err != nil { - t.Errorf("got error when calling parsePairFromCreationInfo2_2: %v", err) + t.Errorf("got error when calling parsePairFromCreationInfo: %v", err) } if parser.doc.CreationInfo == nil { t.Errorf("doc.CreationInfo is still nil after parsing first pair") } } -func TestParser2_2CanParseCreationInfoTags(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCanParseCreationInfoTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } // License List Version - err := parser.parsePairFromCreationInfo2_2("LicenseListVersion", "2.2") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "2.2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -208,11 +208,11 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { "Person: Person A", "Person: Person B", } - err = parser.parsePairFromCreationInfo2_2("Creator", refPersons[0]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_2("Creator", refPersons[1]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -227,11 +227,11 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { "Organization: Organization A", "Organization: Organization B", } - err = parser.parsePairFromCreationInfo2_2("Creator", refOrgs[0]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_2("Creator", refOrgs[1]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -246,11 +246,11 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { "Tool: Tool A", "Tool: Tool B", } - err = parser.parsePairFromCreationInfo2_2("Creator", refTools[0]) + err = parser.parsePairFromCreationInfo("Creator", refTools[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_2("Creator", refTools[1]) + err = parser.parsePairFromCreationInfo("Creator", refTools[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -261,7 +261,7 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { } // Created date - err = parser.parsePairFromCreationInfo2_2("Created", "2018-09-10T11:46:00Z") + err = parser.parsePairFromCreationInfo("Created", "2018-09-10T11:46:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -270,7 +270,7 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { } // Creator Comment - err = parser.parsePairFromCreationInfo2_2("CreatorComment", "Blah whatever") + err = parser.parsePairFromCreationInfo("CreatorComment", "Blah whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -279,56 +279,56 @@ func TestParser2_2CanParseCreationInfoTags(t *testing.T) { } } -func TestParser2_2InvalidCreatorTagsFail(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserInvalidCreatorTagsFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_2("Creator", "blah: somebody") + err := parser.parsePairFromCreationInfo("Creator", "blah: somebody") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } - err = parser.parsePairFromCreationInfo2_2("Creator", "Tool with no colons") + err = parser.parsePairFromCreationInfo("Creator", "Tool with no colons") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } } -func TestParser2_2CreatorTagWithMultipleColonsPasses(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCreatorTagWithMultipleColonsPasses(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_2("Creator", "Tool: tool1:2:3") + err := parser.parsePairFromCreationInfo("Creator", "Tool: tool1:2:3") if err != nil { t.Errorf("unexpected error from parsing valid Creator format") } } -func TestParser2_2CIUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCIUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_2("blah", "something") + err := parser.parsePairFromCreationInfo("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_2CICreatesRelationship(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCICreatesRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -338,15 +338,15 @@ func TestParser2_2CICreatesRelationship(t *testing.T) { } } -func TestParser2_2CICreatesAnnotation(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, +func TestParserCICreatesAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") diff --git a/tvloader/parser2v2/parse_file.go b/spdx/v2/v2_2/tagvalue/reader/parse_file.go similarity index 81% rename from tvloader/parser2v2/parse_file.go rename to spdx/v2/v2_2/tagvalue/reader/parse_file.go index 1828bb0b..25537df5 100644 --- a/tvloader/parser2v2/parse_file.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_file.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromFile2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromFile(tag string, value string) error { // expire fileAOP for anything other than an AOPHomePage or AOPURI // (we'll actually handle the HomePage and URI further below) if tag != "ArtifactOfProjectHomePage" && tag != "ArtifactOfProjectURI" { @@ -20,28 +20,28 @@ func (parser *tvParser2_2) parsePairFromFile2_2(tag string, value string) error // tag for creating new file section case "FileName": // check if the previous file contained an spdx Id or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_2 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.file = &v2_2.File{} parser.file.FileName = value // tag for creating new package section and going back to parsing Package case "PackageName": - parser.st = psPackage2_2 + parser.st = psPackage // check if the previous file contained an spdx Id or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_2 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.file = nil - return parser.parsePairFromPackage2_2(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to snippet section case "SnippetSPDXID": - parser.st = psSnippet2_2 - return parser.parsePairFromSnippet2_2(tag, value) + parser.st = psSnippet + return parser.parsePairFromSnippet(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_2 - return parser.parsePairFromOtherLicense2_2(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for file data case "SPDXID": eID, err := extractElementID(value) @@ -121,26 +121,26 @@ func (parser *tvParser2_2) parsePairFromFile2_2(tag string, value string) error case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_2 - return parser.parsePairFromReview2_2(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in File section", tag) } diff --git a/tvloader/parser2v2/parse_file_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_file_test.go similarity index 73% rename from tvloader/parser2v2/parse_file_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_file_test.go index 0bcea7d7..b7b5533c 100644 --- a/tvloader/parser2v2/parse_file_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_file_test.go @@ -1,22 +1,22 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser file section state change tests ===== -func TestParser2_2FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { +func TestParserFileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // create the first file fileOldName := "f1.txt" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: fileOldName, FileSPDXIdentifier: "f1"}, } @@ -36,13 +36,13 @@ func TestParser2_2FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // now add a new file fileName := "f2.txt" - err := parser.parsePair2_2("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_2 { - t.Errorf("expected state to be %v, got %v", psFile2_2, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and a file should be created if parser.file == nil { @@ -65,9 +65,9 @@ func TestParser2_2FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } // now parse an SPDX identifier tag - err = parser.parsePair2_2("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // and the Package's Files should now be of size 2 and have this new one if len(parser.pkg.Files) != 2 { @@ -87,22 +87,22 @@ func TestParser2_2FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_2FileAddsToPackageOrUnpackagedFiles(t *testing.T) { +func TestParserFileAddsToPackageOrUnpackagedFiles(t *testing.T) { // start with no packages - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{}, - st: psCreationInfo2_2, + st: psCreationInfo, } // add a file and SPDX identifier fileName := "f2.txt" - err := parser.parsePair2_2("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_2("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } fileOld := parser.file // should have been added to Files @@ -113,21 +113,21 @@ func TestParser2_2FileAddsToPackageOrUnpackagedFiles(t *testing.T) { t.Errorf("expected file %v in Files[f2ID], got %v", fileOld, parser.doc.Files[0]) } // now create a package and a new file - err = parser.parsePair2_2("PackageName", "package1") + err = parser.parsePair("PackageName", "package1") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_2("SPDXID", "SPDXRef-pkg1") + err = parser.parsePair("SPDXID", "SPDXRef-pkg1") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_2("FileName", "f3.txt") + err = parser.parsePair("FileName", "f3.txt") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_2("SPDXID", "SPDXRef-f3ID") + err = parser.parsePair("SPDXID", "SPDXRef-f3ID") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // Files should still be size 1 and have old file only if len(parser.doc.Files) != 1 { @@ -145,14 +145,14 @@ func TestParser2_2FileAddsToPackageOrUnpackagedFiles(t *testing.T) { } } -func TestParser2_2FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserFileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first file and package p1Name := "package1" f1Name := "f1.txt" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: p1Name, PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: f1Name, FileSPDXIdentifier: "f1"}, } @@ -163,13 +163,13 @@ func TestParser2_2FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // now add a new package p2Name := "package2" - err := parser.parsePair2_2("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -217,10 +217,10 @@ func TestParser2_2FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_2FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -228,13 +228,13 @@ func TestParser2_2FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) fileCurrent := parser.file - err := parser.parsePair2_2("SnippetSPDXID", "SPDXRef-Test1") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-Test1") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_2 { - t.Errorf("expected state to be %v, got %v", psSnippet2_2, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current file should remain what it was if parser.file != fileCurrent { @@ -242,135 +242,135 @@ func TestParser2_2FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { } } -func TestParser2_2FileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_2FileMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_2FileStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psFile2_2 { - t.Errorf("expected state to be %v, got %v", psFile2_2, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psFile2_2 { - t.Errorf("expected state to be %v, got %v", psFile2_2, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } } -func TestParser2_2FileStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_2) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } } // ===== File data section tests ===== -func TestParser2_2CanParseFileTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseFileTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -384,7 +384,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // File SPDX Identifier - err = parser.parsePairFromFile2_2("SPDXID", "SPDXRef-f1") + err = parser.parsePairFromFile("SPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -405,7 +405,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { "DOCUMENTATION", } for _, ty := range fileTypes { - err = parser.parsePairFromFile2_2("FileType", ty) + err = parser.parsePairFromFile("FileType", ty) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -434,7 +434,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } for algo, tc := range testChecksums { - if err := parser.parsePairFromFile2_2( + if err := parser.parsePairFromFile( "FileChecksum", fmt.Sprintf("%s: %s", algo, tc)); err != nil { t.Errorf("expected error, got %v", err) } @@ -450,7 +450,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // Concluded License - err = parser.parsePairFromFile2_2("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromFile("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -465,7 +465,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromFile2_2("LicenseInfoInFile", lic) + err = parser.parsePairFromFile("LicenseInfoInFile", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -487,7 +487,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // Comments on License - err = parser.parsePairFromFile2_2("LicenseComments", "this is a comment") + err = parser.parsePairFromFile("LicenseComments", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -496,7 +496,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // Copyright Text - err = parser.parsePairFromFile2_2("FileCopyrightText", "copyright (c) me") + err = parser.parsePairFromFile("FileCopyrightText", "copyright (c) me") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -506,38 +506,38 @@ func TestParser2_2CanParseFileTags(t *testing.T) { // Artifact of Projects: Name, HomePage and URI // Artifact set 1 - err = parser.parsePairFromFile2_2("ArtifactOfProjectName", "project1") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_2("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_2("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 2 -- just name - err = parser.parsePairFromFile2_2("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 3 -- just name and home page - err = parser.parsePairFromFile2_2("ArtifactOfProjectName", "project3") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project3") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_2("ArtifactOfProjectHomePage", "http://example.com/3/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/3/") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 4 -- just name and URI - err = parser.parsePairFromFile2_2("ArtifactOfProjectName", "project4") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project4") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_2("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -591,7 +591,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // File Comment - err = parser.parsePairFromFile2_2("FileComment", "this is a comment") + err = parser.parsePairFromFile("FileComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -600,7 +600,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } // File Notice - err = parser.parsePairFromFile2_2("FileNotice", "this is a Notice") + err = parser.parsePairFromFile("FileNotice", "this is a Notice") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -614,7 +614,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { "EvilCorp", } for _, contrib := range contribs { - err = parser.parsePairFromFile2_2("FileContributor", contrib) + err = parser.parsePairFromFile("FileContributor", contrib) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -641,7 +641,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { "g.txt", } for _, dep := range deps { - err = parser.parsePairFromFile2_2("FileDependency", dep) + err = parser.parsePairFromFile("FileDependency", dep) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -668,7 +668,7 @@ func TestParser2_2CanParseFileTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromFile2_2("FileAttributionText", attr) + err = parser.parsePairFromFile("FileAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -691,19 +691,19 @@ func TestParser2_2CanParseFileTags(t *testing.T) { } -func TestParser2_2FileCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -713,19 +713,19 @@ func TestParser2_2FileCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_2FileCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -735,33 +735,33 @@ func TestParser2_2FileCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_2FileUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ +func TestParserFileUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_2("blah", "something") + err := parser.parsePairFromFile("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } func TestFileAOPPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_2("ArtifactOfProjectName", "project1") + err := parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -771,7 +771,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { curPtr := parser.fileAOP // now, a home page; pointer should stay - err = parser.parsePairFromFile2_2("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -780,7 +780,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // a URI; pointer should stay - err = parser.parsePairFromFile2_2("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -790,7 +790,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { // now, another artifact name; pointer should change but be non-nil // now, a home page; pointer should stay - err = parser.parsePairFromFile2_2("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -799,7 +799,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // finally, an unrelated tag; pointer should go away - err = parser.parsePairFromFile2_2("FileComment", "whatever") + err = parser.parsePairFromFile("FileComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -808,115 +808,115 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_2FailsIfInvalidSPDXIDInFileSection(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidSPDXIDInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid SPDX Identifier - err = parser.parsePairFromFile2_2("SPDXID", "whoops") + err = parser.parsePairFromFile("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfInvalidChecksumFormatInFileSection(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidChecksumFormatInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid format for checksum line, missing colon - err = parser.parsePairFromFile2_2("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfUnknownChecksumTypeInFileSection(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfUnknownChecksumTypeInFileSection(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // unknown checksum type - err = parser.parsePairFromFile2_2("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_2("ArtifactOfProjectHomePage", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfArtifactURIBeforeArtifactName(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfArtifactURIBeforeArtifactName(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_2("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_2("ArtifactOfProjectURI", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FilesWithoutSpdxIdThrowError(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowError(t *testing.T) { // case 1: The previous file (packaged or unpackaged) does not contain spdx ID - parser1 := tvParser2_2{ + parser1 := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, file: &v2_2.File{FileName: "FileName"}, } - err := parser1.parsePair2_2("FileName", "f2") + err := parser1.parsePair("FileName", "f2") if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -925,12 +925,12 @@ func TestParser2_2FilesWithoutSpdxIdThrowError(t *testing.T) { // Last unpackaged file before the snippet start fileName := "f2.txt" sid1 := common.ElementID("s1") - parser2 := tvParser2_2{ + parser2 := tvParser{ doc: &v2_2.Document{}, - st: psCreationInfo2_2, + st: psCreationInfo, file: &v2_2.File{FileName: fileName}, } - err = parser2.parsePair2_2("SnippetSPDXID", string(sid1)) + err = parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -938,16 +938,16 @@ func TestParser2_2FilesWithoutSpdxIdThrowError(t *testing.T) { // case 3: Invalid File without snippets // Last unpackaged file before the package starts // Last file of a package and New package starts - parser3 := tvParser2_2{ + parser3 := tvParser{ doc: &v2_2.Document{}, - st: psCreationInfo2_2, + st: psCreationInfo, } fileName = "f3.txt" - err = parser3.parsePair2_2("FileName", fileName) + err = parser3.parsePair("FileName", fileName) if err != nil { t.Errorf("%s", err) } - err = parser3.parsePair2_2("PackageName", "p2") + err = parser3.parsePair("PackageName", "p2") if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v2/parse_other_license.go b/spdx/v2/v2_2/tagvalue/reader/parse_other_license.go similarity index 68% rename from tvloader/parser2v2/parse_other_license.go rename to spdx/v2/v2_2/tagvalue/reader/parse_other_license.go index 95250a60..51ff6718 100644 --- a/tvloader/parser2v2/parse_other_license.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_other_license.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromOtherLicense2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromOtherLicense(tag string, value string) error { switch tag { // tag for creating new other license section case "LicenseID": @@ -27,26 +27,26 @@ func (parser *tvParser2_2) parsePairFromOtherLicense2_2(tag string, value string case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_2 - return parser.parsePairFromReview2_2(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in OtherLicense section", tag) } diff --git a/tvloader/parser2v2/parse_other_license_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_other_license_test.go similarity index 70% rename from tvloader/parser2v2/parse_other_license_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_other_license_test.go index cdba8401..76d86b2e 100644 --- a/tvloader/parser2v2/parse_other_license_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_other_license_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser other license section state change tests ===== -func TestParser2_2OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { +func TestParserOLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { // create the first other license olid1 := "LicenseRef-Lic11" olname1 := "License 11" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -39,26 +39,26 @@ func TestParser2_2OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) // now add a new other license olid2 := "LicenseRef-22" olname2 := "License 22" - err := parser.parsePair2_2("LicenseID", olid2) + err := parser.parsePair("LicenseID", olid2) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and an other license should be created if parser.otherLic == nil { t.Fatalf("parser didn't create new other license") } // also parse the new license's name - err = parser.parsePair2_2("LicenseName", olname2) + err = parser.parsePair("LicenseName", olname2) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still be correct - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the other license name should be as expected if parser.otherLic.LicenseName != olname2 { @@ -88,10 +88,10 @@ func TestParser2_2OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) } } -func TestParser2_2OLMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserOLMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -99,19 +99,19 @@ func TestParser2_2OLMovesToReviewAfterParsingReviewerTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_2("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_2OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserOtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -123,13 +123,13 @@ func TestParser2_2OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -140,20 +140,20 @@ func TestParser2_2OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_2OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserOtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -165,44 +165,44 @@ func TestParser2_2OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } // and the annotation should be in the Document's Annotations @@ -214,10 +214,10 @@ func TestParser2_2OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_2OLFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserOLFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -230,25 +230,25 @@ func TestParser2_2OLFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // can't go back to old sections - err := parser.parsePair2_2("SPDXVersion", "SPDX-2.2") + err := parser.parsePair("SPDXVersion", "SPDX-2.2") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_2("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_2("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Other License data section tests ===== -func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseOtherLicenseTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -257,7 +257,7 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // License Identifier - err := parser.parsePairFromOtherLicense2_2("LicenseID", "LicenseRef-Lic11") + err := parser.parsePairFromOtherLicense("LicenseID", "LicenseRef-Lic11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -266,7 +266,7 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { } // Extracted Text - err = parser.parsePairFromOtherLicense2_2("ExtractedText", "You are permitted to do anything with the software, hooray!") + err = parser.parsePairFromOtherLicense("ExtractedText", "You are permitted to do anything with the software, hooray!") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -275,7 +275,7 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { } // License Name - err = parser.parsePairFromOtherLicense2_2("LicenseName", "License 11") + err = parser.parsePairFromOtherLicense("LicenseName", "License 11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -290,7 +290,7 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { "https://example.com/3", } for _, cr := range crossRefs { - err = parser.parsePairFromOtherLicense2_2("LicenseCrossReference", cr) + err = parser.parsePairFromOtherLicense("LicenseCrossReference", cr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -312,7 +312,7 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { } // License Comment - err = parser.parsePairFromOtherLicense2_2("LicenseComment", "this is a comment") + err = parser.parsePairFromOtherLicense("LicenseComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -321,10 +321,10 @@ func TestParser2_2CanParseOtherLicenseTags(t *testing.T) { } } -func TestParser2_2OLUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ +func TestParserOLUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psOtherLicense2_2, + st: psOtherLicense, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } @@ -332,7 +332,7 @@ func TestParser2_2OLUnknownTagFails(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePairFromOtherLicense2_2("blah", "something") + err := parser.parsePairFromOtherLicense("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v2/parse_package.go b/spdx/v2/v2_2/tagvalue/reader/parse_package.go similarity index 89% rename from tvloader/parser2v2/parse_package.go rename to spdx/v2/v2_2/tagvalue/reader/parse_package.go index dbea6df7..c6a17b02 100644 --- a/tvloader/parser2v2/parse_package.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_package.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromPackage2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromPackage(tag string, value string) error { // expire pkgExtRef for anything other than a comment // (we'll actually handle the comment further below) if tag != "ExternalRefComment" { @@ -22,7 +22,7 @@ func (parser *tvParser2_2) parsePairFromPackage2_2(tag string, value string) err // if package already has a name, create and go on to a new package if parser.pkg == nil || parser.pkg.PackageName != "" { // check if the previous package contained an spdx Id or not - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_2 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } parser.pkg = &v2_2.Package{ @@ -33,12 +33,12 @@ func (parser *tvParser2_2) parsePairFromPackage2_2(tag string, value string) err parser.pkg.PackageName = value // tag for going on to file section case "FileName": - parser.st = psFile2_2 - return parser.parsePairFromFile2_2(tag, value) + parser.st = psFile + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_2 - return parser.parsePairFromOtherLicense2_2(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) case "SPDXID": eID, err := extractElementID(value) if err != nil { @@ -168,26 +168,26 @@ func (parser *tvParser2_2) parsePairFromPackage2_2(tag string, value string) err case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_2 - return parser.parsePairFromReview2_2(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Package section", tag) } diff --git a/tvloader/parser2v2/parse_package_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_package_test.go similarity index 72% rename from tvloader/parser2v2/parse_package_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_package_test.go index e07cf5ad..92ed2725 100644 --- a/tvloader/parser2v2/parse_package_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_package_test.go @@ -1,22 +1,22 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser package section state change tests ===== -func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first package pkgOldName := "p1" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: pkgOldName, PackageSPDXIdentifier: "p1"}, } pkgOld := parser.pkg @@ -31,13 +31,13 @@ func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package pkgName := "p2" - err := parser.parsePair2_2("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -67,12 +67,12 @@ func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { // pkg is nil, so that Files appearing before the first PackageName tag // are added to Files instead of Packages - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psFile2_2, + st: psFile, pkg: nil, } // the Document's Packages should be empty @@ -82,13 +82,13 @@ func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack // now add a new package pkgName := "p2" - err := parser.parsePair2_2("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -112,22 +112,22 @@ func TestParser2_2PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack } } -func TestParser2_2PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageMovesToFileAfterParsingFileNameTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) pkgCurrent := parser.pkg - err := parser.parsePair2_2("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_2 { - t.Errorf("expected state to be %v, got %v", psFile2_2, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and current package should remain what it was if parser.pkg != pkgCurrent { @@ -135,121 +135,121 @@ func TestParser2_2PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_2PackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_2PackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_2PackageStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } } -func TestParser2_2PackageStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this package") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this package") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_2) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } } // ===== Package data section tests ===== -func TestParser2_2CanParsePackageTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } @@ -259,7 +259,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -272,7 +272,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package SPDX Identifier - err = parser.parsePairFromPackage2_2("SPDXID", "SPDXRef-p1") + err = parser.parsePairFromPackage("SPDXID", "SPDXRef-p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -289,7 +289,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Version - err = parser.parsePairFromPackage2_2("PackageVersion", "2.1.1") + err = parser.parsePairFromPackage("PackageVersion", "2.1.1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -298,7 +298,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package File Name - err = parser.parsePairFromPackage2_2("PackageFileName", "p1-2.1.1.tar.gz") + err = parser.parsePairFromPackage("PackageFileName", "p1-2.1.1.tar.gz") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -313,7 +313,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { // SKIP -- separate tests for subvalues below // Package Download Location - err = parser.parsePairFromPackage2_2("PackageDownloadLocation", "https://example.com/whatever") + err = parser.parsePairFromPackage("PackageDownloadLocation", "https://example.com/whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -322,7 +322,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Files Analyzed - err = parser.parsePairFromPackage2_2("FilesAnalyzed", "false") + err = parser.parsePairFromPackage("FilesAnalyzed", "false") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -344,7 +344,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } for algo, tc := range testChecksums { - if err := parser.parsePairFromPackage2_2( + if err := parser.parsePairFromPackage( "PackageChecksum", fmt.Sprintf("%s: %s", algo, tc)); err != nil { t.Errorf("expected error, got %v", err) } @@ -360,7 +360,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Home Page - err = parser.parsePairFromPackage2_2("PackageHomePage", "https://example.com/whatever2") + err = parser.parsePairFromPackage("PackageHomePage", "https://example.com/whatever2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -369,7 +369,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Source Info - err = parser.parsePairFromPackage2_2("PackageSourceInfo", "random comment") + err = parser.parsePairFromPackage("PackageSourceInfo", "random comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -378,7 +378,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package License Concluded - err = parser.parsePairFromPackage2_2("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -393,7 +393,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromPackage2_2("PackageLicenseInfoFromFiles", lic) + err = parser.parsePairFromPackage("PackageLicenseInfoFromFiles", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -415,7 +415,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package License Declared - err = parser.parsePairFromPackage2_2("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -424,7 +424,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package License Comments - err = parser.parsePairFromPackage2_2("PackageLicenseComments", "this is a license comment") + err = parser.parsePairFromPackage("PackageLicenseComments", "this is a license comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -433,7 +433,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Copyright Text - err = parser.parsePairFromPackage2_2("PackageCopyrightText", "Copyright (c) me myself and i") + err = parser.parsePairFromPackage("PackageCopyrightText", "Copyright (c) me myself and i") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -442,7 +442,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Summary - err = parser.parsePairFromPackage2_2("PackageSummary", "i wrote this package") + err = parser.parsePairFromPackage("PackageSummary", "i wrote this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -451,7 +451,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Description - err = parser.parsePairFromPackage2_2("PackageDescription", "i wrote this package a lot") + err = parser.parsePairFromPackage("PackageDescription", "i wrote this package a lot") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -460,7 +460,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } // Package Comment - err = parser.parsePairFromPackage2_2("PackageComment", "i scanned this package") + err = parser.parsePairFromPackage("PackageComment", "i scanned this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -474,7 +474,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromPackage2_2("PackageAttributionText", attr) + err = parser.parsePairFromPackage("PackageAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -506,7 +506,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { ref2Type := "LocationRef-acmeforge" ref2Locator := "acmecorp/acmenator/4.1.3alpha" ref2Comment := "this is comment #2" - err = parser.parsePairFromPackage2_2("ExternalRef", ref1) + err = parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -522,11 +522,11 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[0] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[0], got no match") } - err = parser.parsePairFromPackage2_2("ExternalRefComment", ref1Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref1Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromPackage2_2("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -542,7 +542,7 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[1] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[1], got no match") } - err = parser.parsePairFromPackage2_2("ExternalRefComment", ref2Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref2Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -576,16 +576,16 @@ func TestParser2_2CanParsePackageTags(t *testing.T) { } -func TestParser2_2CanParsePackageSupplierPersonTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageSupplierPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Person - err := parser.parsePairFromPackage2_2("PackageSupplier", "Person: John Doe") + err := parser.parsePairFromPackage("PackageSupplier", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -594,16 +594,16 @@ func TestParser2_2CanParsePackageSupplierPersonTag(t *testing.T) { } } -func TestParser2_2CanParsePackageSupplierOrganizationTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageSupplierOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Organization - err := parser.parsePairFromPackage2_2("PackageSupplier", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageSupplier", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -612,16 +612,16 @@ func TestParser2_2CanParsePackageSupplierOrganizationTag(t *testing.T) { } } -func TestParser2_2CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageSupplierNOASSERTIONTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: NOASSERTION - err := parser.parsePairFromPackage2_2("PackageSupplier", "NOASSERTION") + err := parser.parsePairFromPackage("PackageSupplier", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -630,16 +630,16 @@ func TestParser2_2CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { } } -func TestParser2_2CanParsePackageOriginatorPersonTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageOriginatorPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Person - err := parser.parsePairFromPackage2_2("PackageOriginator", "Person: John Doe") + err := parser.parsePairFromPackage("PackageOriginator", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -648,16 +648,16 @@ func TestParser2_2CanParsePackageOriginatorPersonTag(t *testing.T) { } } -func TestParser2_2CanParsePackageOriginatorOrganizationTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageOriginatorOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Organization - err := parser.parsePairFromPackage2_2("PackageOriginator", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageOriginator", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -666,16 +666,16 @@ func TestParser2_2CanParsePackageOriginatorOrganizationTag(t *testing.T) { } } -func TestParser2_2CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: NOASSERTION - err := parser.parsePairFromPackage2_2("PackageOriginator", "NOASSERTION") + err := parser.parsePairFromPackage("PackageOriginator", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -684,10 +684,10 @@ func TestParser2_2CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { } } -func TestParser2_2CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) @@ -696,7 +696,7 @@ func TestParser2_2CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { code := "d6a770ba38583ed4bb4525bd96e50461655d2758" fileName := "./package.spdx" fullCodeValue := "d6a770ba38583ed4bb4525bd96e50461655d2758 (excludes: ./package.spdx)" - err := parser.parsePairFromPackage2_2("PackageVerificationCode", fullCodeValue) + err := parser.parsePairFromPackage("PackageVerificationCode", fullCodeValue) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -709,17 +709,17 @@ func TestParser2_2CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { } -func TestParser2_2CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Verification Code without excludes parenthetical code := "d6a770ba38583ed4bb4525bd96e50461655d2758" - err := parser.parsePairFromPackage2_2("PackageVerificationCode", code) + err := parser.parsePairFromPackage("PackageVerificationCode", code) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -732,16 +732,16 @@ func TestParser2_2CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T } -func TestParser2_2PackageExternalRefPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageExternalRefPointerChangesAfterTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) ref1 := "SECURITY cpe23Type cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*" - err := parser.parsePairFromPackage2_2("ExternalRef", ref1) + err := parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -750,7 +750,7 @@ func TestParser2_2PackageExternalRefPointerChangesAfterTags(t *testing.T) { } // now, a comment; pointer should go away - err = parser.parsePairFromPackage2_2("ExternalRefComment", "whatever") + err = parser.parsePairFromPackage("ExternalRefComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -759,7 +759,7 @@ func TestParser2_2PackageExternalRefPointerChangesAfterTags(t *testing.T) { } ref2 := "Other LocationRef-something https://example.com/whatever" - err = parser.parsePairFromPackage2_2("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -768,7 +768,7 @@ func TestParser2_2PackageExternalRefPointerChangesAfterTags(t *testing.T) { } // and some other random tag makes the pointer go away too - err = parser.parsePairFromPackage2_2("PackageSummary", "whatever") + err = parser.parsePairFromPackage("PackageSummary", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -777,17 +777,17 @@ func TestParser2_2PackageExternalRefPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_2PackageCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -797,17 +797,17 @@ func TestParser2_2PackageCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_2PackageCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -817,129 +817,129 @@ func TestParser2_2PackageCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_2PackageUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ +func TestParserPackageUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePairFromPackage2_2("blah", "something") + err := parser.parsePairFromPackage("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_2FailsIfInvalidSPDXIDInPackageSection(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidSPDXIDInPackageSection(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid ID format - err = parser.parsePairFromPackage2_2("SPDXID", "whoops") + err = parser.parsePairFromPackage("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfInvalidPackageSupplierFormat(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidPackageSupplierFormat(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier format - err = parser.parsePairFromPackage2_2("PackageSupplier", "whoops") + err = parser.parsePairFromPackage("PackageSupplier", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfUnknownPackageSupplierType(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfUnknownPackageSupplierType(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier type - err = parser.parsePairFromPackage2_2("PackageSupplier", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageSupplier", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfInvalidPackageOriginatorFormat(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidPackageOriginatorFormat(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator format - err = parser.parsePairFromPackage2_2("PackageOriginator", "whoops") + err = parser.parsePairFromPackage("PackageOriginator", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfUnknownPackageOriginatorType(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfUnknownPackageOriginatorType(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator type - err = parser.parsePairFromPackage2_2("PackageOriginator", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageOriginator", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2SetsFilesAnalyzedTagsCorrectly(t *testing.T) { - parser := tvParser2_2{ +func TestParserSetsFilesAnalyzedTagsCorrectly(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // set tag - err = parser.parsePairFromPackage2_2("FilesAnalyzed", "true") + err = parser.parsePairFromPackage("FilesAnalyzed", "true") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -951,77 +951,77 @@ func TestParser2_2SetsFilesAnalyzedTagsCorrectly(t *testing.T) { } } -func TestParser2_2FailsIfInvalidPackageChecksumFormat(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidPackageChecksumFormat(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum format - err = parser.parsePairFromPackage2_2("PackageChecksum", "whoops") + err = parser.parsePairFromPackage("PackageChecksum", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfInvalidPackageChecksumType(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidPackageChecksumType(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum type - err = parser.parsePairFromPackage2_2("PackageChecksum", "whoops: blah") + err = parser.parsePairFromPackage("PackageChecksum", "whoops: blah") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfInvalidExternalRefFormat(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfInvalidExternalRefFormat(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid external ref format - err = parser.parsePairFromPackage2_2("ExternalRef", "whoops") + err = parser.parsePairFromPackage("ExternalRef", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_2("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // external ref comment before external ref - err = parser.parsePairFromPackage2_2("ExternalRefComment", "whoops") + err = parser.parsePairFromPackage("ExternalRefComment", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } @@ -1092,12 +1092,12 @@ func TestFailsPackageExternalRefWithInvalidFormat(t *testing.T) { } } -func TestParser2_2PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { +func TestParserPackageWithoutSpdxIdentifierThrowsError(t *testing.T) { // More than one package, the previous package doesn't contain an SPDX ID pkgOldName := "p1" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psPackage2_2, + st: psPackage, pkg: &v2_2.Package{PackageName: pkgOldName}, } pkgOld := parser.pkg @@ -1111,7 +1111,7 @@ func TestParser2_2PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { } pkgName := "p2" - err := parser.parsePair2_2("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err == nil { t.Errorf("package without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v3/parse_relationship.go b/spdx/v2/v2_2/tagvalue/reader/parse_relationship.go similarity index 91% rename from tvloader/parser2v3/parse_relationship.go rename to spdx/v2/v2_2/tagvalue/reader/parse_relationship.go index 8f49417d..db2a3986 100644 --- a/tvloader/parser2v3/parse_relationship.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_relationship.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" ) -func (parser *tvParser2_3) parsePairForRelationship2_3(tag string, value string) error { +func (parser *tvParser) parsePairForRelationship(tag string, value string) error { if parser.rln == nil { return fmt.Errorf("no relationship struct created in parser rln pointer") } diff --git a/spdx/v2/v2_2/tagvalue/reader/parse_relationship_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_relationship_test.go new file mode 100644 index 00000000..539c1e0b --- /dev/null +++ b/spdx/v2/v2_2/tagvalue/reader/parse_relationship_test.go @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" +) + +// ===== Relationship section tests ===== +func TestParserFailsIfRelationshipNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForRelationship("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") + if err == nil { + t.Errorf("expected error when calling parsePairFromRelationship without setting rln pointer") + } +} + +func TestParserFailsIfRelationshipCommentWithoutRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("RelationshipComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for RelationshipComment without Relationship first") + } +} + +func TestParserCanParseRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { + t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) + } + if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { + t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) + } + if parser.rln.Relationship != "CONTAINS" { + t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) + } + + // Relationship Comment + cmt := "this is a comment" + err = parser.parsePair("RelationshipComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RelationshipComment != cmt { + t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) + } +} + +func TestParserInvalidRelationshipTagsNoValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // no items + parser.rln = nil + err := parser.parsePair("Relationship", "") + if err == nil { + t.Errorf("expected error for empty items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsOneValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // one item + parser.rln = nil + err := parser.parsePair("Relationship", "DESCRIBES") + if err == nil { + t.Errorf("expected error for only one item in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsTwoValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // two items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-DOCUMENT DESCRIBES") + if err == nil { + t.Errorf("expected error for only two items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // three items but with interspersed additional whitespace + parser.rln = nil + err := parser.parsePair("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") + if err != nil { + t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) + } +} + +func TestParserInvalidRelationshipTagsFourValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") + if err == nil { + t.Errorf("expected error for more than three items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsInvalidRefIDs(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } + + parser.rln = nil + err = parser.parsePair("Relationship", "a DESCRIBES SPDXRef-b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } +} + +func TestParserSpecialValuesValidForRightSideOfRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // NONE in right side of relationship should pass + err := parser.parsePair("Relationship", "SPDXRef-a CONTAINS NONE") + if err != nil { + t.Errorf("expected nil error for CONTAINS NONE, got %v", err) + } + + // NOASSERTION in right side of relationship should pass + err = parser.parsePair("Relationship", "SPDXRef-a CONTAINS NOASSERTION") + if err != nil { + t.Errorf("expected nil error for CONTAINS NOASSERTION, got %v", err) + } + + // NONE in left side of relationship should fail + err = parser.parsePair("Relationship", "NONE CONTAINS SPDXRef-a") + if err == nil { + t.Errorf("expected non-nil error for NONE CONTAINS, got nil") + } + + // NOASSERTION in left side of relationship should fail + err = parser.parsePair("Relationship", "NOASSERTION CONTAINS SPDXRef-a") + if err == nil { + t.Errorf("expected non-nil error for NOASSERTION CONTAINS, got nil") + } +} + +func TestParserFailsToParseUnknownTagInRelationshipSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // invalid tag + err = parser.parsePairForRelationship("blah", "whoops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v2/parse_review.go b/spdx/v2/v2_2/tagvalue/reader/parse_review.go similarity index 72% rename from tvloader/parser2v2/parse_review.go rename to spdx/v2/v2_2/tagvalue/reader/parse_review.go index 09691c87..5d689577 100644 --- a/tvloader/parser2v2/parse_review.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_review.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromReview2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromReview(tag string, value string) error { switch tag { // tag for creating new review section case "Reviewer": @@ -39,22 +39,22 @@ func (parser *tvParser2_2) parsePairFromReview2_2(tag string, value string) erro case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in Review section", tag) } diff --git a/tvloader/parser2v2/parse_review_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_review_test.go similarity index 74% rename from tvloader/parser2v2/parse_review_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_review_test.go index f93afbf8..8f65bdbc 100644 --- a/tvloader/parser2v2/parse_review_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_review_test.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser review section state change tests ===== -func TestParser2_2ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { +func TestParserReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // create the first review rev1 := "John Doe" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -45,13 +45,13 @@ func TestParser2_2ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // now add a new review rev2 := "Steve" rp2 := "Person: Steve" - err := parser.parsePair2_2("Reviewer", rp2) + err := parser.parsePair("Reviewer", rp2) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and a review should be created if parser.rev == nil { @@ -80,10 +80,10 @@ func TestParser2_2ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { } -func TestParser2_2ReviewStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserReviewStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -100,13 +100,13 @@ func TestParser2_2ReviewStaysAfterParsingRelationshipTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -117,20 +117,20 @@ func TestParser2_2ReviewStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_2ReviewStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserReviewStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -147,44 +147,44 @@ func TestParser2_2ReviewStaysAfterParsingAnnotationTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_2) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } // and the annotation should be in the Document's Annotations @@ -196,10 +196,10 @@ func TestParser2_2ReviewStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_2ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserReviewFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -214,29 +214,29 @@ func TestParser2_2ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // can't go back to old sections - err := parser.parsePair2_2("SPDXVersion", "SPDX-2.2") + err := parser.parsePair("SPDXVersion", "SPDX-2.2") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_2("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_2("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_2("LicenseID", "LicenseRef-Lic22") + err = parser.parsePair("LicenseID", "LicenseRef-Lic22") if err == nil { - t.Errorf("expected error when calling parsePair2_2, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Review data section tests ===== -func TestParser2_2CanParseReviewTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseReviewTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -254,7 +254,7 @@ func TestParser2_2CanParseReviewTags(t *testing.T) { // handled in subsequent subtests // Review Date (DEPRECATED) - err := parser.parsePairFromReview2_2("ReviewDate", "2018-09-23T08:30:00Z") + err := parser.parsePairFromReview("ReviewDate", "2018-09-23T08:30:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -263,7 +263,7 @@ func TestParser2_2CanParseReviewTags(t *testing.T) { } // Review Comment (DEPRECATED) - err = parser.parsePairFromReview2_2("ReviewComment", "this is a comment") + err = parser.parsePairFromReview("ReviewComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -272,10 +272,10 @@ func TestParser2_2CanParseReviewTags(t *testing.T) { } } -func TestParser2_2CanParseReviewerPersonTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseReviewerPersonTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -290,7 +290,7 @@ func TestParser2_2CanParseReviewerPersonTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Person - err := parser.parsePairFromReview2_2("Reviewer", "Person: John Doe") + err := parser.parsePairFromReview("Reviewer", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -302,10 +302,10 @@ func TestParser2_2CanParseReviewerPersonTag(t *testing.T) { } } -func TestParser2_2CanParseReviewerOrganizationTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseReviewerOrganizationTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -320,7 +320,7 @@ func TestParser2_2CanParseReviewerOrganizationTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Organization - err := parser.parsePairFromReview2_2("Reviewer", "Organization: John Doe, Inc.") + err := parser.parsePairFromReview("Reviewer", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -332,10 +332,10 @@ func TestParser2_2CanParseReviewerOrganizationTag(t *testing.T) { } } -func TestParser2_2CanParseReviewerToolTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseReviewerToolTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -350,7 +350,7 @@ func TestParser2_2CanParseReviewerToolTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Tool - err := parser.parsePairFromReview2_2("Reviewer", "Tool: scannertool - 1.2.12") + err := parser.parsePairFromReview("Reviewer", "Tool: scannertool - 1.2.12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -362,38 +362,38 @@ func TestParser2_2CanParseReviewerToolTag(t *testing.T) { } } -func TestParser2_2FailsIfReviewerInvalidFormat(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfReviewerInvalidFormat(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, rev: &v2_2.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_2("Reviewer", "oops") + err := parser.parsePairFromReview("Reviewer", "oops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsIfReviewerUnknownType(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsIfReviewerUnknownType(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, rev: &v2_2.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_2("Reviewer", "whoops: John Doe") + err := parser.parsePairFromReview("Reviewer", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2ReviewUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ +func TestParserReviewUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psReview2_2, + st: psReview, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, otherLic: &v2_2.OtherLicense{ @@ -407,7 +407,7 @@ func TestParser2_2ReviewUnknownTagFails(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_2("blah", "something") + err := parser.parsePairFromReview("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v2/parse_snippet.go b/spdx/v2/v2_2/tagvalue/reader/parse_snippet.go similarity index 80% rename from tvloader/parser2v2/parse_snippet.go rename to spdx/v2/v2_2/tagvalue/reader/parse_snippet.go index 5b4f41a0..a769a790 100644 --- a/tvloader/parser2v2/parse_snippet.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_snippet.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strconv" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func (parser *tvParser2_2) parsePairFromSnippet2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromSnippet(tag string, value string) error { switch tag { // tag for creating new snippet section case "SnippetSPDXID": // check here whether the file contained an SPDX ID or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_2 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.snippet = &v2_2.Snippet{} @@ -33,19 +33,19 @@ func (parser *tvParser2_2) parsePairFromSnippet2_2(tag string, value string) err parser.snippet.SnippetSPDXIdentifier = eID // tag for creating new file section and going back to parsing File case "FileName": - parser.st = psFile2_2 + parser.st = psFile parser.snippet = nil - return parser.parsePairFromFile2_2(tag, value) + return parser.parsePairFromFile(tag, value) // tag for creating new package section and going back to parsing Package case "PackageName": - parser.st = psPackage2_2 + parser.st = psPackage parser.file = nil parser.snippet = nil - return parser.parsePairFromPackage2_2(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_2 - return parser.parsePairFromOtherLicense2_2(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for snippet data case "SnippetFromFileSPDXID": deID, err := extractDocElementID(value) @@ -109,26 +109,26 @@ func (parser *tvParser2_2) parsePairFromSnippet2_2(tag string, value string) err case "Relationship": parser.rln = &v2_2.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_2(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": parser.ann = &v2_2.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_2(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_2 - return parser.parsePairFromReview2_2(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Snippet section", tag) } diff --git a/tvloader/parser2v2/parse_snippet_test.go b/spdx/v2/v2_2/tagvalue/reader/parse_snippet_test.go similarity index 75% rename from tvloader/parser2v2/parse_snippet_test.go rename to spdx/v2/v2_2/tagvalue/reader/parse_snippet_test.go index 1d6ddb2b..09c94732 100644 --- a/tvloader/parser2v2/parse_snippet_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parse_snippet_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Parser snippet section state change tests ===== -func TestParser2_2SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { +func TestParserSnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { // create the first snippet sid1 := common.ElementID("s1") - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: sid1}, @@ -36,13 +36,13 @@ func TestParser2_2SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } // now add a new snippet - err := parser.parsePair2_2("SnippetSPDXID", "SPDXRef-s2") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-s2") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_2 { - t.Errorf("expected state to be %v, got %v", psSnippet2_2, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and a snippet should be created if parser.snippet == nil { @@ -70,10 +70,10 @@ func TestParser2_2SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } } -func TestParser2_2SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -86,13 +86,13 @@ func TestParser2_2SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package p2Name := "package2" - err := parser.parsePair2_2("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_2 { - t.Errorf("expected state to be %v, got %v", psPackage2_2, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -144,11 +144,11 @@ func TestParser2_2SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_2SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { +func TestParserSnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { f1Name := "f1.txt" - parser := tvParser2_2{ + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -160,13 +160,13 @@ func TestParser2_2SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { parser.file.Snippets["s1"] = parser.snippet f2Name := "f2.txt" - err := parser.parsePair2_2("FileName", f2Name) + err := parser.parsePair("FileName", f2Name) if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_2 { - t.Errorf("expected state to be %v, got %v", psSnippet2_2, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current package should remain what it was if parser.pkg != p1 { @@ -197,10 +197,10 @@ func TestParser2_2SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_2SnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -209,19 +209,19 @@ func TestParser2_2SnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing. parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_2("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_2 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_2SnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -230,19 +230,19 @@ func TestParser2_2SnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_2("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_2 { - t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_2SnippetStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -251,13 +251,13 @@ func TestParser2_2SnippetStaysAfterParsingRelationshipTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psSnippet2_2 { - t.Errorf("expected state to be %v, got %v", psSnippet2_2, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -268,20 +268,20 @@ func TestParser2_2SnippetStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_2("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psSnippet2_2 { - t.Errorf("expected state to be %v, got %v", psSnippet2_2, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } } -func TestParser2_2SnippetStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -290,44 +290,44 @@ func TestParser2_2SnippetStaysAfterParsingAnnotationTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_2("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_2) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_2) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_2("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_2) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_2) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_2 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_2) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } // and the annotation should be in the Document's Annotations @@ -340,10 +340,10 @@ func TestParser2_2SnippetStaysAfterParsingAnnotationTags(t *testing.T) { } // ===== Snippet data section tests ===== -func TestParser2_2CanParseSnippetTags(t *testing.T) { - parser := tvParser2_2{ +func TestParserCanParseSnippetTags(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{}, @@ -352,7 +352,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_2("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -361,7 +361,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet from File SPDX Identifier - err = parser.parsePairFromSnippet2_2("SnippetFromFileSPDXID", "SPDXRef-f1") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -371,7 +371,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Byte Range - err = parser.parsePairFromSnippet2_2("SnippetByteRange", "20:320") + err = parser.parsePairFromSnippet("SnippetByteRange", "20:320") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -383,7 +383,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Line Range - err = parser.parsePairFromSnippet2_2("SnippetLineRange", "5:12") + err = parser.parsePairFromSnippet("SnippetLineRange", "5:12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -395,7 +395,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Concluded License - err = parser.parsePairFromSnippet2_2("SnippetLicenseConcluded", "BSD-3-Clause") + err = parser.parsePairFromSnippet("SnippetLicenseConcluded", "BSD-3-Clause") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -410,7 +410,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromSnippet2_2("LicenseInfoInSnippet", lic) + err = parser.parsePairFromSnippet("LicenseInfoInSnippet", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -432,7 +432,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Comments on License - err = parser.parsePairFromSnippet2_2("SnippetLicenseComments", "this is a comment about the licenses") + err = parser.parsePairFromSnippet("SnippetLicenseComments", "this is a comment about the licenses") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -441,7 +441,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Copyright Text - err = parser.parsePairFromSnippet2_2("SnippetCopyrightText", "copyright (c) John Doe and friends") + err = parser.parsePairFromSnippet("SnippetCopyrightText", "copyright (c) John Doe and friends") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -450,7 +450,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Comment - err = parser.parsePairFromSnippet2_2("SnippetComment", "this is a comment about the snippet") + err = parser.parsePairFromSnippet("SnippetComment", "this is a comment about the snippet") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -459,7 +459,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } // Snippet Name - err = parser.parsePairFromSnippet2_2("SnippetName", "from some other package called abc") + err = parser.parsePairFromSnippet("SnippetName", "from some other package called abc") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -473,7 +473,7 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromSnippet2_2("SnippetAttributionText", attr) + err = parser.parsePairFromSnippet("SnippetAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -496,10 +496,10 @@ func TestParser2_2CanParseSnippetTags(t *testing.T) { } -func TestParser2_2SnippetUnknownTagFails(t *testing.T) { - parser := tvParser2_2{ +func TestParserSnippetUnknownTagFails(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{SnippetSPDXIdentifier: "s1"}, @@ -507,16 +507,16 @@ func TestParser2_2SnippetUnknownTagFails(t *testing.T) { parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromSnippet2_2("blah", "something") + err := parser.parsePairFromSnippet("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_2FailsForInvalidSnippetSPDXID(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsForInvalidSnippetSPDXID(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{}, @@ -525,16 +525,16 @@ func TestParser2_2FailsForInvalidSnippetSPDXID(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // invalid Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_2("SnippetSPDXID", "whoops") + err := parser.parsePairFromSnippet("SnippetSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsForInvalidSnippetFromFileSPDXID(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsForInvalidSnippetFromFileSPDXID(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{}, @@ -543,21 +543,21 @@ func TestParser2_2FailsForInvalidSnippetFromFileSPDXID(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_2("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid From File identifier - err = parser.parsePairFromSnippet2_2("SnippetFromFileSPDXID", "whoops") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsForInvalidSnippetByteValues(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsForInvalidSnippetByteValues(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{}, @@ -566,29 +566,29 @@ func TestParser2_2FailsForInvalidSnippetByteValues(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_2("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_2("SnippetByteRange", "200 210") + err = parser.parsePairFromSnippet("SnippetByteRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_2("SnippetByteRange", "a:210") + err = parser.parsePairFromSnippet("SnippetByteRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_2("SnippetByteRange", "200:a") + err = parser.parsePairFromSnippet("SnippetByteRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FailsForInvalidSnippetLineValues(t *testing.T) { - parser := tvParser2_2{ +func TestParserFailsForInvalidSnippetLineValues(t *testing.T) { + parser := tvParser{ doc: &v2_2.Document{Packages: []*v2_2.Package{}}, - st: psSnippet2_2, + st: psSnippet, pkg: &v2_2.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_2.File{}}, file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_2.Snippet{}}, snippet: &v2_2.Snippet{}, @@ -597,37 +597,37 @@ func TestParser2_2FailsForInvalidSnippetLineValues(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_2("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_2("SnippetLineRange", "200 210") + err = parser.parsePairFromSnippet("SnippetLineRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_2("SnippetLineRange", "a:210") + err = parser.parsePairFromSnippet("SnippetLineRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_2("SnippetLineRange", "200:a") + err = parser.parsePairFromSnippet("SnippetLineRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { // Invalid file with snippet // Last unpackaged file before the snippet starts // Last file of a package and New package starts fileName := "f2.txt" sid1 := common.ElementID("s1") - parser2 := tvParser2_2{ + parser2 := tvParser{ doc: &v2_2.Document{}, - st: psCreationInfo2_2, + st: psCreationInfo, file: &v2_2.File{FileName: fileName}, } - err := parser2.parsePair2_2("SnippetSPDXID", string(sid1)) + err := parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v2/parser.go b/spdx/v2/v2_2/tagvalue/reader/parser.go similarity index 60% rename from tvloader/parser2v2/parser.go rename to spdx/v2/v2_2/tagvalue/reader/parser.go index 72f67beb..c936258e 100644 --- a/tvloader/parser2v2/parser.go +++ b/spdx/v2/v2_2/tagvalue/reader/parser.go @@ -1,65 +1,65 @@ // Package parser2v2 contains functions to read, load and parse // SPDX tag-value files, version 2.2. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/tvloader/reader" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ParseTagValues takes a list of (tag, value) pairs, parses it and returns // a pointer to a parsed SPDX Document. -func ParseTagValues(tvs []reader.TagValuePair) (*v2_2.Document, error) { - parser := tvParser2_2{} +func ParseTagValues(tvs []reader.TagValuePair) (*spdx.Document, error) { + parser := tvParser{} for _, tv := range tvs { - err := parser.parsePair2_2(tv.Tag, tv.Value) + err := parser.parsePair(tv.Tag, tv.Value) if err != nil { return nil, err } } - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_2 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_2 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } return parser.doc, nil } -func (parser *tvParser2_2) parsePair2_2(tag string, value string) error { +func (parser *tvParser) parsePair(tag string, value string) error { switch parser.st { - case psStart2_2: - return parser.parsePairFromStart2_2(tag, value) - case psCreationInfo2_2: - return parser.parsePairFromCreationInfo2_2(tag, value) - case psPackage2_2: - return parser.parsePairFromPackage2_2(tag, value) - case psFile2_2: - return parser.parsePairFromFile2_2(tag, value) - case psSnippet2_2: - return parser.parsePairFromSnippet2_2(tag, value) - case psOtherLicense2_2: - return parser.parsePairFromOtherLicense2_2(tag, value) - case psReview2_2: - return parser.parsePairFromReview2_2(tag, value) + case psStart: + return parser.parsePairFromStart(tag, value) + case psCreationInfo: + return parser.parsePairFromCreationInfo(tag, value) + case psPackage: + return parser.parsePairFromPackage(tag, value) + case psFile: + return parser.parsePairFromFile(tag, value) + case psSnippet: + return parser.parsePairFromSnippet(tag, value) + case psOtherLicense: + return parser.parsePairFromOtherLicense(tag, value) + case psReview: + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("parser state %v not recognized when parsing (%s, %s)", parser.st, tag, value) } } -func (parser *tvParser2_2) parsePairFromStart2_2(tag string, value string) error { +func (parser *tvParser) parsePairFromStart(tag string, value string) error { // fail if not in Start parser state - if parser.st != psStart2_2 { - return fmt.Errorf("got invalid state %v in parsePairFromStart2_2", parser.st) + if parser.st != psStart { + return fmt.Errorf("got invalid state %v in parsePairFromStart", parser.st) } // create an SPDX Document data struct if we don't have one already if parser.doc == nil { - parser.doc = &v2_2.Document{ExternalDocumentReferences: []v2_2.ExternalDocumentRef{}} + parser.doc = &spdx.Document{ExternalDocumentReferences: []spdx.ExternalDocumentRef{}} } switch tag { @@ -84,7 +84,7 @@ func (parser *tvParser2_2) parsePairFromStart2_2(tag string, value string) error if err != nil { return err } - edr := v2_2.ExternalDocumentRef{ + edr := spdx.ExternalDocumentRef{ DocumentRefID: documentRefID, URI: uri, Checksum: common.Checksum{Algorithm: common.ChecksumAlgorithm(alg), Value: checksum}, @@ -92,8 +92,8 @@ func (parser *tvParser2_2) parsePairFromStart2_2(tag string, value string) error parser.doc.ExternalDocumentReferences = append(parser.doc.ExternalDocumentReferences, edr) default: // move to Creation Info parser state - parser.st = psCreationInfo2_2 - return parser.parsePairFromCreationInfo2_2(tag, value) + parser.st = psCreationInfo + return parser.parsePairFromCreationInfo(tag, value) } return nil diff --git a/tvloader/parser2v2/parser_test.go b/spdx/v2/v2_2/tagvalue/reader/parser_test.go similarity index 66% rename from tvloader/parser2v2/parser_test.go rename to spdx/v2/v2_2/tagvalue/reader/parser_test.go index 148264d6..3a1a564e 100644 --- a/tvloader/parser2v2/parser_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/parser_test.go @@ -1,20 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/tvloader/reader" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ===== Parser exported entry point tests ===== -func TestParser2_2CanParseTagValues(t *testing.T) { +func TestParserCanParseTagValues(t *testing.T) { var tvPairs []reader.TagValuePair // create some pairs tvPair1 := reader.TagValuePair{Tag: "SPDXVersion", Value: "SPDX-2.2"} tvPairs = append(tvPairs, tvPair1) - tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: "CC0-1.0"} + tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: spdx.DataLicense} tvPairs = append(tvPairs, tvPair2) tvPair3 := reader.TagValuePair{Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"} tvPairs = append(tvPairs, tvPair3) @@ -27,7 +28,7 @@ func TestParser2_2CanParseTagValues(t *testing.T) { if doc.SPDXVersion != "SPDX-2.2" { t.Errorf("expected SPDXVersion to be SPDX-2.2, got %v", doc.SPDXVersion) } - if doc.DataLicense != "CC0-1.0" { + if doc.DataLicense != spdx.DataLicense { t.Errorf("expected DataLicense to be CC0-1.0, got %v", doc.DataLicense) } if doc.SPDXIdentifier != "DOCUMENT" { @@ -37,9 +38,9 @@ func TestParser2_2CanParseTagValues(t *testing.T) { } // ===== Parser initialization tests ===== -func TestParser2_2InitCreatesResetStatus(t *testing.T) { - parser := tvParser2_2{} - if parser.st != psStart2_2 { +func TestParserInitCreatesResetStatus(t *testing.T) { + parser := tvParser{} + if parser.st != psStart { t.Errorf("parser did not begin in start state") } if parser.doc != nil { @@ -47,32 +48,32 @@ func TestParser2_2InitCreatesResetStatus(t *testing.T) { } } -func TestParser2_2HasDocumentAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_2{} - err := parser.parsePair2_2("SPDXVersion", "SPDX-2.2") +func TestParserHasDocumentAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{} + err := parser.parsePair("SPDXVersion", "SPDX-2.2") if err != nil { - t.Errorf("got error when calling parsePair2_2: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.doc == nil { t.Errorf("doc is still nil after parsing first pair") } } -func TestParser2_2StartFailsToParseIfInInvalidState(t *testing.T) { - parser := tvParser2_2{st: psReview2_2} - err := parser.parsePairFromStart2_2("SPDXVersion", "SPDX-2.2") +func TestParserStartFailsToParseIfInInvalidState(t *testing.T) { + parser := tvParser{st: psReview} + err := parser.parsePairFromStart("SPDXVersion", "SPDX-2.2") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_2FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: Checks the last file // Last unpackaged file with no packages in doc // Last file of last package in the doc tvPairs := []reader.TagValuePair{ {Tag: "SPDXVersion", Value: "SPDX-2.2"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "FileName", Value: "f1"}, } @@ -82,11 +83,11 @@ func TestParser2_2FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { } } -func TestParser2_2PackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserPackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: Checks the last package tvPairs := []reader.TagValuePair{ {Tag: "SPDXVersion", Value: "SPDX-2.2"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "PackageName", Value: "p1"}, } diff --git a/tvloader/parser2v2/types.go b/spdx/v2/v2_2/tagvalue/reader/types.go similarity index 71% rename from tvloader/parser2v2/types.go rename to spdx/v2/v2_2/tagvalue/reader/types.go index 52d6a9ac..15673a49 100644 --- a/tvloader/parser2v2/types.go +++ b/spdx/v2/v2_2/tagvalue/reader/types.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -type tvParser2_2 struct { +type tvParser struct { // document into which data is being parsed doc *v2_2.Document // current parser state - st tvParserState2_2 + st tvParserState // current SPDX item being filled in, if any pkg *v2_2.Package @@ -29,29 +29,29 @@ type tvParser2_2 struct { } // parser state (SPDX document version 2.2) -type tvParserState2_2 int +type tvParserState int const ( // at beginning of document - psStart2_2 tvParserState2_2 = iota + psStart tvParserState = iota // in document creation info section - psCreationInfo2_2 + psCreationInfo // in package data section - psPackage2_2 + psPackage // in file data section (including "unpackaged" files) - psFile2_2 + psFile // in snippet data section (including "unpackaged" files) - psSnippet2_2 + psSnippet // in other license section - psOtherLicense2_2 + psOtherLicense // in review section - psReview2_2 + psReview ) -const nullSpdxElementId2_2 = common.ElementID("") +const nullSpdxElementId = common.ElementID("") diff --git a/tvloader/parser2v2/util.go b/spdx/v2/v2_2/tagvalue/reader/util.go similarity index 98% rename from tvloader/parser2v2/util.go rename to spdx/v2/v2_2/tagvalue/reader/util.go index 5e844338..31daafc7 100644 --- a/tvloader/parser2v2/util.go +++ b/spdx/v2/v2_2/tagvalue/reader/util.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // used to extract key / value from embedded substrings diff --git a/tvloader/parser2v2/util_test.go b/spdx/v2/v2_2/tagvalue/reader/util_test.go similarity index 99% rename from tvloader/parser2v2/util_test.go rename to spdx/v2/v2_2/tagvalue/reader/util_test.go index 80050f36..83895075 100644 --- a/tvloader/parser2v2/util_test.go +++ b/spdx/v2/v2_2/tagvalue/reader/util_test.go @@ -1,10 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== Helper function tests ===== diff --git a/tvsaver/saver2v1/save_annotation.go b/spdx/v2/v2_2/tagvalue/writer/save_annotation.go similarity index 80% rename from tvsaver/saver2v1/save_annotation.go rename to spdx/v2/v2_2/tagvalue/writer/save_annotation.go index 3fa351fd..a1e64e00 100644 --- a/tvsaver/saver2v1/save_annotation.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_annotation.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderAnnotation2_1(ann *v2_1.Annotation, w io.Writer) error { +func renderAnnotation(ann *spdx.Annotation, w io.Writer) error { if ann.Annotator.Annotator != "" && ann.Annotator.AnnotatorType != "" { fmt.Fprintf(w, "Annotator: %s: %s\n", ann.Annotator.AnnotatorType, ann.Annotator.Annotator) } diff --git a/tvsaver/saver2v3/save_annotation_test.go b/spdx/v2/v2_2/tagvalue/writer/save_annotation_test.go similarity index 85% rename from tvsaver/saver2v3/save_annotation_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_annotation_test.go index 7471260a..ad5426bc 100644 --- a/tvsaver/saver2v3/save_annotation_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_annotation_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Annotation section Saver tests ===== -func TestSaver2_3AnnotationSavesTextForPerson(t *testing.T) { - ann := &v2_3.Annotation{ +func TestSaverAnnotationSavesTextForPerson(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Person", Annotator: "John Doe"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -31,7 +31,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_3(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -43,8 +43,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_3AnnotationSavesTextForOrganization(t *testing.T) { - ann := &v2_3.Annotation{ +func TestSaverAnnotationSavesTextForOrganization(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Organization", Annotator: "John Doe, Inc."}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -63,7 +63,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_3(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -75,8 +75,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_3AnnotationSavesTextForTool(t *testing.T) { - ann := &v2_3.Annotation{ +func TestSaverAnnotationSavesTextForTool(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Tool", Annotator: "magictool-1.1"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -95,7 +95,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_3(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_creation_info.go b/spdx/v2/v2_2/tagvalue/writer/save_creation_info.go similarity index 81% rename from tvsaver/saver2v3/save_creation_info.go rename to spdx/v2/v2_2/tagvalue/writer/save_creation_info.go index 2e8037dc..b94011f9 100644 --- a/tvsaver/saver2v3/save_creation_info.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_creation_info.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderCreationInfo2_3(ci *v2_3.CreationInfo, w io.Writer) error { +func renderCreationInfo(ci *spdx.CreationInfo, w io.Writer) error { if ci.LicenseListVersion != "" { fmt.Fprintf(w, "LicenseListVersion: %s\n", ci.LicenseListVersion) } diff --git a/tvsaver/saver2v3/save_creation_info_test.go b/spdx/v2/v2_2/tagvalue/writer/save_creation_info_test.go similarity index 86% rename from tvsaver/saver2v3/save_creation_info_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_creation_info_test.go index a433dc5a..1ff384db 100644 --- a/tvsaver/saver2v3/save_creation_info_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_creation_info_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Creation Info section Saver tests ===== -func TestSaver2_3CISavesText(t *testing.T) { - ci := &v2_3.CreationInfo{ +func TestSaverCISavesText(t *testing.T) { + ci := &spdx.CreationInfo{ LicenseListVersion: "3.9", Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, @@ -43,7 +43,7 @@ CreatorComment: this is a creator comment // render as buffer of bytes var got bytes.Buffer - err := renderCreationInfo2_3(ci, &got) + err := renderCreationInfo(ci, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -55,9 +55,9 @@ CreatorComment: this is a creator comment } } -func TestSaver2_3CIOmitsOptionalFieldsIfEmpty(t *testing.T) { +func TestSaverCIOmitsOptionalFieldsIfEmpty(t *testing.T) { // --- need at least one creator; do first for Persons --- - ci1 := &v2_3.CreationInfo{ + ci1 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -72,7 +72,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got1 bytes.Buffer - err := renderCreationInfo2_3(ci1, &got1) + err := renderCreationInfo(ci1, &got1) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -84,7 +84,7 @@ Created: 2018-10-10T06:20:00Z } // --- need at least one creator; now switch to organization --- - ci2 := &v2_3.CreationInfo{ + ci2 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe, Inc.", CreatorType: "Organization"}, }, @@ -99,7 +99,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got2 bytes.Buffer - err = renderCreationInfo2_3(ci2, &got2) + err = renderCreationInfo(ci2, &got2) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_document.go b/spdx/v2/v2_2/tagvalue/writer/save_document.go similarity index 85% rename from tvsaver/saver2v2/save_document.go rename to spdx/v2/v2_2/tagvalue/writer/save_document.go index efec2a1e..2020a763 100644 --- a/tvsaver/saver2v2/save_document.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_document.go @@ -2,22 +2,22 @@ // formatted version of an in-memory SPDX document and its sections // (version 2.2). // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -// RenderDocument2_2 is the main entry point to take an SPDX in-memory +// RenderDocument is the main entry point to take an SPDX in-memory // Document (version 2.2), and render it to the received io.Writer. // It is only exported in order to be available to the tvsaver package, // and typically does not need to be called by client code. -func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { +func RenderDocument(doc *spdx.Document, w io.Writer) error { if doc.CreationInfo == nil { return fmt.Errorf("Document had nil CreationInfo section") } @@ -49,7 +49,7 @@ func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { fmt.Fprintf(w, "DocumentComment: %s\n", textify(doc.DocumentComment)) } - renderCreationInfo2_2(doc.CreationInfo, w) + renderCreationInfo(doc.CreationInfo, w) if len(doc.Files) > 0 { fmt.Fprintf(w, "##### Unpackaged files\n\n") @@ -57,7 +57,7 @@ func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { return doc.Files[i].FileSPDXIdentifier < doc.Files[j].FileSPDXIdentifier }) for _, fi := range doc.Files { - renderFile2_2(fi, w) + renderFile(fi, w) } } @@ -67,20 +67,20 @@ func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { }) for _, pkg := range doc.Packages { fmt.Fprintf(w, "##### Package: %s\n\n", pkg.PackageName) - renderPackage2_2(pkg, w) + renderPackage(pkg, w) } if len(doc.OtherLicenses) > 0 { fmt.Fprintf(w, "##### Other Licenses\n\n") for _, ol := range doc.OtherLicenses { - renderOtherLicense2_2(ol, w) + renderOtherLicense(ol, w) } } if len(doc.Relationships) > 0 { fmt.Fprintf(w, "##### Relationships\n\n") for _, rln := range doc.Relationships { - renderRelationship2_2(rln, w) + renderRelationship(rln, w) } fmt.Fprintf(w, "\n") } @@ -88,7 +88,7 @@ func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { if len(doc.Annotations) > 0 { fmt.Fprintf(w, "##### Annotations\n\n") for _, ann := range doc.Annotations { - renderAnnotation2_2(ann, w) + renderAnnotation(ann, w) fmt.Fprintf(w, "\n") } } @@ -96,7 +96,7 @@ func RenderDocument2_2(doc *v2_2.Document, w io.Writer) error { if len(doc.Reviews) > 0 { fmt.Fprintf(w, "##### Reviews\n\n") for _, rev := range doc.Reviews { - renderReview2_2(rev, w) + renderReview(rev, w) } } diff --git a/tvsaver/saver2v2/save_document_test.go b/spdx/v2/v2_2/tagvalue/writer/save_document_test.go similarity index 89% rename from tvsaver/saver2v2/save_document_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_document_test.go index 39146dd0..a4b86707 100644 --- a/tvsaver/saver2v2/save_document_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_document_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== entire Document Saver tests ===== -func TestSaver2_2DocumentSavesText(t *testing.T) { +func TestSaverDocumentSavesText(t *testing.T) { // Creation Info section - ci := &v2_2.CreationInfo{ + ci := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -22,7 +22,7 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { } // unpackaged files - f1 := &v2_2.File{ + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983c", Algorithm: common.SHA1}}, @@ -31,7 +31,7 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_2.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983d", Algorithm: common.SHA1}}, @@ -40,13 +40,13 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - unFiles := []*v2_2.File{ + unFiles := []*spdx.File{ f1, f2, } // Package 1: packaged files with snippets - sn1 := &v2_2.Snippet{ + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet19", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -54,7 +54,7 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_2.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet20", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -62,7 +62,7 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - f3 := &v2_2.File{ + f3 := &spdx.File{ FileName: "/tmp/file-with-snippets.txt", FileSPDXIdentifier: common.ElementID("FileHasSnippets"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983e", Algorithm: common.SHA1}}, @@ -73,13 +73,13 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { "WTFPL", }, FileCopyrightText: "Copyright (c) Jane Doe", - Snippets: map[common.ElementID]*v2_2.Snippet{ + Snippets: map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, }, } - f4 := &v2_2.File{ + f4 := &spdx.File{ FileName: "/tmp/another-file.txt", FileSPDXIdentifier: common.ElementID("FileAnother"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983f", Algorithm: common.SHA1}}, @@ -88,7 +88,7 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe LLC", } - pkgWith := &v2_2.Package{ + pkgWith := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -104,14 +104,14 @@ func TestSaver2_2DocumentSavesText(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_2.File{ + Files: []*spdx.File{ f3, f4, }, } // Other Licenses 1 and 2 - ol1 := &v2_2.OtherLicense{ + ol1 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -119,33 +119,33 @@ blah blah blah blah`, LicenseName: "License 1", } - ol2 := &v2_2.OtherLicense{ + ol2 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-2", ExtractedText: `License 2 text - this is a license that does some stuff`, LicenseName: "License 2", } // Relationships - rln1 := &v2_2.Relationship{ + rln1 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), Relationship: "DESCRIBES", } - rln2 := &v2_2.Relationship{ + rln2 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1231"), Relationship: "DESCRIBES", } - rln3 := &v2_2.Relationship{ + rln3 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1232"), Relationship: "DESCRIBES", } // Annotations - ann1 := &v2_2.Annotation{ + ann1 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe", AnnotatorType: "Person"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -154,7 +154,7 @@ blah blah blah blah`, AnnotationComment: "This is an annotation about the SPDX document", } - ann2 := &v2_2.Annotation{ + ann2 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe, Inc.", AnnotatorType: "Organization"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -164,12 +164,12 @@ blah blah blah blah`, } // Reviews - rev1 := &v2_2.Review{ + rev1 := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", } - rev2 := &v2_2.Review{ + rev2 := &spdx.Review{ Reviewer: "Jane Doe LLC", ReviewerType: "Organization", ReviewDate: "2018-10-14T10:28:00Z", @@ -177,31 +177,31 @@ blah blah blah blah`, } // now, build the document - doc := &v2_2.Document{ + doc := &spdx.Document{ SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), DocumentName: "tools-golang-0.0.1.abcdef", DocumentNamespace: "https://github.com/spdx/spdx-docs/tools-golang/tools-golang-0.0.1.abcdef.whatever", CreationInfo: ci, - Packages: []*v2_2.Package{ + Packages: []*spdx.Package{ pkgWith, }, Files: unFiles, - OtherLicenses: []*v2_2.OtherLicense{ + OtherLicenses: []*spdx.OtherLicense{ ol1, ol2, }, - Relationships: []*v2_2.Relationship{ + Relationships: []*spdx.Relationship{ rln1, rln2, rln3, }, - Annotations: []*v2_2.Annotation{ + Annotations: []*spdx.Annotation{ ann1, ann2, }, - Reviews: []*v2_2.Review{ + Reviews: []*spdx.Review{ rev1, rev2, }, @@ -319,7 +319,7 @@ ReviewComment: I have reviewed this SPDX document and it is awesome // render as buffer of bytes var got bytes.Buffer - err := RenderDocument2_2(doc, &got) + err := RenderDocument(doc, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -332,11 +332,11 @@ ReviewComment: I have reviewed this SPDX document and it is awesome } -func TestSaver2_2DocumentReturnsErrorIfNilCreationInfo(t *testing.T) { - doc := &v2_2.Document{} +func TestSaverDocumentReturnsErrorIfNilCreationInfo(t *testing.T) { + doc := &spdx.Document{} var got bytes.Buffer - err := RenderDocument2_2(doc, &got) + err := RenderDocument(doc, &got) if err == nil { t.Errorf("Expected error, got nil") } diff --git a/tvsaver/saver2v3/save_file.go b/spdx/v2/v2_2/tagvalue/writer/save_file.go similarity index 91% rename from tvsaver/saver2v3/save_file.go rename to spdx/v2/v2_2/tagvalue/writer/save_file.go index 18a6d98f..8a90dc13 100644 --- a/tvsaver/saver2v3/save_file.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_file.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderFile2_3(f *v2_3.File, w io.Writer) error { +func renderFile(f *spdx.File, w io.Writer) error { if f.FileName != "" { fmt.Fprintf(w, "FileName: %s\n", f.FileName) } @@ -74,7 +74,7 @@ func renderFile2_3(f *v2_3.File, w io.Writer) error { sort.Strings(snippetKeys) for _, sID := range snippetKeys { s := f.Snippets[common.ElementID(sID)] - renderSnippet2_3(s, w) + renderSnippet(s, w) } return nil diff --git a/tvsaver/saver2v2/save_file_test.go b/spdx/v2/v2_2/tagvalue/writer/save_file_test.go similarity index 90% rename from tvsaver/saver2v2/save_file_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_file_test.go index f8a8f324..c93a8c60 100644 --- a/tvsaver/saver2v2/save_file_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_file_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== File section Saver tests ===== -func TestSaver2_2FileSavesText(t *testing.T) { - f := &v2_2.File{ +func TestSaverFileSavesText(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), FileTypes: []string{ @@ -31,20 +31,20 @@ func TestSaver2_2FileSavesText(t *testing.T) { }, LicenseComments: "this is a license comment(s)", FileCopyrightText: "Copyright (c) Jane Doe", - ArtifactOfProjects: []*v2_2.ArtifactOfProject{ - &v2_2.ArtifactOfProject{ + ArtifactOfProjects: []*spdx.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project1", HomePage: "http://example.com/1/", URI: "http://example.com/1/uri.whatever", }, - &v2_2.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project2", }, - &v2_2.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project3", HomePage: "http://example.com/3/", }, - &v2_2.ArtifactOfProject{ + &spdx.ArtifactOfProject{ Name: "project4", URI: "http://example.com/4/uri.whatever", }, @@ -101,7 +101,7 @@ FileDependency: g.txt // render as buffer of bytes var got bytes.Buffer - err := renderFile2_2(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -113,8 +113,8 @@ FileDependency: g.txt } } -func TestSaver2_2FileSavesSnippetsAlso(t *testing.T) { - sn1 := &v2_2.Snippet{ +func TestSaverFileSavesSnippetsAlso(t *testing.T) { + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet19"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -122,7 +122,7 @@ func TestSaver2_2FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_2.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet20"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -130,12 +130,12 @@ func TestSaver2_2FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - sns := map[common.ElementID]*v2_2.Snippet{ + sns := map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, } - f := &v2_2.File{ + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -173,7 +173,7 @@ SnippetCopyrightText: NOASSERTION // render as buffer of bytes var got bytes.Buffer - err := renderFile2_2(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -185,8 +185,8 @@ SnippetCopyrightText: NOASSERTION } } -func TestSaver2_2FileOmitsOptionalFieldsIfEmpty(t *testing.T) { - f := &v2_2.File{ +func TestSaverFileOmitsOptionalFieldsIfEmpty(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -211,7 +211,7 @@ FileCopyrightText: Copyright (c) Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_2(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -223,8 +223,8 @@ FileCopyrightText: Copyright (c) Jane Doe } } -func TestSaver2_2FileWrapsCopyrightMultiLine(t *testing.T) { - f := &v2_2.File{ +func TestSaverFileWrapsCopyrightMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -251,7 +251,7 @@ Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_2(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -263,8 +263,8 @@ Copyright (c) John Doe } } -func TestSaver2_2FileWrapsCommentsAndNoticesMultiLine(t *testing.T) { - f := &v2_2.File{ +func TestSaverFileWrapsCommentsAndNoticesMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -301,7 +301,7 @@ under either Apache-2.0 or Apache-1.1. // render as buffer of bytes var got bytes.Buffer - err := renderFile2_2(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_other_license.go b/spdx/v2/v2_2/tagvalue/writer/save_other_license.go similarity index 81% rename from tvsaver/saver2v1/save_other_license.go rename to spdx/v2/v2_2/tagvalue/writer/save_other_license.go index b30aecf1..c8a17af8 100644 --- a/tvsaver/saver2v1/save_other_license.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_other_license.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderOtherLicense2_1(ol *v2_1.OtherLicense, w io.Writer) error { +func renderOtherLicense(ol *spdx.OtherLicense, w io.Writer) error { if ol.LicenseIdentifier != "" { fmt.Fprintf(w, "LicenseID: %s\n", ol.LicenseIdentifier) } diff --git a/tvsaver/saver2v3/save_other_license_test.go b/spdx/v2/v2_2/tagvalue/writer/save_other_license_test.go similarity index 84% rename from tvsaver/saver2v3/save_other_license_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_other_license_test.go index 00134dd5..3172bee4 100644 --- a/tvsaver/saver2v3/save_other_license_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_other_license_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Other License section Saver tests ===== -func TestSaver2_3OtherLicenseSavesText(t *testing.T) { - ol := &v2_3.OtherLicense{ +func TestSaverOtherLicenseSavesText(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -38,7 +38,7 @@ LicenseComment: this is a license comment // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_3(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -50,8 +50,8 @@ LicenseComment: this is a license comment } } -func TestSaver2_3OtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { - ol := &v2_3.OtherLicense{ +func TestSaverOtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -70,7 +70,7 @@ LicenseName: License 1 // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_3(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_package.go b/spdx/v2/v2_2/tagvalue/writer/save_package.go similarity index 95% rename from tvsaver/saver2v2/save_package.go rename to spdx/v2/v2_2/tagvalue/writer/save_package.go index 387fe2ae..8e04d6a8 100644 --- a/tvsaver/saver2v2/save_package.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_package.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" @@ -8,11 +8,11 @@ import ( "sort" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderPackage2_2(pkg *v2_2.Package, w io.Writer) error { +func renderPackage(pkg *spdx.Package, w io.Writer) error { if pkg.PackageName != "" { fmt.Fprintf(w, "PackageName: %s\n", pkg.PackageName) } @@ -110,7 +110,7 @@ func renderPackage2_2(pkg *v2_2.Package, w io.Writer) error { return pkg.Files[i].FileSPDXIdentifier < pkg.Files[j].FileSPDXIdentifier }) for _, fi := range pkg.Files { - renderFile2_2(fi, w) + renderFile(fi, w) } return nil diff --git a/tvsaver/saver2v2/save_package_test.go b/spdx/v2/v2_2/tagvalue/writer/save_package_test.go similarity index 93% rename from tvsaver/saver2v2/save_package_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_package_test.go index 45f9851e..ebd42bc6 100644 --- a/tvsaver/saver2v2/save_package_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_package_test.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Package section Saver tests ===== -func TestSaver2_2PackageSavesTextCombo1(t *testing.T) { +func TestSaverPackageSavesTextCombo1(t *testing.T) { // include package external refs // test Supplier:Organization, Originator:Person // FilesAnalyzed true, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile has string // NOTE, this is an entirely made up CPE and the format is likely invalid - per1 := &v2_2.PackageExternalReference{ + per1 := &spdx.PackageExternalReference{ Category: "SECURITY", RefType: "cpe22Type", Locator: "cpe:/a:john_doe_inc:p1:0.1.0", @@ -26,7 +26,7 @@ func TestSaver2_2PackageSavesTextCombo1(t *testing.T) { } // NOTE, this is an entirely made up NPM - per2 := &v2_2.PackageExternalReference{ + per2 := &spdx.PackageExternalReference{ Category: "PACKAGE-MANAGER", RefType: "npm", Locator: "p1@0.1.0", @@ -35,21 +35,21 @@ multi-line external ref comment`, } // NOTE, this is an entirely made up SWH persistent ID - per3 := &v2_2.PackageExternalReference{ + per3 := &spdx.PackageExternalReference{ Category: "PERSISTENT-ID", RefType: "swh", Locator: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", // no ExternalRefComment for this one } - per4 := &v2_2.PackageExternalReference{ + per4 := &spdx.PackageExternalReference{ Category: "OTHER", RefType: "anything", Locator: "anything-without-spaces-can-go-here", // no ExternalRefComment for this one } - pkg := &v2_2.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -92,7 +92,7 @@ multi-line external ref comment`, PackageDescription: "this is a description comment", PackageComment: "this is a comment comment", PackageAttributionTexts: []string{"Include this notice in all advertising materials"}, - PackageExternalReferences: []*v2_2.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ per1, per2, per3, @@ -138,7 +138,7 @@ PackageAttributionText: Include this notice in all advertising materials // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -150,13 +150,13 @@ PackageAttributionText: Include this notice in all advertising materials } } -func TestSaver2_2PackageSavesTextCombo2(t *testing.T) { +func TestSaverPackageSavesTextCombo2(t *testing.T) { // no package external refs // test Supplier:NOASSERTION, Originator:Organization // FilesAnalyzed true, IsFilesAnalyzedTagPresent false // PackageVerificationCodeExcludedFile is empty - pkg := &v2_2.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -228,7 +228,7 @@ PackageAttributionText: Include this notice in all advertising materials // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -240,14 +240,14 @@ PackageAttributionText: Include this notice in all advertising materials } } -func TestSaver2_2PackageSavesTextCombo3(t *testing.T) { +func TestSaverPackageSavesTextCombo3(t *testing.T) { // no package external refs // test Supplier:Person, Originator:NOASSERTION // FilesAnalyzed false, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile is empty // three PackageAttributionTexts, one with multi-line text - pkg := &v2_2.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -328,7 +328,7 @@ which goes across two lines // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -340,8 +340,8 @@ which goes across two lines } } -func TestSaver2_2PackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { - pkg := &v2_2.Package{ +func TestSaverPackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -374,7 +374,7 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -386,8 +386,8 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. } } -func TestSaver2_2PackageSavesFilesIfPresent(t *testing.T) { - f1 := &v2_2.File{ +func TestSaverPackageSavesFilesIfPresent(t *testing.T) { + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{ @@ -401,7 +401,7 @@ func TestSaver2_2PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_2.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{ @@ -415,7 +415,7 @@ func TestSaver2_2PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - pkg := &v2_2.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -433,7 +433,7 @@ func TestSaver2_2PackageSavesFilesIfPresent(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_2.File{ + Files: []*spdx.File{ f1, f2, }, @@ -466,7 +466,7 @@ FileCopyrightText: Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -478,8 +478,8 @@ FileCopyrightText: Copyright (c) John Doe } } -func TestSaver2_2PackageWrapsMultiLine(t *testing.T) { - pkg := &v2_2.Package{ +func TestSaverPackageWrapsMultiLine(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -510,7 +510,7 @@ Copyright Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_2(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_relationship.go b/spdx/v2/v2_2/tagvalue/writer/save_relationship.go similarity index 72% rename from tvsaver/saver2v3/save_relationship.go rename to spdx/v2/v2_2/tagvalue/writer/save_relationship.go index c83310f3..cd3142e6 100644 --- a/tvsaver/saver2v3/save_relationship.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_relationship.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderRelationship2_3(rln *v2_3.Relationship, w io.Writer) error { +func renderRelationship(rln *spdx.Relationship, w io.Writer) error { rlnAStr := common.RenderDocElementID(rln.RefA) rlnBStr := common.RenderDocElementID(rln.RefB) if rlnAStr != "SPDXRef-" && rlnBStr != "SPDXRef-" && rln.Relationship != "" { diff --git a/tvsaver/saver2v3/save_relationship_test.go b/spdx/v2/v2_2/tagvalue/writer/save_relationship_test.go similarity index 80% rename from tvsaver/saver2v3/save_relationship_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_relationship_test.go index 26ce0c3e..15dc876f 100644 --- a/tvsaver/saver2v3/save_relationship_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_relationship_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Relationship section Saver tests ===== -func TestSaver2_3RelationshipSavesText(t *testing.T) { - rln := &v2_3.Relationship{ +func TestSaverRelationshipSavesText(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -27,7 +27,7 @@ RelationshipComment: this is a comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_3(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ RelationshipComment: this is a comment } } -func TestSaver2_3RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { - rln := &v2_3.Relationship{ +func TestSaverRelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -52,7 +52,7 @@ func TestSaver2_3RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_3(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -64,8 +64,8 @@ func TestSaver2_3RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { } } -func TestSaver2_3RelationshipCanHaveNONEOnRight(t *testing.T) { - rln := &v2_3.Relationship{ +func TestSaverRelationshipCanHaveNONEOnRight(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "PackageA"), RefB: common.MakeDocElementSpecial("NONE"), Relationship: "DEPENDS_ON", @@ -77,7 +77,7 @@ func TestSaver2_3RelationshipCanHaveNONEOnRight(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_3(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -89,8 +89,8 @@ func TestSaver2_3RelationshipCanHaveNONEOnRight(t *testing.T) { } } -func TestSaver2_3RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { - rln := &v2_3.Relationship{ +func TestSaverRelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "PackageA"), RefB: common.MakeDocElementSpecial("NOASSERTION"), Relationship: "DEPENDS_ON", @@ -102,7 +102,7 @@ func TestSaver2_3RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_3(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -114,8 +114,8 @@ func TestSaver2_3RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { } } -func TestSaver2_3RelationshipWrapsCommentMultiLine(t *testing.T) { - rln := &v2_3.Relationship{ +func TestSaverRelationshipWrapsCommentMultiLine(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -132,7 +132,7 @@ multi-line comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_3(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_review.go b/spdx/v2/v2_2/tagvalue/writer/save_review.go similarity index 78% rename from tvsaver/saver2v2/save_review.go rename to spdx/v2/v2_2/tagvalue/writer/save_review.go index 8f1add6f..68617bfb 100644 --- a/tvsaver/saver2v2/save_review.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_review.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderReview2_2(rev *v2_2.Review, w io.Writer) error { +func renderReview(rev *spdx.Review, w io.Writer) error { if rev.Reviewer != "" && rev.ReviewerType != "" { fmt.Fprintf(w, "Reviewer: %s: %s\n", rev.ReviewerType, rev.Reviewer) } diff --git a/tvsaver/saver2v1/save_review_test.go b/spdx/v2/v2_2/tagvalue/writer/save_review_test.go similarity index 82% rename from tvsaver/saver2v1/save_review_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_review_test.go index 10d030b9..d93d13a3 100644 --- a/tvsaver/saver2v1/save_review_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_review_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Review section Saver tests ===== -func TestSaver2_1ReviewSavesText(t *testing.T) { - rev := &v2_1.Review{ +func TestSaverReviewSavesText(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -27,7 +27,7 @@ ReviewComment: this is a review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_1(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ ReviewComment: this is a review comment } } -func TestSaver2_1ReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { - rev := &v2_1.Review{ +func TestSaverReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -54,7 +54,7 @@ ReviewDate: 2018-10-14T10:28:00Z // render as buffer of bytes var got bytes.Buffer - err := renderReview2_1(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -66,8 +66,8 @@ ReviewDate: 2018-10-14T10:28:00Z } } -func TestSaver2_1ReviewWrapsMultiLine(t *testing.T) { - rev := &v2_1.Review{ +func TestSaverReviewWrapsMultiLine(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -85,7 +85,7 @@ multi-line review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_1(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_snippet.go b/spdx/v2/v2_2/tagvalue/writer/save_snippet.go similarity index 90% rename from tvsaver/saver2v3/save_snippet.go rename to spdx/v2/v2_2/tagvalue/writer/save_snippet.go index 2ba4a3bb..6845ec98 100644 --- a/tvsaver/saver2v3/save_snippet.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_snippet.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) -func renderSnippet2_3(sn *v2_3.Snippet, w io.Writer) error { +func renderSnippet(sn *spdx.Snippet, w io.Writer) error { if sn.SnippetSPDXIdentifier != "" { fmt.Fprintf(w, "SnippetSPDXID: %s\n", common.RenderElementID(sn.SnippetSPDXIdentifier)) } diff --git a/tvsaver/saver2v3/save_snippet_test.go b/spdx/v2/v2_2/tagvalue/writer/save_snippet_test.go similarity index 89% rename from tvsaver/saver2v3/save_snippet_test.go rename to spdx/v2/v2_2/tagvalue/writer/save_snippet_test.go index ef101942..941b214e 100644 --- a/tvsaver/saver2v3/save_snippet_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/save_snippet_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_2" ) // ===== Snippet section Saver tests ===== -func TestSaver2_3SnippetSavesText(t *testing.T) { - sn := &v2_3.Snippet{ +func TestSaverSnippetSavesText(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -55,7 +55,7 @@ SnippetAttributionText: some attributions // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_3(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -67,8 +67,8 @@ SnippetAttributionText: some attributions } } -func TestSaver2_3SnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { - sn := &v2_3.Snippet{ +func TestSaverSnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -92,7 +92,7 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_3(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -104,8 +104,8 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 } } -func TestSaver2_3SnippetWrapsCopyrightMultiline(t *testing.T) { - sn := &v2_3.Snippet{ +func TestSaverSnippetWrapsCopyrightMultiline(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -131,7 +131,7 @@ Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_3(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/util.go b/spdx/v2/v2_2/tagvalue/writer/util.go similarity index 92% rename from tvsaver/saver2v3/util.go rename to spdx/v2/v2_2/tagvalue/writer/util.go index 4dec7244..5298ee0a 100644 --- a/tvsaver/saver2v3/util.go +++ b/spdx/v2/v2_2/tagvalue/writer/util.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" diff --git a/tvsaver/saver2v2/util_test.go b/spdx/v2/v2_2/tagvalue/writer/util_test.go similarity index 97% rename from tvsaver/saver2v2/util_test.go rename to spdx/v2/v2_2/tagvalue/writer/util_test.go index cdbf6f56..94a20c86 100644 --- a/tvsaver/saver2v2/util_test.go +++ b/spdx/v2/v2_2/tagvalue/writer/util_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "testing" diff --git a/spdx/v2/v2_2/yaml/yaml_test.go b/spdx/v2/v2_2/yaml/yaml_test.go new file mode 100644 index 00000000..01e7bceb --- /dev/null +++ b/spdx/v2/v2_2/yaml/yaml_test.go @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package yaml + +import ( + "bytes" + "fmt" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_2/example" + "github.com/spdx/tools-golang/yaml" +) + +func Test_Read(t *testing.T) { + want := example.Copy() + + file, err := os.Open("../../../../examples/sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml") + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + var got v2_2.Document + err = yaml.ReadInto(file, &got) + if err != nil { + t.Errorf("yaml.Read() error = %v", err) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} + +func Test_Write(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + if err := yaml.Write(want, w); err != nil { + t.Errorf("Save() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original handwritten struct + var got v2_2.Document + err := yaml.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} diff --git a/spdx/v2_3/annotation.go b/spdx/v2/v2_3/annotation.go similarity index 88% rename from spdx/v2_3/annotation.go rename to spdx/v2/v2_3/annotation.go index 121e9952..338394cf 100644 --- a/spdx/v2_3/annotation.go +++ b/spdx/v2/v2_3/annotation.go @@ -2,9 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// Annotation is an Annotation section of an SPDX Document for version 2.3 of the spec. +// Annotation is an Annotation section of an SPDX Document type Annotation struct { // 12.1: Annotator // Cardinality: conditional (mandatory, one) if there is an Annotation diff --git a/spdx/v2_3/creation_info.go b/spdx/v2/v2_3/creation_info.go similarity index 87% rename from spdx/v2_3/creation_info.go rename to spdx/v2/v2_3/creation_info.go index 33b2caf0..90ee4030 100644 --- a/spdx/v2_3/creation_info.go +++ b/spdx/v2/v2_3/creation_info.go @@ -2,10 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// CreationInfo is a Document Creation Information section of an -// SPDX Document for version 2.3 of the spec. +// CreationInfo is a Document Creation Information section of an SPDX Document type CreationInfo struct { // 6.7: License List Version // Cardinality: optional, one diff --git a/spdx/v2_3/document.go b/spdx/v2/v2_3/document.go similarity index 80% rename from spdx/v2_3/document.go rename to spdx/v2/v2_3/document.go index 32fdb8db..0aa026d9 100644 --- a/spdx/v2_3/document.go +++ b/spdx/v2/v2_3/document.go @@ -1,12 +1,18 @@ -// Package spdx contains the struct definition for an SPDX Document +// Package v2_3 Package contains the struct definition for an SPDX Document // and its constituent parts. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/anchore/go-struct-converter" -// ExternalDocumentRef is a reference to an external SPDX document -// as defined in section 6.6 for version 2.3 of the spec. + "github.com/spdx/tools-golang/spdx/v2/common" +) + +const Version = "SPDX-2.3" +const DataLicense = "CC0-1.0" + +// ExternalDocumentRef is a reference to an external SPDX document as defined in section 6.6 type ExternalDocumentRef struct { // DocumentRefID is the ID string defined in the start of the // reference. It should _not_ contain the "DocumentRef-" part @@ -20,10 +26,10 @@ type ExternalDocumentRef struct { Checksum common.Checksum `json:"checksum"` } -// Document is an SPDX Document for version 2.3 of the spec. +// Document is an SPDX Document: // See https://spdx.github.io/spdx-spec/v2.3/document-creation-information type Document struct { - // 6.1: SPDX Version; should be in the format "SPDX-2.3" + // 6.1: SPDX Version; should be in the format "SPDX-" // Cardinality: mandatory, one SPDXVersion string `json:"spdxVersion"` @@ -63,3 +69,10 @@ type Document struct { // DEPRECATED in version 2.0 of spec Reviews []*Review `json:"-" yaml:"-"` } + +func (d *Document) ConvertFrom(_ interface{}) error { + d.SPDXVersion = Version + return nil +} + +var _ converter.ConvertFrom = (*Document)(nil) diff --git a/yaml/yaml_v2_2_test.go b/spdx/v2/v2_3/example/example.go similarity index 87% rename from yaml/yaml_v2_2_test.go rename to spdx/v2/v2_3/example/example.go index 9d66f5b7..ee327534 100644 --- a/yaml/yaml_v2_2_test.go +++ b/spdx/v2/v2_3/example/example.go @@ -1,72 +1,34 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_yaml +package example import ( - "bytes" "fmt" - "os" - "testing" - "github.com/google/go-cmp/cmp" + converter "github.com/anchore/go-struct-converter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func TestLoad2_2(t *testing.T) { - file, err := os.Open("../examples/sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml") - if err != nil { - panic(fmt.Errorf("error opening File: %s", err)) - } - - got, err := Load2_2(file) - if err != nil { - t.Errorf("yaml.parser.Load2_2() error = %v", err) - return - } - - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_2 - - if cmp.Equal(handwrittenExample, got) { - t.Errorf("got incorrect struct after parsing YAML example") - return - } -} - -func TestWrite2_2(t *testing.T) { - w := &bytes.Buffer{} - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_2 - if err := Save2_2(&handwrittenExample, w); err != nil { - t.Errorf("Save2_2() error = %v", err.Error()) - return - } - - // we should be able to parse what the writer wrote, and it should be identical to the original handwritten struct - parsedDoc, err := Load2_2(bytes.NewReader(w.Bytes())) +// Copy provides a deep copy of the example +func Copy() spdx.Document { + out := spdx.Document{} + err := converter.Convert(example, &out) if err != nil { - t.Errorf("failed to parse written document: %v", err.Error()) - return - } - - if cmp.Equal(handwrittenExample, parsedDoc) { - t.Errorf("got incorrect struct after writing and re-parsing YAML example") - return + panic(fmt.Errorf("unable to convert example doc: %w", err)) } + return out } -// want is handwritten translation of the official example YAML SPDX v2.2 document into a Go struct. +// Example is handwritten translation of an official example SPDX document into a Go struct. // We expect that the result of parsing the official document should be this value. // We expect that the result of writing this struct should match the official example document. -var want2_2 = v2_2.Document{ - DataLicense: "CC0-1.0", - SPDXVersion: "SPDX-2.2", - SPDXIdentifier: "SPDXRef-DOCUMENT", +var example = spdx.Document{ + DataLicense: spdx.DataLicense, + SPDXVersion: spdx.Version, + SPDXIdentifier: "DOCUMENT", DocumentName: "SPDX-Tools-v2.0", DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", - CreationInfo: &v2_2.CreationInfo{ + CreationInfo: &spdx.CreationInfo{ LicenseListVersion: "3.9", Creators: []common.Creator{ {CreatorType: "Tool", Creator: "LicenseFind-1.0"}, @@ -77,7 +39,7 @@ var want2_2 = v2_2.Document{ CreatorComment: "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.", }, DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.", - ExternalDocumentReferences: []v2_2.ExternalDocumentRef{ + ExternalDocumentReferences: []spdx.ExternalDocumentRef{ { DocumentRefID: "DocumentRef-spdx-tool-1.2", URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301", @@ -87,7 +49,7 @@ var want2_2 = v2_2.Document{ }, }, }, - OtherLicenses: []*v2_2.OtherLicense{ + OtherLicenses: []*spdx.OtherLicense{ { LicenseIdentifier: "LicenseRef-1", ExtractedText: "/*\n * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", @@ -118,7 +80,7 @@ var want2_2 = v2_2.Document{ LicenseComment: "This is tye CyperNeko License", }, }, - Annotations: []*v2_2.Annotation{ + Annotations: []*spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "Jane Doe ()", @@ -147,10 +109,10 @@ var want2_2 = v2_2.Document{ AnnotationComment: "Another example reviewer.", }, }, - Packages: []*v2_2.Package{ + Packages: []*spdx.Package{ { PackageName: "glibc", - PackageSPDXIdentifier: "SPDXRef-Package", + PackageSPDXIdentifier: "Package", PackageVersion: "2.11.1", PackageFileName: "glibc-2.11.1.tar.gz", PackageSupplier: &common.Supplier{ @@ -163,7 +125,7 @@ var want2_2 = v2_2.Document{ }, PackageDownloadLocation: "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz", FilesAnalyzed: true, - PackageVerificationCode: common.PackageVerificationCode{ + PackageVerificationCode: &common.PackageVerificationCode{ Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", ExcludedFiles: []string{"./package.spdx"}, }, @@ -195,7 +157,7 @@ var want2_2 = v2_2.Document{ PackageSummary: "GNU C library.", PackageDescription: "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.", PackageComment: "", - PackageExternalReferences: []*v2_2.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ { Category: "SECURITY", RefType: "cpe23Type", @@ -212,7 +174,7 @@ var want2_2 = v2_2.Document{ "The GNU C Library is free software. See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed. License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually.", }, Files: nil, - Annotations: []v2_2.Annotation{ + Annotations: []spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "Package Commenter", @@ -225,7 +187,7 @@ var want2_2 = v2_2.Document{ }, }, { - PackageSPDXIdentifier: "SPDXRef-fromDoap-1", + PackageSPDXIdentifier: "fromDoap-1", PackageCopyrightText: "NOASSERTION", PackageDownloadLocation: "NOASSERTION", FilesAnalyzed: false, @@ -236,12 +198,12 @@ var want2_2 = v2_2.Document{ }, { PackageName: "Jena", - PackageSPDXIdentifier: "SPDXRef-fromDoap-0", + PackageSPDXIdentifier: "fromDoap-0", PackageCopyrightText: "NOASSERTION", PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz", - PackageExternalReferences: []*v2_2.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ { - Category: "PACKAGE_MANAGER", + Category: "PACKAGE-MANAGER", RefType: "purl", Locator: "pkg:maven/org.apache.jena/apache-jena@3.12.0", }, @@ -253,7 +215,7 @@ var want2_2 = v2_2.Document{ PackageVersion: "3.12.0", }, { - PackageSPDXIdentifier: "SPDXRef-Saxon", + PackageSPDXIdentifier: "Saxon", PackageChecksums: []common.Checksum{ { Algorithm: "SHA1", @@ -272,11 +234,26 @@ var want2_2 = v2_2.Document{ PackageFileName: "saxonB-8.8.zip", PackageVersion: "8.8", }, + { + PrimaryPackagePurpose: "CONTAINER", + PackageSPDXIdentifier: "CentOS-7", + PackageCopyrightText: "NOASSERTION", + PackageDescription: "The CentOS container used to run the application.", + PackageDownloadLocation: "NOASSERTION", + FilesAnalyzed: false, + PackageHomePage: "https://www.centos.org/", + PackageName: "centos", + PackageFileName: "saxonB-8.8.zip", + PackageVersion: "centos7.9.2009", + BuiltDate: "2021-09-15T02:38:00Z", + ValidUntilDate: "2022-10-15T02:38:00Z", + ReleaseDate: "2021-10-15T02:38:00Z", + }, }, - Files: []*v2_2.File{ + Files: []*spdx.File{ { FileName: "./src/org/spdx/parser/DOAPProject.java", - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", FileTypes: []string{ "SOURCE", }, @@ -300,7 +277,7 @@ var want2_2 = v2_2.Document{ }, }, { - FileSPDXIdentifier: "SPDXRef-CommonsLangSrc", + FileSPDXIdentifier: "CommonsLangSrc", Checksums: []common.Checksum{ { Algorithm: "SHA1", @@ -317,7 +294,7 @@ var want2_2 = v2_2.Document{ FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())", }, { - FileSPDXIdentifier: "SPDXRef-JenaLib", + FileSPDXIdentifier: "JenaLib", Checksums: []common.Checksum{ { Algorithm: "SHA1", @@ -334,8 +311,8 @@ var want2_2 = v2_2.Document{ LicenseInfoInFiles: []string{"LicenseRef-1"}, }, { - FileSPDXIdentifier: "SPDXRef-File", - Annotations: []v2_2.Annotation{ + FileSPDXIdentifier: "File", + Annotations: []spdx.Annotation{ { Annotator: common.Annotator{ Annotator: "File Commenter", @@ -367,29 +344,29 @@ var want2_2 = v2_2.Document{ FileNotice: "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", }, }, - Snippets: []v2_2.Snippet{ + Snippets: []spdx.Snippet{ { - SnippetSPDXIdentifier: "SPDXRef-Snippet", - SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource", + SnippetSPDXIdentifier: "Snippet", + SnippetFromFileSPDXIdentifier: "DoapSource", Ranges: []common.SnippetRange{ { StartPointer: common.SnippetRangePointer{ Offset: 310, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, EndPointer: common.SnippetRangePointer{ Offset: 420, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, }, { StartPointer: common.SnippetRangePointer{ LineNumber: 5, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, EndPointer: common.SnippetRangePointer{ LineNumber: 23, - FileSPDXIdentifier: "SPDXRef-DoapSource", + FileSPDXIdentifier: "DoapSource", }, }, }, @@ -399,13 +376,15 @@ var want2_2 = v2_2.Document{ SnippetCopyrightText: "Copyright 2008-2010 John Smith", SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.", SnippetName: "from linux kernel", + // omitted: SnippetAttributionTexts: []string{"Snippet attribution"}, }, }, - Relationships: []*v2_2.Relationship{ + Relationships: []*spdx.Relationship{ { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", + RefA: common.MakeDocElementID("", "DOCUMENT"), + RefB: common.MakeDocElementID("", "Package"), + Relationship: "CONTAINS", + RelationshipComment: "A relationship comment", }, { RefA: common.MakeDocElementID("", "DOCUMENT"), @@ -448,4 +427,12 @@ var want2_2 = v2_2.Document{ Relationship: "GENERATED_FROM", }, }, + // omitted: Reviews: []*spdx.Review{ + // { + // Reviewer: "joe@example.com", + // ReviewerType: "Person", + // ReviewDate: "2021-11-03T05:43:21Z", + // ReviewComment: "This is a review comment", + // }, + //}, } diff --git a/spdx/v2_3/file.go b/spdx/v2/v2_3/file.go similarity index 94% rename from spdx/v2_3/file.go rename to spdx/v2/v2_3/file.go index c472fdb2..9f8f28ac 100644 --- a/spdx/v2_3/file.go +++ b/spdx/v2/v2_3/file.go @@ -2,9 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// File is a File section of an SPDX Document for version 2.3 of the spec. +// File is a File section of an SPDX Document type File struct { // 8.1: File Name // Cardinality: mandatory, one @@ -74,7 +76,7 @@ type File struct { } // ArtifactOfProject is a DEPRECATED collection of data regarding -// a Package, as defined in sections 8.9-8.11 in version 2.3 of the spec. +// a Package, as defined in sections 8.9-8.11. // NOTE: the JSON schema does not define the structure of this object: // https://github.com/spdx/spdx-spec/blob/development/v2.3.1/schemas/spdx-schema.json#L480 type ArtifactOfProject struct { diff --git a/spdx/v2/v2_3/json/json_test.go b/spdx/v2/v2_3/json/json_test.go new file mode 100644 index 00000000..e75ec3ad --- /dev/null +++ b/spdx/v2/v2_3/json/json_test.go @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package json + +import ( + "bytes" + "flag" + "fmt" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + + "github.com/spdx/tools-golang/json" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/spdx/v2/v2_3/example" +) + +var update = *flag.Bool("update-snapshots", false, "update the example snapshot") + +func Test_Read(t *testing.T) { + fileName := "../../../../examples/sample-docs/json/SPDXJSONExample-v2.3.spdx.json" + + want := example.Copy() + + if update { + w := &bytes.Buffer{} + + err := json.Write(want, w) + if err != nil { + t.Errorf("unable to serialize SPDX 2.3 example to JSON: %v", err) + } + err = os.WriteFile(fileName, w.Bytes(), 0644) + if err != nil { + t.Errorf("unable to write SPDX 2.3 example to JSON: %v", err) + } + } + + file, err := os.Open(fileName) + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + var got spdx.Document + err = json.ReadInto(file, &got) + if err != nil { + t.Errorf("json.parser.Load() error = %v", err) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} + +func Test_Write(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + + if err := json.Write(&want, w); err != nil { + t.Errorf("Write() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote + var got spdx.Document + err := json.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after writing and re-parsing JSON example: %s", cmp.Diff(want, got)) + return + } +} diff --git a/spdx/v2_3/other_license.go b/spdx/v2/v2_3/other_license.go similarity index 90% rename from spdx/v2_3/other_license.go rename to spdx/v2/v2_3/other_license.go index 363bb412..55971f42 100644 --- a/spdx/v2_3/other_license.go +++ b/spdx/v2/v2_3/other_license.go @@ -2,8 +2,7 @@ package v2_3 -// OtherLicense is an Other License Information section of an -// SPDX Document for version 2.3 of the spec. +// OtherLicense is an Other License Information section of an SPDX Document type OtherLicense struct { // 10.1: License Identifier: "LicenseRef-[idstring]" // Cardinality: conditional (mandatory, one) if license is not diff --git a/spdx/v2_3/package.go b/spdx/v2/v2_3/package.go similarity index 95% rename from spdx/v2_3/package.go rename to spdx/v2/v2_3/package.go index b9d5b951..36457e93 100644 --- a/spdx/v2_3/package.go +++ b/spdx/v2/v2_3/package.go @@ -2,9 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// Package is a Package section of an SPDX Document for version 2.3 of the spec. +// Package is a Package section of an SPDX Document type Package struct { // NOT PART OF SPEC // flag: does this "package" contain files that were in fact "unpackaged", @@ -102,7 +104,7 @@ type Package struct { // 7.22: Package External Reference Comment // Cardinality: conditional (optional, one) for each External Reference - // contained within PackageExternalReference2_1 struct, if present + // contained within PackageExternalReference struct, if present // 7.23: Package Attribution Text // Cardinality: optional, one or many @@ -132,7 +134,7 @@ type Package struct { } // PackageExternalReference is an External Reference to additional info -// about a Package, as defined in section 7.21 in version 2.3 of the spec. +// about a Package, as defined in section 7.21 type PackageExternalReference struct { // category is "SECURITY", "PACKAGE-MANAGER" or "OTHER" Category string `json:"referenceCategory"` diff --git a/spdx/v2/v2_3/rdf/reader.go b/spdx/v2/v2_3/rdf/reader.go new file mode 100644 index 00000000..8bd5e66a --- /dev/null +++ b/spdx/v2/v2_3/rdf/reader.go @@ -0,0 +1,21 @@ +package rdf + +import ( + "io" + + "github.com/spdx/gordf/rdfloader" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/spdx/v2/v2_3/rdf/reader" +) + +// Takes in a file Reader and returns the pertaining spdx document +// or the error if any is encountered while setting the doc. +func Read(content io.Reader) (*spdx.Document, error) { + var rdfParserObj, err = rdfloader.LoadFromReaderObject(content) + if err != nil { + return nil, err + } + + doc, err := reader.LoadFromGoRDFParser(rdfParserObj) + return doc, err +} diff --git a/rdfloader/parser2v3/constants.go b/spdx/v2/v2_3/rdf/reader/constants.go similarity index 99% rename from rdfloader/parser2v3/constants.go rename to spdx/v2/v2_3/rdf/reader/constants.go index 44ca0d4c..42206025 100644 --- a/rdfloader/parser2v3/constants.go +++ b/spdx/v2/v2_3/rdf/reader/constants.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import "github.com/spdx/gordf/rdfloader/parser" diff --git a/rdfloader/parser2v3/license_utils.go b/spdx/v2/v2_3/rdf/reader/license_utils.go similarity index 98% rename from rdfloader/parser2v3/license_utils.go rename to spdx/v2/v2_3/rdf/reader/license_utils.go index 11bb3c80..74ade497 100644 --- a/rdfloader/parser2v3/license_utils.go +++ b/spdx/v2/v2_3/rdf/reader/license_utils.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) /* util methods for licenses and checksums below:*/ diff --git a/rdfloader/parser2v3/license_utils_test.go b/spdx/v2/v2_3/rdf/reader/license_utils_test.go similarity index 99% rename from rdfloader/parser2v3/license_utils_test.go rename to spdx/v2/v2_3/rdf/reader/license_utils_test.go index 0156b993..26a9fdfb 100644 --- a/rdfloader/parser2v3/license_utils_test.go +++ b/spdx/v2/v2_3/rdf/reader/license_utils_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" diff --git a/rdfloader/parser2v3/parse_annotation.go b/spdx/v2/v2_3/rdf/reader/parse_annotation.go similarity index 85% rename from rdfloader/parser2v3/parse_annotation.go rename to spdx/v2/v2_3/rdf/reader/parse_annotation.go index 507158ab..012bcf5a 100644 --- a/rdfloader/parser2v3/parse_annotation.go +++ b/spdx/v2/v2_3/rdf/reader/parse_annotation.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "errors" "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // creates a new instance of annotation and sets the annotation attributes // associated with the given node. // The newly created annotation is appended to the doc. func (parser *rdfParser2_3) parseAnnotationFromNode(node *gordfParser.Node) (err error) { - ann := &v2_3.Annotation{} + ann := &spdx.Annotation{} for _, subTriple := range parser.nodeToTriples(node) { switch subTriple.Predicate.ID { case SPDX_ANNOTATOR: @@ -42,19 +42,19 @@ func (parser *rdfParser2_3) parseAnnotationFromNode(node *gordfParser.Node) (err return setAnnotationToParser(parser, ann) } -func setAnnotationToParser(parser *rdfParser2_3, annotation *v2_3.Annotation) error { +func setAnnotationToParser(parser *rdfParser2_3, annotation *spdx.Annotation) error { if parser.doc == nil { return errors.New("uninitialized spdx document") } if parser.doc.Annotations == nil { - parser.doc.Annotations = []*v2_3.Annotation{} + parser.doc.Annotations = []*spdx.Annotation{} } parser.doc.Annotations = append(parser.doc.Annotations, annotation) return nil } // annotator is of type [Person|Organization|Tool]:String -func setAnnotatorFromString(annotatorString string, ann *v2_3.Annotation) error { +func setAnnotatorFromString(annotatorString string, ann *spdx.Annotation) error { subkey, subvalue, err := ExtractSubs(annotatorString, ":") if err != nil { return err @@ -68,7 +68,7 @@ func setAnnotatorFromString(annotatorString string, ann *v2_3.Annotation) error } // it can be NS_SPDX+annotationType_[review|other] -func setAnnotationType(annType string, ann *v2_3.Annotation) error { +func setAnnotationType(annType string, ann *spdx.Annotation) error { switch annType { case SPDX_ANNOTATION_TYPE_OTHER: ann.AnnotationType = "OTHER" diff --git a/rdfloader/parser2v3/parse_annotation_test.go b/spdx/v2/v2_3/rdf/reader/parse_annotation_test.go similarity index 95% rename from rdfloader/parser2v3/parse_annotation_test.go rename to spdx/v2/v2_3/rdf/reader/parse_annotation_test.go index 1ce0bd73..54c85453 100644 --- a/rdfloader/parser2v3/parse_annotation_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_annotation_test.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func Test_setAnnotatorFromString(t *testing.T) { // TestCase 1: Empty String must raise an error - ann := &v2_3.Annotation{} + ann := &spdx.Annotation{} input := "" err := setAnnotatorFromString(input, ann) if err == nil { @@ -18,7 +18,7 @@ func Test_setAnnotatorFromString(t *testing.T) { } // TestCase 2: Invalid annotator type - ann = &v2_3.Annotation{} + ann = &spdx.Annotation{} input = "Company: some_company" err = setAnnotatorFromString(input, ann) if err == nil { @@ -26,7 +26,7 @@ func Test_setAnnotatorFromString(t *testing.T) { } // TestCase 3: Valid annotator - ann = &v2_3.Annotation{} + ann = &spdx.Annotation{} input = "Person: Rishabh" err = setAnnotatorFromString(input, ann) if err != nil { @@ -41,7 +41,7 @@ func Test_setAnnotatorFromString(t *testing.T) { } func Test_setAnnotationType(t *testing.T) { - ann := &v2_3.Annotation{} + ann := &spdx.Annotation{} // TestCase 1: invalid input (empty annotationType) err := setAnnotationType("", ann) if err == nil { @@ -77,7 +77,7 @@ func Test_setAnnotationToParser(t *testing.T) { // TestCase 1: doc is nil (must raise an error) parser, _ := parserFromBodyContent(``) parser.doc = nil - err := setAnnotationToParser(parser, &v2_3.Annotation{}) + err := setAnnotationToParser(parser, &spdx.Annotation{}) if err == nil { t.Errorf("empty doc should've raised an error") } @@ -86,7 +86,7 @@ func Test_setAnnotationToParser(t *testing.T) { // list and append the input to it. parser, _ = parserFromBodyContent(``) parser.doc.Annotations = nil - err = setAnnotationToParser(parser, &v2_3.Annotation{}) + err = setAnnotationToParser(parser, &spdx.Annotation{}) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/rdfloader/parser2v3/parse_creation_info.go b/spdx/v2/v2_3/rdf/reader/parse_creation_info.go similarity index 84% rename from rdfloader/parser2v3/parse_creation_info.go rename to spdx/v2/v2_3/rdf/reader/parse_creation_info.go index 93a5628a..e07ed7cd 100644 --- a/rdfloader/parser2v3/parse_creation_info.go +++ b/spdx/v2/v2_3/rdf/reader/parse_creation_info.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // Cardinality: Mandatory, one. -func (parser *rdfParser2_3) parseCreationInfoFromNode(ci *v2_3.CreationInfo, node *gordfParser.Node) error { +func (parser *rdfParser2_3) parseCreationInfoFromNode(ci *spdx.CreationInfo, node *gordfParser.Node) error { for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case SPDX_LICENSE_LIST_VERSION: // 2.7 @@ -37,7 +37,7 @@ func (parser *rdfParser2_3) parseCreationInfoFromNode(ci *v2_3.CreationInfo, nod return nil } -func setCreator(creatorStr string, ci *v2_3.CreationInfo) error { +func setCreator(creatorStr string, ci *spdx.CreationInfo) error { entityType, entity, err := ExtractSubs(creatorStr, ":") if err != nil { return fmt.Errorf("error setting creator of a creation info: %s", err) diff --git a/rdfloader/parser2v3/parse_creation_info_test.go b/spdx/v2/v2_3/rdf/reader/parse_creation_info_test.go similarity index 89% rename from rdfloader/parser2v3/parse_creation_info_test.go rename to spdx/v2/v2_3/rdf/reader/parse_creation_info_test.go index 354ac73b..b52e4d44 100644 --- a/rdfloader/parser2v3/parse_creation_info_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_creation_info_test.go @@ -1,31 +1,31 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func Test_setCreator(t *testing.T) { // TestCase 1: invalid creator (empty) input := "" - err := setCreator(input, &v2_3.CreationInfo{}) + err := setCreator(input, &spdx.CreationInfo{}) if err == nil { t.Errorf("shoud've raised an error due to invalid input") } // TestCase 2: invalid entity type input = "Company: some company" - err = setCreator(input, &v2_3.CreationInfo{}) + err = setCreator(input, &spdx.CreationInfo{}) if err == nil { t.Errorf("shoud've raised an error due to unknown entity type") } // TestCase 3: valid input input = "Person: Jane Doe" - ci := &v2_3.CreationInfo{} + ci := &spdx.CreationInfo{} err = setCreator(input, ci) if err != nil { t.Errorf("error parsing a valid input: %v", err) @@ -49,7 +49,7 @@ func Test_rdfParser2_3_parseCreationInfoFromNode(t *testing.T) { `) ciNode := parser.gordfParserObj.Triples[0].Subject - err := parser.parseCreationInfoFromNode(&v2_3.CreationInfo{}, ciNode) + err := parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode) if err == nil { t.Errorf("invalid creator must raise an error") } @@ -66,7 +66,7 @@ func Test_rdfParser2_3_parseCreationInfoFromNode(t *testing.T) { `) ciNode = parser.gordfParserObj.Triples[0].Subject - err = parser.parseCreationInfoFromNode(&v2_3.CreationInfo{}, ciNode) + err = parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode) if err == nil { t.Errorf("unknown predicate must raise an error") } @@ -81,7 +81,7 @@ func Test_rdfParser2_3_parseCreationInfoFromNode(t *testing.T) { `) ciNode = parser.gordfParserObj.Triples[0].Subject - ci := &v2_3.CreationInfo{} + ci := &spdx.CreationInfo{} err = parser.parseCreationInfoFromNode(ci, ciNode) if err != nil { t.Errorf("unexpected error: %v", err) diff --git a/rdfloader/parser2v3/parse_file.go b/spdx/v2/v2_3/rdf/reader/parse_file.go similarity index 93% rename from rdfloader/parser2v3/parse_file.go rename to spdx/v2/v2_3/rdf/reader/parse_file.go index 54c92c1c..cce0f9f8 100644 --- a/rdfloader/parser2v3/parse_file.go +++ b/spdx/v2/v2_3/rdf/reader/parse_file.go @@ -1,19 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // returns a file instance and the error if any encountered. -func (parser *rdfParser2_3) getFileFromNode(fileNode *gordfParser.Node) (file *v2_3.File, err error) { - file = &v2_3.File{} +func (parser *rdfParser2_3) getFileFromNode(fileNode *gordfParser.Node) (file *spdx.File, err error) { + file = &spdx.File{} currState := parser.cache[fileNode.ID] if currState == nil { @@ -24,7 +24,7 @@ func (parser *rdfParser2_3) getFileFromNode(fileNode *gordfParser.Node) (file *v } } else if currState.Color == GREY { // we have already started parsing this file node and we needn't parse it again. - return currState.object.(*v2_3.File), nil + return currState.object.(*spdx.File), nil } // setting color to grey to indicate that we've started parsing this node. @@ -87,7 +87,7 @@ func (parser *rdfParser2_3) getFileFromNode(fileNode *gordfParser.Node) (file *v // deprecated artifactOf (see sections 4.9, 4.10, 4.11) case SPDX_ARTIFACT_OF: // cardinality: min 0 - var artifactOf *v2_3.ArtifactOfProject + var artifactOf *spdx.ArtifactOfProject artifactOf, err = parser.getArtifactFromNode(subTriple.Object) file.ArtifactOfProjects = append(file.ArtifactOfProjects, artifactOf) case RDFS_COMMENT: // 4.12 @@ -126,7 +126,7 @@ func (parser *rdfParser2_3) getFileFromNode(fileNode *gordfParser.Node) (file *v return file, nil } -func (parser *rdfParser2_3) setFileChecksumFromNode(file *v2_3.File, checksumNode *gordfParser.Node) error { +func (parser *rdfParser2_3) setFileChecksumFromNode(file *spdx.File, checksumNode *gordfParser.Node) error { checksumAlgorithm, checksumValue, err := parser.getChecksumFromNode(checksumNode) if err != nil { return fmt.Errorf("error parsing checksumNode of a file: %v", err) @@ -161,8 +161,8 @@ func (parser *rdfParser2_3) setFileChecksumFromNode(file *v2_3.File, checksumNod return nil } -func (parser *rdfParser2_3) getArtifactFromNode(node *gordfParser.Node) (*v2_3.ArtifactOfProject, error) { - artifactOf := &v2_3.ArtifactOfProject{} +func (parser *rdfParser2_3) getArtifactFromNode(node *gordfParser.Node) (*spdx.ArtifactOfProject, error) { + artifactOf := &spdx.ArtifactOfProject{} // setting artifactOfProjectURI attribute (which is optional) if node.NodeType == gordfParser.IRI { artifactOf.URI = node.ID @@ -203,7 +203,7 @@ func (parser *rdfParser2_3) setUnpackagedFiles() { } } -func setFileIdentifier(idURI string, file *v2_3.File) (err error) { +func setFileIdentifier(idURI string, file *spdx.File) (err error) { idURI = strings.TrimSpace(idURI) uriFragment := getLastPartOfURI(idURI) file.FileSPDXIdentifier, err = ExtractElementID(uriFragment) diff --git a/rdfloader/parser2v3/parse_file_test.go b/spdx/v2/v2_3/rdf/reader/parse_file_test.go similarity index 98% rename from rdfloader/parser2v3/parse_file_test.go rename to spdx/v2/v2_3/rdf/reader/parse_file_test.go index f7348ef6..f6aa993a 100644 --- a/rdfloader/parser2v3/parse_file_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_file_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "bufio" @@ -10,8 +10,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" rdfloader2 "github.com/spdx/gordf/rdfloader/xmlreader" gordfWriter "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // content is the tags within the rdf:RDF tag @@ -144,9 +144,9 @@ func Test_rdfParser2_3_setUnpackagedFiles(t *testing.T) { // unpackaged files are the files which are not associated with any package // file associated with a package sets parser.assocWithPackage[fileID] to true. rdfParser, _ := parserFromBodyContent(``) - file1 := &v2_3.File{FileSPDXIdentifier: common.ElementID("file1")} - file2 := &v2_3.File{FileSPDXIdentifier: common.ElementID("file2")} - file3 := &v2_3.File{FileSPDXIdentifier: common.ElementID("file3")} + file1 := &spdx.File{FileSPDXIdentifier: common.ElementID("file1")} + file2 := &spdx.File{FileSPDXIdentifier: common.ElementID("file2")} + file3 := &spdx.File{FileSPDXIdentifier: common.ElementID("file3")} // setting files to the document as if it were to be set when it was parsed using triples. rdfParser.files[file1.FileSPDXIdentifier] = file1 @@ -175,7 +175,7 @@ func Test_rdfParser2_3_setUnpackagedFiles(t *testing.T) { } func Test_setFileIdentifier(t *testing.T) { - file := &v2_3.File{} + file := &spdx.File{} // TestCase 1: valid example err := setFileIdentifier("http://spdx.org/documents/spdx-toolsv2.1.7-SNAPSHOT#SPDXRef-129", file) @@ -202,7 +202,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file := &v2_3.File{} + file := &spdx.File{} err := parser.setFileChecksumFromNode(file, checksumNode) if err != nil { t.Errorf("error parsing a valid checksum node") @@ -233,7 +233,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode = gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file = &v2_3.File{} + file = &spdx.File{} err = parser.setFileChecksumFromNode(file, checksumNode) if err != nil { t.Errorf("error parsing a valid checksum node") @@ -263,7 +263,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode = gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file = &v2_3.File{} + file = &spdx.File{} err = parser.setFileChecksumFromNode(file, checksumNode) if err != nil { t.Errorf("error parsing a valid checksum node") @@ -292,7 +292,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode = gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file = &v2_3.File{} + file = &spdx.File{} err = parser.setFileChecksumFromNode(file, checksumNode) if err == nil { t.Errorf("should've raised an error parsing an invalid checksum node") @@ -306,7 +306,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode = gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file = &v2_3.File{} + file = &spdx.File{} err = parser.setFileChecksumFromNode(file, checksumNode) if err == nil { t.Errorf("should've raised an error parsing an invalid checksum node") @@ -320,7 +320,7 @@ func Test_rdfParser2_3_setFileChecksumFromNode(t *testing.T) { `) checksumNode = gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_CHECKSUM_CAPITALIZED)[0].Subject - file = &v2_3.File{} + file = &spdx.File{} err = parser.setFileChecksumFromNode(file, checksumNode) if err == nil { t.Errorf("should've raised an error parsing an invalid checksum algorithm for a file") @@ -485,7 +485,7 @@ func Test_rdfParser2_3_getFileFromNode(t *testing.T) { } parser, _ = parserFromBodyContent(strings.Join(fileDefinitions, "")) - var file *v2_3.File + var file *spdx.File packageTypeTriples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_PACKAGE) for _, typeTriple := range packageTypeTriples { pkg, err := parser.getPackageFromNode(typeTriple.Subject) diff --git a/rdfloader/parser2v3/parse_license.go b/spdx/v2/v2_3/rdf/reader/parse_license.go similarity index 99% rename from rdfloader/parser2v3/parse_license.go rename to spdx/v2/v2_3/rdf/reader/parse_license.go index b75ca05c..ae501052 100644 --- a/rdfloader/parser2v3/parse_license.go +++ b/spdx/v2/v2_3/rdf/reader/parse_license.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "errors" diff --git a/rdfloader/parser2v3/parse_license_test.go b/spdx/v2/v2_3/rdf/reader/parse_license_test.go similarity index 99% rename from rdfloader/parser2v3/parse_license_test.go rename to spdx/v2/v2_3/rdf/reader/parse_license_test.go index 9ff2c884..cd852b6c 100644 --- a/rdfloader/parser2v3/parse_license_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_license_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" diff --git a/rdfloader/parser2v3/parse_other_license_info.go b/spdx/v2/v2_3/rdf/reader/parse_other_license_info.go similarity index 90% rename from rdfloader/parser2v3/parse_other_license_info.go rename to spdx/v2/v2_3/rdf/reader/parse_other_license_info.go index 3e289799..4b136eb1 100644 --- a/rdfloader/parser2v3/parse_other_license_info.go +++ b/spdx/v2/v2_3/rdf/reader/parse_other_license_info.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func (parser *rdfParser2_3) getExtractedLicensingInfoFromNode(node *gordfParser.Node) (lic ExtractedLicensingInfo, err error) { @@ -28,7 +28,7 @@ func (parser *rdfParser2_3) getExtractedLicensingInfoFromNode(node *gordfParser. return lic, nil } -func (parser *rdfParser2_3) extractedLicenseToOtherLicense(extLicense ExtractedLicensingInfo) (othLic v2_3.OtherLicense) { +func (parser *rdfParser2_3) extractedLicenseToOtherLicense(extLicense ExtractedLicensingInfo) (othLic spdx.OtherLicense) { othLic.LicenseIdentifier = extLicense.licenseID othLic.ExtractedText = extLicense.extractedText othLic.LicenseComment = extLicense.comment diff --git a/rdfloader/parser2v3/parse_other_license_info_test.go b/spdx/v2/v2_3/rdf/reader/parse_other_license_info_test.go similarity index 99% rename from rdfloader/parser2v3/parse_other_license_info_test.go rename to spdx/v2/v2_3/rdf/reader/parse_other_license_info_test.go index cb121818..a5d84241 100644 --- a/rdfloader/parser2v3/parse_other_license_info_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_other_license_info_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" diff --git a/rdfloader/parser2v3/parse_package.go b/spdx/v2/v2_3/rdf/reader/parse_package.go similarity index 93% rename from rdfloader/parser2v3/parse_package.go rename to spdx/v2/v2_3/rdf/reader/parse_package.go index 0cdf68ca..b868e7d1 100644 --- a/rdfloader/parser2v3/parse_package.go +++ b/spdx/v2/v2_3/rdf/reader/parse_package.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *rdfParser2_3) getPackageFromNode(packageNode *gordfParser.Node) (pkg *v2_3.Package, err error) { - pkg = &v2_3.Package{} // new package which will be returned +func (parser *rdfParser2_3) getPackageFromNode(packageNode *gordfParser.Node) (pkg *spdx.Package, err error) { + pkg = &spdx.Package{} // new package which will be returned currState := parser.cache[packageNode.ID] if currState == nil { @@ -24,7 +24,7 @@ func (parser *rdfParser2_3) getPackageFromNode(packageNode *gordfParser.Node) (p } } else if currState.Color == GREY { // we have already started parsing this package node and we needn't parse it again. - return currState.object.(*v2_3.Package), nil + return currState.object.(*spdx.Package), nil } // setting color of the state to grey to indicate that we've started to @@ -179,8 +179,8 @@ func (parser *rdfParser2_3) getPackageFromNode(packageNode *gordfParser.Node) (p } // parses externalReference found in the package by the associated triple. -func (parser *rdfParser2_3) getPackageExternalRef(node *gordfParser.Node) (externalDocRef *v2_3.PackageExternalReference, err error) { - externalDocRef = &v2_3.PackageExternalReference{} +func (parser *rdfParser2_3) getPackageExternalRef(node *gordfParser.Node) (externalDocRef *spdx.PackageExternalReference, err error) { + externalDocRef = &spdx.PackageExternalReference{} for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case SPDX_REFERENCE_CATEGORY: @@ -230,7 +230,7 @@ func getPrimaryPackagePurpose(purpose string) string { return "" } -func (parser *rdfParser2_3) setPackageVerificationCode(pkg *v2_3.Package, node *gordfParser.Node) error { +func (parser *rdfParser2_3) setPackageVerificationCode(pkg *spdx.Package, node *gordfParser.Node) error { if pkg.PackageVerificationCode == nil { pkg.PackageVerificationCode = &common.PackageVerificationCode{} } @@ -254,9 +254,9 @@ func (parser *rdfParser2_3) setPackageVerificationCode(pkg *v2_3.Package, node * // appends the file to the package and also sets the assocWithPackage for the // file to indicate the file is associated with a package -func (parser *rdfParser2_3) setFileToPackage(pkg *v2_3.Package, file *v2_3.File) { +func (parser *rdfParser2_3) setFileToPackage(pkg *spdx.Package, file *spdx.File) { if pkg.Files == nil { - pkg.Files = []*v2_3.File{} + pkg.Files = []*spdx.File{} } pkg.Files = append(pkg.Files, file) parser.assocWithPackage[file.FileSPDXIdentifier] = true @@ -266,7 +266,7 @@ func (parser *rdfParser2_3) setFileToPackage(pkg *v2_3.Package, file *v2_3.File) // Args: // // value: [NOASSERTION | [Person | Organization]: string] -func setPackageSupplier(pkg *v2_3.Package, value string) error { +func setPackageSupplier(pkg *spdx.Package, value string) error { value = strings.TrimSpace(value) supplier := &common.Supplier{} if strings.ToUpper(value) == "NOASSERTION" { @@ -296,7 +296,7 @@ func setPackageSupplier(pkg *v2_3.Package, value string) error { // Args: // // value: [NOASSERTION | [Person | Organization]: string] -func setPackageOriginator(pkg *v2_3.Package, value string) error { +func setPackageOriginator(pkg *spdx.Package, value string) error { value = strings.TrimSpace(value) originator := &common.Originator{} if strings.ToUpper(value) == "NOASSERTION" { @@ -323,7 +323,7 @@ func setPackageOriginator(pkg *v2_3.Package, value string) error { } // validates the uri and sets the location if it is valid -func setDocumentLocationFromURI(pkg *v2_3.Package, locationURI string) error { +func setDocumentLocationFromURI(pkg *spdx.Package, locationURI string) error { switch locationURI { case SPDX_NOASSERTION_CAPS, SPDX_NOASSERTION_SMALL: pkg.PackageDownloadLocation = "NOASSERTION" @@ -340,13 +340,13 @@ func setDocumentLocationFromURI(pkg *v2_3.Package, locationURI string) error { // sets the FilesAnalyzed attribute to the given package // boolValue is a string of type "true" or "false" -func setFilesAnalyzed(pkg *v2_3.Package, boolValue string) (err error) { +func setFilesAnalyzed(pkg *spdx.Package, boolValue string) (err error) { pkg.IsFilesAnalyzedTagPresent = true pkg.FilesAnalyzed, err = boolFromString(boolValue) return err } -func (parser *rdfParser2_3) setPackageChecksum(pkg *v2_3.Package, node *gordfParser.Node) error { +func (parser *rdfParser2_3) setPackageChecksum(pkg *spdx.Package, node *gordfParser.Node) error { checksumAlgorithm, checksumValue, err := parser.getChecksumFromNode(node) if err != nil { return fmt.Errorf("error getting checksum algorithm and value from %v", node) diff --git a/rdfloader/parser2v3/parse_package_test.go b/spdx/v2/v2_3/rdf/reader/parse_package_test.go similarity index 96% rename from rdfloader/parser2v3/parse_package_test.go rename to spdx/v2/v2_3/rdf/reader/parse_package_test.go index 23be653d..1bb14466 100644 --- a/rdfloader/parser2v3/parse_package_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_package_test.go @@ -1,21 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func Test_setPackageSupplier(t *testing.T) { var err error // TestCase 1: no assertion must set PackageSupplierNOASSERTION field to true - pkg := &v2_3.Package{} + pkg := &spdx.Package{} err = setPackageSupplier(pkg, "NOASSERTION") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -26,7 +26,7 @@ func Test_setPackageSupplier(t *testing.T) { // TestCase 2: lower-case noassertion must also set the // PackageSupplierNOASSERTION to true. - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setPackageSupplier(pkg, "noassertion") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -36,7 +36,7 @@ func Test_setPackageSupplier(t *testing.T) { } // TestCase 3: invalid input without colon separator. must raise an error - pkg = &v2_3.Package{} + pkg = &spdx.Package{} input := "string without colon separator" err = setPackageSupplier(pkg, input) if err == nil { @@ -44,7 +44,7 @@ func Test_setPackageSupplier(t *testing.T) { } // TestCase 4: Valid Person - pkg = &v2_3.Package{} + pkg = &spdx.Package{} personName := "Rishabh Bhatnagar" input = "Person: " + personName err = setPackageSupplier(pkg, input) @@ -56,7 +56,7 @@ func Test_setPackageSupplier(t *testing.T) { } // TestCase 5: Valid Organization - pkg = &v2_3.Package{} + pkg = &spdx.Package{} orgName := "SPDX" input = "Organization: " + orgName err = setPackageSupplier(pkg, input) @@ -68,7 +68,7 @@ func Test_setPackageSupplier(t *testing.T) { } // TestCase 6: Invalid EntityType - pkg = &v2_3.Package{} + pkg = &spdx.Package{} input = "InvalidEntity: entity" err = setPackageSupplier(pkg, input) if err == nil { @@ -80,7 +80,7 @@ func Test_setPackageOriginator(t *testing.T) { var err error // TestCase 1: no assertion must set PackageSupplierNOASSERTION field to true - pkg := &v2_3.Package{} + pkg := &spdx.Package{} err = setPackageOriginator(pkg, "NOASSERTION") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -91,7 +91,7 @@ func Test_setPackageOriginator(t *testing.T) { // TestCase 2: lower-case noassertion must also set the // PackageOriginatorNOASSERTION to true. - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setPackageOriginator(pkg, "noassertion") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -101,7 +101,7 @@ func Test_setPackageOriginator(t *testing.T) { } // TestCase 3: invalid input without colon separator. must raise an error - pkg = &v2_3.Package{} + pkg = &spdx.Package{} input := "string without colon separator" err = setPackageOriginator(pkg, input) if err == nil { @@ -109,7 +109,7 @@ func Test_setPackageOriginator(t *testing.T) { } // TestCase 4: Valid Person - pkg = &v2_3.Package{} + pkg = &spdx.Package{} personName := "Rishabh Bhatnagar" input = "Person: " + personName err = setPackageOriginator(pkg, input) @@ -121,7 +121,7 @@ func Test_setPackageOriginator(t *testing.T) { } // TestCase 5: Valid Organization - pkg = &v2_3.Package{} + pkg = &spdx.Package{} orgName := "SPDX" input = "Organization: " + orgName err = setPackageOriginator(pkg, input) @@ -133,7 +133,7 @@ func Test_setPackageOriginator(t *testing.T) { } // TestCase 6: Invalid EntityType - pkg = &v2_3.Package{} + pkg = &spdx.Package{} input = "InvalidEntity: entity" err = setPackageOriginator(pkg, input) if err == nil { @@ -144,7 +144,7 @@ func Test_setPackageOriginator(t *testing.T) { func Test_rdfParser2_3_setPackageVerificationCode(t *testing.T) { var parser *rdfParser2_3 var node *gordfParser.Node - var pkg *v2_3.Package + var pkg *spdx.Package var err error // TestCase 1: invalid predicate must raise an error @@ -156,7 +156,7 @@ func Test_rdfParser2_3_setPackageVerificationCode(t *testing.T) { `) node = parser.gordfParserObj.Triples[0].Subject - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = parser.setPackageVerificationCode(pkg, node) if err == nil { t.Errorf("expected an error due to invalid predicate, got ") @@ -170,7 +170,7 @@ func Test_rdfParser2_3_setPackageVerificationCode(t *testing.T) { `) node = parser.gordfParserObj.Triples[0].Subject - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = parser.setPackageVerificationCode(pkg, node) if err != nil { t.Errorf("unexpected error: %v", err) @@ -186,7 +186,7 @@ func Test_rdfParser2_3_setPackageVerificationCode(t *testing.T) { } func Test_rdfParser2_3_getPackageExternalRef(t *testing.T) { - var extRef *v2_3.PackageExternalReference + var extRef *spdx.PackageExternalReference var err error var parser *rdfParser2_3 var node *gordfParser.Node @@ -240,7 +240,7 @@ func Test_rdfParser2_3_getPackageExternalRef(t *testing.T) { if err != nil { t.Fatalf("unexpected error parsing a valid example: %v", err) } - expectedExtRef := &v2_3.PackageExternalReference{ + expectedExtRef := &spdx.PackageExternalReference{ Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", RefType: "http://spdx.org/rdf/references/cpe23Type", Category: "SECURITY", @@ -266,7 +266,7 @@ func Test_rdfParser2_3_getPackageExternalRef(t *testing.T) { if err != nil { t.Fatalf("unexpected error parsing a valid example: %v", err) } - expectedExtRef = &v2_3.PackageExternalReference{ + expectedExtRef = &spdx.PackageExternalReference{ Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", RefType: "http://spdx.org/rdf/references/cpe23Type", Category: "PACKAGE-MANAGER", @@ -292,7 +292,7 @@ func Test_rdfParser2_3_getPackageExternalRef(t *testing.T) { if err != nil { t.Fatalf("unexpected error parsing a valid example: %v", err) } - expectedExtRef = &v2_3.PackageExternalReference{ + expectedExtRef = &spdx.PackageExternalReference{ Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", RefType: "http://spdx.org/rdf/references/cpe23Type", Category: "OTHER", @@ -585,14 +585,14 @@ func Test_rdfParser2_3_getPackageFromNode(t *testing.T) { } func Test_rdfParser2_3_setFileToPackage(t *testing.T) { - var pkg *v2_3.Package - var file *v2_3.File + var pkg *spdx.Package + var file *spdx.File var parser *rdfParser2_3 // TestCase 1: setting to a nil files attribute shouldn't panic. parser, _ = parserFromBodyContent(``) - pkg = &v2_3.Package{} - file = &v2_3.File{} + pkg = &spdx.Package{} + file = &spdx.File{} parser.setFileToPackage(pkg, file) if len(pkg.Files) != 1 { t.Errorf("expected given package to have one file after setting, got %d", len(pkg.Files)) @@ -605,7 +605,7 @@ func Test_rdfParser2_3_setFileToPackage(t *testing.T) { func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { var parser *rdfParser2_3 var node *gordfParser.Node - var pkg *v2_3.Package + var pkg *spdx.Package var expectedChecksumValue string var err error @@ -616,7 +616,7 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { `) - pkg = &v2_3.Package{} + pkg = &spdx.Package{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setPackageChecksum(pkg, node) if err == nil { @@ -630,7 +630,7 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { `) - pkg = &v2_3.Package{} + pkg = &spdx.Package{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setPackageChecksum(pkg, node) if err == nil { @@ -644,7 +644,7 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { `) - pkg = &v2_3.Package{} + pkg = &spdx.Package{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setPackageChecksum(pkg, node) if err != nil { @@ -668,7 +668,7 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { `) - pkg = &v2_3.Package{} + pkg = &spdx.Package{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setPackageChecksum(pkg, node) if err != nil { @@ -691,7 +691,7 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { `) - pkg = &v2_3.Package{} + pkg = &spdx.Package{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setPackageChecksum(pkg, node) if err != nil { @@ -709,14 +709,14 @@ func Test_rdfParser2_3_setPackageChecksum(t *testing.T) { } func Test_setDocumentLocationFromURI(t *testing.T) { - var pkg *v2_3.Package + var pkg *spdx.Package var expectedDocumentLocation, gotDocumentLocation string var inputURI string var err error // TestCase 1: NOASSERTION inputURI = SPDX_NOASSERTION_SMALL - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setDocumentLocationFromURI(pkg, inputURI) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -729,7 +729,7 @@ func Test_setDocumentLocationFromURI(t *testing.T) { // TestCase 2: NONE inputURI = SPDX_NONE_CAPS - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setDocumentLocationFromURI(pkg, inputURI) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -742,7 +742,7 @@ func Test_setDocumentLocationFromURI(t *testing.T) { // TestCase 3: valid uri inputURI = "https://www.gnu.org/software/texinfo/" - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setDocumentLocationFromURI(pkg, inputURI) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -755,7 +755,7 @@ func Test_setDocumentLocationFromURI(t *testing.T) { // TestCase 3: invalid uri inputURI = " " - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setDocumentLocationFromURI(pkg, inputURI) if err == nil { t.Fatalf("expected an error due to invalid uri, got %v", err) @@ -763,18 +763,18 @@ func Test_setDocumentLocationFromURI(t *testing.T) { } func Test_setFilesAnalyzed(t *testing.T) { - var pkg *v2_3.Package + var pkg *spdx.Package var err error // TestCase 1: not a valid bool value: - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setFilesAnalyzed(pkg, "no") if err == nil { t.Errorf("expected an error due to invalid bool input, got %v", err) } // TestCase 2: valid input - pkg = &v2_3.Package{} + pkg = &spdx.Package{} err = setFilesAnalyzed(pkg, "true") if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/rdfloader/parser2v3/parse_relationship.go b/spdx/v2/v2_3/rdf/reader/parse_relationship.go similarity index 96% rename from rdfloader/parser2v3/parse_relationship.go rename to spdx/v2/v2_3/rdf/reader/parse_relationship.go index e182a45e..b1753dbc 100644 --- a/rdfloader/parser2v3/parse_relationship.go +++ b/spdx/v2/v2_3/rdf/reader/parse_relationship.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" @@ -8,8 +8,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // parsing the relationship that exists in the rdf document. @@ -17,7 +17,7 @@ import ( // parsing the relationship appends the relationship to the current document's // Relationships Slice. func (parser *rdfParser2_3) parseRelationship(triple *gordfParser.Triple) (err error) { - reln := v2_3.Relationship{} + reln := spdx.Relationship{} reln.RefA, err = getReferenceFromURI(triple.Subject.ID) if err != nil { @@ -87,7 +87,7 @@ func (parser *rdfParser2_3) parseRelationship(triple *gordfParser.Triple) (err e return nil } -func (parser *rdfParser2_3) parseRelatedElementFromTriple(reln *v2_3.Relationship, triple *gordfParser.Triple) error { +func (parser *rdfParser2_3) parseRelatedElementFromTriple(reln *spdx.Relationship, triple *gordfParser.Triple) error { // iterate over relatedElement Type and check which SpdxElement it is. var err error switch triple.Object.ID { diff --git a/rdfloader/parser2v3/parse_relationship_test.go b/spdx/v2/v2_3/rdf/reader/parse_relationship_test.go similarity index 98% rename from rdfloader/parser2v3/parse_relationship_test.go rename to spdx/v2/v2_3/rdf/reader/parse_relationship_test.go index 9c4ddffa..54f77bd1 100644 --- a/rdfloader/parser2v3/parse_relationship_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_relationship_test.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" "testing" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func Test_getReferenceFromURI(t *testing.T) { @@ -83,7 +83,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln := &v2_3.Relationship{} + reln := &spdx.Relationship{} triple := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_PACKAGE)[0] err := parser.parseRelatedElementFromTriple(reln, triple) if err != nil { @@ -112,7 +112,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln = &v2_3.Relationship{} + reln = &spdx.Relationship{} triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_PACKAGE)[0] err = parser.parseRelatedElementFromTriple(reln, triple) if err == nil { @@ -127,7 +127,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln = &v2_3.Relationship{} + reln = &spdx.Relationship{} triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_FILE)[0] err = parser.parseRelatedElementFromTriple(reln, triple) if err != nil { @@ -156,7 +156,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln = &v2_3.Relationship{} + reln = &spdx.Relationship{} triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_FILE)[0] err = parser.parseRelatedElementFromTriple(reln, triple) if err == nil { @@ -171,7 +171,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln = &v2_3.Relationship{} + reln = &spdx.Relationship{} triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_SPDX_ELEMENT)[0] err = parser.parseRelatedElementFromTriple(reln, triple) if err != nil { @@ -200,7 +200,7 @@ func Test_rdfParser2_3_parseRelatedElementFromTriple(t *testing.T) { `) - reln = &v2_3.Relationship{} + reln = &spdx.Relationship{} triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_SPDX_ELEMENT)[0] err = parser.parseRelatedElementFromTriple(reln, triple) if err == nil { diff --git a/rdfloader/parser2v3/parse_review.go b/spdx/v2/v2_3/rdf/reader/parse_review.go similarity index 91% rename from rdfloader/parser2v3/parse_review.go rename to spdx/v2/v2_3/rdf/reader/parse_review.go index c1c8b023..5f45a216 100644 --- a/rdfloader/parser2v3/parse_review.go +++ b/spdx/v2/v2_3/rdf/reader/parse_review.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func (parser *rdfParser2_3) setReviewFromNode(reviewedNode *gordfParser.Node) error { - review := v2_3.Review{} + review := spdx.Review{} for _, triple := range parser.nodeToTriples(reviewedNode) { switch triple.Predicate.ID { case RDF_TYPE: diff --git a/rdfloader/parser2v3/parse_review_test.go b/spdx/v2/v2_3/rdf/reader/parse_review_test.go similarity index 99% rename from rdfloader/parser2v3/parse_review_test.go rename to spdx/v2/v2_3/rdf/reader/parse_review_test.go index 18818aad..64f43dd7 100644 --- a/rdfloader/parser2v3/parse_review_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_review_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" diff --git a/rdfloader/parser2v3/parse_snippet_info.go b/spdx/v2/v2_3/rdf/reader/parse_snippet_info.go similarity index 92% rename from rdfloader/parser2v3/parse_snippet_info.go rename to spdx/v2/v2_3/rdf/reader/parse_snippet_info.go index 90da8736..9e24b7ac 100644 --- a/rdfloader/parser2v3/parse_snippet_info.go +++ b/spdx/v2/v2_3/rdf/reader/parse_snippet_info.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" @@ -8,14 +8,14 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // Snippet Information // Cardinality: Optional, Many -func (parser *rdfParser2_3) getSnippetInformationFromNode2_3(node *gordfParser.Node) (si *v2_3.Snippet, err error) { - si = &v2_3.Snippet{} +func (parser *rdfParser2_3) getSnippetInformationFromNode2_3(node *gordfParser.Node) (si *spdx.Snippet, err error) { + si = &spdx.Snippet{} err = setSnippetID(node.ID, si) if err != nil { @@ -74,14 +74,14 @@ func (parser *rdfParser2_3) getSnippetInformationFromNode2_3(node *gordfParser.N } // given is the id of the file, sets the snippet to the file in parser. -func (parser *rdfParser2_3) setSnippetToFileWithID(snippet *v2_3.Snippet, fileID common.ElementID) error { +func (parser *rdfParser2_3) setSnippetToFileWithID(snippet *spdx.Snippet, fileID common.ElementID) error { if parser.files[fileID] == nil { return fmt.Errorf("snippet refers to an undefined file with ID: %s", fileID) } // initializing snippet of the files if it is not defined already if parser.files[fileID].Snippets == nil { - parser.files[fileID].Snippets = map[common.ElementID]*v2_3.Snippet{} + parser.files[fileID].Snippets = map[common.ElementID]*spdx.Snippet{} } // setting the snippet to the file. @@ -90,7 +90,7 @@ func (parser *rdfParser2_3) setSnippetToFileWithID(snippet *v2_3.Snippet, fileID return nil } -func (parser *rdfParser2_3) setSnippetRangeFromNode(node *gordfParser.Node, si *v2_3.Snippet) error { +func (parser *rdfParser2_3) setSnippetRangeFromNode(node *gordfParser.Node, si *spdx.Snippet) error { // for a range object, we can have only 3 associated triples: // node -> RDF_TYPE -> Object // node -> startPointer -> Object @@ -149,7 +149,7 @@ func (parser *rdfParser2_3) setSnippetRangeFromNode(node *gordfParser.Node, si * return nil } -func (parser *rdfParser2_3) getPointerFromNode(node *gordfParser.Node, si *v2_3.Snippet) (rt RangeType, number int, err error) { +func (parser *rdfParser2_3) getPointerFromNode(node *gordfParser.Node, si *spdx.Snippet) (rt RangeType, number int, err error) { for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case RDF_TYPE: @@ -174,7 +174,7 @@ func (parser *rdfParser2_3) getPointerFromNode(node *gordfParser.Node, si *v2_3. return } -func (parser *rdfParser2_3) parseRangeReference(node *gordfParser.Node, snippet *v2_3.Snippet) error { +func (parser *rdfParser2_3) parseRangeReference(node *gordfParser.Node, snippet *spdx.Snippet) error { // reference is supposed to be either a resource reference to an already // defined or a new file. Unfortunately, I didn't find field where this can be set in the tools-golang data model. // todo: set this reference to the snippet @@ -189,7 +189,7 @@ func (parser *rdfParser2_3) parseRangeReference(node *gordfParser.Node, snippet return nil } -func setSnippetID(uri string, si *v2_3.Snippet) (err error) { +func setSnippetID(uri string, si *spdx.Snippet) (err error) { fragment := getLastPartOfURI(uri) si.SnippetSPDXIdentifier, err = ExtractElementID(fragment) if err != nil { diff --git a/rdfloader/parser2v3/parse_snippet_info_test.go b/spdx/v2/v2_3/rdf/reader/parse_snippet_info_test.go similarity index 96% rename from rdfloader/parser2v3/parse_snippet_info_test.go rename to spdx/v2/v2_3/rdf/reader/parse_snippet_info_test.go index 21475862..28f266ed 100644 --- a/rdfloader/parser2v3/parse_snippet_info_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_snippet_info_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func Test_rdfParser2_3_getSnippetInformationFromTriple2_3(t *testing.T) { @@ -131,13 +131,13 @@ func Test_rdfParser2_3_getSnippetInformationFromTriple2_3(t *testing.T) { func Test_setSnippetID(t *testing.T) { // TestCase 1: invalid input (empty) - err := setSnippetID("", &v2_3.Snippet{}) + err := setSnippetID("", &spdx.Snippet{}) if err == nil { t.Errorf("should've raised an error for empty input") } // TestCase 2: valid input - si := &v2_3.Snippet{} + si := &spdx.Snippet{} err = setSnippetID("http://spdx.org/spdxdocs/spdx-example#SPDXRef-Snippet", si) if err != nil { t.Errorf("unexpected error: %v", err) @@ -151,10 +151,10 @@ func Test_rdfParser2_3_parseRangeReference(t *testing.T) { var err error var node *gordfParser.Node var parser *rdfParser2_3 - var si *v2_3.Snippet + var si *spdx.Snippet // TestCase 1: ResourceLiteral node without a new file shouldn't raise any error. - si = &v2_3.Snippet{} + si = &spdx.Snippet{} parser, _ = parserFromBodyContent(``) node = &gordfParser.Node{ NodeType: gordfParser.RESOURCELITERAL, @@ -166,7 +166,7 @@ func Test_rdfParser2_3_parseRangeReference(t *testing.T) { } // TestCase 2: invalid file in the reference should raise an error - si = &v2_3.Snippet{} + si = &spdx.Snippet{} parser, _ = parserFromBodyContent(` test file @@ -179,7 +179,7 @@ func Test_rdfParser2_3_parseRangeReference(t *testing.T) { } // TestCase 3: A valid reference must set the file to the files map of the parser. - si = &v2_3.Snippet{} + si = &spdx.Snippet{} parser, _ = parserFromBodyContent(` test file @@ -198,7 +198,7 @@ func Test_rdfParser2_3_parseRangeReference(t *testing.T) { func Test_rdfParser2_3_getPointerFromNode(t *testing.T) { var parser *rdfParser2_3 var node *gordfParser.Node - var si *v2_3.Snippet + var si *spdx.Snippet var err error var rt RangeType var number int @@ -280,7 +280,7 @@ func Test_rdfParser2_3_getPointerFromNode(t *testing.T) { func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { var parser *rdfParser2_3 var err error - var si *v2_3.Snippet + var si *spdx.Snippet var node *gordfParser.Node // TestCase 1: range with less one pointer less must raise an error @@ -296,7 +296,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -322,7 +322,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject dummyTriple := parser.gordfParserObj.Triples[0] // resetting the node to be associated with 3 triples which will have @@ -352,7 +352,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -376,7 +376,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -400,7 +400,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -424,7 +424,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -448,7 +448,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err == nil { @@ -472,7 +472,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err != nil { @@ -496,7 +496,7 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { `) - si = &v2_3.Snippet{} + si = &spdx.Snippet{} node = parser.gordfParserObj.Triples[0].Subject err = parser.setSnippetRangeFromNode(node, si) if err != nil { @@ -507,13 +507,13 @@ func Test_rdfParser2_3_setSnippetRangeFromNode(t *testing.T) { func Test_rdfParser2_3_setSnippetToFileWithID(t *testing.T) { var parser *rdfParser2_3 var fileId common.ElementID - var si *v2_3.Snippet - var file *v2_3.File + var si *spdx.Snippet + var file *spdx.File var err error // TestCase 1: file id which is not associated with any file must raise an error. parser, _ = parserFromBodyContent("") - si = &v2_3.Snippet{} + si = &spdx.Snippet{} err = parser.setSnippetToFileWithID(si, fileId) if err == nil { t.Errorf("expected an error saying undefined file") @@ -521,7 +521,7 @@ func Test_rdfParser2_3_setSnippetToFileWithID(t *testing.T) { // TestCase 2: file exists, but snippet of the file doesn't ( it mustn't raise any error ) fileId = common.ElementID("File1") - file = &v2_3.File{ + file = &spdx.File{ FileSPDXIdentifier: fileId, } parser.files[fileId] = file diff --git a/rdfloader/parser2v3/parse_spdx_document.go b/spdx/v2/v2_3/rdf/reader/parse_spdx_document.go similarity index 94% rename from rdfloader/parser2v3/parse_spdx_document.go rename to spdx/v2/v2_3/rdf/reader/parse_spdx_document.go index abf89f9a..4b3cf1aa 100644 --- a/rdfloader/parser2v3/parse_spdx_document.go +++ b/spdx/v2/v2_3/rdf/reader/parse_spdx_document.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) func (parser *rdfParser2_3) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) (err error) { @@ -44,7 +44,7 @@ func (parser *rdfParser2_3) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) parser.doc.DocumentName = objectValue case SPDX_EXTERNAL_DOCUMENT_REF: // 2.6: externalDocumentReferences // cardinality: min 0 - var extRef v2_3.ExternalDocumentRef + var extRef spdx.ExternalDocumentRef extRef, err = parser.getExternalDocumentRefFromNode(subTriple.Object) if err != nil { return err @@ -61,7 +61,7 @@ func (parser *rdfParser2_3) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) err = parser.setReviewFromNode(subTriple.Object) case SPDX_DESCRIBES_PACKAGE: // describes Package // cardinality: min 0 - var pkg *v2_3.Package + var pkg *spdx.Package pkg, err = parser.getPackageFromNode(subTriple.Object) if err != nil { return err @@ -91,7 +91,7 @@ func (parser *rdfParser2_3) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) return nil } -func (parser *rdfParser2_3) getExternalDocumentRefFromNode(node *gordfParser.Node) (edr v2_3.ExternalDocumentRef, err error) { +func (parser *rdfParser2_3) getExternalDocumentRefFromNode(node *gordfParser.Node) (edr spdx.ExternalDocumentRef, err error) { for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case SPDX_EXTERNAL_DOCUMENT_ID: diff --git a/rdfloader/parser2v3/parse_spdx_document_test.go b/spdx/v2/v2_3/rdf/reader/parse_spdx_document_test.go similarity index 99% rename from rdfloader/parser2v3/parse_spdx_document_test.go rename to spdx/v2/v2_3/rdf/reader/parse_spdx_document_test.go index f2c0f2f7..569a67b2 100644 --- a/rdfloader/parser2v3/parse_spdx_document_test.go +++ b/spdx/v2/v2_3/rdf/reader/parse_spdx_document_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" diff --git a/rdfloader/parser2v3/parser.go b/spdx/v2/v2_3/rdf/reader/parser.go similarity index 87% rename from rdfloader/parser2v3/parser.go rename to spdx/v2/v2_3/rdf/reader/parser.go index 961af311..93a382ef 100644 --- a/rdfloader/parser2v3/parser.go +++ b/spdx/v2/v2_3/rdf/reader/parser.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "errors" @@ -8,8 +8,8 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" gordfWriter "github.com/spdx/gordf/rdfwriter" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // returns a new instance of rdfParser2_3 given the gordf object and nodeToTriples mapping @@ -17,17 +17,17 @@ func NewParser2_3(gordfParserObj *gordfParser.Parser, nodeToTriples map[string][ parser := rdfParser2_3{ gordfParserObj: gordfParserObj, nodeStringToTriples: nodeToTriples, - doc: &v2_3.Document{ - ExternalDocumentReferences: []v2_3.ExternalDocumentRef{}, - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{}, - Files: []*v2_3.File{}, - OtherLicenses: []*v2_3.OtherLicense{}, - Relationships: []*v2_3.Relationship{}, - Annotations: []*v2_3.Annotation{}, - Reviews: []*v2_3.Review{}, + doc: &spdx.Document{ + ExternalDocumentReferences: []spdx.ExternalDocumentRef{}, + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{}, + Files: []*spdx.File{}, + OtherLicenses: []*spdx.OtherLicense{}, + Relationships: []*spdx.Relationship{}, + Annotations: []*spdx.Annotation{}, + Reviews: []*spdx.Review{}, }, - files: map[common.ElementID]*v2_3.File{}, + files: map[common.ElementID]*spdx.File{}, assocWithPackage: map[common.ElementID]bool{}, cache: map[string]*nodeState{}, } @@ -36,7 +36,7 @@ func NewParser2_3(gordfParserObj *gordfParser.Parser, nodeToTriples map[string][ // main function which takes in a gordfParser and returns // a spdxDocument model or the error encountered while parsing it -func LoadFromGoRDFParser(gordfParserObj *gordfParser.Parser) (*v2_3.Document, error) { +func LoadFromGoRDFParser(gordfParserObj *gordfParser.Parser) (*spdx.Document, error) { // nodeToTriples is a mapping from a node to list of triples. // for every node in the set of subjects of all the triples, // it provides a list of triples that are associated with that subject node. diff --git a/rdfloader/parser2v3/parser_test.go b/spdx/v2/v2_3/rdf/reader/parser_test.go similarity index 99% rename from rdfloader/parser2v3/parser_test.go rename to spdx/v2/v2_3/rdf/reader/parser_test.go index 5aebe943..439eb993 100644 --- a/rdfloader/parser2v3/parser_test.go +++ b/spdx/v2/v2_3/rdf/reader/parser_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" diff --git a/rdfloader/parser2v3/types.go b/spdx/v2/v2_3/rdf/reader/types.go similarity index 93% rename from rdfloader/parser2v3/types.go rename to spdx/v2/v2_3/rdf/reader/types.go index 9efd04ef..1b0cd1bd 100644 --- a/rdfloader/parser2v3/types.go +++ b/spdx/v2/v2_3/rdf/reader/types.go @@ -1,11 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later // copied from tvloader/parser2v3/types.go -package parser2v3 +package reader import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) type rdfParser2_3 struct { @@ -15,10 +15,10 @@ type rdfParser2_3 struct { nodeStringToTriples map[string][]*gordfParser.Triple // document into which data is being parsed - doc *v2_3.Document + doc *spdx.Document // map of packages and files. - files map[common.ElementID]*v2_3.File + files map[common.ElementID]*spdx.File assocWithPackage map[common.ElementID]bool // mapping of nodeStrings to parsed object to save double computation. diff --git a/rdfloader/parser2v3/utils.go b/spdx/v2/v2_3/rdf/reader/utils.go similarity index 98% rename from rdfloader/parser2v3/utils.go rename to spdx/v2/v2_3/rdf/reader/utils.go index 356471bb..793f6fc5 100644 --- a/rdfloader/parser2v3/utils.go +++ b/spdx/v2/v2_3/rdf/reader/utils.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "errors" @@ -10,7 +10,7 @@ import ( gordfParser "github.com/spdx/gordf/rdfloader/parser" "github.com/spdx/gordf/rdfwriter" urilib "github.com/spdx/gordf/uri" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // a uri is of type baseURI#fragment or baseFragment/subFragment diff --git a/rdfloader/parser2v3/utils_test.go b/spdx/v2/v2_3/rdf/reader/utils_test.go similarity index 99% rename from rdfloader/parser2v3/utils_test.go rename to spdx/v2/v2_3/rdf/reader/utils_test.go index 24aa4e9d..bbc85e81 100644 --- a/rdfloader/parser2v3/utils_test.go +++ b/spdx/v2/v2_3/rdf/reader/utils_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "reflect" "testing" gordfParser "github.com/spdx/gordf/rdfloader/parser" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) func Test_getLastPartOfURI(t *testing.T) { diff --git a/rdfloader/rdfloader_test.go b/spdx/v2/v2_3/rdf/reader_test.go similarity index 76% rename from rdfloader/rdfloader_test.go rename to spdx/v2/v2_3/rdf/reader_test.go index 4f77bdfc..3c6255aa 100644 --- a/rdfloader/rdfloader_test.go +++ b/spdx/v2/v2_3/rdf/reader_test.go @@ -1,6 +1,4 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package rdfloader +package rdf import ( "io" @@ -8,38 +6,13 @@ import ( "testing" ) -func TestLoad2_2(t *testing.T) { - var reader io.Reader - var err error - - // TestCase 1: invalid rdf/xml must raise an error - reader = strings.NewReader("") - _, err = Load2_2(reader) - if err == nil { - t.Errorf("expected an EOF error reading an empty file, got %v", err) - } - - // TestCase 2: Valid rdf/xml but invalid spdx document must raise an error - reader = strings.NewReader(` - - - `) - _, err = Load2_2(reader) - if err == nil { - t.Errorf("expected an error due to no SpdxDocument Node in the document") - } -} - -func TestLoad2_3(t *testing.T) { +func Test_Read(t *testing.T) { var reader io.Reader var err error // TestCase 1: invalid rdf/xml must raise an error reader = strings.NewReader("") - _, err = Load2_3(reader) + _, err = Read(reader) if err == nil { t.Errorf("expected an EOF error reading an empty file, got %v", err) } @@ -52,7 +25,7 @@ func TestLoad2_3(t *testing.T) { xmlns="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#"> `) - _, err = Load2_3(reader) + _, err = Read(reader) if err == nil { t.Errorf("expected an error due to no SpdxDocument Node in the document") } @@ -86,7 +59,7 @@ func TestLoad2_3(t *testing.T) { `) - doc, err := Load2_3(reader) + doc, err := Read(reader) if err != nil { t.Errorf("expected valid SPDX document: %v", err) } diff --git a/spdx/v2_3/relationship.go b/spdx/v2/v2_3/relationship.go similarity index 81% rename from spdx/v2_3/relationship.go rename to spdx/v2/v2_3/relationship.go index af4c07d1..d5cd8d8b 100644 --- a/spdx/v2_3/relationship.go +++ b/spdx/v2/v2_3/relationship.go @@ -2,10 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// Relationship is a Relationship section of an SPDX Document for -// version 2.3 of the spec. +// Relationship is a Relationship section of an SPDX Document type Relationship struct { // 11.1: Relationship diff --git a/spdx/v2_3/review.go b/spdx/v2/v2_3/review.go similarity index 89% rename from spdx/v2_3/review.go rename to spdx/v2/v2_3/review.go index 0463807f..cf1a1c71 100644 --- a/spdx/v2_3/review.go +++ b/spdx/v2/v2_3/review.go @@ -2,7 +2,7 @@ package v2_3 -// Review is a Review section of an SPDX Document for version 2.3 of the spec. +// Review is a Review section of an SPDX Document. // DEPRECATED in version 2.0 of spec; retained here for compatibility. type Review struct { diff --git a/spdx/v2_3/snippet.go b/spdx/v2/v2_3/snippet.go similarity index 92% rename from spdx/v2_3/snippet.go rename to spdx/v2/v2_3/snippet.go index d55a1a96..9c479d23 100644 --- a/spdx/v2_3/snippet.go +++ b/spdx/v2/v2_3/snippet.go @@ -2,9 +2,11 @@ package v2_3 -import "github.com/spdx/tools-golang/spdx/common" +import ( + "github.com/spdx/tools-golang/spdx/v2/common" +) -// Snippet is a Snippet section of an SPDX Document for version 2.3 of the spec. +// Snippet is a Snippet section of an SPDX Document type Snippet struct { // 9.1: Snippet SPDX Identifier: "SPDXRef-[idstring]" diff --git a/tvloader/parser2v2/parse_annotation.go b/spdx/v2/v2_3/tagvalue/reader/parse_annotation.go similarity index 90% rename from tvloader/parser2v2/parse_annotation.go rename to spdx/v2/v2_3/tagvalue/reader/parse_annotation.go index 4c5188e9..0d7e251d 100644 --- a/tvloader/parser2v2/parse_annotation.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_annotation.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" ) -func (parser *tvParser2_2) parsePairForAnnotation2_2(tag string, value string) error { +func (parser *tvParser) parsePairForAnnotation(tag string, value string) error { if parser.ann == nil { return fmt.Errorf("no annotation struct created in parser ann pointer") } diff --git a/spdx/v2/v2_3/tagvalue/reader/parse_annotation_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_annotation_test.go new file mode 100644 index 00000000..b4806679 --- /dev/null +++ b/spdx/v2/v2_3/tagvalue/reader/parse_annotation_test.go @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +// ===== Annotation section tests ===== +func TestParserFailsIfAnnotationNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForAnnotation("Annotator", "Person: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected error when calling parsePairFromAnnotation without setting ann pointer") + } +} + +func TestParserFailsIfAnnotationTagUnknown(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // parse invalid tag, using parsePairForAnnotation( + err = parser.parsePairForAnnotation("blah", "oops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("AnnotationDate", "2018-09-15T17:25:00Z") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationDate without Annotator first") + } + err = parser.parsePair("AnnotationType", "REVIEW") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationType without Annotator first") + } + err = parser.parsePair("SPDXREF", "SPDXRef-45") + if err == nil { + t.Errorf("expected error when calling parsePair for SPDXREF without Annotator first") + } + err = parser.parsePair("AnnotationComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for AnnotationComment without Annotator first") + } +} + +func TestParserCanParseAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Annotator without email address + err := parser.parsePair("Annotator", "Person: John Doe") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.Annotator.Annotator != "John Doe" { + t.Errorf("got %+v for Annotator, expected John Doe", parser.ann.Annotator.Annotator) + } + if parser.ann.Annotator.AnnotatorType != "Person" { + t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) + } + + // Annotation Date + dt := "2018-09-15T17:32:00Z" + err = parser.parsePair("AnnotationDate", dt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationDate != dt { + t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) + } + + // Annotation type + aType := "REVIEW" + err = parser.parsePair("AnnotationType", aType) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationType != aType { + t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) + } + + // SPDX Identifier Reference + ref := "SPDXRef-30" + err = parser.parsePair("SPDXREF", ref) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + deID := parser.ann.AnnotationSPDXIdentifier + if deID.DocumentRefID != "" || deID.ElementRefID != "30" { + t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") + } + + // Annotation Comment + cmt := "this is a comment" + err = parser.parsePair("AnnotationComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.ann.AnnotationComment != cmt { + t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) + } +} + +func TestParserFailsIfAnnotatorInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotatorTypeInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("Annotator", "Human: John Doe (jdoe@example.com)") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} + +func TestParserFailsIfAnnotationRefInvalid(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + // start with valid annotator + err := parser.parsePair("Annotator", "Person: John Doe (jdoe@example.com)") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + err = parser.parsePair("SPDXREF", "blah:other") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v3/parse_creation_info.go b/spdx/v2/v2_3/tagvalue/reader/parse_creation_info.go similarity index 77% rename from tvloader/parser2v3/parse_creation_info.go rename to spdx/v2/v2_3/tagvalue/reader/parse_creation_info.go index 693a56fb..44c12302 100644 --- a/tvloader/parser2v3/parse_creation_info.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_creation_info.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromCreationInfo2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromCreationInfo(tag string, value string) error { // fail if not in Creation Info parser state - if parser.st != psCreationInfo2_3 { - return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo2_3", parser.st) + if parser.st != psCreationInfo { + return fmt.Errorf("got invalid state %v in parsePairFromCreationInfo", parser.st) } // create an SPDX Creation Info data struct if we don't have one already if parser.doc.CreationInfo == nil { - parser.doc.CreationInfo = &v2_3.CreationInfo{} + parser.doc.CreationInfo = &spdx.CreationInfo{} } ci := parser.doc.CreationInfo @@ -51,49 +51,49 @@ func (parser *tvParser2_3) parsePairFromCreationInfo2_3(tag string, value string // this may be a null case: can we ever have a "last file" in // the "creation info" state? should go on to "file" state // even when parsing unpackaged files. - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_3 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.st = psPackage2_3 - parser.pkg = &v2_3.Package{ + parser.st = psPackage + parser.pkg = &spdx.Package{ FilesAnalyzed: true, IsFilesAnalyzedTagPresent: false, } - return parser.parsePairFromPackage2_3(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to _unpackaged_ file section case "FileName": // leave pkg as nil, so that packages will be placed in Files - parser.st = psFile2_3 + parser.st = psFile parser.pkg = nil - return parser.parsePairFromFile2_3(tag, value) + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_3 - return parser.parsePairFromOtherLicense2_3(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_3 - return parser.parsePairFromReview2_3(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in CreationInfo section", tag) } diff --git a/tvloader/parser2v3/parse_creation_info_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_creation_info_test.go similarity index 60% rename from tvloader/parser2v3/parse_creation_info_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_creation_info_test.go index 24cb80a6..84f02704 100644 --- a/tvloader/parser2v3/parse_creation_info_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_creation_info_test.go @@ -1,26 +1,26 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser creation info state change tests ===== -func TestParser2_3CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } pkgName := "testPkg" - err := parser.parsePair2_3("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } // and a package should be created if parser.pkg == nil { @@ -44,18 +44,18 @@ func TestParser2_3CIMovesToPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_3CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } // and current package should be nil, meaning Files are placed in the // Files map instead of in a Package @@ -64,142 +64,142 @@ func TestParser2_3CIMovesToFileAfterParsingFileNameTagWithNoPackages(t *testing. } } -func TestParser2_3CIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } } -func TestParser2_3CIMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } } -func TestParser2_3CIStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_3CIStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this spdx file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this spdx file") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psCreationInfo2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo2_3) + if parser.st != psCreationInfo { + t.Errorf("parser is in state %v, expected %v", parser.st, psCreationInfo) } } -func TestParser2_3FailsParsingCreationInfoWithInvalidState(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psPackage2_3, +func TestParserFailsParsingCreationInfoWithInvalidState(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psPackage, } - err := parser.parsePairFromCreationInfo2_3("SPDXVersion", "SPDX-2.3") + err := parser.parsePairFromCreationInfo("SPDXVersion", spdx.Version) if err == nil { t.Errorf("expected non-nil error, got nil") } } // ===== Creation Info section tests ===== -func TestParser2_3HasCreationInfoAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserHasCreationInfoAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_3("LicenseListVersion", "3.9") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "3.9") if err != nil { - t.Errorf("got error when calling parsePairFromCreationInfo2_3: %v", err) + t.Errorf("got error when calling parsePairFromCreationInfo: %v", err) } if parser.doc.CreationInfo == nil { t.Errorf("doc.CreationInfo is still nil after parsing first pair") } } -func TestParser2_3CanParseCreationInfoTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCanParseCreationInfoTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } // License List Version - err := parser.parsePairFromCreationInfo2_3("LicenseListVersion", "2.3") + err := parser.parsePairFromCreationInfo("LicenseListVersion", "1.3") if err != nil { t.Errorf("expected nil error, got %v", err) } - if parser.doc.CreationInfo.LicenseListVersion != "2.3" { + if parser.doc.CreationInfo.LicenseListVersion != "1.3" { t.Errorf("got %v for LicenseListVersion", parser.doc.CreationInfo.LicenseListVersion) } @@ -208,11 +208,11 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { "Person: Person A", "Person: Person B", } - err = parser.parsePairFromCreationInfo2_3("Creator", refPersons[0]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_3("Creator", refPersons[1]) + err = parser.parsePairFromCreationInfo("Creator", refPersons[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -227,11 +227,11 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { "Organization: Organization A", "Organization: Organization B", } - err = parser.parsePairFromCreationInfo2_3("Creator", refOrgs[0]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_3("Creator", refOrgs[1]) + err = parser.parsePairFromCreationInfo("Creator", refOrgs[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -246,11 +246,11 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { "Tool: Tool A", "Tool: Tool B", } - err = parser.parsePairFromCreationInfo2_3("Creator", refTools[0]) + err = parser.parsePairFromCreationInfo("Creator", refTools[0]) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromCreationInfo2_3("Creator", refTools[1]) + err = parser.parsePairFromCreationInfo("Creator", refTools[1]) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -261,7 +261,7 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { } // Created date - err = parser.parsePairFromCreationInfo2_3("Created", "2018-09-10T11:46:00Z") + err = parser.parsePairFromCreationInfo("Created", "2018-09-10T11:46:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -270,7 +270,7 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { } // Creator Comment - err = parser.parsePairFromCreationInfo2_3("CreatorComment", "Blah whatever") + err = parser.parsePairFromCreationInfo("CreatorComment", "Blah whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -279,56 +279,56 @@ func TestParser2_3CanParseCreationInfoTags(t *testing.T) { } } -func TestParser2_3InvalidCreatorTagsFail(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserInvalidCreatorTagsFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_3("Creator", "blah: somebody") + err := parser.parsePairFromCreationInfo("Creator", "blah: somebody") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } - err = parser.parsePairFromCreationInfo2_3("Creator", "Tool with no colons") + err = parser.parsePairFromCreationInfo("Creator", "Tool with no colons") if err == nil { t.Errorf("expected error from parsing invalid Creator format, got nil") } } -func TestParser2_3CreatorTagWithMultipleColonsPasses(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCreatorTagWithMultipleColonsPasses(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_3("Creator", "Tool: tool1:2:3") + err := parser.parsePairFromCreationInfo("Creator", "Tool: tool1:2:3") if err != nil { t.Errorf("unexpected error from parsing valid Creator format") } } -func TestParser2_3CIUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCIUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePairFromCreationInfo2_3("blah", "something") + err := parser.parsePairFromCreationInfo("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_3CICreatesRelationship(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCICreatesRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -338,15 +338,15 @@ func TestParser2_3CICreatesRelationship(t *testing.T) { } } -func TestParser2_3CICreatesAnnotation(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, +func TestParserCICreatesAnnotation(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") diff --git a/tvloader/parser2v3/parse_file.go b/spdx/v2/v2_3/tagvalue/reader/parse_file.go similarity index 78% rename from tvloader/parser2v3/parse_file.go rename to spdx/v2/v2_3/tagvalue/reader/parse_file.go index e502648c..25badf03 100644 --- a/tvloader/parser2v3/parse_file.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_file.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromFile2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromFile(tag string, value string) error { // expire fileAOP for anything other than an AOPHomePage or AOPURI // (we'll actually handle the HomePage and URI further below) if tag != "ArtifactOfProjectHomePage" && tag != "ArtifactOfProjectURI" { @@ -20,28 +20,28 @@ func (parser *tvParser2_3) parsePairFromFile2_3(tag string, value string) error // tag for creating new file section case "FileName": // check if the previous file contained an spdx Id or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_3 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.file = &v2_3.File{} + parser.file = &spdx.File{} parser.file.FileName = value // tag for creating new package section and going back to parsing Package case "PackageName": - parser.st = psPackage2_3 + parser.st = psPackage // check if the previous file contained an spdx Id or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_3 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } parser.file = nil - return parser.parsePairFromPackage2_3(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to snippet section case "SnippetSPDXID": - parser.st = psSnippet2_3 - return parser.parsePairFromSnippet2_3(tag, value) + parser.st = psSnippet + return parser.parsePairFromSnippet(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_3 - return parser.parsePairFromOtherLicense2_3(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for file data case "SPDXID": eID, err := extractElementID(value) @@ -51,12 +51,12 @@ func (parser *tvParser2_3) parsePairFromFile2_3(tag string, value string) error parser.file.FileSPDXIdentifier = eID if parser.pkg == nil { if parser.doc.Files == nil { - parser.doc.Files = []*v2_3.File{} + parser.doc.Files = []*spdx.File{} } parser.doc.Files = append(parser.doc.Files, parser.file) } else { if parser.pkg.Files == nil { - parser.pkg.Files = []*v2_3.File{} + parser.pkg.Files = []*spdx.File{} } parser.pkg.Files = append(parser.pkg.Files, parser.file) } @@ -102,7 +102,7 @@ func (parser *tvParser2_3) parsePairFromFile2_3(tag string, value string) error case "FileCopyrightText": parser.file.FileCopyrightText = value case "ArtifactOfProjectName": - parser.fileAOP = &v2_3.ArtifactOfProject{} + parser.fileAOP = &spdx.ArtifactOfProject{} parser.file.ArtifactOfProjects = append(parser.file.ArtifactOfProjects, parser.fileAOP) parser.fileAOP.Name = value case "ArtifactOfProjectHomePage": @@ -127,28 +127,28 @@ func (parser *tvParser2_3) parsePairFromFile2_3(tag string, value string) error parser.file.FileAttributionTexts = append(parser.file.FileAttributionTexts, value) // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_3 - return parser.parsePairFromReview2_3(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in File section", tag) } diff --git a/tvloader/parser2v3/parse_file_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_file_test.go similarity index 62% rename from tvloader/parser2v3/parse_file_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_file_test.go index f158baf5..0c09ac0d 100644 --- a/tvloader/parser2v3/parse_file_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_file_test.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser file section state change tests ===== -func TestParser2_3FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { +func TestParserFileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // create the first file fileOldName := "f1.txt" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: fileOldName, FileSPDXIdentifier: "f1"}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: fileOldName, FileSPDXIdentifier: "f1"}, } fileOld := parser.file parser.doc.Packages = append(parser.doc.Packages, parser.pkg) @@ -36,13 +36,13 @@ func TestParser2_3FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { // now add a new file fileName := "f2.txt" - err := parser.parsePair2_3("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_3 { - t.Errorf("expected state to be %v, got %v", psFile2_3, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and a file should be created if parser.file == nil { @@ -65,9 +65,9 @@ func TestParser2_3FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } // now parse an SPDX identifier tag - err = parser.parsePair2_3("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // and the Package's Files should now be of size 2 and have this new one if len(parser.pkg.Files) != 2 { @@ -87,22 +87,22 @@ func TestParser2_3FileStartsNewFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_3FileAddsToPackageOrUnpackagedFiles(t *testing.T) { +func TestParserFileAddsToPackageOrUnpackagedFiles(t *testing.T) { // start with no packages - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } // add a file and SPDX identifier fileName := "f2.txt" - err := parser.parsePair2_3("FileName", fileName) + err := parser.parsePair("FileName", fileName) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_3("SPDXID", "SPDXRef-f2ID") + err = parser.parsePair("SPDXID", "SPDXRef-f2ID") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } fileOld := parser.file // should have been added to Files @@ -113,21 +113,21 @@ func TestParser2_3FileAddsToPackageOrUnpackagedFiles(t *testing.T) { t.Errorf("expected file %v in Files[f2ID], got %v", fileOld, parser.doc.Files[0]) } // now create a package and a new file - err = parser.parsePair2_3("PackageName", "package1") + err = parser.parsePair("PackageName", "package1") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_3("SPDXID", "SPDXRef-pkg1") + err = parser.parsePair("SPDXID", "SPDXRef-pkg1") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_3("FileName", "f3.txt") + err = parser.parsePair("FileName", "f3.txt") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - err = parser.parsePair2_3("SPDXID", "SPDXRef-f3ID") + err = parser.parsePair("SPDXID", "SPDXRef-f3ID") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // Files should still be size 1 and have old file only if len(parser.doc.Files) != 1 { @@ -145,16 +145,16 @@ func TestParser2_3FileAddsToPackageOrUnpackagedFiles(t *testing.T) { } } -func TestParser2_3FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserFileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first file and package p1Name := "package1" f1Name := "f1.txt" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: p1Name, PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: f1Name, FileSPDXIdentifier: "f1"}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: p1Name, PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: f1Name, FileSPDXIdentifier: "f1"}, } p1 := parser.pkg f1 := parser.file @@ -163,13 +163,13 @@ func TestParser2_3FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // now add a new package p2Name := "package2" - err := parser.parsePair2_3("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -217,24 +217,24 @@ func TestParser2_3FileStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { } } -func TestParser2_3FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) fileCurrent := parser.file - err := parser.parsePair2_3("SnippetSPDXID", "SPDXRef-Test1") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-Test1") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_3 { - t.Errorf("expected state to be %v, got %v", psSnippet2_3, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current file should remain what it was if parser.file != fileCurrent { @@ -242,135 +242,135 @@ func TestParser2_3FileMovesToSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { } } -func TestParser2_3FileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_3FileMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_3FileStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psFile2_3 { - t.Errorf("expected state to be %v, got %v", psFile2_3, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psFile2_3 { - t.Errorf("expected state to be %v, got %v", psFile2_3, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } } -func TestParser2_3FileStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psFile2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psFile2_3) + if parser.st != psFile { + t.Errorf("parser is in state %v, expected %v", parser.st, psFile) } } // ===== File data section tests ===== -func TestParser2_3CanParseFileTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserCanParseFileTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -384,7 +384,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // File SPDX Identifier - err = parser.parsePairFromFile2_3("SPDXID", "SPDXRef-f1") + err = parser.parsePairFromFile("SPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -405,7 +405,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { "DOCUMENTATION", } for _, ty := range fileTypes { - err = parser.parsePairFromFile2_3("FileType", ty) + err = parser.parsePairFromFile("FileType", ty) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -435,7 +435,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } for algo, tc := range testChecksums { - if err := parser.parsePairFromFile2_3( + if err := parser.parsePairFromFile( "FileChecksum", fmt.Sprintf("%s: %s", algo, tc)); err != nil { t.Errorf("expected error, got %v", err) } @@ -451,7 +451,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // Concluded License - err = parser.parsePairFromFile2_3("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromFile("LicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -466,7 +466,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromFile2_3("LicenseInfoInFile", lic) + err = parser.parsePairFromFile("LicenseInfoInFile", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -488,7 +488,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // Comments on License - err = parser.parsePairFromFile2_3("LicenseComments", "this is a comment") + err = parser.parsePairFromFile("LicenseComments", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -497,7 +497,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // Copyright Text - err = parser.parsePairFromFile2_3("FileCopyrightText", "copyright (c) me") + err = parser.parsePairFromFile("FileCopyrightText", "copyright (c) me") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -507,38 +507,38 @@ func TestParser2_3CanParseFileTags(t *testing.T) { // Artifact of Projects: Name, HomePage and URI // Artifact set 1 - err = parser.parsePairFromFile2_3("ArtifactOfProjectName", "project1") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_3("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_3("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 2 -- just name - err = parser.parsePairFromFile2_3("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 3 -- just name and home page - err = parser.parsePairFromFile2_3("ArtifactOfProjectName", "project3") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project3") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_3("ArtifactOfProjectHomePage", "http://example.com/3/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/3/") if err != nil { t.Errorf("expected nil error, got %v", err) } // Artifact set 4 -- just name and URI - err = parser.parsePairFromFile2_3("ArtifactOfProjectName", "project4") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project4") if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromFile2_3("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/4/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -592,7 +592,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // File Comment - err = parser.parsePairFromFile2_3("FileComment", "this is a comment") + err = parser.parsePairFromFile("FileComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -601,7 +601,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } // File Notice - err = parser.parsePairFromFile2_3("FileNotice", "this is a Notice") + err = parser.parsePairFromFile("FileNotice", "this is a Notice") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -615,7 +615,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { "EvilCorp", } for _, contrib := range contribs { - err = parser.parsePairFromFile2_3("FileContributor", contrib) + err = parser.parsePairFromFile("FileContributor", contrib) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -642,7 +642,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { "g.txt", } for _, dep := range deps { - err = parser.parsePairFromFile2_3("FileDependency", dep) + err = parser.parsePairFromFile("FileDependency", dep) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -669,7 +669,7 @@ func TestParser2_3CanParseFileTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromFile2_3("FileAttributionText", attr) + err = parser.parsePairFromFile("FileAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -692,19 +692,19 @@ func TestParser2_3CanParseFileTags(t *testing.T) { } -func TestParser2_3FileCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -714,19 +714,19 @@ func TestParser2_3FileCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_3FileCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -736,33 +736,33 @@ func TestParser2_3FileCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_3FileUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserFileUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_3("blah", "something") + err := parser.parsePairFromFile("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } func TestFileAOPPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromFile2_3("ArtifactOfProjectName", "project1") + err := parser.parsePairFromFile("ArtifactOfProjectName", "project1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -772,7 +772,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { curPtr := parser.fileAOP // now, a home page; pointer should stay - err = parser.parsePairFromFile2_3("ArtifactOfProjectHomePage", "http://example.com/1/") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "http://example.com/1/") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -781,7 +781,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // a URI; pointer should stay - err = parser.parsePairFromFile2_3("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "http://example.com/1/uri.whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -791,7 +791,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { // now, another artifact name; pointer should change but be non-nil // now, a home page; pointer should stay - err = parser.parsePairFromFile2_3("ArtifactOfProjectName", "project2") + err = parser.parsePairFromFile("ArtifactOfProjectName", "project2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -800,7 +800,7 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } // finally, an unrelated tag; pointer should go away - err = parser.parsePairFromFile2_3("FileComment", "whatever") + err = parser.parsePairFromFile("FileComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -809,115 +809,115 @@ func TestFileAOPPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_3FailsIfInvalidSPDXIDInFileSection(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserFailsIfInvalidSPDXIDInFileSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid SPDX Identifier - err = parser.parsePairFromFile2_3("SPDXID", "whoops") + err = parser.parsePairFromFile("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfInvalidChecksumFormatInFileSection(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserFailsIfInvalidChecksumFormatInFileSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid format for checksum line, missing colon - err = parser.parsePairFromFile2_3("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "SHA1 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_1FailsIfUnknownChecksumTypeInFileSection(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserFailsIfUnknownChecksumTypeInFileSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // unknown checksum type - err = parser.parsePairFromFile2_3("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") + err = parser.parsePairFromFile("FileChecksum", "Special: 85ed0817af83a24ad8da68c2b5094de69833983c") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserFailsIfArtifactHomePageBeforeArtifactName(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_3("ArtifactOfProjectHomePage", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectHomePage", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfArtifactURIBeforeArtifactName(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, +func TestParserFailsIfArtifactURIBeforeArtifactName(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // start with File Name - err := parser.parsePairFromFile2_3("FileName", "f1.txt") + err := parser.parsePairFromFile("FileName", "f1.txt") if err != nil { t.Errorf("expected nil error, got %v", err) } // artifact home page appears before artifact name - err = parser.parsePairFromFile2_3("ArtifactOfProjectURI", "https://example.com") + err = parser.parsePairFromFile("ArtifactOfProjectURI", "https://example.com") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FilesWithoutSpdxIdThrowError(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowError(t *testing.T) { // case 1: The previous file (packaged or unpackaged) does not contain spdx ID - parser1 := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, - file: &v2_3.File{FileName: "FileName"}, + parser1 := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, + file: &spdx.File{FileName: "FileName"}, } - err := parser1.parsePair2_3("FileName", "f2") + err := parser1.parsePair("FileName", "f2") if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -926,12 +926,12 @@ func TestParser2_3FilesWithoutSpdxIdThrowError(t *testing.T) { // Last unpackaged file before the snippet start fileName := "f2.txt" sid1 := common.ElementID("s1") - parser2 := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - file: &v2_3.File{FileName: fileName}, + parser2 := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + file: &spdx.File{FileName: fileName}, } - err = parser2.parsePair2_3("SnippetSPDXID", string(sid1)) + err = parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } @@ -939,16 +939,16 @@ func TestParser2_3FilesWithoutSpdxIdThrowError(t *testing.T) { // case 3: Invalid File without snippets // Last unpackaged file before the package starts // Last file of a package and New package starts - parser3 := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, + parser3 := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, } fileName = "f3.txt" - err = parser3.parsePair2_3("FileName", fileName) + err = parser3.parsePair("FileName", fileName) if err != nil { t.Errorf("%s", err) } - err = parser3.parsePair2_3("PackageName", "p2") + err = parser3.parsePair("PackageName", "p2") if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v3/parse_other_license.go b/spdx/v2/v2_3/tagvalue/reader/parse_other_license.go similarity index 62% rename from tvloader/parser2v3/parse_other_license.go rename to spdx/v2/v2_3/tagvalue/reader/parse_other_license.go index d2f41ea0..ada8b6b4 100644 --- a/tvloader/parser2v3/parse_other_license.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_other_license.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromOtherLicense2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromOtherLicense(tag string, value string) error { switch tag { // tag for creating new other license section case "LicenseID": - parser.otherLic = &v2_3.OtherLicense{} + parser.otherLic = &spdx.OtherLicense{} parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.otherLic.LicenseIdentifier = value case "ExtractedText": @@ -25,28 +25,28 @@ func (parser *tvParser2_3) parsePairFromOtherLicense2_3(tag string, value string parser.otherLic.LicenseComment = value // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_3 - return parser.parsePairFromReview2_3(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in OtherLicense section", tag) } diff --git a/tvloader/parser2v3/parse_other_license_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_other_license_test.go similarity index 57% rename from tvloader/parser2v3/parse_other_license_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_other_license_test.go index 2939e671..fa1ab32f 100644 --- a/tvloader/parser2v3/parse_other_license_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_other_license_test.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser other license section state change tests ===== -func TestParser2_3OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { +func TestParserOLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { // create the first other license olid1 := "LicenseRef-Lic11" olname1 := "License 11" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: olid1, LicenseName: olname1, }, @@ -39,26 +39,26 @@ func TestParser2_3OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) // now add a new other license olid2 := "LicenseRef-22" olname2 := "License 22" - err := parser.parsePair2_3("LicenseID", olid2) + err := parser.parsePair("LicenseID", olid2) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and an other license should be created if parser.otherLic == nil { t.Fatalf("parser didn't create new other license") } // also parse the new license's name - err = parser.parsePair2_3("LicenseName", olname2) + err = parser.parsePair("LicenseName", olname2) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still be correct - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the other license name should be as expected if parser.otherLic.LicenseName != olname2 { @@ -88,33 +88,33 @@ func TestParser2_3OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) } } -func TestParser2_3OLMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserOLMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_3("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_3OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserOtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-whatever", LicenseName: "the whatever license", }, @@ -123,13 +123,13 @@ func TestParser2_3OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -140,23 +140,23 @@ func TestParser2_3OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_3OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserOtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-whatever", LicenseName: "the whatever license", }, @@ -165,44 +165,44 @@ func TestParser2_3OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_3) + if parser.st != psOtherLicense { + t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense) } // and the annotation should be in the Document's Annotations @@ -214,13 +214,13 @@ func TestParser2_3OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_3OLFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserOLFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, @@ -230,34 +230,34 @@ func TestParser2_3OLFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // can't go back to old sections - err := parser.parsePair2_3("SPDXVersion", "SPDX-2.3") + err := parser.parsePair("SPDXVersion", spdx.Version) if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_3("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_3("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Other License data section tests ===== -func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserCanParseOtherLicenseTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) // License Identifier - err := parser.parsePairFromOtherLicense2_3("LicenseID", "LicenseRef-Lic11") + err := parser.parsePairFromOtherLicense("LicenseID", "LicenseRef-Lic11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -266,7 +266,7 @@ func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { } // Extracted Text - err = parser.parsePairFromOtherLicense2_3("ExtractedText", "You are permitted to do anything with the software, hooray!") + err = parser.parsePairFromOtherLicense("ExtractedText", "You are permitted to do anything with the software, hooray!") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -275,7 +275,7 @@ func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { } // License Name - err = parser.parsePairFromOtherLicense2_3("LicenseName", "License 11") + err = parser.parsePairFromOtherLicense("LicenseName", "License 11") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -290,7 +290,7 @@ func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { "https://example.com/3", } for _, cr := range crossRefs { - err = parser.parsePairFromOtherLicense2_3("LicenseCrossReference", cr) + err = parser.parsePairFromOtherLicense("LicenseCrossReference", cr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -312,7 +312,7 @@ func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { } // License Comment - err = parser.parsePairFromOtherLicense2_3("LicenseComment", "this is a comment") + err = parser.parsePairFromOtherLicense("LicenseComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -321,18 +321,18 @@ func TestParser2_3CanParseOtherLicenseTags(t *testing.T) { } } -func TestParser2_3OLUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psOtherLicense2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, +func TestParserOLUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psOtherLicense, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) - err := parser.parsePairFromOtherLicense2_3("blah", "something") + err := parser.parsePairFromOtherLicense("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v3/parse_package.go b/spdx/v2/v2_3/tagvalue/reader/parse_package.go similarity index 87% rename from tvloader/parser2v3/parse_package.go rename to spdx/v2/v2_3/tagvalue/reader/parse_package.go index 989261d0..b01fbef4 100644 --- a/tvloader/parser2v3/parse_package.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_package.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromPackage(tag string, value string) error { // expire pkgExtRef for anything other than a comment // (we'll actually handle the comment further below) if tag != "ExternalRefComment" { @@ -22,10 +22,10 @@ func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) err // if package already has a name, create and go on to a new package if parser.pkg == nil || parser.pkg.PackageName != "" { // check if the previous package contained an spdx Id or not - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_3 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } - parser.pkg = &v2_3.Package{ + parser.pkg = &spdx.Package{ FilesAnalyzed: true, IsFilesAnalyzedTagPresent: false, } @@ -33,12 +33,12 @@ func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) err parser.pkg.PackageName = value // tag for going on to file section case "FileName": - parser.st = psFile2_3 - return parser.parsePairFromFile2_3(tag, value) + parser.st = psFile + return parser.parsePairFromFile(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_3 - return parser.parsePairFromOtherLicense2_3(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) case "SPDXID": eID, err := extractElementID(value) if err != nil { @@ -46,7 +46,7 @@ func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) err } parser.pkg.PackageSPDXIdentifier = eID if parser.doc.Packages == nil { - parser.doc.Packages = []*v2_3.Package{} + parser.doc.Packages = []*spdx.Package{} } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) case "PackageVersion": @@ -164,7 +164,7 @@ func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) err case "PackageAttributionText": parser.pkg.PackageAttributionTexts = append(parser.pkg.PackageAttributionTexts, value) case "ExternalRef": - parser.pkgExtRef = &v2_3.PackageExternalReference{} + parser.pkgExtRef = &spdx.PackageExternalReference{} parser.pkg.PackageExternalReferences = append(parser.pkg.PackageExternalReferences, parser.pkgExtRef) category, refType, locator, err := extractPackageExternalReference(value) if err != nil { @@ -182,28 +182,28 @@ func (parser *tvParser2_3) parsePairFromPackage2_3(tag string, value string) err parser.pkgExtRef = nil // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_3 - return parser.parsePairFromReview2_3(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Package section", tag) } diff --git a/tvloader/parser2v3/parse_package_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_package_test.go similarity index 64% rename from tvloader/parser2v3/parse_package_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_package_test.go index c5f09af5..6009f56e 100644 --- a/tvloader/parser2v3/parse_package_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_package_test.go @@ -1,23 +1,23 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser package section state change tests ===== -func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { // create the first package pkgOldName := "p1" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: pkgOldName, PackageSPDXIdentifier: "p1"}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: pkgOldName, PackageSPDXIdentifier: "p1"}, } pkgOld := parser.pkg parser.doc.Packages = append(parser.doc.Packages, pkgOld) @@ -31,13 +31,13 @@ func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package pkgName := "p2" - err := parser.parsePair2_3("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -67,12 +67,12 @@ func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { +func TestParserPackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpackaged(t *testing.T) { // pkg is nil, so that Files appearing before the first PackageName tag // are added to Files instead of Packages - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psFile2_3, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psFile, pkg: nil, } // the Document's Packages should be empty @@ -82,13 +82,13 @@ func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack // now add a new package pkgName := "p2" - err := parser.parsePair2_3("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -112,22 +112,22 @@ func TestParser2_3PackageStartsNewPackageAfterParsingPackageNameTagWhileInUnpack } } -func TestParser2_3PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageMovesToFileAfterParsingFileNameTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) pkgCurrent := parser.pkg - err := parser.parsePair2_3("FileName", "testFile") + err := parser.parsePair("FileName", "testFile") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_3 { - t.Errorf("expected state to be %v, got %v", psFile2_3, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psFile, parser.st) } // and current package should remain what it was if parser.pkg != pkgCurrent { @@ -135,122 +135,122 @@ func TestParser2_3PackageMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_3PackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_3PackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_3PackageStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } } -func TestParser2_3PackageStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this package") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this package") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psPackage2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psPackage2_3) + if parser.st != psPackage { + t.Errorf("parser is in state %v, expected %v", parser.st, psPackage) } } // ===== Package data section tests ===== -func TestParser2_3CanParsePackageTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserCanParsePackageTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // should not yet be in Packages map, b/c no SPDX identifier @@ -259,7 +259,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -272,7 +272,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package SPDX Identifier - err = parser.parsePairFromPackage2_3("SPDXID", "SPDXRef-p1") + err = parser.parsePairFromPackage("SPDXID", "SPDXRef-p1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -289,7 +289,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Version - err = parser.parsePairFromPackage2_3("PackageVersion", "2.1.1") + err = parser.parsePairFromPackage("PackageVersion", "2.1.1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -298,7 +298,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package File Name - err = parser.parsePairFromPackage2_3("PackageFileName", "p1-2.1.1.tar.gz") + err = parser.parsePairFromPackage("PackageFileName", "p1-2.1.1.tar.gz") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -313,7 +313,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { // SKIP -- separate tests for subvalues below // Package Download Location - err = parser.parsePairFromPackage2_3("PackageDownloadLocation", "https://example.com/whatever") + err = parser.parsePairFromPackage("PackageDownloadLocation", "https://example.com/whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -322,7 +322,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Files Analyzed - err = parser.parsePairFromPackage2_3("FilesAnalyzed", "false") + err = parser.parsePairFromPackage("FilesAnalyzed", "false") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -345,7 +345,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } for algo, tc := range testChecksums { - if err := parser.parsePairFromPackage2_3( + if err := parser.parsePairFromPackage( "PackageChecksum", fmt.Sprintf("%s: %s", algo, tc)); err != nil { t.Errorf("expected error, got %v", err) } @@ -361,7 +361,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Home Page - err = parser.parsePairFromPackage2_3("PackageHomePage", "https://example.com/whatever2") + err = parser.parsePairFromPackage("PackageHomePage", "https://example.com/whatever2") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -370,7 +370,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Source Info - err = parser.parsePairFromPackage2_3("PackageSourceInfo", "random comment") + err = parser.parsePairFromPackage("PackageSourceInfo", "random comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -379,7 +379,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package License Concluded - err = parser.parsePairFromPackage2_3("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseConcluded", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -394,7 +394,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromPackage2_3("PackageLicenseInfoFromFiles", lic) + err = parser.parsePairFromPackage("PackageLicenseInfoFromFiles", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -416,7 +416,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package License Declared - err = parser.parsePairFromPackage2_3("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") + err = parser.parsePairFromPackage("PackageLicenseDeclared", "Apache-2.0 OR GPL-2.0-or-later") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -425,7 +425,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package License Comments - err = parser.parsePairFromPackage2_3("PackageLicenseComments", "this is a license comment") + err = parser.parsePairFromPackage("PackageLicenseComments", "this is a license comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -434,7 +434,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Copyright Text - err = parser.parsePairFromPackage2_3("PackageCopyrightText", "Copyright (c) me myself and i") + err = parser.parsePairFromPackage("PackageCopyrightText", "Copyright (c) me myself and i") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -443,7 +443,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Summary - err = parser.parsePairFromPackage2_3("PackageSummary", "i wrote this package") + err = parser.parsePairFromPackage("PackageSummary", "i wrote this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -452,7 +452,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Description - err = parser.parsePairFromPackage2_3("PackageDescription", "i wrote this package a lot") + err = parser.parsePairFromPackage("PackageDescription", "i wrote this package a lot") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -461,7 +461,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } // Package Comment - err = parser.parsePairFromPackage2_3("PackageComment", "i scanned this package") + err = parser.parsePairFromPackage("PackageComment", "i scanned this package") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -475,7 +475,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromPackage2_3("PackageAttributionText", attr) + err = parser.parsePairFromPackage("PackageAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -507,7 +507,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { ref2Type := "LocationRef-acmeforge" ref2Locator := "acmecorp/acmenator/4.1.3alpha" ref2Comment := "this is comment #2" - err = parser.parsePairFromPackage2_3("ExternalRef", ref1) + err = parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -523,11 +523,11 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[0] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[0], got no match") } - err = parser.parsePairFromPackage2_3("ExternalRefComment", ref1Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref1Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } - err = parser.parsePairFromPackage2_3("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -543,7 +543,7 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { if parser.pkgExtRef != parser.pkg.PackageExternalReferences[1] { t.Errorf("expected pkgExtRef to match PackageExternalReferences[1], got no match") } - err = parser.parsePairFromPackage2_3("ExternalRefComment", ref2Comment) + err = parser.parsePairFromPackage("ExternalRefComment", ref2Comment) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -577,16 +577,16 @@ func TestParser2_3CanParsePackageTags(t *testing.T) { } -func TestParser2_3CanParsePackageSupplierPersonTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageSupplierPersonTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Person - err := parser.parsePairFromPackage2_3("PackageSupplier", "Person: John Doe") + err := parser.parsePairFromPackage("PackageSupplier", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -595,16 +595,16 @@ func TestParser2_3CanParsePackageSupplierPersonTag(t *testing.T) { } } -func TestParser2_3CanParsePackageSupplierOrganizationTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageSupplierOrganizationTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: Organization - err := parser.parsePairFromPackage2_3("PackageSupplier", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageSupplier", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -613,16 +613,16 @@ func TestParser2_3CanParsePackageSupplierOrganizationTag(t *testing.T) { } } -func TestParser2_3CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageSupplierNOASSERTIONTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Supplier: NOASSERTION - err := parser.parsePairFromPackage2_3("PackageSupplier", "NOASSERTION") + err := parser.parsePairFromPackage("PackageSupplier", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -631,16 +631,16 @@ func TestParser2_3CanParsePackageSupplierNOASSERTIONTag(t *testing.T) { } } -func TestParser2_3CanParsePackageOriginatorPersonTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageOriginatorPersonTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Person - err := parser.parsePairFromPackage2_3("PackageOriginator", "Person: John Doe") + err := parser.parsePairFromPackage("PackageOriginator", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -649,16 +649,16 @@ func TestParser2_3CanParsePackageOriginatorPersonTag(t *testing.T) { } } -func TestParser2_3CanParsePackageOriginatorOrganizationTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageOriginatorOrganizationTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: Organization - err := parser.parsePairFromPackage2_3("PackageOriginator", "Organization: John Doe, Inc.") + err := parser.parsePairFromPackage("PackageOriginator", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -667,16 +667,16 @@ func TestParser2_3CanParsePackageOriginatorOrganizationTag(t *testing.T) { } } -func TestParser2_3CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Originator: NOASSERTION - err := parser.parsePairFromPackage2_3("PackageOriginator", "NOASSERTION") + err := parser.parsePairFromPackage("PackageOriginator", "NOASSERTION") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -685,11 +685,11 @@ func TestParser2_3CanParsePackageOriginatorNOASSERTIONTag(t *testing.T) { } } -func TestParser2_3CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) @@ -697,7 +697,7 @@ func TestParser2_3CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { code := "d6a770ba38583ed4bb4525bd96e50461655d2758" fileName := "./package.spdx" fullCodeValue := "d6a770ba38583ed4bb4525bd96e50461655d2758 (excludes: ./package.spdx)" - err := parser.parsePairFromPackage2_3("PackageVerificationCode", fullCodeValue) + err := parser.parsePairFromPackage("PackageVerificationCode", fullCodeValue) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -710,17 +710,17 @@ func TestParser2_3CanParsePackageVerificationCodeTagWithExcludes(t *testing.T) { } -func TestParser2_3CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserCanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) // Package Verification Code without excludes parenthetical code := "d6a770ba38583ed4bb4525bd96e50461655d2758" - err := parser.parsePairFromPackage2_3("PackageVerificationCode", code) + err := parser.parsePairFromPackage("PackageVerificationCode", code) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -733,16 +733,16 @@ func TestParser2_3CanParsePackageVerificationCodeTagWithoutExcludes(t *testing.T } -func TestParser2_3PackageExternalRefPointerChangesAfterTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageExternalRefPointerChangesAfterTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) ref1 := "SECURITY cpe23Type cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*" - err := parser.parsePairFromPackage2_3("ExternalRef", ref1) + err := parser.parsePairFromPackage("ExternalRef", ref1) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -751,7 +751,7 @@ func TestParser2_3PackageExternalRefPointerChangesAfterTags(t *testing.T) { } // now, a comment; pointer should go away - err = parser.parsePairFromPackage2_3("ExternalRefComment", "whatever") + err = parser.parsePairFromPackage("ExternalRefComment", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -760,7 +760,7 @@ func TestParser2_3PackageExternalRefPointerChangesAfterTags(t *testing.T) { } ref2 := "Other LocationRef-something https://example.com/whatever" - err = parser.parsePairFromPackage2_3("ExternalRef", ref2) + err = parser.parsePairFromPackage("ExternalRef", ref2) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -769,7 +769,7 @@ func TestParser2_3PackageExternalRefPointerChangesAfterTags(t *testing.T) { } // and some other random tag makes the pointer go away too - err = parser.parsePairFromPackage2_3("PackageSummary", "whatever") + err = parser.parsePairFromPackage("PackageSummary", "whatever") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -778,17 +778,17 @@ func TestParser2_3PackageExternalRefPointerChangesAfterTags(t *testing.T) { } } -func TestParser2_3PackageCreatesRelationshipInDocument(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageCreatesRelationshipInDocument(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-whatever") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.rln == nil { t.Fatalf("parser didn't create and point to Relationship struct") @@ -798,17 +798,17 @@ func TestParser2_3PackageCreatesRelationshipInDocument(t *testing.T) { } } -func TestParser2_3PackageCreatesAnnotationInDocument(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageCreatesAnnotationInDocument(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.ann == nil { t.Fatalf("parser didn't create and point to Annotation struct") @@ -818,129 +818,129 @@ func TestParser2_3PackageCreatesAnnotationInDocument(t *testing.T) { } } -func TestParser2_3PackageUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, +func TestParserPackageUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: "p1", PackageSPDXIdentifier: "p1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) - err := parser.parsePairFromPackage2_3("blah", "something") + err := parser.parsePairFromPackage("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_3FailsIfInvalidSPDXIDInPackageSection(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidSPDXIDInPackageSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid ID format - err = parser.parsePairFromPackage2_3("SPDXID", "whoops") + err = parser.parsePairFromPackage("SPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfInvalidPackageSupplierFormat(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidPackageSupplierFormat(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier format - err = parser.parsePairFromPackage2_3("PackageSupplier", "whoops") + err = parser.parsePairFromPackage("PackageSupplier", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfUnknownPackageSupplierType(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfUnknownPackageSupplierType(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid supplier type - err = parser.parsePairFromPackage2_3("PackageSupplier", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageSupplier", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfInvalidPackageOriginatorFormat(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidPackageOriginatorFormat(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator format - err = parser.parsePairFromPackage2_3("PackageOriginator", "whoops") + err = parser.parsePairFromPackage("PackageOriginator", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfUnknownPackageOriginatorType(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfUnknownPackageOriginatorType(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid originator type - err = parser.parsePairFromPackage2_3("PackageOriginator", "whoops: John Doe") + err = parser.parsePairFromPackage("PackageOriginator", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3SetsFilesAnalyzedTagsCorrectly(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserSetsFilesAnalyzedTagsCorrectly(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // set tag - err = parser.parsePairFromPackage2_3("FilesAnalyzed", "true") + err = parser.parsePairFromPackage("FilesAnalyzed", "true") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -952,77 +952,77 @@ func TestParser2_3SetsFilesAnalyzedTagsCorrectly(t *testing.T) { } } -func TestParser2_3FailsIfInvalidPackageChecksumFormat(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidPackageChecksumFormat(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum format - err = parser.parsePairFromPackage2_3("PackageChecksum", "whoops") + err = parser.parsePairFromPackage("PackageChecksum", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfInvalidPackageChecksumType(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidPackageChecksumType(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid checksum type - err = parser.parsePairFromPackage2_3("PackageChecksum", "whoops: blah") + err = parser.parsePairFromPackage("PackageChecksum", "whoops: blah") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfInvalidExternalRefFormat(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfInvalidExternalRefFormat(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid external ref format - err = parser.parsePairFromPackage2_3("ExternalRef", "whoops") + err = parser.parsePairFromPackage("ExternalRef", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{}, +func TestParserFailsIfExternalRefCommentBeforeExternalRef(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{}, } // start with Package Name - err := parser.parsePairFromPackage2_3("PackageName", "p1") + err := parser.parsePairFromPackage("PackageName", "p1") if err != nil { t.Errorf("expected nil error, got %v", err) } // external ref comment before external ref - err = parser.parsePairFromPackage2_3("ExternalRefComment", "whoops") + err = parser.parsePairFromPackage("ExternalRefComment", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } @@ -1093,13 +1093,13 @@ func TestFailsPackageExternalRefWithInvalidFormat(t *testing.T) { } } -func TestParser2_3PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { +func TestParserPackageWithoutSpdxIdentifierThrowsError(t *testing.T) { // More than one package, the previous package doesn't contain an SPDX ID pkgOldName := "p1" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psPackage2_3, - pkg: &v2_3.Package{PackageName: pkgOldName}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psPackage, + pkg: &spdx.Package{PackageName: pkgOldName}, } pkgOld := parser.pkg parser.doc.Packages = append(parser.doc.Packages, pkgOld) @@ -1112,7 +1112,7 @@ func TestParser2_3PackageWithoutSpdxIdentifierThrowsError(t *testing.T) { } pkgName := "p2" - err := parser.parsePair2_3("PackageName", pkgName) + err := parser.parsePair("PackageName", pkgName) if err == nil { t.Errorf("package without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v2/parse_relationship.go b/spdx/v2/v2_3/tagvalue/reader/parse_relationship.go similarity index 91% rename from tvloader/parser2v2/parse_relationship.go rename to spdx/v2/v2_3/tagvalue/reader/parse_relationship.go index 092b554c..db2a3986 100644 --- a/tvloader/parser2v2/parse_relationship.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_relationship.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 +package reader import ( "fmt" "strings" ) -func (parser *tvParser2_2) parsePairForRelationship2_2(tag string, value string) error { +func (parser *tvParser) parsePairForRelationship(tag string, value string) error { if parser.rln == nil { return fmt.Errorf("no relationship struct created in parser rln pointer") } diff --git a/spdx/v2/v2_3/tagvalue/reader/parse_relationship_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_relationship_test.go new file mode 100644 index 00000000..9691fe9d --- /dev/null +++ b/spdx/v2/v2_3/tagvalue/reader/parse_relationship_test.go @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package reader + +import ( + "testing" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +// ===== Relationship section tests ===== +func TestParserFailsIfRelationshipNotSet(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePairForRelationship("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") + if err == nil { + t.Errorf("expected error when calling parsePairFromRelationship without setting rln pointer") + } +} + +func TestParserFailsIfRelationshipCommentWithoutRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + err := parser.parsePair("RelationshipComment", "comment whatever") + if err == nil { + t.Errorf("expected error when calling parsePair for RelationshipComment without Relationship first") + } +} + +func TestParserCanParseRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { + t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) + } + if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { + t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) + } + if parser.rln.Relationship != "CONTAINS" { + t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) + } + + // Relationship Comment + cmt := "this is a comment" + err = parser.parsePair("RelationshipComment", cmt) + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + if parser.rln.RelationshipComment != cmt { + t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) + } +} + +func TestParserInvalidRelationshipTagsNoValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // no items + parser.rln = nil + err := parser.parsePair("Relationship", "") + if err == nil { + t.Errorf("expected error for empty items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsOneValueFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // one item + parser.rln = nil + err := parser.parsePair("Relationship", "DESCRIBES") + if err == nil { + t.Errorf("expected error for only one item in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsTwoValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // two items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-DOCUMENT DESCRIBES") + if err == nil { + t.Errorf("expected error for only two items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // three items but with interspersed additional whitespace + parser.rln = nil + err := parser.parsePair("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") + if err != nil { + t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) + } +} + +func TestParserInvalidRelationshipTagsFourValuesFail(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") + if err == nil { + t.Errorf("expected error for more than three items in relationship, got nil") + } +} + +func TestParserInvalidRelationshipTagsInvalidRefIDs(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // four items + parser.rln = nil + err := parser.parsePair("Relationship", "SPDXRef-a DESCRIBES b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } + + parser.rln = nil + err = parser.parsePair("Relationship", "a DESCRIBES SPDXRef-b") + if err == nil { + t.Errorf("expected error for missing SPDXRef- prefix, got nil") + } +} + +func TestParserSpecialValuesValidForRightSideOfRelationship(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // NONE in right side of relationship should pass + err := parser.parsePair("Relationship", "SPDXRef-a CONTAINS NONE") + if err != nil { + t.Errorf("expected nil error for CONTAINS NONE, got %v", err) + } + + // NOASSERTION in right side of relationship should pass + err = parser.parsePair("Relationship", "SPDXRef-a CONTAINS NOASSERTION") + if err != nil { + t.Errorf("expected nil error for CONTAINS NOASSERTION, got %v", err) + } + + // NONE in left side of relationship should fail + err = parser.parsePair("Relationship", "NONE CONTAINS SPDXRef-a") + if err == nil { + t.Errorf("expected non-nil error for NONE CONTAINS, got nil") + } + + // NOASSERTION in left side of relationship should fail + err = parser.parsePair("Relationship", "NOASSERTION CONTAINS SPDXRef-a") + if err == nil { + t.Errorf("expected non-nil error for NOASSERTION CONTAINS, got nil") + } +} + +func TestParserFailsToParseUnknownTagInRelationshipSection(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + } + + // Relationship + err := parser.parsePair("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + // invalid tag + err = parser.parsePairForRelationship("blah", "whoops") + if err == nil { + t.Errorf("expected non-nil error, got nil") + } +} diff --git a/tvloader/parser2v3/parse_review.go b/spdx/v2/v2_3/tagvalue/reader/parse_review.go similarity index 67% rename from tvloader/parser2v3/parse_review.go rename to spdx/v2/v2_3/tagvalue/reader/parse_review.go index c7ff99c5..6e46220a 100644 --- a/tvloader/parser2v3/parse_review.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_review.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromReview2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromReview(tag string, value string) error { switch tag { // tag for creating new review section case "Reviewer": - parser.rev = &v2_3.Review{} + parser.rev = &spdx.Review{} parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) subkey, subvalue, err := extractSubs(value) if err != nil { @@ -37,24 +37,24 @@ func (parser *tvParser2_3) parsePairFromReview2_3(tag string, value string) erro parser.rev.ReviewComment = value // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) default: return fmt.Errorf("received unknown tag %v in Review section", tag) } diff --git a/tvloader/parser2v3/parse_review_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_review_test.go similarity index 56% rename from tvloader/parser2v3/parse_review_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_review_test.go index 7026a367..946dd5f5 100644 --- a/tvloader/parser2v3/parse_review_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_review_test.go @@ -1,26 +1,26 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser review section state change tests ===== -func TestParser2_3ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { +func TestParserReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // create the first review rev1 := "John Doe" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{ + rev: &spdx.Review{ Reviewer: rev1, ReviewerType: "Person", }, @@ -45,13 +45,13 @@ func TestParser2_3ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { // now add a new review rev2 := "Steve" rp2 := "Person: Steve" - err := parser.parsePair2_3("Reviewer", rp2) + err := parser.parsePair("Reviewer", rp2) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and a review should be created if parser.rev == nil { @@ -80,17 +80,17 @@ func TestParser2_3ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { } -func TestParser2_3ReviewStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserReviewStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{ + rev: &spdx.Review{ Reviewer: "Jane Doe", ReviewerType: "Person", }, @@ -100,13 +100,13 @@ func TestParser2_3ReviewStaysAfterParsingRelationshipTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -117,27 +117,27 @@ func TestParser2_3ReviewStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_3ReviewStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserReviewStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{ + rev: &spdx.Review{ Reviewer: "Jane Doe", ReviewerType: "Person", }, @@ -147,44 +147,44 @@ func TestParser2_3ReviewStaysAfterParsingAnnotationTags(t *testing.T) { parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) + if parser.st != psReview { + t.Errorf("parser is in state %v, expected %v", parser.st, psReview) } // and the annotation should be in the Document's Annotations @@ -196,17 +196,17 @@ func TestParser2_3ReviewStaysAfterParsingAnnotationTags(t *testing.T) { } } -func TestParser2_3ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserReviewFailsAfterParsingOtherSectionTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) @@ -214,36 +214,36 @@ func TestParser2_3ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // can't go back to old sections - err := parser.parsePair2_3("SPDXVersion", "SPDX-2.3") + err := parser.parsePair("SPDXVersion", spdx.Version) if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_3("PackageName", "whatever") + err = parser.parsePair("PackageName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_3("FileName", "whatever") + err = parser.parsePair("FileName", "whatever") if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } - err = parser.parsePair2_3("LicenseID", "LicenseRef-Lic22") + err = parser.parsePair("LicenseID", "LicenseRef-Lic22") if err == nil { - t.Errorf("expected error when calling parsePair2_3, got nil") + t.Errorf("expected error when calling parsePair, got nil") } } // ===== Review data section tests ===== -func TestParser2_3CanParseReviewTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserCanParseReviewTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) @@ -254,7 +254,7 @@ func TestParser2_3CanParseReviewTags(t *testing.T) { // handled in subsequent subtests // Review Date (DEPRECATED) - err := parser.parsePairFromReview2_3("ReviewDate", "2018-09-23T08:30:00Z") + err := parser.parsePairFromReview("ReviewDate", "2018-09-23T08:30:00Z") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -263,7 +263,7 @@ func TestParser2_3CanParseReviewTags(t *testing.T) { } // Review Comment (DEPRECATED) - err = parser.parsePairFromReview2_3("ReviewComment", "this is a comment") + err = parser.parsePairFromReview("ReviewComment", "this is a comment") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -272,17 +272,17 @@ func TestParser2_3CanParseReviewTags(t *testing.T) { } } -func TestParser2_3CanParseReviewerPersonTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserCanParseReviewerPersonTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) @@ -290,7 +290,7 @@ func TestParser2_3CanParseReviewerPersonTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Person - err := parser.parsePairFromReview2_3("Reviewer", "Person: John Doe") + err := parser.parsePairFromReview("Reviewer", "Person: John Doe") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -302,17 +302,17 @@ func TestParser2_3CanParseReviewerPersonTag(t *testing.T) { } } -func TestParser2_3CanParseReviewerOrganizationTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserCanParseReviewerOrganizationTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) @@ -320,7 +320,7 @@ func TestParser2_3CanParseReviewerOrganizationTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Organization - err := parser.parsePairFromReview2_3("Reviewer", "Organization: John Doe, Inc.") + err := parser.parsePairFromReview("Reviewer", "Organization: John Doe, Inc.") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -332,17 +332,17 @@ func TestParser2_3CanParseReviewerOrganizationTag(t *testing.T) { } } -func TestParser2_3CanParseReviewerToolTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserCanParseReviewerToolTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) @@ -350,7 +350,7 @@ func TestParser2_3CanParseReviewerToolTag(t *testing.T) { parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) // Reviewer: Tool - err := parser.parsePairFromReview2_3("Reviewer", "Tool: scannertool - 1.2.12") + err := parser.parsePairFromReview("Reviewer", "Tool: scannertool - 1.2.12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -362,52 +362,52 @@ func TestParser2_3CanParseReviewerToolTag(t *testing.T) { } } -func TestParser2_3FailsIfReviewerInvalidFormat(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - rev: &v2_3.Review{}, +func TestParserFailsIfReviewerInvalidFormat(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + rev: &spdx.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_3("Reviewer", "oops") + err := parser.parsePairFromReview("Reviewer", "oops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsIfReviewerUnknownType(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - rev: &v2_3.Review{}, +func TestParserFailsIfReviewerUnknownType(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + rev: &spdx.Review{}, } parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_3("Reviewer", "whoops: John Doe") + err := parser.parsePairFromReview("Reviewer", "whoops: John Doe") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3ReviewUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psReview2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, - otherLic: &v2_3.OtherLicense{ +func TestParserReviewUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psReview, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, + otherLic: &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-Lic11", LicenseName: "License 11", }, - rev: &v2_3.Review{}, + rev: &spdx.Review{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) - err := parser.parsePairFromReview2_3("blah", "something") + err := parser.parsePairFromReview("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } diff --git a/tvloader/parser2v3/parse_snippet.go b/spdx/v2/v2_3/tagvalue/reader/parse_snippet.go similarity index 76% rename from tvloader/parser2v3/parse_snippet.go rename to spdx/v2/v2_3/tagvalue/reader/parse_snippet.go index d718dd0d..8bad2da9 100644 --- a/tvloader/parser2v3/parse_snippet.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_snippet.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strconv" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func (parser *tvParser2_3) parsePairFromSnippet2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromSnippet(tag string, value string) error { switch tag { // tag for creating new snippet section case "SnippetSPDXID": // check here whether the file contained an SPDX ID or not - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_3 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - parser.snippet = &v2_3.Snippet{} + parser.snippet = &spdx.Snippet{} eID, err := extractElementID(value) if err != nil { return err @@ -26,26 +26,26 @@ func (parser *tvParser2_3) parsePairFromSnippet2_3(tag string, value string) err // FIXME: how should we handle where not associated with current file? if parser.file != nil { if parser.file.Snippets == nil { - parser.file.Snippets = map[common.ElementID]*v2_3.Snippet{} + parser.file.Snippets = map[common.ElementID]*spdx.Snippet{} } parser.file.Snippets[eID] = parser.snippet } parser.snippet.SnippetSPDXIdentifier = eID // tag for creating new file section and going back to parsing File case "FileName": - parser.st = psFile2_3 + parser.st = psFile parser.snippet = nil - return parser.parsePairFromFile2_3(tag, value) + return parser.parsePairFromFile(tag, value) // tag for creating new package section and going back to parsing Package case "PackageName": - parser.st = psPackage2_3 + parser.st = psPackage parser.file = nil parser.snippet = nil - return parser.parsePairFromPackage2_3(tag, value) + return parser.parsePairFromPackage(tag, value) // tag for going on to other license section case "LicenseID": - parser.st = psOtherLicense2_3 - return parser.parsePairFromOtherLicense2_3(tag, value) + parser.st = psOtherLicense + return parser.parsePairFromOtherLicense(tag, value) // tags for snippet data case "SnippetFromFileSPDXID": deID, err := extractDocElementID(value) @@ -107,28 +107,28 @@ func (parser *tvParser2_3) parsePairFromSnippet2_3(tag string, value string) err parser.snippet.SnippetAttributionTexts = append(parser.snippet.SnippetAttributionTexts, value) // for relationship tags, pass along but don't change state case "Relationship": - parser.rln = &v2_3.Relationship{} + parser.rln = &spdx.Relationship{} parser.doc.Relationships = append(parser.doc.Relationships, parser.rln) - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) case "RelationshipComment": - return parser.parsePairForRelationship2_3(tag, value) + return parser.parsePairForRelationship(tag, value) // for annotation tags, pass along but don't change state case "Annotator": - parser.ann = &v2_3.Annotation{} + parser.ann = &spdx.Annotation{} parser.doc.Annotations = append(parser.doc.Annotations, parser.ann) - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationDate": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationType": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "SPDXREF": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) case "AnnotationComment": - return parser.parsePairForAnnotation2_3(tag, value) + return parser.parsePairForAnnotation(tag, value) // tag for going on to review section (DEPRECATED) case "Reviewer": - parser.st = psReview2_3 - return parser.parsePairFromReview2_3(tag, value) + parser.st = psReview + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("received unknown tag %v in Snippet section", tag) } diff --git a/tvloader/parser2v3/parse_snippet_test.go b/spdx/v2/v2_3/tagvalue/reader/parse_snippet_test.go similarity index 57% rename from tvloader/parser2v3/parse_snippet_test.go rename to spdx/v2/v2_3/tagvalue/reader/parse_snippet_test.go index 362f22ca..06d04c6f 100644 --- a/tvloader/parser2v3/parse_snippet_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parse_snippet_test.go @@ -1,23 +1,23 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Parser snippet section state change tests ===== -func TestParser2_3SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { +func TestParserSnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing.T) { // create the first snippet sid1 := common.ElementID("s1") - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: sid1}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: sid1}, } s1 := parser.snippet parser.doc.Packages = append(parser.doc.Packages, parser.pkg) @@ -36,13 +36,13 @@ func TestParser2_3SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } // now add a new snippet - err := parser.parsePair2_3("SnippetSPDXID", "SPDXRef-s2") + err := parser.parsePair("SnippetSPDXID", "SPDXRef-s2") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psSnippet2_3 { - t.Errorf("expected state to be %v, got %v", psSnippet2_3, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and a snippet should be created if parser.snippet == nil { @@ -70,13 +70,13 @@ func TestParser2_3SnippetStartsNewSnippetAfterParsingSnippetSPDXIDTag(t *testing } } -func TestParser2_3SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } p1 := parser.pkg f1 := parser.file @@ -86,13 +86,13 @@ func TestParser2_3SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T // now add a new package p2Name := "package2" - err := parser.parsePair2_3("PackageName", p2Name) + err := parser.parsePair("PackageName", p2Name) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should go back to Package - if parser.st != psPackage2_3 { - t.Errorf("expected state to be %v, got %v", psPackage2_3, parser.st) + if parser.st != psPackage { + t.Errorf("expected state to be %v, got %v", psPackage, parser.st) } // and a package should be created if parser.pkg == nil { @@ -144,14 +144,14 @@ func TestParser2_3SnippetStartsNewPackageAfterParsingPackageNameTag(t *testing.T } } -func TestParser2_3SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { +func TestParserSnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { f1Name := "f1.txt" - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } p1 := parser.pkg f1 := parser.file @@ -160,13 +160,13 @@ func TestParser2_3SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { parser.file.Snippets["s1"] = parser.snippet f2Name := "f2.txt" - err := parser.parsePair2_3("FileName", f2Name) + err := parser.parsePair("FileName", f2Name) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should be correct - if parser.st != psFile2_3 { - t.Errorf("expected state to be %v, got %v", psSnippet2_3, parser.st) + if parser.st != psFile { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and current package should remain what it was if parser.pkg != p1 { @@ -197,67 +197,67 @@ func TestParser2_3SnippetMovesToFileAfterParsingFileNameTag(t *testing.T) { } } -func TestParser2_3SnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetMovesToOtherLicenseAfterParsingLicenseIDTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_3("LicenseID", "LicenseRef-TestLic") + err := parser.parsePair("LicenseID", "LicenseRef-TestLic") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psOtherLicense2_3 { - t.Errorf("expected state to be %v, got %v", psOtherLicense2_3, parser.st) + if parser.st != psOtherLicense { + t.Errorf("expected state to be %v, got %v", psOtherLicense, parser.st) } } -func TestParser2_3SnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetMovesToReviewAfterParsingReviewerTag(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_3("Reviewer", "Person: John Doe") + err := parser.parsePair("Reviewer", "Person: John Doe") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psReview2_3 { - t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) + if parser.st != psReview { + t.Errorf("expected state to be %v, got %v", psReview, parser.st) } } -func TestParser2_3SnippetStaysAfterParsingRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetStaysAfterParsingRelationshipTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") + err := parser.parsePair("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should remain unchanged - if parser.st != psSnippet2_3 { - t.Errorf("expected state to be %v, got %v", psSnippet2_3, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } // and the relationship should be in the Document's Relationships if len(parser.doc.Relationships) != 1 { @@ -268,66 +268,66 @@ func TestParser2_3SnippetStaysAfterParsingRelationshipTags(t *testing.T) { t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) } - err = parser.parsePair2_3("RelationshipComment", "blah") + err = parser.parsePair("RelationshipComment", "blah") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } // state should still remain unchanged - if parser.st != psSnippet2_3 { - t.Errorf("expected state to be %v, got %v", psSnippet2_3, parser.st) + if parser.st != psSnippet { + t.Errorf("expected state to be %v, got %v", psSnippet, parser.st) } } -func TestParser2_3SnippetStaysAfterParsingAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetStaysAfterParsingAnnotationTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) parser.file.Snippets["s1"] = parser.snippet - err := parser.parsePair2_3("Annotator", "Person: John Doe ()") + err := parser.parsePair("Annotator", "Person: John Doe ()") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_3) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") + err = parser.parsePair("AnnotationDate", "2018-09-15T00:36:00Z") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_3) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_3("AnnotationType", "REVIEW") + err = parser.parsePair("AnnotationType", "REVIEW") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_3) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") + err = parser.parsePair("SPDXREF", "SPDXRef-45") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_3) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } - err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this particular file") + err = parser.parsePair("AnnotationComment", "i guess i had something to say about this particular file") if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } - if parser.st != psSnippet2_3 { - t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet2_3) + if parser.st != psSnippet { + t.Errorf("parser is in state %v, expected %v", parser.st, psSnippet) } // and the annotation should be in the Document's Annotations @@ -340,19 +340,19 @@ func TestParser2_3SnippetStaysAfterParsingAnnotationTags(t *testing.T) { } // ===== Snippet data section tests ===== -func TestParser2_3CanParseSnippetTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{}, +func TestParserCanParseSnippetTags(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) // Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_3("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -361,7 +361,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet from File SPDX Identifier - err = parser.parsePairFromSnippet2_3("SnippetFromFileSPDXID", "SPDXRef-f1") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "SPDXRef-f1") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -371,7 +371,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Byte Range - err = parser.parsePairFromSnippet2_3("SnippetByteRange", "20:320") + err = parser.parsePairFromSnippet("SnippetByteRange", "20:320") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -383,7 +383,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Line Range - err = parser.parsePairFromSnippet2_3("SnippetLineRange", "5:12") + err = parser.parsePairFromSnippet("SnippetLineRange", "5:12") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -395,7 +395,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Concluded License - err = parser.parsePairFromSnippet2_3("SnippetLicenseConcluded", "BSD-3-Clause") + err = parser.parsePairFromSnippet("SnippetLicenseConcluded", "BSD-3-Clause") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -410,7 +410,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { "CC0-1.0", } for _, lic := range lics { - err = parser.parsePairFromSnippet2_3("LicenseInfoInSnippet", lic) + err = parser.parsePairFromSnippet("LicenseInfoInSnippet", lic) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -432,7 +432,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Comments on License - err = parser.parsePairFromSnippet2_3("SnippetLicenseComments", "this is a comment about the licenses") + err = parser.parsePairFromSnippet("SnippetLicenseComments", "this is a comment about the licenses") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -441,7 +441,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Copyright Text - err = parser.parsePairFromSnippet2_3("SnippetCopyrightText", "copyright (c) John Doe and friends") + err = parser.parsePairFromSnippet("SnippetCopyrightText", "copyright (c) John Doe and friends") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -450,7 +450,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Comment - err = parser.parsePairFromSnippet2_3("SnippetComment", "this is a comment about the snippet") + err = parser.parsePairFromSnippet("SnippetComment", "this is a comment about the snippet") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -459,7 +459,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } // Snippet Name - err = parser.parsePairFromSnippet2_3("SnippetName", "from some other package called abc") + err = parser.parsePairFromSnippet("SnippetName", "from some other package called abc") if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -473,7 +473,7 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { "This is a \nmulti-line string", } for _, attr := range attrs { - err = parser.parsePairFromSnippet2_3("SnippetAttributionText", attr) + err = parser.parsePairFromSnippet("SnippetAttributionText", attr) if err != nil { t.Errorf("expected nil error, got %v", err) } @@ -496,138 +496,138 @@ func TestParser2_3CanParseSnippetTags(t *testing.T) { } -func TestParser2_3SnippetUnknownTagFails(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{SnippetSPDXIdentifier: "s1"}, +func TestParserSnippetUnknownTagFails(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{SnippetSPDXIdentifier: "s1"}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) - err := parser.parsePairFromSnippet2_3("blah", "something") + err := parser.parsePairFromSnippet("blah", "something") if err == nil { t.Errorf("expected error from parsing unknown tag") } } -func TestParser2_3FailsForInvalidSnippetSPDXID(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{}, +func TestParserFailsForInvalidSnippetSPDXID(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) // invalid Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_3("SnippetSPDXID", "whoops") + err := parser.parsePairFromSnippet("SnippetSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsForInvalidSnippetFromFileSPDXID(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{}, +func TestParserFailsForInvalidSnippetFromFileSPDXID(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_3("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid From File identifier - err = parser.parsePairFromSnippet2_3("SnippetFromFileSPDXID", "whoops") + err = parser.parsePairFromSnippet("SnippetFromFileSPDXID", "whoops") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsForInvalidSnippetByteValues(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{}, +func TestParserFailsForInvalidSnippetByteValues(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_3("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_3("SnippetByteRange", "200 210") + err = parser.parsePairFromSnippet("SnippetByteRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_3("SnippetByteRange", "a:210") + err = parser.parsePairFromSnippet("SnippetByteRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_3("SnippetByteRange", "200:a") + err = parser.parsePairFromSnippet("SnippetByteRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FailsForInvalidSnippetLineValues(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{Packages: []*v2_3.Package{}}, - st: psSnippet2_3, - pkg: &v2_3.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*v2_3.File{}}, - file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*v2_3.Snippet{}}, - snippet: &v2_3.Snippet{}, +func TestParserFailsForInvalidSnippetLineValues(t *testing.T) { + parser := tvParser{ + doc: &spdx.Document{Packages: []*spdx.Package{}}, + st: psSnippet, + pkg: &spdx.Package{PackageName: "package1", PackageSPDXIdentifier: "package1", Files: []*spdx.File{}}, + file: &spdx.File{FileName: "f1.txt", FileSPDXIdentifier: "f1", Snippets: map[common.ElementID]*spdx.Snippet{}}, + snippet: &spdx.Snippet{}, } parser.doc.Packages = append(parser.doc.Packages, parser.pkg) parser.pkg.Files = append(parser.pkg.Files, parser.file) // start with Snippet SPDX Identifier - err := parser.parsePairFromSnippet2_3("SnippetSPDXID", "SPDXRef-s1") + err := parser.parsePairFromSnippet("SnippetSPDXID", "SPDXRef-s1") if err != nil { t.Errorf("expected nil error, got %v", err) } // invalid byte formats and values - err = parser.parsePairFromSnippet2_3("SnippetLineRange", "200 210") + err = parser.parsePairFromSnippet("SnippetLineRange", "200 210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_3("SnippetLineRange", "a:210") + err = parser.parsePairFromSnippet("SnippetLineRange", "a:210") if err == nil { t.Errorf("expected non-nil error, got nil") } - err = parser.parsePairFromSnippet2_3("SnippetLineRange", "200:a") + err = parser.parsePairFromSnippet("SnippetLineRange", "200:a") if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorWithSnippets(t *testing.T) { // Invalid file with snippet // Last unpackaged file before the snippet starts // Last file of a package and New package starts fileName := "f2.txt" sid1 := common.ElementID("s1") - parser2 := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - file: &v2_3.File{FileName: fileName}, + parser2 := tvParser{ + doc: &spdx.Document{}, + st: psCreationInfo, + file: &spdx.File{FileName: fileName}, } - err := parser2.parsePair2_3("SnippetSPDXID", string(sid1)) + err := parser2.parsePair("SnippetSPDXID", string(sid1)) if err == nil { t.Errorf("file without SPDX Identifier getting accepted") } diff --git a/tvloader/parser2v3/parser.go b/spdx/v2/v2_3/tagvalue/reader/parser.go similarity index 56% rename from tvloader/parser2v3/parser.go rename to spdx/v2/v2_3/tagvalue/reader/parser.go index af84d93d..b6da8d94 100644 --- a/tvloader/parser2v3/parser.go +++ b/spdx/v2/v2_3/tagvalue/reader/parser.go @@ -1,65 +1,64 @@ -// Package parser2v3 contains functions to read, load and parse -// SPDX tag-value files, version 2.3. +// Package reader contains functions to read, load and parse SPDX tag-value files. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" - "github.com/spdx/tools-golang/tvloader/reader" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ParseTagValues takes a list of (tag, value) pairs, parses it and returns // a pointer to a parsed SPDX Document. -func ParseTagValues(tvs []reader.TagValuePair) (*v2_3.Document, error) { - parser := tvParser2_3{} +func ParseTagValues(tvs []reader.TagValuePair) (*spdx.Document, error) { + parser := tvParser{} for _, tv := range tvs { - err := parser.parsePair2_3(tv.Tag, tv.Value) + err := parser.parsePair(tv.Tag, tv.Value) if err != nil { return nil, err } } - if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId2_3 { + if parser.file != nil && parser.file.FileSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("file with FileName %s does not have SPDX identifier", parser.file.FileName) } - if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_3 { + if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId { return nil, fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName) } return parser.doc, nil } -func (parser *tvParser2_3) parsePair2_3(tag string, value string) error { +func (parser *tvParser) parsePair(tag string, value string) error { switch parser.st { - case psStart2_3: - return parser.parsePairFromStart2_3(tag, value) - case psCreationInfo2_3: - return parser.parsePairFromCreationInfo2_3(tag, value) - case psPackage2_3: - return parser.parsePairFromPackage2_3(tag, value) - case psFile2_3: - return parser.parsePairFromFile2_3(tag, value) - case psSnippet2_3: - return parser.parsePairFromSnippet2_3(tag, value) - case psOtherLicense2_3: - return parser.parsePairFromOtherLicense2_3(tag, value) - case psReview2_3: - return parser.parsePairFromReview2_3(tag, value) + case psStart: + return parser.parsePairFromStart(tag, value) + case psCreationInfo: + return parser.parsePairFromCreationInfo(tag, value) + case psPackage: + return parser.parsePairFromPackage(tag, value) + case psFile: + return parser.parsePairFromFile(tag, value) + case psSnippet: + return parser.parsePairFromSnippet(tag, value) + case psOtherLicense: + return parser.parsePairFromOtherLicense(tag, value) + case psReview: + return parser.parsePairFromReview(tag, value) default: return fmt.Errorf("parser state %v not recognized when parsing (%s, %s)", parser.st, tag, value) } } -func (parser *tvParser2_3) parsePairFromStart2_3(tag string, value string) error { +func (parser *tvParser) parsePairFromStart(tag string, value string) error { // fail if not in Start parser state - if parser.st != psStart2_3 { - return fmt.Errorf("got invalid state %v in parsePairFromStart2_3", parser.st) + if parser.st != psStart { + return fmt.Errorf("got invalid state %v in parsePairFromStart", parser.st) } // create an SPDX Document data struct if we don't have one already if parser.doc == nil { - parser.doc = &v2_3.Document{ExternalDocumentReferences: []v2_3.ExternalDocumentRef{}} + parser.doc = &spdx.Document{ExternalDocumentReferences: []spdx.ExternalDocumentRef{}} } switch tag { @@ -84,7 +83,7 @@ func (parser *tvParser2_3) parsePairFromStart2_3(tag string, value string) error if err != nil { return err } - edr := v2_3.ExternalDocumentRef{ + edr := spdx.ExternalDocumentRef{ DocumentRefID: documentRefID, URI: uri, Checksum: common.Checksum{Algorithm: common.ChecksumAlgorithm(alg), Value: checksum}, @@ -92,8 +91,8 @@ func (parser *tvParser2_3) parsePairFromStart2_3(tag string, value string) error parser.doc.ExternalDocumentReferences = append(parser.doc.ExternalDocumentReferences, edr) default: // move to Creation Info parser state - parser.st = psCreationInfo2_3 - return parser.parsePairFromCreationInfo2_3(tag, value) + parser.st = psCreationInfo + return parser.parsePairFromCreationInfo(tag, value) } return nil diff --git a/tvloader/parser2v3/parser_test.go b/spdx/v2/v2_3/tagvalue/reader/parser_test.go similarity index 54% rename from tvloader/parser2v3/parser_test.go rename to spdx/v2/v2_3/tagvalue/reader/parser_test.go index dfd8c042..8f1c980d 100644 --- a/tvloader/parser2v3/parser_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/parser_test.go @@ -1,20 +1,21 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/tvloader/reader" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/tagvalue/reader" ) // ===== Parser exported entry point tests ===== -func TestParser2_3CanParseTagValues(t *testing.T) { +func TestParserCanParseTagValues(t *testing.T) { var tvPairs []reader.TagValuePair // create some pairs - tvPair1 := reader.TagValuePair{Tag: "SPDXVersion", Value: "SPDX-2.3"} + tvPair1 := reader.TagValuePair{Tag: "SPDXVersion", Value: spdx.Version} tvPairs = append(tvPairs, tvPair1) - tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: "CC0-1.0"} + tvPair2 := reader.TagValuePair{Tag: "DataLicense", Value: spdx.DataLicense} tvPairs = append(tvPairs, tvPair2) tvPair3 := reader.TagValuePair{Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"} tvPairs = append(tvPairs, tvPair3) @@ -24,11 +25,11 @@ func TestParser2_3CanParseTagValues(t *testing.T) { if err != nil { t.Errorf("got error when calling ParseTagValues: %v", err) } - if doc.SPDXVersion != "SPDX-2.3" { - t.Errorf("expected SPDXVersion to be SPDX-2.3, got %v", doc.SPDXVersion) + if doc.SPDXVersion != spdx.Version { + t.Errorf("expected SPDXVersion to be %s, got %v", spdx.Version, doc.SPDXVersion) } - if doc.DataLicense != "CC0-1.0" { - t.Errorf("expected DataLicense to be CC0-1.0, got %v", doc.DataLicense) + if doc.DataLicense != spdx.DataLicense { + t.Errorf("expected DataLicense to be %s, got %v", spdx.DataLicense, doc.DataLicense) } if doc.SPDXIdentifier != "DOCUMENT" { t.Errorf("expected SPDXIdentifier to be DOCUMENT, got %v", doc.SPDXIdentifier) @@ -37,9 +38,9 @@ func TestParser2_3CanParseTagValues(t *testing.T) { } // ===== Parser initialization tests ===== -func TestParser2_3InitCreatesResetStatus(t *testing.T) { - parser := tvParser2_3{} - if parser.st != psStart2_3 { +func TestParserInitCreatesResetStatus(t *testing.T) { + parser := tvParser{} + if parser.st != psStart { t.Errorf("parser did not begin in start state") } if parser.doc != nil { @@ -47,32 +48,32 @@ func TestParser2_3InitCreatesResetStatus(t *testing.T) { } } -func TestParser2_3HasDocumentAfterCallToParseFirstTag(t *testing.T) { - parser := tvParser2_3{} - err := parser.parsePair2_3("SPDXVersion", "SPDX-2.3") +func TestParserHasDocumentAfterCallToParseFirstTag(t *testing.T) { + parser := tvParser{} + err := parser.parsePair("SPDXVersion", spdx.Version) if err != nil { - t.Errorf("got error when calling parsePair2_3: %v", err) + t.Errorf("got error when calling parsePair: %v", err) } if parser.doc == nil { t.Errorf("doc is still nil after parsing first pair") } } -func TestParser2_3StartFailsToParseIfInInvalidState(t *testing.T) { - parser := tvParser2_3{st: psReview2_3} - err := parser.parsePairFromStart2_3("SPDXVersion", "SPDX-2.3") +func TestParserStartFailsToParseIfInInvalidState(t *testing.T) { + parser := tvParser{st: psReview} + err := parser.parsePairFromStart("SPDXVersion", spdx.Version) if err == nil { t.Errorf("expected non-nil error, got nil") } } -func TestParser2_3FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserFilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: Checks the last file // Last unpackaged file with no packages in doc // Last file of last package in the doc tvPairs := []reader.TagValuePair{ - {Tag: "SPDXVersion", Value: "SPDX-2.3"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "SPDXVersion", Value: spdx.Version}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "FileName", Value: "f1"}, } @@ -82,11 +83,11 @@ func TestParser2_3FilesWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { } } -func TestParser2_3PackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { +func TestParserPackageWithoutSpdxIdThrowErrorAtCompleteParse(t *testing.T) { // case: Checks the last package tvPairs := []reader.TagValuePair{ - {Tag: "SPDXVersion", Value: "SPDX-2.3"}, - {Tag: "DataLicense", Value: "CC0-1.0"}, + {Tag: "SPDXVersion", Value: spdx.Version}, + {Tag: "DataLicense", Value: spdx.DataLicense}, {Tag: "SPDXID", Value: "SPDXRef-DOCUMENT"}, {Tag: "PackageName", Value: "p1"}, } diff --git a/spdx/v2/v2_3/tagvalue/reader/types.go b/spdx/v2/v2_3/tagvalue/reader/types.go new file mode 100644 index 00000000..654a5ea2 --- /dev/null +++ b/spdx/v2/v2_3/tagvalue/reader/types.go @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package reader + +import ( + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +type tvParser struct { + // document into which data is being parsed + doc *spdx.Document + + // current parser state + st tvParserState + + // current SPDX item being filled in, if any + pkg *spdx.Package + pkgExtRef *spdx.PackageExternalReference + file *spdx.File + fileAOP *spdx.ArtifactOfProject + snippet *spdx.Snippet + otherLic *spdx.OtherLicense + rln *spdx.Relationship + ann *spdx.Annotation + rev *spdx.Review + // don't need creation info pointer b/c only one, + // and we can get to it via doc.CreationInfo +} + +// parser state (SPDX document) +type tvParserState int + +const ( + // at beginning of document + psStart tvParserState = iota + + // in document creation info section + psCreationInfo + + // in package data section + psPackage + + // in file data section (including "unpackaged" files) + psFile + + // in snippet data section (including "unpackaged" files) + psSnippet + + // in other license section + psOtherLicense + + // in review section + psReview +) + +const nullSpdxElementId = common.ElementID("") diff --git a/tvloader/parser2v3/util.go b/spdx/v2/v2_3/tagvalue/reader/util.go similarity index 96% rename from tvloader/parser2v3/util.go rename to spdx/v2/v2_3/tagvalue/reader/util.go index 743b3f60..027ff4b0 100644 --- a/tvloader/parser2v3/util.go +++ b/spdx/v2/v2_3/tagvalue/reader/util.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "fmt" "strings" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // used to extract key / value from embedded substrings @@ -75,7 +75,7 @@ func extractDocElementID(value string) (common.DocElementID, error) { // used to extract SPDXRef values from an SPDX Identifier, OR "special" strings // from a specified set of permitted values. The primary use case for this is -// the right-hand side of Relationships, where beginning in SPDX 2.3 the values +// the right-hand side of Relationships, // "NONE" and "NOASSERTION" are permitted. If the value does not match one of // the specified permitted values, it will fall back to the ordinary // DocElementID extractor. diff --git a/tvloader/parser2v3/util_test.go b/spdx/v2/v2_3/tagvalue/reader/util_test.go similarity index 99% rename from tvloader/parser2v3/util_test.go rename to spdx/v2/v2_3/tagvalue/reader/util_test.go index 6269b91d..83895075 100644 --- a/tvloader/parser2v3/util_test.go +++ b/spdx/v2/v2_3/tagvalue/reader/util_test.go @@ -1,10 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 +package reader import ( "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // ===== Helper function tests ===== diff --git a/spdx/v2/v2_3/tagvalue/tagvalue_test.go b/spdx/v2/v2_3/tagvalue/tagvalue_test.go new file mode 100644 index 00000000..95e445ca --- /dev/null +++ b/spdx/v2/v2_3/tagvalue/tagvalue_test.go @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package tagvalue + +import ( + "bytes" + "flag" + "fmt" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/spdx/v2/v2_3/example" + "github.com/spdx/tools-golang/tagvalue" +) + +var update = *flag.Bool("update-snapshots", false, "update the example snapshot") + +func Test_Read(t *testing.T) { + fileName := "../../../../examples/sample-docs/tv/SPDXTagExample-v2.3.spdx" + + want := example.Copy() + + if update { + f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + t.Errorf("unable to open file to write: %v", err) + } + err = tagvalue.Write(want, f) + if err != nil { + t.Errorf("unable to call tagvalue.Write: %v", err) + } + } else { + // tagvalue.Write sorts a few items in the in-memory SPDX document, run this + // so the same sorting happens: + err := tagvalue.Write(want, &bytes.Buffer{}) + if err != nil { + t.Errorf("unable to call tagvalue.Write: %v", err) + } + } + + file, err := os.Open(fileName) + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + var got spdx.Document + err = tagvalue.ReadInto(file, &got) + if err != nil { + t.Errorf("Read() error = %v", err) + return + } + + if !cmp.Equal(want, got, ignores...) { + t.Errorf("got incorrect struct after parsing example: %s", cmp.Diff(want, got, ignores...)) + return + } +} + +func Test_ReadWrite(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + // get a copy of the handwritten struct so we don't mutate it on accident + if err := tagvalue.Write(want, w); err != nil { + t.Errorf("Write() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote + var got spdx.Document + err := tagvalue.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got, ignores...) { + t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got, ignores...)) + return + } +} + +var ignores = []cmp.Option{ + cmpopts.IgnoreFields(spdx.Document{}, "Snippets"), + cmpopts.IgnoreFields(spdx.File{}, "Annotations"), + cmpopts.IgnoreFields(spdx.Package{}, "IsFilesAnalyzedTagPresent", "Annotations"), +} diff --git a/tvsaver/saver2v2/save_annotation.go b/spdx/v2/v2_3/tagvalue/writer/save_annotation.go similarity index 80% rename from tvsaver/saver2v2/save_annotation.go rename to spdx/v2/v2_3/tagvalue/writer/save_annotation.go index f5ea8931..7fa11d2a 100644 --- a/tvsaver/saver2v2/save_annotation.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_annotation.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderAnnotation2_2(ann *v2_2.Annotation, w io.Writer) error { +func renderAnnotation(ann *spdx.Annotation, w io.Writer) error { if ann.Annotator.Annotator != "" && ann.Annotator.AnnotatorType != "" { fmt.Fprintf(w, "Annotator: %s: %s\n", ann.Annotator.AnnotatorType, ann.Annotator.Annotator) } diff --git a/tvsaver/saver2v1/save_annotation_test.go b/spdx/v2/v2_3/tagvalue/writer/save_annotation_test.go similarity index 85% rename from tvsaver/saver2v1/save_annotation_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_annotation_test.go index 405bf8aa..3fd41dc5 100644 --- a/tvsaver/saver2v1/save_annotation_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_annotation_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Annotation section Saver tests ===== -func TestSaver2_1AnnotationSavesTextForPerson(t *testing.T) { - ann := &v2_1.Annotation{ +func TestSaverAnnotationSavesTextForPerson(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Person", Annotator: "John Doe"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -31,7 +31,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_1(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -43,8 +43,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_1AnnotationSavesTextForOrganization(t *testing.T) { - ann := &v2_1.Annotation{ +func TestSaverAnnotationSavesTextForOrganization(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Organization", Annotator: "John Doe, Inc."}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -63,7 +63,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_1(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -75,8 +75,8 @@ AnnotationComment: This is an annotation about the SPDX document } } -func TestSaver2_1AnnotationSavesTextForTool(t *testing.T) { - ann := &v2_1.Annotation{ +func TestSaverAnnotationSavesTextForTool(t *testing.T) { + ann := &spdx.Annotation{ Annotator: common.Annotator{AnnotatorType: "Tool", Annotator: "magictool-1.1"}, AnnotationDate: "2018-10-10T17:52:00Z", AnnotationType: "REVIEW", @@ -95,7 +95,7 @@ AnnotationComment: This is an annotation about the SPDX document // render as buffer of bytes var got bytes.Buffer - err := renderAnnotation2_1(ann, &got) + err := renderAnnotation(ann, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_creation_info.go b/spdx/v2/v2_3/tagvalue/writer/save_creation_info.go similarity index 81% rename from tvsaver/saver2v2/save_creation_info.go rename to spdx/v2/v2_3/tagvalue/writer/save_creation_info.go index 87533a2a..45ec4617 100644 --- a/tvsaver/saver2v2/save_creation_info.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_creation_info.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderCreationInfo2_2(ci *v2_2.CreationInfo, w io.Writer) error { +func renderCreationInfo(ci *spdx.CreationInfo, w io.Writer) error { if ci.LicenseListVersion != "" { fmt.Fprintf(w, "LicenseListVersion: %s\n", ci.LicenseListVersion) } diff --git a/tvsaver/saver2v2/save_creation_info_test.go b/spdx/v2/v2_3/tagvalue/writer/save_creation_info_test.go similarity index 86% rename from tvsaver/saver2v2/save_creation_info_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_creation_info_test.go index bb10aa37..70fd537a 100644 --- a/tvsaver/saver2v2/save_creation_info_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_creation_info_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Creation Info section Saver tests ===== -func TestSaver2_2CISavesText(t *testing.T) { - ci := &v2_2.CreationInfo{ +func TestSaverCISavesText(t *testing.T) { + ci := &spdx.CreationInfo{ LicenseListVersion: "3.9", Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, @@ -43,7 +43,7 @@ CreatorComment: this is a creator comment // render as buffer of bytes var got bytes.Buffer - err := renderCreationInfo2_2(ci, &got) + err := renderCreationInfo(ci, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -55,9 +55,9 @@ CreatorComment: this is a creator comment } } -func TestSaver2_2CIOmitsOptionalFieldsIfEmpty(t *testing.T) { +func TestSaverCIOmitsOptionalFieldsIfEmpty(t *testing.T) { // --- need at least one creator; do first for Persons --- - ci1 := &v2_2.CreationInfo{ + ci1 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -72,7 +72,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got1 bytes.Buffer - err := renderCreationInfo2_2(ci1, &got1) + err := renderCreationInfo(ci1, &got1) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -84,7 +84,7 @@ Created: 2018-10-10T06:20:00Z } // --- need at least one creator; now switch to organization --- - ci2 := &v2_2.CreationInfo{ + ci2 := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe, Inc.", CreatorType: "Organization"}, }, @@ -99,7 +99,7 @@ Created: 2018-10-10T06:20:00Z // render as buffer of bytes var got2 bytes.Buffer - err = renderCreationInfo2_2(ci2, &got2) + err = renderCreationInfo(ci2, &got2) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_document.go b/spdx/v2/v2_3/tagvalue/writer/save_document.go similarity index 82% rename from tvsaver/saver2v3/save_document.go rename to spdx/v2/v2_3/tagvalue/writer/save_document.go index e8c25359..f3a3d644 100644 --- a/tvsaver/saver2v3/save_document.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_document.go @@ -1,23 +1,22 @@ // Package saver2v3 contains functions to render and write a tag-value -// formatted version of an in-memory SPDX document and its sections -// (version 2.2). +// formatted version of an in-memory SPDX document and its sections. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -// RenderDocument2_3 is the main entry point to take an SPDX in-memory -// Document (version 2.2), and render it to the received io.Writer. +// RenderDocument is the main entry point to take an SPDX in-memory +// Document, and render it to the received io.Writer. // It is only exported in order to be available to the tvsaver package, // and typically does not need to be called by client code. -func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { +func RenderDocument(doc *spdx.Document, w io.Writer) error { if doc.CreationInfo == nil { return fmt.Errorf("Document had nil CreationInfo section") } @@ -49,7 +48,7 @@ func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { fmt.Fprintf(w, "DocumentComment: %s\n", textify(doc.DocumentComment)) } - renderCreationInfo2_3(doc.CreationInfo, w) + renderCreationInfo(doc.CreationInfo, w) if len(doc.Files) > 0 { fmt.Fprintf(w, "##### Unpackaged files\n\n") @@ -57,7 +56,7 @@ func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { return doc.Files[i].FileSPDXIdentifier < doc.Files[j].FileSPDXIdentifier }) for _, fi := range doc.Files { - renderFile2_3(fi, w) + renderFile(fi, w) } } @@ -67,20 +66,20 @@ func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { }) for _, pkg := range doc.Packages { fmt.Fprintf(w, "##### Package: %s\n\n", pkg.PackageName) - renderPackage2_3(pkg, w) + renderPackage(pkg, w) } if len(doc.OtherLicenses) > 0 { fmt.Fprintf(w, "##### Other Licenses\n\n") for _, ol := range doc.OtherLicenses { - renderOtherLicense2_3(ol, w) + renderOtherLicense(ol, w) } } if len(doc.Relationships) > 0 { fmt.Fprintf(w, "##### Relationships\n\n") for _, rln := range doc.Relationships { - renderRelationship2_3(rln, w) + renderRelationship(rln, w) } fmt.Fprintf(w, "\n") } @@ -88,7 +87,7 @@ func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { if len(doc.Annotations) > 0 { fmt.Fprintf(w, "##### Annotations\n\n") for _, ann := range doc.Annotations { - renderAnnotation2_3(ann, w) + renderAnnotation(ann, w) fmt.Fprintf(w, "\n") } } @@ -96,7 +95,7 @@ func RenderDocument2_3(doc *v2_3.Document, w io.Writer) error { if len(doc.Reviews) > 0 { fmt.Fprintf(w, "##### Reviews\n\n") for _, rev := range doc.Reviews { - renderReview2_3(rev, w) + renderReview(rev, w) } } diff --git a/tvsaver/saver2v3/save_document_test.go b/spdx/v2/v2_3/tagvalue/writer/save_document_test.go similarity index 88% rename from tvsaver/saver2v3/save_document_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_document_test.go index 10aa3115..b4bc4ac7 100644 --- a/tvsaver/saver2v3/save_document_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_document_test.go @@ -1,20 +1,20 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== entire Document Saver tests ===== -func TestSaver2_3DocumentSavesText(t *testing.T) { +func TestSaverDocumentSavesText(t *testing.T) { // Creation Info section - ci := &v2_3.CreationInfo{ + ci := &spdx.CreationInfo{ Creators: []common.Creator{ {Creator: "John Doe", CreatorType: "Person"}, }, @@ -22,7 +22,7 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { } // unpackaged files - f1 := &v2_3.File{ + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983c", Algorithm: common.SHA1}}, @@ -31,7 +31,7 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_3.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983d", Algorithm: common.SHA1}}, @@ -40,13 +40,13 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - unFiles := []*v2_3.File{ + unFiles := []*spdx.File{ f1, f2, } // Package 1: packaged files with snippets - sn1 := &v2_3.Snippet{ + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet19", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -54,7 +54,7 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_3.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: "Snippet20", SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -62,7 +62,7 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - f3 := &v2_3.File{ + f3 := &spdx.File{ FileName: "/tmp/file-with-snippets.txt", FileSPDXIdentifier: common.ElementID("FileHasSnippets"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983e", Algorithm: common.SHA1}}, @@ -73,13 +73,13 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { "WTFPL", }, FileCopyrightText: "Copyright (c) Jane Doe", - Snippets: map[common.ElementID]*v2_3.Snippet{ + Snippets: map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, }, } - f4 := &v2_3.File{ + f4 := &spdx.File{ FileName: "/tmp/another-file.txt", FileSPDXIdentifier: common.ElementID("FileAnother"), Checksums: []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983f", Algorithm: common.SHA1}}, @@ -88,7 +88,7 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe LLC", } - pkgWith := &v2_3.Package{ + pkgWith := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -104,14 +104,14 @@ func TestSaver2_3DocumentSavesText(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_3.File{ + Files: []*spdx.File{ f3, f4, }, } // Other Licenses 1 and 2 - ol1 := &v2_3.OtherLicense{ + ol1 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -119,33 +119,33 @@ blah blah blah blah`, LicenseName: "License 1", } - ol2 := &v2_3.OtherLicense{ + ol2 := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-2", ExtractedText: `License 2 text - this is a license that does some stuff`, LicenseName: "License 2", } // Relationships - rln1 := &v2_3.Relationship{ + rln1 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), Relationship: "DESCRIBES", } - rln2 := &v2_3.Relationship{ + rln2 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1231"), Relationship: "DESCRIBES", } - rln3 := &v2_3.Relationship{ + rln3 := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "File1232"), Relationship: "DESCRIBES", } // Annotations - ann1 := &v2_3.Annotation{ + ann1 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe", AnnotatorType: "Person"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -154,7 +154,7 @@ blah blah blah blah`, AnnotationComment: "This is an annotation about the SPDX document", } - ann2 := &v2_3.Annotation{ + ann2 := &spdx.Annotation{ Annotator: common.Annotator{Annotator: "John Doe, Inc.", AnnotatorType: "Organization"}, AnnotationDate: "2018-10-10T17:52:00Z", @@ -164,12 +164,12 @@ blah blah blah blah`, } // Reviews - rev1 := &v2_3.Review{ + rev1 := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", } - rev2 := &v2_3.Review{ + rev2 := &spdx.Review{ Reviewer: "Jane Doe LLC", ReviewerType: "Organization", ReviewDate: "2018-10-14T10:28:00Z", @@ -177,37 +177,37 @@ blah blah blah blah`, } // now, build the document - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), DocumentName: "tools-golang-0.0.1.abcdef", DocumentNamespace: "https://github.com/spdx/spdx-docs/tools-golang/tools-golang-0.0.1.abcdef.whatever", CreationInfo: ci, - Packages: []*v2_3.Package{ + Packages: []*spdx.Package{ pkgWith, }, Files: unFiles, - OtherLicenses: []*v2_3.OtherLicense{ + OtherLicenses: []*spdx.OtherLicense{ ol1, ol2, }, - Relationships: []*v2_3.Relationship{ + Relationships: []*spdx.Relationship{ rln1, rln2, rln3, }, - Annotations: []*v2_3.Annotation{ + Annotations: []*spdx.Annotation{ ann1, ann2, }, - Reviews: []*v2_3.Review{ + Reviews: []*spdx.Review{ rev1, rev2, }, } - want := bytes.NewBufferString(`SPDXVersion: SPDX-2.2 + want := bytes.NewBufferString(`SPDXVersion: ` + spdx.Version + ` DataLicense: CC0-1.0 SPDXID: SPDXRef-DOCUMENT DocumentName: tools-golang-0.0.1.abcdef @@ -319,7 +319,7 @@ ReviewComment: I have reviewed this SPDX document and it is awesome // render as buffer of bytes var got bytes.Buffer - err := RenderDocument2_3(doc, &got) + err := RenderDocument(doc, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -332,11 +332,11 @@ ReviewComment: I have reviewed this SPDX document and it is awesome } -func TestSaver2_3DocumentReturnsErrorIfNilCreationInfo(t *testing.T) { - doc := &v2_3.Document{} +func TestSaverDocumentReturnsErrorIfNilCreationInfo(t *testing.T) { + doc := &spdx.Document{} var got bytes.Buffer - err := RenderDocument2_3(doc, &got) + err := RenderDocument(doc, &got) if err == nil { t.Errorf("Expected error, got nil") } diff --git a/tvsaver/saver2v2/save_file.go b/spdx/v2/v2_3/tagvalue/writer/save_file.go similarity index 91% rename from tvsaver/saver2v2/save_file.go rename to spdx/v2/v2_3/tagvalue/writer/save_file.go index 1c367a85..cdd3cc7c 100644 --- a/tvsaver/saver2v2/save_file.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_file.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" "sort" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderFile2_2(f *v2_2.File, w io.Writer) error { +func renderFile(f *spdx.File, w io.Writer) error { if f.FileName != "" { fmt.Fprintf(w, "FileName: %s\n", f.FileName) } @@ -74,7 +74,7 @@ func renderFile2_2(f *v2_2.File, w io.Writer) error { sort.Strings(snippetKeys) for _, sID := range snippetKeys { s := f.Snippets[common.ElementID(sID)] - renderSnippet2_2(s, w) + renderSnippet(s, w) } return nil diff --git a/tvsaver/saver2v3/save_file_test.go b/spdx/v2/v2_3/tagvalue/writer/save_file_test.go similarity index 91% rename from tvsaver/saver2v3/save_file_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_file_test.go index 05fc2cf4..e6b8e0b4 100644 --- a/tvsaver/saver2v3/save_file_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_file_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== File section Saver tests ===== -func TestSaver2_3FileSavesText(t *testing.T) { - f := &v2_3.File{ +func TestSaverFileSavesText(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), FileTypes: []string{ @@ -31,7 +31,7 @@ func TestSaver2_3FileSavesText(t *testing.T) { }, LicenseComments: "this is a license comment(s)", FileCopyrightText: "Copyright (c) Jane Doe", - ArtifactOfProjects: []*v2_3.ArtifactOfProject{ + ArtifactOfProjects: []*spdx.ArtifactOfProject{ { Name: "project1", HomePage: "http://example.com/1/", @@ -101,7 +101,7 @@ FileDependency: g.txt // render as buffer of bytes var got bytes.Buffer - err := renderFile2_3(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -113,8 +113,8 @@ FileDependency: g.txt } } -func TestSaver2_3FileSavesSnippetsAlso(t *testing.T) { - sn1 := &v2_3.Snippet{ +func TestSaverFileSavesSnippetsAlso(t *testing.T) { + sn1 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet19"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, @@ -122,7 +122,7 @@ func TestSaver2_3FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "Copyright (c) John Doe 20x6", } - sn2 := &v2_3.Snippet{ + sn2 := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet20"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, @@ -130,12 +130,12 @@ func TestSaver2_3FileSavesSnippetsAlso(t *testing.T) { SnippetCopyrightText: "NOASSERTION", } - sns := map[common.ElementID]*v2_3.Snippet{ + sns := map[common.ElementID]*spdx.Snippet{ common.ElementID("Snippet19"): sn1, common.ElementID("Snippet20"): sn2, } - f := &v2_3.File{ + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -173,7 +173,7 @@ SnippetCopyrightText: NOASSERTION // render as buffer of bytes var got bytes.Buffer - err := renderFile2_3(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -185,8 +185,8 @@ SnippetCopyrightText: NOASSERTION } } -func TestSaver2_3FileOmitsOptionalFieldsIfEmpty(t *testing.T) { - f := &v2_3.File{ +func TestSaverFileOmitsOptionalFieldsIfEmpty(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -211,7 +211,7 @@ FileCopyrightText: Copyright (c) Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_3(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -223,8 +223,8 @@ FileCopyrightText: Copyright (c) Jane Doe } } -func TestSaver2_3FileWrapsCopyrightMultiLine(t *testing.T) { - f := &v2_3.File{ +func TestSaverFileWrapsCopyrightMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -251,7 +251,7 @@ Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderFile2_3(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -263,8 +263,8 @@ Copyright (c) John Doe } } -func TestSaver2_3FileWrapsCommentsAndNoticesMultiLine(t *testing.T) { - f := &v2_3.File{ +func TestSaverFileWrapsCommentsAndNoticesMultiLine(t *testing.T) { + f := &spdx.File{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: common.ElementID("File123"), Checksums: []common.Checksum{ @@ -301,7 +301,7 @@ under either Apache-2.0 or Apache-1.1. // render as buffer of bytes var got bytes.Buffer - err := renderFile2_3(f, &got) + err := renderFile(f, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_other_license.go b/spdx/v2/v2_3/tagvalue/writer/save_other_license.go similarity index 81% rename from tvsaver/saver2v2/save_other_license.go rename to spdx/v2/v2_3/tagvalue/writer/save_other_license.go index 9fad2097..97a9b5a8 100644 --- a/tvsaver/saver2v2/save_other_license.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_other_license.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderOtherLicense2_2(ol *v2_2.OtherLicense, w io.Writer) error { +func renderOtherLicense(ol *spdx.OtherLicense, w io.Writer) error { if ol.LicenseIdentifier != "" { fmt.Fprintf(w, "LicenseID: %s\n", ol.LicenseIdentifier) } diff --git a/tvsaver/saver2v2/save_other_license_test.go b/spdx/v2/v2_3/tagvalue/writer/save_other_license_test.go similarity index 84% rename from tvsaver/saver2v2/save_other_license_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_other_license_test.go index 6b715220..944a9634 100644 --- a/tvsaver/saver2v2/save_other_license_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_other_license_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_2" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Other License section Saver tests ===== -func TestSaver2_2OtherLicenseSavesText(t *testing.T) { - ol := &v2_2.OtherLicense{ +func TestSaverOtherLicenseSavesText(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -38,7 +38,7 @@ LicenseComment: this is a license comment // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_2(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -50,8 +50,8 @@ LicenseComment: this is a license comment } } -func TestSaver2_2OtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { - ol := &v2_2.OtherLicense{ +func TestSaverOtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) { + ol := &spdx.OtherLicense{ LicenseIdentifier: "LicenseRef-1", ExtractedText: `License 1 text blah blah blah @@ -70,7 +70,7 @@ LicenseName: License 1 // render as buffer of bytes var got bytes.Buffer - err := renderOtherLicense2_2(ol, &got) + err := renderOtherLicense(ol, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v3/save_package.go b/spdx/v2/v2_3/tagvalue/writer/save_package.go similarity index 95% rename from tvsaver/saver2v3/save_package.go rename to spdx/v2/v2_3/tagvalue/writer/save_package.go index d323af9b..01574ddc 100644 --- a/tvsaver/saver2v3/save_package.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_package.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "fmt" @@ -8,11 +8,11 @@ import ( "sort" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderPackage2_3(pkg *v2_3.Package, w io.Writer) error { +func renderPackage(pkg *spdx.Package, w io.Writer) error { if pkg.PackageName != "" { fmt.Fprintf(w, "PackageName: %s\n", pkg.PackageName) } @@ -122,7 +122,7 @@ func renderPackage2_3(pkg *v2_3.Package, w io.Writer) error { return pkg.Files[i].FileSPDXIdentifier < pkg.Files[j].FileSPDXIdentifier }) for _, fi := range pkg.Files { - renderFile2_3(fi, w) + renderFile(fi, w) } return nil diff --git a/tvsaver/saver2v3/save_package_test.go b/spdx/v2/v2_3/tagvalue/writer/save_package_test.go similarity index 94% rename from tvsaver/saver2v3/save_package_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_package_test.go index 435b5b59..33e17c96 100644 --- a/tvsaver/saver2v3/save_package_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_package_test.go @@ -1,24 +1,24 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Package section Saver tests ===== -func TestSaver2_3PackageSavesTextCombo1(t *testing.T) { +func TestSaverPackageSavesTextCombo1(t *testing.T) { // include package external refs // test Supplier:Organization, Originator:Person // FilesAnalyzed true, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile has string // NOTE, this is an entirely made up CPE and the format is likely invalid - per1 := &v2_3.PackageExternalReference{ + per1 := &spdx.PackageExternalReference{ Category: "SECURITY", RefType: "cpe22Type", Locator: "cpe:/a:john_doe_inc:p1:0.1.0", @@ -26,7 +26,7 @@ func TestSaver2_3PackageSavesTextCombo1(t *testing.T) { } // NOTE, this is an entirely made up NPM - per2 := &v2_3.PackageExternalReference{ + per2 := &spdx.PackageExternalReference{ Category: "PACKAGE-MANAGER", RefType: "npm", Locator: "p1@0.1.0", @@ -35,21 +35,21 @@ multi-line external ref comment`, } // NOTE, this is an entirely made up SWH persistent ID - per3 := &v2_3.PackageExternalReference{ + per3 := &spdx.PackageExternalReference{ Category: "PERSISTENT-ID", RefType: "swh", Locator: "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", // no ExternalRefComment for this one } - per4 := &v2_3.PackageExternalReference{ + per4 := &spdx.PackageExternalReference{ Category: "OTHER", RefType: "anything", Locator: "anything-without-spaces-can-go-here", // no ExternalRefComment for this one } - pkg := &v2_3.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -92,7 +92,7 @@ multi-line external ref comment`, PackageDescription: "this is a description comment", PackageComment: "this is a comment comment", PackageAttributionTexts: []string{"Include this notice in all advertising materials"}, - PackageExternalReferences: []*v2_3.PackageExternalReference{ + PackageExternalReferences: []*spdx.PackageExternalReference{ per1, per2, per3, @@ -146,7 +146,7 @@ PackageAttributionText: Include this notice in all advertising materials // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -158,13 +158,13 @@ PackageAttributionText: Include this notice in all advertising materials } } -func TestSaver2_3PackageSavesTextCombo2(t *testing.T) { +func TestSaverPackageSavesTextCombo2(t *testing.T) { // no package external refs // test Supplier:NOASSERTION, Originator:Organization // FilesAnalyzed true, IsFilesAnalyzedTagPresent false // PackageVerificationCodeExcludedFile is empty - pkg := &v2_3.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -236,7 +236,7 @@ PackageAttributionText: Include this notice in all advertising materials // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -248,14 +248,14 @@ PackageAttributionText: Include this notice in all advertising materials } } -func TestSaver2_3PackageSavesTextCombo3(t *testing.T) { +func TestSaverPackageSavesTextCombo3(t *testing.T) { // no package external refs // test Supplier:Person, Originator:NOASSERTION // FilesAnalyzed false, IsFilesAnalyzedTagPresent true // PackageVerificationCodeExcludedFile is empty // three PackageAttributionTexts, one with multi-line text - pkg := &v2_3.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageVersion: "0.1.0", @@ -336,7 +336,7 @@ which goes across two lines // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -348,8 +348,8 @@ which goes across two lines } } -func TestSaver2_3PackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { - pkg := &v2_3.Package{ +func TestSaverPackageSaveOmitsOptionalFieldsIfEmpty(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -382,7 +382,7 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -394,8 +394,8 @@ PackageCopyrightText: Copyright (c) John Doe, Inc. } } -func TestSaver2_3PackageSavesFilesIfPresent(t *testing.T) { - f1 := &v2_3.File{ +func TestSaverPackageSavesFilesIfPresent(t *testing.T) { + f1 := &spdx.File{ FileName: "/tmp/whatever1.txt", FileSPDXIdentifier: common.ElementID("File1231"), Checksums: []common.Checksum{ @@ -409,7 +409,7 @@ func TestSaver2_3PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) Jane Doe", } - f2 := &v2_3.File{ + f2 := &spdx.File{ FileName: "/tmp/whatever2.txt", FileSPDXIdentifier: common.ElementID("File1232"), Checksums: []common.Checksum{ @@ -423,7 +423,7 @@ func TestSaver2_3PackageSavesFilesIfPresent(t *testing.T) { FileCopyrightText: "Copyright (c) John Doe", } - pkg := &v2_3.Package{ + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -441,7 +441,7 @@ func TestSaver2_3PackageSavesFilesIfPresent(t *testing.T) { }, PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later", PackageCopyrightText: "Copyright (c) John Doe, Inc.", - Files: []*v2_3.File{ + Files: []*spdx.File{ f1, f2, }, @@ -474,7 +474,7 @@ FileCopyrightText: Copyright (c) John Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -486,8 +486,8 @@ FileCopyrightText: Copyright (c) John Doe } } -func TestSaver2_3PackageWrapsMultiLine(t *testing.T) { - pkg := &v2_3.Package{ +func TestSaverPackageWrapsMultiLine(t *testing.T) { + pkg := &spdx.Package{ PackageName: "p1", PackageSPDXIdentifier: common.ElementID("p1"), PackageDownloadLocation: "http://example.com/p1/p1-0.1.0-master.tar.gz", @@ -518,7 +518,7 @@ Copyright Jane Doe // render as buffer of bytes var got bytes.Buffer - err := renderPackage2_3(pkg, &got) + err := renderPackage(pkg, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_relationship.go b/spdx/v2/v2_3/tagvalue/writer/save_relationship.go similarity index 72% rename from tvsaver/saver2v1/save_relationship.go rename to spdx/v2/v2_3/tagvalue/writer/save_relationship.go index 01aa3dde..5fd2ab78 100644 --- a/tvsaver/saver2v1/save_relationship.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_relationship.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderRelationship2_1(rln *v2_1.Relationship, w io.Writer) error { +func renderRelationship(rln *spdx.Relationship, w io.Writer) error { rlnAStr := common.RenderDocElementID(rln.RefA) rlnBStr := common.RenderDocElementID(rln.RefB) if rlnAStr != "SPDXRef-" && rlnBStr != "SPDXRef-" && rln.Relationship != "" { diff --git a/tvsaver/saver2v2/save_relationship_test.go b/spdx/v2/v2_3/tagvalue/writer/save_relationship_test.go similarity index 80% rename from tvsaver/saver2v2/save_relationship_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_relationship_test.go index 927164c3..992723ce 100644 --- a/tvsaver/saver2v2/save_relationship_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_relationship_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Relationship section Saver tests ===== -func TestSaver2_2RelationshipSavesText(t *testing.T) { - rln := &v2_2.Relationship{ +func TestSaverRelationshipSavesText(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -27,7 +27,7 @@ RelationshipComment: this is a comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_2(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ RelationshipComment: this is a comment } } -func TestSaver2_2RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { - rln := &v2_2.Relationship{ +func TestSaverRelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -52,7 +52,7 @@ func TestSaver2_2RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_2(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -64,8 +64,8 @@ func TestSaver2_2RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { } } -func TestSaver2_2RelationshipCanHaveNONEOnRight(t *testing.T) { - rln := &v2_2.Relationship{ +func TestSaverRelationshipCanHaveNONEOnRight(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "PackageA"), RefB: common.MakeDocElementSpecial("NONE"), Relationship: "DEPENDS_ON", @@ -77,7 +77,7 @@ func TestSaver2_2RelationshipCanHaveNONEOnRight(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_2(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -89,8 +89,8 @@ func TestSaver2_2RelationshipCanHaveNONEOnRight(t *testing.T) { } } -func TestSaver2_2RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { - rln := &v2_2.Relationship{ +func TestSaverRelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "PackageA"), RefB: common.MakeDocElementSpecial("NOASSERTION"), Relationship: "DEPENDS_ON", @@ -102,7 +102,7 @@ func TestSaver2_2RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_2(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -114,8 +114,8 @@ func TestSaver2_2RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { } } -func TestSaver2_2RelationshipWrapsCommentMultiLine(t *testing.T) { - rln := &v2_2.Relationship{ +func TestSaverRelationshipWrapsCommentMultiLine(t *testing.T) { + rln := &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "2"), Relationship: "DESCRIBES", @@ -132,7 +132,7 @@ multi-line comment // render as buffer of bytes var got bytes.Buffer - err := renderRelationship2_2(rln, &got) + err := renderRelationship(rln, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/save_review.go b/spdx/v2/v2_3/tagvalue/writer/save_review.go similarity index 78% rename from tvsaver/saver2v1/save_review.go rename to spdx/v2/v2_3/tagvalue/writer/save_review.go index 33511a36..7ced2073 100644 --- a/tvsaver/saver2v1/save_review.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_review.go @@ -1,15 +1,15 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/v2_1" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderReview2_1(rev *v2_1.Review, w io.Writer) error { +func renderReview(rev *spdx.Review, w io.Writer) error { if rev.Reviewer != "" && rev.ReviewerType != "" { fmt.Fprintf(w, "Reviewer: %s: %s\n", rev.ReviewerType, rev.Reviewer) } diff --git a/tvsaver/saver2v3/save_review_test.go b/spdx/v2/v2_3/tagvalue/writer/save_review_test.go similarity index 82% rename from tvsaver/saver2v3/save_review_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_review_test.go index 5eec3bc7..6f21b8fc 100644 --- a/tvsaver/saver2v3/save_review_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_review_test.go @@ -1,17 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v3 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/v2_3" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Review section Saver tests ===== -func TestSaver2_3ReviewSavesText(t *testing.T) { - rev := &v2_3.Review{ +func TestSaverReviewSavesText(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -27,7 +27,7 @@ ReviewComment: this is a review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_3(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -39,8 +39,8 @@ ReviewComment: this is a review comment } } -func TestSaver2_3ReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { - rev := &v2_3.Review{ +func TestSaverReviewOmitsOptionalFieldsIfEmpty(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -54,7 +54,7 @@ ReviewDate: 2018-10-14T10:28:00Z // render as buffer of bytes var got bytes.Buffer - err := renderReview2_3(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -66,8 +66,8 @@ ReviewDate: 2018-10-14T10:28:00Z } } -func TestSaver2_3ReviewWrapsMultiLine(t *testing.T) { - rev := &v2_3.Review{ +func TestSaverReviewWrapsMultiLine(t *testing.T) { + rev := &spdx.Review{ Reviewer: "John Doe", ReviewerType: "Person", ReviewDate: "2018-10-14T10:28:00Z", @@ -85,7 +85,7 @@ multi-line review comment // render as buffer of bytes var got bytes.Buffer - err := renderReview2_3(rev, &got) + err := renderReview(rev, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v2/save_snippet.go b/spdx/v2/v2_3/tagvalue/writer/save_snippet.go similarity index 90% rename from tvsaver/saver2v2/save_snippet.go rename to spdx/v2/v2_3/tagvalue/writer/save_snippet.go index d1254db7..f7914af7 100644 --- a/tvsaver/saver2v2/save_snippet.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_snippet.go @@ -1,16 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "fmt" "io" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) -func renderSnippet2_2(sn *v2_2.Snippet, w io.Writer) error { +func renderSnippet(sn *spdx.Snippet, w io.Writer) error { if sn.SnippetSPDXIdentifier != "" { fmt.Fprintf(w, "SnippetSPDXID: %s\n", common.RenderElementID(sn.SnippetSPDXIdentifier)) } diff --git a/tvsaver/saver2v2/save_snippet_test.go b/spdx/v2/v2_3/tagvalue/writer/save_snippet_test.go similarity index 89% rename from tvsaver/saver2v2/save_snippet_test.go rename to spdx/v2/v2_3/tagvalue/writer/save_snippet_test.go index 72daf159..7062d40f 100644 --- a/tvsaver/saver2v2/save_snippet_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/save_snippet_test.go @@ -1,18 +1,18 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v2 +package writer import ( "bytes" "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx/v2/common" + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" ) // ===== Snippet section Saver tests ===== -func TestSaver2_2SnippetSavesText(t *testing.T) { - sn := &v2_2.Snippet{ +func TestSaverSnippetSavesText(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -55,7 +55,7 @@ SnippetAttributionText: some attributions // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_2(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -67,8 +67,8 @@ SnippetAttributionText: some attributions } } -func TestSaver2_2SnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { - sn := &v2_2.Snippet{ +func TestSaverSnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -92,7 +92,7 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_2(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } @@ -104,8 +104,8 @@ SnippetCopyrightText: Copyright (c) John Doe 20x6 } } -func TestSaver2_2SnippetWrapsCopyrightMultiline(t *testing.T) { - sn := &v2_2.Snippet{ +func TestSaverSnippetWrapsCopyrightMultiline(t *testing.T) { + sn := &spdx.Snippet{ SnippetSPDXIdentifier: common.ElementID("Snippet17"), SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, Ranges: []common.SnippetRange{ @@ -131,7 +131,7 @@ Copyright (c) John Doe 20x6 // render as buffer of bytes var got bytes.Buffer - err := renderSnippet2_2(sn, &got) + err := renderSnippet(sn, &got) if err != nil { t.Errorf("Expected nil error, got %v", err) } diff --git a/tvsaver/saver2v1/util.go b/spdx/v2/v2_3/tagvalue/writer/util.go similarity index 92% rename from tvsaver/saver2v1/util.go rename to spdx/v2/v2_3/tagvalue/writer/util.go index e2064497..5298ee0a 100644 --- a/tvsaver/saver2v1/util.go +++ b/spdx/v2/v2_3/tagvalue/writer/util.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "fmt" diff --git a/tvsaver/saver2v1/util_test.go b/spdx/v2/v2_3/tagvalue/writer/util_test.go similarity index 97% rename from tvsaver/saver2v1/util_test.go rename to spdx/v2/v2_3/tagvalue/writer/util_test.go index aceccb7b..94a20c86 100644 --- a/tvsaver/saver2v1/util_test.go +++ b/spdx/v2/v2_3/tagvalue/writer/util_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package saver2v1 +package writer import ( "testing" diff --git a/spdx/v2/v2_3/yaml/yaml_test.go b/spdx/v2/v2_3/yaml/yaml_test.go new file mode 100644 index 00000000..65eedcaf --- /dev/null +++ b/spdx/v2/v2_3/yaml/yaml_test.go @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package yaml + +import ( + "bytes" + "flag" + "fmt" + "os" + "testing" + + "github.com/google/go-cmp/cmp" + + spdx "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/spdx/v2/v2_3/example" + "github.com/spdx/tools-golang/yaml" +) + +var update = *flag.Bool("update-snapshots", false, "update the example snapshot") + +func Test_Read(t *testing.T) { + fileName := "../../../../examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml" + + want := example.Copy() + + if update { + f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + t.Errorf("unable to write SPDX 2.3 example to YAML: %v", err) + } + err = yaml.Write(want, f) + if err != nil { + t.Errorf("unable to serialize SPDX 2.3 example to YAML: %v", err) + } + } + + file, err := os.Open(fileName) + if err != nil { + panic(fmt.Errorf("error opening File: %s", err)) + } + + var got spdx.Document + err = yaml.ReadInto(file, &got) + if err != nil { + t.Errorf("yaml.Read() error = %v", err) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} + +func Test_Write(t *testing.T) { + want := example.Copy() + + w := &bytes.Buffer{} + + if err := yaml.Write(&want, w); err != nil { + t.Errorf("Save() error = %v", err.Error()) + return + } + + // we should be able to parse what the writer wrote, and it should be identical to the original handwritten struct + var got spdx.Document + err := yaml.ReadInto(bytes.NewReader(w.Bytes()), &got) + if err != nil { + t.Errorf("failed to parse written document: %v", err.Error()) + return + } + + if !cmp.Equal(want, got) { + t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got)) + return + } +} diff --git a/spdxlib/described_elements.go b/spdxlib/described_elements.go index a2a63562..048a6ab8 100644 --- a/spdxlib/described_elements.go +++ b/spdxlib/described_elements.go @@ -6,18 +6,16 @@ package spdxlib import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// GetDescribedPackageIDs2_1 returns a slice of ElementIDs for all Packages +// GetDescribedPackageIDs returns a slice of ElementIDs for all Packages // in this Document that it "describes," according to SPDX rules: // - If the document has only one Package, its ID is returned. // - If the document has 2+ Packages, it returns the IDs of those that have // a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT. -func GetDescribedPackageIDs2_1(doc *v2_1.Document) ([]common.ElementID, error) { +func GetDescribedPackageIDs(doc *spdx.Document) ([]common.ElementID, error) { // if nil Packages map or zero packages in it, return empty slice if doc.Packages == nil { return nil, fmt.Errorf("Packages map is nil") @@ -39,109 +37,7 @@ func GetDescribedPackageIDs2_1(doc *v2_1.Document) ([]common.ElementID, error) { return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil") } - eIDs, err := FilterRelationships2_1(doc, func(relationship *v2_1.Relationship) *common.ElementID { - refDocument := common.MakeDocElementID("", "DOCUMENT") - - if relationship.Relationship == "DESCRIBES" && relationship.RefA == refDocument { - return &relationship.RefB.ElementRefID - } else if relationship.Relationship == "DESCRIBED_BY" && relationship.RefB == refDocument { - return &relationship.RefA.ElementRefID - } - - return nil - }) - if err != nil { - return nil, err - } - - if len(eIDs) == 0 { - return nil, fmt.Errorf("no DESCRIBES or DESCRIBED_BY relationships found for this Document") - } - - eIDs = SortElementIDs(eIDs) - - return eIDs, nil -} - -// GetDescribedPackageIDs2_2 returns a slice of ElementIDs for all Packages -// in this Document that it "describes," according to SPDX rules: -// - If the document has only one Package, its ID is returned. -// - If the document has 2+ Packages, it returns the IDs of those that have -// a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT. -func GetDescribedPackageIDs2_2(doc *v2_2.Document) ([]common.ElementID, error) { - // if nil Packages map or zero packages in it, return empty slice - if doc.Packages == nil { - return nil, fmt.Errorf("Packages map is nil") - } - if len(doc.Packages) == 0 { - return nil, fmt.Errorf("no Packages in Document") - } - if len(doc.Packages) == 1 { - // get first (only) one and return its ID - for _, pkg := range doc.Packages { - return []common.ElementID{pkg.PackageSPDXIdentifier}, nil - } - } - - // two or more packages, so we need to go through the relationships, - // find DESCRIBES or DESCRIBED_BY for this DOCUMENT, verify they are - // valid IDs in this document's packages, and return them - if doc.Relationships == nil { - return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil") - } - - eIDs, err := FilterRelationships2_2(doc, func(relationship *v2_2.Relationship) *common.ElementID { - refDocument := common.MakeDocElementID("", "DOCUMENT") - - if relationship.Relationship == "DESCRIBES" && relationship.RefA == refDocument { - return &relationship.RefB.ElementRefID - } else if relationship.Relationship == "DESCRIBED_BY" && relationship.RefB == refDocument { - return &relationship.RefA.ElementRefID - } - - return nil - }) - if err != nil { - return nil, err - } - - if len(eIDs) == 0 { - return nil, fmt.Errorf("no DESCRIBES or DESCRIBED_BY relationships found for this Document") - } - - eIDs = SortElementIDs(eIDs) - - return eIDs, nil -} - -// GetDescribedPackageIDs2_3 returns a slice of ElementIDs for all Packages -// in this Document that it "describes," according to SPDX rules: -// - If the document has only one Package, its ID is returned. -// - If the document has 2+ Packages, it returns the IDs of those that have -// a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT. -func GetDescribedPackageIDs2_3(doc *v2_3.Document) ([]common.ElementID, error) { - // if nil Packages map or zero packages in it, return empty slice - if doc.Packages == nil { - return nil, fmt.Errorf("Packages map is nil") - } - if len(doc.Packages) == 0 { - return nil, fmt.Errorf("no Packages in Document") - } - if len(doc.Packages) == 1 { - // get first (only) one and return its ID - for _, pkg := range doc.Packages { - return []common.ElementID{pkg.PackageSPDXIdentifier}, nil - } - } - - // two or more packages, so we need to go through the relationships, - // find DESCRIBES or DESCRIBED_BY for this DOCUMENT, verify they are - // valid IDs in this document's packages, and return them - if doc.Relationships == nil { - return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil") - } - - eIDs, err := FilterRelationships2_3(doc, func(relationship *v2_3.Relationship) *common.ElementID { + eIDs, err := FilterRelationships(doc, func(relationship *spdx.Relationship) *common.ElementID { refDocument := common.MakeDocElementID("", "DOCUMENT") if relationship.Relationship == "DESCRIBES" && relationship.RefA == refDocument { diff --git a/spdxlib/described_elements_test.go b/spdxlib/described_elements_test.go index 7398ec95..c553f79e 100644 --- a/spdxlib/described_elements_test.go +++ b/spdxlib/described_elements_test.go @@ -5,385 +5,43 @@ package spdxlib import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 tests ===== - -func Test2_1CanGetIDsOfDescribedPackages(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_1.Relationship{ - &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - // inverse relationship -- should also get detected - &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "p4"), - RefB: common.MakeDocElementID("", "DOCUMENT"), - Relationship: "DESCRIBED_BY", - }, - // different relationship - &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_1(doc) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - // should be three of the five IDs, returned in alphabetical order - if len(describedPkgIDs) != 3 { - t.Fatalf("expected %d packages, got %d", 3, len(describedPkgIDs)) - } - if describedPkgIDs[0] != common.ElementID("p1") { - t.Errorf("expected %v, got %v", common.ElementID("p1"), describedPkgIDs[0]) - } - if describedPkgIDs[1] != common.ElementID("p4") { - t.Errorf("expected %v, got %v", common.ElementID("p4"), describedPkgIDs[1]) - } - if describedPkgIDs[2] != common.ElementID("p5") { - t.Errorf("expected %v, got %v", common.ElementID("p5"), describedPkgIDs[2]) - } -} - -func Test2_1GetDescribedPackagesReturnsSinglePackageIfOnlyOne(t *testing.T) { - // set up document and one package, but no relationships - // b/c only one package - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - }, - } - - // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_1(doc) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - // should return the single package - if len(describedPkgIDs) != 1 { - t.Fatalf("expected %d package, got %d", 1, len(describedPkgIDs)) - } - if describedPkgIDs[0] != common.ElementID("p1") { - t.Errorf("expected %v, got %v", common.ElementID("p1"), describedPkgIDs[0]) - } -} - -func Test2_1FailsToGetDescribedPackagesIfMoreThanOneWithoutDescribesRelationship(t *testing.T) { - // set up document and multiple packages, but no DESCRIBES relationships - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_1.Relationship{ - // different relationship - &v2_1.Relationship{ - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - _, err := GetDescribedPackageIDs2_1(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_1FailsToGetDescribedPackagesIfMoreThanOneWithNilRelationships(t *testing.T) { - // set up document and multiple packages, but no relationships slice - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - }, - } - - _, err := GetDescribedPackageIDs2_1(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_1FailsToGetDescribedPackagesIfZeroPackagesInMap(t *testing.T) { - // set up document but no packages - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{}, - } - - _, err := GetDescribedPackageIDs2_1(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_1FailsToGetDescribedPackagesIfNilMap(t *testing.T) { - // set up document but no packages - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - } - - _, err := GetDescribedPackageIDs2_1(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.2 tests ===== - -func Test2_2CanGetIDsOfDescribedPackages(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_2.Relationship{ - &v2_2.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - &v2_2.Relationship{ - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - // inverse relationship -- should also get detected - &v2_2.Relationship{ - RefA: common.MakeDocElementID("", "p4"), - RefB: common.MakeDocElementID("", "DOCUMENT"), - Relationship: "DESCRIBED_BY", - }, - // different relationship - &v2_2.Relationship{ - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_2(doc) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - // should be three of the five IDs, returned in alphabetical order - if len(describedPkgIDs) != 3 { - t.Fatalf("expected %d packages, got %d", 3, len(describedPkgIDs)) - } - if describedPkgIDs[0] != common.ElementID("p1") { - t.Errorf("expected %v, got %v", common.ElementID("p1"), describedPkgIDs[0]) - } - if describedPkgIDs[1] != common.ElementID("p4") { - t.Errorf("expected %v, got %v", common.ElementID("p4"), describedPkgIDs[1]) - } - if describedPkgIDs[2] != common.ElementID("p5") { - t.Errorf("expected %v, got %v", common.ElementID("p5"), describedPkgIDs[2]) - } -} - -func Test2_2GetDescribedPackagesReturnsSinglePackageIfOnlyOne(t *testing.T) { - // set up document and one package, but no relationships - // b/c only one package - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - }, - } - - // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_2(doc) - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - // should return the single package - if len(describedPkgIDs) != 1 { - t.Fatalf("expected %d package, got %d", 1, len(describedPkgIDs)) - } - if describedPkgIDs[0] != common.ElementID("p1") { - t.Errorf("expected %v, got %v", common.ElementID("p1"), describedPkgIDs[0]) - } -} - -func Test2_2FailsToGetDescribedPackagesIfMoreThanOneWithoutDescribesRelationship(t *testing.T) { - // set up document and multiple packages, but no DESCRIBES relationships - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_2.Relationship{ - // different relationship - &v2_2.Relationship{ - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - _, err := GetDescribedPackageIDs2_2(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_2FailsToGetDescribedPackagesIfMoreThanOneWithNilRelationships(t *testing.T) { - // set up document and multiple packages, but no relationships slice - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - }, - } - - _, err := GetDescribedPackageIDs2_2(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_2FailsToGetDescribedPackagesIfZeroPackagesInMap(t *testing.T) { - // set up document but no packages - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{}, - } - - _, err := GetDescribedPackageIDs2_2(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -func Test2_2FailsToGetDescribedPackagesIfNilMap(t *testing.T) { - // set up document but no packages - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - } - - _, err := GetDescribedPackageIDs2_2(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.3 tests ===== - -func Test2_3CanGetIDsOfDescribedPackages(t *testing.T) { +func TestCanGetIDsOfDescribedPackages(t *testing.T) { // set up document and some packages and relationships - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, }, - Relationships: []*v2_3.Relationship{ - &v2_3.Relationship{ + Relationships: []*spdx.Relationship{ + &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), Relationship: "DESCRIBES", }, - &v2_3.Relationship{ + &spdx.Relationship{ RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p5"), Relationship: "DESCRIBES", }, // inverse relationship -- should also get detected - &v2_3.Relationship{ + &spdx.Relationship{ RefA: common.MakeDocElementID("", "p4"), RefB: common.MakeDocElementID("", "DOCUMENT"), Relationship: "DESCRIBED_BY", }, // different relationship - &v2_3.Relationship{ + &spdx.Relationship{ RefA: common.MakeDocElementID("", "p1"), RefB: common.MakeDocElementID("", "p2"), Relationship: "DEPENDS_ON", @@ -392,7 +50,7 @@ func Test2_3CanGetIDsOfDescribedPackages(t *testing.T) { } // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_3(doc) + describedPkgIDs, err := GetDescribedPackageIDs(doc) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -411,21 +69,21 @@ func Test2_3CanGetIDsOfDescribedPackages(t *testing.T) { } } -func Test2_3GetDescribedPackagesReturnsSinglePackageIfOnlyOne(t *testing.T) { +func TestGetDescribedPackagesReturnsSinglePackageIfOnlyOne(t *testing.T) { // set up document and one package, but no relationships // b/c only one package - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, }, } // request IDs for DESCRIBES / DESCRIBED_BY relationships - describedPkgIDs, err := GetDescribedPackageIDs2_3(doc) + describedPkgIDs, err := GetDescribedPackageIDs(doc) if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -438,23 +96,23 @@ func Test2_3GetDescribedPackagesReturnsSinglePackageIfOnlyOne(t *testing.T) { } } -func Test2_3FailsToGetDescribedPackagesIfMoreThanOneWithoutDescribesRelationship(t *testing.T) { +func TestFailsToGetDescribedPackagesIfMoreThanOneWithoutDescribesRelationship(t *testing.T) { // set up document and multiple packages, but no DESCRIBES relationships - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, }, - Relationships: []*v2_3.Relationship{ + Relationships: []*spdx.Relationship{ // different relationship - &v2_3.Relationship{ + &spdx.Relationship{ RefA: common.MakeDocElementID("", "p1"), RefB: common.MakeDocElementID("", "p2"), Relationship: "DEPENDS_ON", @@ -462,57 +120,57 @@ func Test2_3FailsToGetDescribedPackagesIfMoreThanOneWithoutDescribesRelationship }, } - _, err := GetDescribedPackageIDs2_3(doc) + _, err := GetDescribedPackageIDs(doc) if err == nil { t.Fatalf("expected non-nil error, got nil") } } -func Test2_3FailsToGetDescribedPackagesIfMoreThanOneWithNilRelationships(t *testing.T) { +func TestFailsToGetDescribedPackagesIfMoreThanOneWithNilRelationships(t *testing.T) { // set up document and multiple packages, but no relationships slice - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, }, } - _, err := GetDescribedPackageIDs2_3(doc) + _, err := GetDescribedPackageIDs(doc) if err == nil { t.Fatalf("expected non-nil error, got nil") } } -func Test2_3FailsToGetDescribedPackagesIfZeroPackagesInMap(t *testing.T) { +func TestFailsToGetDescribedPackagesIfZeroPackagesInMap(t *testing.T) { // set up document but no packages - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{}, + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{}, } - _, err := GetDescribedPackageIDs2_3(doc) + _, err := GetDescribedPackageIDs(doc) if err == nil { t.Fatalf("expected non-nil error, got nil") } } -func Test2_3FailsToGetDescribedPackagesIfNilMap(t *testing.T) { +func TestFailsToGetDescribedPackagesIfNilMap(t *testing.T) { // set up document but no packages - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, + CreationInfo: &spdx.CreationInfo{}, } - _, err := GetDescribedPackageIDs2_3(doc) + _, err := GetDescribedPackageIDs(doc) if err == nil { t.Fatalf("expected non-nil error, got nil") } diff --git a/spdxlib/documents.go b/spdxlib/documents.go index dc418034..a6a34ca0 100644 --- a/spdxlib/documents.go +++ b/spdxlib/documents.go @@ -4,76 +4,14 @@ package spdxlib import ( "fmt" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ValidateDocument2_1 returns an error if the Document is found to be invalid, or nil if the Document is valid. +// ValidateDocument returns an error if the Document is found to be invalid, or nil if the Document is valid. // Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a // Package or an UnpackagedFile. -func ValidateDocument2_1(doc *v2_1.Document) error { - // cache a map of valid package IDs for quick lookups - validElementIDs := make(map[common.ElementID]bool) - for _, docPackage := range doc.Packages { - validElementIDs[docPackage.PackageSPDXIdentifier] = true - } - - for _, unpackagedFile := range doc.Files { - validElementIDs[unpackagedFile.FileSPDXIdentifier] = true - } - - // add the Document element ID - validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true - - for _, relationship := range doc.Relationships { - if !validElementIDs[relationship.RefA.ElementRefID] { - return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID)) - } - - if !validElementIDs[relationship.RefB.ElementRefID] { - return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID)) - } - } - - return nil -} - -// ValidateDocument2_2 returns an error if the Document is found to be invalid, or nil if the Document is valid. -// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a -// Package or an UnpackagedFile. -func ValidateDocument2_2(doc *v2_2.Document) error { - // cache a map of package IDs for quick lookups - validElementIDs := make(map[common.ElementID]bool) - for _, docPackage := range doc.Packages { - validElementIDs[docPackage.PackageSPDXIdentifier] = true - } - - for _, unpackagedFile := range doc.Files { - validElementIDs[unpackagedFile.FileSPDXIdentifier] = true - } - - // add the Document element ID - validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true - - for _, relationship := range doc.Relationships { - if !validElementIDs[relationship.RefA.ElementRefID] { - return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID)) - } - - if !validElementIDs[relationship.RefB.ElementRefID] { - return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID)) - } - } - - return nil -} - -// ValidateDocument2_3 returns an error if the Document is found to be invalid, or nil if the Document is valid. -// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a -// Package or an UnpackagedFile. -func ValidateDocument2_3(doc *v2_3.Document) error { +func ValidateDocument(doc *spdx.Document) error { // cache a map of package IDs for quick lookups validElementIDs := make(map[common.ElementID]bool) for _, docPackage := range doc.Packages { diff --git a/spdxlib/documents_test.go b/spdxlib/documents_test.go index 3557d349..3219b20d 100644 --- a/spdxlib/documents_test.go +++ b/spdxlib/documents_test.go @@ -5,201 +5,25 @@ package spdxlib import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 tests ===== - -func Test2_1ValidDocumentPassesValidation(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_1.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - // inverse relationship -- should also get detected - { - RefA: common.MakeDocElementID("", "p4"), - RefB: common.MakeDocElementID("", "DOCUMENT"), - Relationship: "DESCRIBED_BY", - }, - // different relationship - { - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - err := ValidateDocument2_1(doc) - if err != nil { - t.Fatalf("expected nil error, got: %s", err.Error()) - } -} - -func Test2_1InvalidDocumentFailsValidation(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - }, - Relationships: []*v2_1.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DESCRIBES", - }, - // invalid ID p99 - { - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p99"), - Relationship: "DEPENDS_ON", - }, - }, - } - - err := ValidateDocument2_1(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.2 tests ===== - -func Test2_2ValidDocumentPassesValidation(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_2.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - // inverse relationship -- should also get detected - { - RefA: common.MakeDocElementID("", "p4"), - RefB: common.MakeDocElementID("", "DOCUMENT"), - Relationship: "DESCRIBED_BY", - }, - // different relationship - { - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - }, - } - - err := ValidateDocument2_2(doc) - if err != nil { - t.Fatalf("expected nil error, got: %s", err.Error()) - } -} - -func Test2_2InvalidDocumentFailsValidation(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - }, - Relationships: []*v2_2.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - // invalid ID p99 - { - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p99"), - Relationship: "DEPENDS_ON", - }, - }, - } - - err := ValidateDocument2_2(doc) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.3 tests ===== - -func Test2_3ValidDocumentPassesValidation(t *testing.T) { +func TestValidDocumentPassesValidation(t *testing.T) { // set up document and some packages and relationships - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, }, - Relationships: []*v2_3.Relationship{ + Relationships: []*spdx.Relationship{ { RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), @@ -225,25 +49,25 @@ func Test2_3ValidDocumentPassesValidation(t *testing.T) { }, } - err := ValidateDocument2_3(doc) + err := ValidateDocument(doc) if err != nil { t.Fatalf("expected nil error, got: %s", err.Error()) } } -func Test2_3InvalidDocumentFailsValidation(t *testing.T) { +func TestInvalidDocumentFailsValidation(t *testing.T) { // set up document and some packages and relationships - doc := &v2_3.Document{ - SPDXVersion: "SPDX-2.3", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_3.CreationInfo{}, - Packages: []*v2_3.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, }, - Relationships: []*v2_3.Relationship{ + Relationships: []*spdx.Relationship{ { RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), @@ -263,7 +87,7 @@ func Test2_3InvalidDocumentFailsValidation(t *testing.T) { }, } - err := ValidateDocument2_3(doc) + err := ValidateDocument(doc) if err == nil { t.Fatalf("expected non-nil error, got nil") } diff --git a/spdxlib/element_ids.go b/spdxlib/element_ids.go index d505d40f..e3e9764e 100644 --- a/spdxlib/element_ids.go +++ b/spdxlib/element_ids.go @@ -5,7 +5,7 @@ package spdxlib import ( "sort" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) // SortElementIDs sorts and returns the given slice of ElementIDs diff --git a/spdxlib/element_ids_test.go b/spdxlib/element_ids_test.go index 83893ba5..1da0f169 100644 --- a/spdxlib/element_ids_test.go +++ b/spdxlib/element_ids_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/common" ) func TestSortElementIDs(t *testing.T) { diff --git a/spdxlib/relationships.go b/spdxlib/relationships.go index aa807d0a..7f5038db 100644 --- a/spdxlib/relationships.go +++ b/spdxlib/relationships.go @@ -3,43 +3,13 @@ package spdxlib import ( - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// FilterRelationships2_1 returns a slice of Element IDs returned by the given filter closure. The closure is passed +// FilterRelationships returns a slice of Element IDs returned by the given filter closure. The closure is passed // one relationship at a time, and it can return an ElementID or nil. -func FilterRelationships2_1(doc *v2_1.Document, filter func(*v2_1.Relationship) *common.ElementID) ([]common.ElementID, error) { - elementIDs := []common.ElementID{} - - for _, relationship := range doc.Relationships { - if id := filter(relationship); id != nil { - elementIDs = append(elementIDs, *id) - } - } - - return elementIDs, nil -} - -// FilterRelationships2_2 returns a slice of Element IDs returned by the given filter closure. The closure is passed -// one relationship at a time, and it can return an ElementID or nil. -func FilterRelationships2_2(doc *v2_2.Document, filter func(*v2_2.Relationship) *common.ElementID) ([]common.ElementID, error) { - elementIDs := []common.ElementID{} - - for _, relationship := range doc.Relationships { - if id := filter(relationship); id != nil { - elementIDs = append(elementIDs, *id) - } - } - - return elementIDs, nil -} - -// FilterRelationships2_3 returns a slice of Element IDs returned by the given filter closure. The closure is passed -// one relationship at a time, and it can return an ElementID or nil. -func FilterRelationships2_3(doc *v2_3.Document, filter func(*v2_3.Relationship) *common.ElementID) ([]common.ElementID, error) { +func FilterRelationships(doc *spdx.Document, filter func(*spdx.Relationship) *common.ElementID) ([]common.ElementID, error) { elementIDs := []common.ElementID{} for _, relationship := range doc.Relationships { diff --git a/spdxlib/relationships_test.go b/spdxlib/relationships_test.go index 57728877..496119d9 100644 --- a/spdxlib/relationships_test.go +++ b/spdxlib/relationships_test.go @@ -5,96 +5,25 @@ package spdxlib import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 tests ===== - -func Test2_1FilterForDependencies(t *testing.T) { - // set up document and some packages and relationships - doc := &v2_1.Document{ - SPDXVersion: "SPDX-2.1", - DataLicense: "CC0-1.0", - SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_1.CreationInfo{}, - Packages: []*v2_1.Package{ - {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, - {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, - {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, - {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, - {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, - }, - Relationships: []*v2_1.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p1"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "p5"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "p4"), - RefB: common.MakeDocElementID("", "DOCUMENT"), - Relationship: "DESCRIBED_BY", - }, - { - RefA: common.MakeDocElementID("", "p1"), - RefB: common.MakeDocElementID("", "p2"), - Relationship: "DEPENDS_ON", - }, - { - RefA: common.MakeDocElementID("", "p3"), - RefB: common.MakeDocElementID("", "p4"), - Relationship: "DEPENDENCY_OF", - }, - }, - } - - eIDs, err := FilterRelationships2_1(doc, func(relationship *v2_1.Relationship) *common.ElementID { - p1EID := common.MakeDocElementID("", "p1") - if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID { - return &relationship.RefB.ElementRefID - } else if relationship.Relationship == "DEPENDENCY_OF" && relationship.RefB == p1EID { - return &relationship.RefA.ElementRefID - } - - return nil - }) - if err != nil { - t.Fatalf("expected non-nil err, got: %s", err.Error()) - } - - if len(eIDs) != 1 { - t.Fatalf("expected 1 ElementID, got: %v", eIDs) - } - - if eIDs[0] != common.MakeDocElementID("", "p2").ElementRefID { - t.Fatalf("received unexpected relationship: %v", eIDs[0]) - } -} - -// ===== 2.2 tests ===== - -func Test2_2FindsDependsOnRelationships(t *testing.T) { +func TestFindsDependsOnRelationships(t *testing.T) { // set up document and some packages and relationships - doc := &v2_2.Document{ - SPDXVersion: "SPDX-2.2", - DataLicense: "CC0-1.0", + doc := &spdx.Document{ + SPDXVersion: spdx.Version, + DataLicense: spdx.DataLicense, SPDXIdentifier: common.ElementID("DOCUMENT"), - CreationInfo: &v2_2.CreationInfo{}, - Packages: []*v2_2.Package{ + CreationInfo: &spdx.CreationInfo{}, + Packages: []*spdx.Package{ {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, }, - Relationships: []*v2_2.Relationship{ + Relationships: []*spdx.Relationship{ { RefA: common.MakeDocElementID("", "DOCUMENT"), RefB: common.MakeDocElementID("", "p1"), @@ -120,7 +49,7 @@ func Test2_2FindsDependsOnRelationships(t *testing.T) { }, } - eIDs, err := FilterRelationships2_2(doc, func(relationship *v2_2.Relationship) *common.ElementID { + eIDs, err := FilterRelationships(doc, func(relationship *spdx.Relationship) *common.ElementID { p1EID := common.MakeDocElementID("", "p1") if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID { return &relationship.RefB.ElementRefID diff --git a/tagvalue/reader.go b/tagvalue/reader.go new file mode 100644 index 00000000..5bb400f1 --- /dev/null +++ b/tagvalue/reader.go @@ -0,0 +1,71 @@ +// Package tvloader is used to load and parse SPDX tag-value documents +// into tools-golang data structures. +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package tagvalue + +import ( + "fmt" + "io" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + v2_1_reader "github.com/spdx/tools-golang/spdx/v2/v2_1/tagvalue/reader" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + v2_2_reader "github.com/spdx/tools-golang/spdx/v2/v2_2/tagvalue/reader" + "github.com/spdx/tools-golang/spdx/v2/v2_3" + v2_3_reader "github.com/spdx/tools-golang/spdx/v2/v2_3/tagvalue/reader" + "github.com/spdx/tools-golang/tagvalue/reader" +) + +// Read takes an io.Reader and returns a fully-parsed current model SPDX Document +// or an error if any error is encountered. +func Read(content io.Reader) (*spdx.Document, error) { + doc := spdx.Document{} + err := ReadInto(content, &doc) + return &doc, err +} + +// ReadInto takes an io.Reader, reads in the SPDX document at the version provided +// and converts to the doc version +func ReadInto(content io.Reader, doc common.AnyDocument) error { + if !convert.IsPtr(doc) { + return fmt.Errorf("doc to read into must be a pointer") + } + + tvPairs, err := reader.ReadTagValues(content) + if err != nil { + return err + } + + if len(tvPairs) == 0 { + return fmt.Errorf("no tag values found") + } + + version := "" + for _, pair := range tvPairs { + if pair.Tag == "SPDXVersion" { + version = pair.Value + break + } + } + + var data interface{} + switch version { + case v2_1.Version: + data, err = v2_1_reader.ParseTagValues(tvPairs) + case v2_2.Version: + data, err = v2_2_reader.ParseTagValues(tvPairs) + case v2_3.Version: + data, err = v2_3_reader.ParseTagValues(tvPairs) + default: + return fmt.Errorf("unsupported SPDX version: '%v'", version) + } + + if err != nil { + return err + } + + return convert.Document(data.(common.AnyDocument), doc) +} diff --git a/tvloader/reader/reader.go b/tagvalue/reader/reader.go similarity index 100% rename from tvloader/reader/reader.go rename to tagvalue/reader/reader.go diff --git a/tvloader/reader/reader_test.go b/tagvalue/reader/reader_test.go similarity index 100% rename from tvloader/reader/reader_test.go rename to tagvalue/reader/reader_test.go diff --git a/tagvalue/writer.go b/tagvalue/writer.go new file mode 100644 index 00000000..d77bd3a9 --- /dev/null +++ b/tagvalue/writer.go @@ -0,0 +1,34 @@ +// Package tvsaver is used to save tools-golang data structures +// as SPDX tag-value documents. +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package tagvalue + +import ( + "fmt" + "io" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + v2_1_writer "github.com/spdx/tools-golang/spdx/v2/v2_1/tagvalue/writer" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + v2_2_writer "github.com/spdx/tools-golang/spdx/v2/v2_2/tagvalue/writer" + "github.com/spdx/tools-golang/spdx/v2/v2_3" + v2_3_writer "github.com/spdx/tools-golang/spdx/v2/v2_3/tagvalue/writer" +) + +// Write takes an io.Writer and an SPDX Document, +// and writes it to the writer in tag-value format. It returns error +// if any error is encountered. +func Write(doc common.AnyDocument, w io.Writer) error { + doc = convert.FromPtr(doc) + switch doc := doc.(type) { + case v2_1.Document: + return v2_1_writer.RenderDocument(&doc, w) + case v2_2.Document: + return v2_2_writer.RenderDocument(&doc, w) + case v2_3.Document: + return v2_3_writer.RenderDocument(&doc, w) + } + return fmt.Errorf("unsupported document type: %s", convert.Describe(doc)) +} diff --git a/test/v2_3/tv_test.go b/test/v2_3/tv_test.go deleted file mode 100644 index 9be0459b..00000000 --- a/test/v2_3/tv_test.go +++ /dev/null @@ -1,491 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package v2_3 - -import ( - "bytes" - "flag" - "fmt" - "os" - "testing" - - "github.com/google/go-cmp/cmp" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" - "github.com/spdx/tools-golang/tvloader" - "github.com/spdx/tools-golang/tvsaver" -) - -var update = *flag.Bool("update-snapshots", false, "update the example snapshot") - -func TestLoad(t *testing.T) { - fileName := "../../examples/sample-docs/tv/SPDXTagExample-v2.3.spdx" - - if update { - f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - t.Errorf("unable to open file to write SPDX 2.3 example: %v", err) - } - err = tvsaver.Save2_3(&want, f) - if err != nil { - t.Errorf("unable to save SPDX 2.3 example: %v", err) - } - } - - file, err := os.Open(fileName) - if err != nil { - panic(fmt.Errorf("error opening File: %s", err)) - } - - got, err := tvloader.Load2_3(file) - if err != nil { - t.Errorf("Load2_3() error = %v", err) - return - } - - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want - - if cmp.Equal(handwrittenExample, got) { - t.Errorf("Got incorrect struct after parsing example") - return - } -} - -func TestWrite(t *testing.T) { - w := &bytes.Buffer{} - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want - if err := tvsaver.Save2_3(&handwrittenExample, w); err != nil { - t.Errorf("Save2_3() error = %v", err.Error()) - return - } - - // we should be able to parse what the writer wrote, and it should be identical to the original struct we wrote - parsedDoc, err := tvloader.Load2_3(bytes.NewReader(w.Bytes())) - if err != nil { - t.Errorf("failed to parse written document: %v", err.Error()) - return - } - - if cmp.Equal(handwrittenExample, parsedDoc) { - t.Errorf("Got incorrect struct after writing and re-parsing example") - return - } -} - -// want is handwritten translation of the official example SPDX v2.3 document into a Go struct. -// We expect that the result of parsing the official document should be this value. -// We expect that the result of writing this struct should match the official example document. -var want = v2_3.Document{ - DataLicense: "CC0-1.0", - SPDXVersion: "SPDX-2.3", - SPDXIdentifier: "SPDXRef-DOCUMENT", - DocumentName: "SPDX-Tools-v2.0", - DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", - CreationInfo: &v2_3.CreationInfo{ - LicenseListVersion: "3.9", - Creators: []common.Creator{ - {CreatorType: "Tool", Creator: "LicenseFind-1.0"}, - {CreatorType: "Organization", Creator: "ExampleCodeInspect ()"}, - {CreatorType: "Person", Creator: "Jane Doe ()"}, - }, - Created: "2010-01-29T18:30:22Z", - CreatorComment: "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.", - }, - DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.", - ExternalDocumentReferences: []v2_3.ExternalDocumentRef{ - { - DocumentRefID: "DocumentRef-spdx-tool-1.2", - URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301", - Checksum: common.Checksum{ - Algorithm: common.SHA1, - Value: "d6a770ba38583ed4bb4525bd96e50461655d2759", - }, - }, - }, - OtherLicenses: []*v2_3.OtherLicense{ - { - LicenseIdentifier: "LicenseRef-1", - ExtractedText: "/*\n * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-2", - ExtractedText: "This package includes the GRDDL parser developed by Hewlett Packard under the following license:\n� Copyright 2007 Hewlett-Packard Development Company, LP\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: \n\nRedistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. \nRedistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. \nThe name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. \nTHIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - }, - { - LicenseIdentifier: "LicenseRef-4", - ExtractedText: "/*\n * (c) Copyright 2009 University of Bristol\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-Beerware-4.2", - ExtractedText: "\"THE BEER-WARE LICENSE\" (Revision 42):\nphk@FreeBSD.ORG wrote this file. As long as you retain this notice you\ncan do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp", - LicenseComment: "The beerware license has a couple of other standard variants.", - LicenseName: "Beer-Ware License (Version 42)", - LicenseCrossReferences: []string{"http://people.freebsd.org/~phk/"}, - }, - { - LicenseIdentifier: "LicenseRef-3", - ExtractedText: "The CyberNeko Software License, Version 1.0\n\n \n(C) Copyright 2002-2005, Andy Clark. All rights reserved.\n \nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions\nare met:\n\n1. Redistributions of source code must retain the above copyright\n notice, this list of conditions and the following disclaimer. \n\n2. Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in\n the documentation and/or other materials provided with the\n distribution.\n\n3. The end-user documentation included with the redistribution,\n if any, must include the following acknowledgment: \n \"This product includes software developed by Andy Clark.\"\n Alternately, this acknowledgment may appear in the software itself,\n if and wherever such third-party acknowledgments normally appear.\n\n4. The names \"CyberNeko\" and \"NekoHTML\" must not be used to endorse\n or promote products derived from this software without prior \n written permission. For written permission, please contact \n andyc@cyberneko.net.\n\n5. Products derived from this software may not be called \"CyberNeko\",\n nor may \"CyberNeko\" appear in their name, without prior written\n permission of the author.\n\nTHIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\nOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS\nBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, \nOR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT \nOF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR \nBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \nWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE \nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, \nEVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - LicenseName: "CyberNeko License", - LicenseCrossReferences: []string{ - "http://people.apache.org/~andyc/neko/LICENSE", - "http://justasample.url.com", - }, - LicenseComment: "This is tye CyperNeko License", - }, - }, - Annotations: []*v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Jane Doe ()", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Document level annotation", - }, - { - Annotator: common.Annotator{ - Annotator: "Joe Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-02-10T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", - }, - { - Annotator: common.Annotator{ - Annotator: "Suzanne Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-03-13T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "Another example reviewer.", - }, - }, - Packages: []*v2_3.Package{ - { - PackageName: "glibc", - PackageSPDXIdentifier: "SPDXRef-Package", - PackageVersion: "2.11.1", - PackageFileName: "glibc-2.11.1.tar.gz", - PackageSupplier: &common.Supplier{ - Supplier: "Jane Doe (jane.doe@example.com)", - SupplierType: "Person", - }, - PackageOriginator: &common.Originator{ - Originator: "ExampleCodeInspect (contact@example.com)", - OriginatorType: "Organization", - }, - PackageDownloadLocation: "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz", - FilesAnalyzed: true, - PackageVerificationCode: &common.PackageVerificationCode{ - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - ExcludedFiles: []string{"./package.spdx"}, - }, - PackageChecksums: []common.Checksum{ - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - { - Algorithm: "SHA256", - Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd", - }, - }, - PackageHomePage: "http://ftp.gnu.org/gnu/glibc", - PackageSourceInfo: "uses glibc-2_11-branch from git://sourceware.org/git/glibc.git.", - PackageLicenseConcluded: "(LGPL-2.0-only OR LicenseRef-3)", - PackageLicenseInfoFromFiles: []string{ - "GPL-2.0-only", - "LicenseRef-2", - "LicenseRef-1", - }, - PackageLicenseDeclared: "(LGPL-2.0-only AND LicenseRef-3)", - PackageLicenseComments: "The license for this project changed with the release of version x.y. The version of the project included here post-dates the license change.", - PackageCopyrightText: "Copyright 2008-2010 John Smith", - PackageSummary: "GNU C library.", - PackageDescription: "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.", - PackageComment: "", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "SECURITY", - RefType: "cpe23Type", - Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", - }, - { - Category: "OTHER", - RefType: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge", - Locator: "acmecorp/acmenator/4.1.3-alpha", - ExternalRefComment: "This is the external ref for Acme", - }, - }, - PackageAttributionTexts: []string{ - "The GNU C Library is free software. See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed. License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually.", - }, - Files: nil, - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Package Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Package level annotation", - }, - }, - }, - { - PackageSPDXIdentifier: "SPDXRef-fromDoap-1", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "http://commons.apache.org/proper/commons-lang/", - PackageLicenseDeclared: "NOASSERTION", - PackageName: "Apache Commons Lang", - }, - { - PackageName: "Jena", - PackageSPDXIdentifier: "SPDXRef-fromDoap-0", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "PACKAGE-MANAGER", - RefType: "purl", - Locator: "pkg:maven/org.apache.jena/apache-jena@3.12.0", - }, - }, - FilesAnalyzed: false, - PackageHomePage: "http://www.openjena.org/", - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseDeclared: "NOASSERTION", - PackageVersion: "3.12.0", - }, - { - PackageSPDXIdentifier: "SPDXRef-Saxon", - PackageChecksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - }, - PackageCopyrightText: "Copyright Saxonica Ltd", - PackageDescription: "The Saxon package is a collection of tools for processing XML documents.", - PackageDownloadLocation: "https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download", - FilesAnalyzed: false, - PackageHomePage: "http://saxon.sourceforge.net/", - PackageLicenseComments: "Other versions available for a commercial license", - PackageLicenseConcluded: "MPL-1.0", - PackageLicenseDeclared: "MPL-1.0", - PackageName: "Saxon", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "8.8", - }, - { - PrimaryPackagePurpose: "CONTAINER", - PackageSPDXIdentifier: "SPDXRef-CentOS-7", - PackageCopyrightText: "NOASSERTION", - PackageDescription: "The CentOS container used to run the application.", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "https://www.centos.org/", - PackageName: "centos", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "centos7.9.2009", - BuiltDate: "2021-09-15T02:38:00Z", - ValidUntilDate: "2022-10-15T02:38:00Z", - ReleaseDate: "2021-10-15T02:38:00Z", - }, - }, - Files: []*v2_3.File{ - { - FileName: "./src/org/spdx/parser/DOAPProject.java", - FileSPDXIdentifier: "SPDXRef-DoapSource", - FileTypes: []string{ - "SOURCE", - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", - }, - }, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "Apache-2.0", - }, - FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.", - FileContributors: []string{ - "Protecode Inc.", - "SPDX Technical Team Members", - "Open Logic Inc.", - "Source Auditor Inc.", - "Black Duck Software In.c", - }, - }, - { - FileSPDXIdentifier: "SPDXRef-CommonsLangSrc", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "c2b4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file is used by Jena", - FileCopyrightText: "Copyright 2001-2011 The Apache Software Foundation", - FileContributors: []string{"Apache Software Foundation"}, - FileName: "./lib-source/commons-lang3-3.1-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{"Apache-2.0"}, - FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())", - }, - { - FileSPDXIdentifier: "SPDXRef-JenaLib", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "3ab4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file belongs to Jena", - FileCopyrightText: "(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP", - FileContributors: []string{"Apache Software Foundation", "Hewlett Packard Inc."}, - FileName: "./lib-source/jena-2.6.3-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseComments: "This license is used by Jena", - LicenseConcluded: "LicenseRef-1", - LicenseInfoInFiles: []string{"LicenseRef-1"}, - }, - { - FileSPDXIdentifier: "SPDXRef-File", - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "File Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "File level annotation", - }, - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - }, - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - }, - FileComment: "The concluded license was taken from the package level that the file was included in.\nThis information was found in the COPYING.txt file in the xyz directory.", - FileCopyrightText: "Copyright 2008-2010 John Smith", - FileContributors: []string{"The Regents of the University of California", "Modified by Paul Mundt lethal@linux-sh.org", "IBM Corporation"}, - FileName: "./package/foo.c", - FileTypes: []string{"SOURCE"}, - LicenseComments: "The concluded license was taken from the package level that the file was included in.", - LicenseConcluded: "(LGPL-2.0-only OR LicenseRef-2)", - LicenseInfoInFiles: []string{"GPL-2.0-only", "LicenseRef-2"}, - FileNotice: "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", - }, - }, - Snippets: []v2_3.Snippet{ - { - SnippetSPDXIdentifier: "SPDXRef-Snippet", - SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource", - Ranges: []common.SnippetRange{ - { - StartPointer: common.SnippetRangePointer{ - Offset: 310, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - Offset: 420, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - { - StartPointer: common.SnippetRangePointer{ - LineNumber: 5, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - LineNumber: 23, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - }, - SnippetLicenseConcluded: "GPL-2.0-only", - LicenseInfoInSnippet: []string{"GPL-2.0-only"}, - SnippetLicenseComments: "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.", - SnippetCopyrightText: "Copyright 2008-2010 John Smith", - SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.", - SnippetName: "from linux kernel", - }, - }, - Relationships: []*v2_3.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("spdx-tool-1.2", "ToolsElement"), - Relationship: "COPY_OF", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "File"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "JenaLib"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "Saxon"), - Relationship: "DYNAMIC_LINK", - }, - { - RefA: common.MakeDocElementID("", "CommonsLangSrc"), - RefB: common.MakeDocElementSpecial("NOASSERTION"), - Relationship: "GENERATED_FROM", - }, - { - RefA: common.MakeDocElementID("", "JenaLib"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "File"), - RefB: common.MakeDocElementID("", "fromDoap-0"), - Relationship: "GENERATED_FROM", - }, - }, - Reviews: []*v2_3.Review{ - { - Reviewer: "joe@example.com", - ReviewerType: "Person", - ReviewDate: "2021-11-03T05:43:21Z", - ReviewComment: "This is a review comment", - }, - }, -} diff --git a/tvloader/parser2v1/parse_annotation_test.go b/tvloader/parser2v1/parse_annotation_test.go deleted file mode 100644 index 41fe6a5a..00000000 --- a/tvloader/parser2v1/parse_annotation_test.go +++ /dev/null @@ -1,158 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_1" -) - -// ===== Annotation section tests ===== -func TestParser2_1FailsIfAnnotationNotSet(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePairForAnnotation2_1("Annotator", "Person: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected error when calling parsePairFromAnnotation2_1 without setting ann pointer") - } -} - -func TestParser2_1FailsIfAnnotationTagUnknown(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - // start with valid annotator - err := parser.parsePair2_1("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // parse invalid tag, using parsePairForAnnotation2_1( - err = parser.parsePairForAnnotation2_1("blah", "oops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_1FailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePair2_1("AnnotationDate", "2018-09-15T17:25:00Z") - if err == nil { - t.Errorf("expected error when calling parsePair2_1 for AnnotationDate without Annotator first") - } - err = parser.parsePair2_1("AnnotationType", "REVIEW") - if err == nil { - t.Errorf("expected error when calling parsePair2_1 for AnnotationType without Annotator first") - } - err = parser.parsePair2_1("SPDXREF", "SPDXRef-45") - if err == nil { - t.Errorf("expected error when calling parsePair2_1 for SPDXREF without Annotator first") - } - err = parser.parsePair2_1("AnnotationComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_1 for AnnotationComment without Annotator first") - } -} - -func TestParser2_1CanParseAnnotationTags(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // Annotator without email address - err := parser.parsePair2_1("Annotator", "Person: John Doe") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.Annotator.Annotator != "John Doe" { - t.Errorf("got %v for Annotator, expected John Doe", parser.ann.Annotator) - } - if parser.ann.Annotator.AnnotatorType != "Person" { - t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) - } - - // Annotation Date - dt := "2018-09-15T17:32:00Z" - err = parser.parsePair2_1("AnnotationDate", dt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationDate != dt { - t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) - } - - // Annotation type - aType := "REVIEW" - err = parser.parsePair2_1("AnnotationType", aType) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationType != aType { - t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) - } - - // SPDX Identifier Reference - ref := "SPDXRef-30" - err = parser.parsePair2_1("SPDXREF", ref) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - deID := parser.ann.AnnotationSPDXIdentifier - if deID.DocumentRefID != "" || deID.ElementRefID != "30" { - t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") - } - - // Annotation Comment - cmt := "this is a comment" - err = parser.parsePair2_1("AnnotationComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationComment != cmt { - t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) - } -} - -func TestParser2_1FailsIfAnnotatorInvalid(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePair2_1("Annotator", "John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_1FailsIfAnnotatorTypeInvalid(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePair2_1("Annotator", "Human: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_1FailsIfAnnotationRefInvalid(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - // start with valid annotator - err := parser.parsePair2_1("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - err = parser.parsePair2_1("SPDXREF", "blah:other") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v1/parse_relationship_test.go b/tvloader/parser2v1/parse_relationship_test.go deleted file mode 100644 index 6d443886..00000000 --- a/tvloader/parser2v1/parse_relationship_test.go +++ /dev/null @@ -1,171 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v1 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_1" -) - -// ===== Relationship section tests ===== -func TestParser2_1FailsIfRelationshipNotSet(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePairForRelationship2_1("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") - if err == nil { - t.Errorf("expected error when calling parsePairFromRelationship2_1 without setting rln pointer") - } -} - -func TestParser2_1FailsIfRelationshipCommentWithoutRelationship(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - err := parser.parsePair2_1("RelationshipComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_1 for RelationshipComment without Relationship first") - } -} - -func TestParser2_1CanParseRelationshipTags(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // Relationship - err := parser.parsePair2_1("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { - t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) - } - if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { - t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) - } - if parser.rln.Relationship != "CONTAINS" { - t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) - } - - // Relationship Comment - cmt := "this is a comment" - err = parser.parsePair2_1("RelationshipComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RelationshipComment != cmt { - t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) - } -} - -func TestParser2_1InvalidRelationshipTagsNoValueFail(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // no items - parser.rln = nil - err := parser.parsePair2_1("Relationship", "") - if err == nil { - t.Errorf("expected error for empty items in relationship, got nil") - } -} - -func TestParser2_1InvalidRelationshipTagsOneValueFail(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // one item - parser.rln = nil - err := parser.parsePair2_1("Relationship", "DESCRIBES") - if err == nil { - t.Errorf("expected error for only one item in relationship, got nil") - } -} - -func TestParser2_1InvalidRelationshipTagsTwoValuesFail(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // two items - parser.rln = nil - err := parser.parsePair2_1("Relationship", "SPDXRef-DOCUMENT DESCRIBES") - if err == nil { - t.Errorf("expected error for only two items in relationship, got nil") - } -} - -func TestParser2_1InvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // three items but with interspersed additional whitespace - parser.rln = nil - err := parser.parsePair2_1("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") - if err != nil { - t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) - } -} - -func TestParser2_1InvalidRelationshipTagsFourValuesFail(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // four items - parser.rln = nil - err := parser.parsePair2_1("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") - if err == nil { - t.Errorf("expected error for more than three items in relationship, got nil") - } -} - -func TestParser2_1InvalidRelationshipTagsInvalidRefIDs(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // four items - parser.rln = nil - err := parser.parsePair2_1("Relationship", "SPDXRef-a DESCRIBES b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } - - parser.rln = nil - err = parser.parsePair2_1("Relationship", "a DESCRIBES SPDXRef-b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } -} - -func TestParser2_1FailsToParseUnknownTagInRelationshipSection(t *testing.T) { - parser := tvParser2_1{ - doc: &v2_1.Document{}, - st: psCreationInfo2_1, - } - - // Relationship - err := parser.parsePair2_1("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // invalid tag - err = parser.parsePairForRelationship2_1("blah", "whoops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v2/parse_annotation_test.go b/tvloader/parser2v2/parse_annotation_test.go deleted file mode 100644 index c2ba4875..00000000 --- a/tvloader/parser2v2/parse_annotation_test.go +++ /dev/null @@ -1,158 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_2" -) - -// ===== Annotation section tests ===== -func TestParser2_2FailsIfAnnotationNotSet(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePairForAnnotation2_2("Annotator", "Person: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected error when calling parsePairFromAnnotation2_2 without setting ann pointer") - } -} - -func TestParser2_2FailsIfAnnotationTagUnknown(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - // start with valid annotator - err := parser.parsePair2_2("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // parse invalid tag, using parsePairForAnnotation2_2( - err = parser.parsePairForAnnotation2_2("blah", "oops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_2FailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePair2_2("AnnotationDate", "2018-09-15T17:25:00Z") - if err == nil { - t.Errorf("expected error when calling parsePair2_2 for AnnotationDate without Annotator first") - } - err = parser.parsePair2_2("AnnotationType", "REVIEW") - if err == nil { - t.Errorf("expected error when calling parsePair2_2 for AnnotationType without Annotator first") - } - err = parser.parsePair2_2("SPDXREF", "SPDXRef-45") - if err == nil { - t.Errorf("expected error when calling parsePair2_2 for SPDXREF without Annotator first") - } - err = parser.parsePair2_2("AnnotationComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_2 for AnnotationComment without Annotator first") - } -} - -func TestParser2_2CanParseAnnotationTags(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // Annotator without email address - err := parser.parsePair2_2("Annotator", "Person: John Doe") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.Annotator.Annotator != "John Doe" { - t.Errorf("got %+v for Annotator, expected John Doe", parser.ann.Annotator.Annotator) - } - if parser.ann.Annotator.AnnotatorType != "Person" { - t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) - } - - // Annotation Date - dt := "2018-09-15T17:32:00Z" - err = parser.parsePair2_2("AnnotationDate", dt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationDate != dt { - t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) - } - - // Annotation type - aType := "REVIEW" - err = parser.parsePair2_2("AnnotationType", aType) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationType != aType { - t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) - } - - // SPDX Identifier Reference - ref := "SPDXRef-30" - err = parser.parsePair2_2("SPDXREF", ref) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - deID := parser.ann.AnnotationSPDXIdentifier - if deID.DocumentRefID != "" || deID.ElementRefID != "30" { - t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") - } - - // Annotation Comment - cmt := "this is a comment" - err = parser.parsePair2_2("AnnotationComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationComment != cmt { - t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) - } -} - -func TestParser2_2FailsIfAnnotatorInvalid(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePair2_2("Annotator", "John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_2FailsIfAnnotatorTypeInvalid(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePair2_2("Annotator", "Human: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_2FailsIfAnnotationRefInvalid(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - // start with valid annotator - err := parser.parsePair2_2("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - err = parser.parsePair2_2("SPDXREF", "blah:other") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v2/parse_relationship_test.go b/tvloader/parser2v2/parse_relationship_test.go deleted file mode 100644 index 48285fc1..00000000 --- a/tvloader/parser2v2/parse_relationship_test.go +++ /dev/null @@ -1,202 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v2 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_2" -) - -// ===== Relationship section tests ===== -func TestParser2_2FailsIfRelationshipNotSet(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePairForRelationship2_2("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") - if err == nil { - t.Errorf("expected error when calling parsePairFromRelationship2_2 without setting rln pointer") - } -} - -func TestParser2_2FailsIfRelationshipCommentWithoutRelationship(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - err := parser.parsePair2_2("RelationshipComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_2 for RelationshipComment without Relationship first") - } -} - -func TestParser2_2CanParseRelationshipTags(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // Relationship - err := parser.parsePair2_2("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { - t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) - } - if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { - t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) - } - if parser.rln.Relationship != "CONTAINS" { - t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) - } - - // Relationship Comment - cmt := "this is a comment" - err = parser.parsePair2_2("RelationshipComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RelationshipComment != cmt { - t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) - } -} - -func TestParser2_2InvalidRelationshipTagsNoValueFail(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // no items - parser.rln = nil - err := parser.parsePair2_2("Relationship", "") - if err == nil { - t.Errorf("expected error for empty items in relationship, got nil") - } -} - -func TestParser2_2InvalidRelationshipTagsOneValueFail(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // one item - parser.rln = nil - err := parser.parsePair2_2("Relationship", "DESCRIBES") - if err == nil { - t.Errorf("expected error for only one item in relationship, got nil") - } -} - -func TestParser2_2InvalidRelationshipTagsTwoValuesFail(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // two items - parser.rln = nil - err := parser.parsePair2_2("Relationship", "SPDXRef-DOCUMENT DESCRIBES") - if err == nil { - t.Errorf("expected error for only two items in relationship, got nil") - } -} - -func TestParser2_2InvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // three items but with interspersed additional whitespace - parser.rln = nil - err := parser.parsePair2_2("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") - if err != nil { - t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) - } -} - -func TestParser2_2InvalidRelationshipTagsFourValuesFail(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // four items - parser.rln = nil - err := parser.parsePair2_2("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") - if err == nil { - t.Errorf("expected error for more than three items in relationship, got nil") - } -} - -func TestParser2_2InvalidRelationshipTagsInvalidRefIDs(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // four items - parser.rln = nil - err := parser.parsePair2_2("Relationship", "SPDXRef-a DESCRIBES b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } - - parser.rln = nil - err = parser.parsePair2_2("Relationship", "a DESCRIBES SPDXRef-b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } -} - -func TestParser2_2SpecialValuesValidForRightSideOfRelationship(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // NONE in right side of relationship should pass - err := parser.parsePair2_2("Relationship", "SPDXRef-a CONTAINS NONE") - if err != nil { - t.Errorf("expected nil error for CONTAINS NONE, got %v", err) - } - - // NOASSERTION in right side of relationship should pass - err = parser.parsePair2_2("Relationship", "SPDXRef-a CONTAINS NOASSERTION") - if err != nil { - t.Errorf("expected nil error for CONTAINS NOASSERTION, got %v", err) - } - - // NONE in left side of relationship should fail - err = parser.parsePair2_2("Relationship", "NONE CONTAINS SPDXRef-a") - if err == nil { - t.Errorf("expected non-nil error for NONE CONTAINS, got nil") - } - - // NOASSERTION in left side of relationship should fail - err = parser.parsePair2_2("Relationship", "NOASSERTION CONTAINS SPDXRef-a") - if err == nil { - t.Errorf("expected non-nil error for NOASSERTION CONTAINS, got nil") - } -} - -func TestParser2_2FailsToParseUnknownTagInRelationshipSection(t *testing.T) { - parser := tvParser2_2{ - doc: &v2_2.Document{}, - st: psCreationInfo2_2, - } - - // Relationship - err := parser.parsePair2_2("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // invalid tag - err = parser.parsePairForRelationship2_2("blah", "whoops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v3/parse_annotation_test.go b/tvloader/parser2v3/parse_annotation_test.go deleted file mode 100644 index 6681ed55..00000000 --- a/tvloader/parser2v3/parse_annotation_test.go +++ /dev/null @@ -1,158 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_3" -) - -// ===== Annotation section tests ===== -func TestParser2_3FailsIfAnnotationNotSet(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePairForAnnotation2_3("Annotator", "Person: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected error when calling parsePairFromAnnotation2_3 without setting ann pointer") - } -} - -func TestParser2_3FailsIfAnnotationTagUnknown(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - // start with valid annotator - err := parser.parsePair2_3("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // parse invalid tag, using parsePairForAnnotation2_3( - err = parser.parsePairForAnnotation2_3("blah", "oops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_3FailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePair2_3("AnnotationDate", "2018-09-15T17:25:00Z") - if err == nil { - t.Errorf("expected error when calling parsePair2_3 for AnnotationDate without Annotator first") - } - err = parser.parsePair2_3("AnnotationType", "REVIEW") - if err == nil { - t.Errorf("expected error when calling parsePair2_3 for AnnotationType without Annotator first") - } - err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") - if err == nil { - t.Errorf("expected error when calling parsePair2_3 for SPDXREF without Annotator first") - } - err = parser.parsePair2_3("AnnotationComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_3 for AnnotationComment without Annotator first") - } -} - -func TestParser2_3CanParseAnnotationTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // Annotator without email address - err := parser.parsePair2_3("Annotator", "Person: John Doe") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.Annotator.Annotator != "John Doe" { - t.Errorf("got %+v for Annotator, expected John Doe", parser.ann.Annotator.Annotator) - } - if parser.ann.Annotator.AnnotatorType != "Person" { - t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) - } - - // Annotation Date - dt := "2018-09-15T17:32:00Z" - err = parser.parsePair2_3("AnnotationDate", dt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationDate != dt { - t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) - } - - // Annotation type - aType := "REVIEW" - err = parser.parsePair2_3("AnnotationType", aType) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationType != aType { - t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) - } - - // SPDX Identifier Reference - ref := "SPDXRef-30" - err = parser.parsePair2_3("SPDXREF", ref) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - deID := parser.ann.AnnotationSPDXIdentifier - if deID.DocumentRefID != "" || deID.ElementRefID != "30" { - t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") - } - - // Annotation Comment - cmt := "this is a comment" - err = parser.parsePair2_3("AnnotationComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.ann.AnnotationComment != cmt { - t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) - } -} - -func TestParser2_3FailsIfAnnotatorInvalid(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePair2_3("Annotator", "John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_3FailsIfAnnotatorTypeInvalid(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePair2_3("Annotator", "Human: John Doe (jdoe@example.com)") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} - -func TestParser2_3FailsIfAnnotationRefInvalid(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - // start with valid annotator - err := parser.parsePair2_3("Annotator", "Person: John Doe (jdoe@example.com)") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - err = parser.parsePair2_3("SPDXREF", "blah:other") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v3/parse_relationship_test.go b/tvloader/parser2v3/parse_relationship_test.go deleted file mode 100644 index 57714d17..00000000 --- a/tvloader/parser2v3/parse_relationship_test.go +++ /dev/null @@ -1,202 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package parser2v3 - -import ( - "testing" - - "github.com/spdx/tools-golang/spdx/v2_3" -) - -// ===== Relationship section tests ===== -func TestParser2_3FailsIfRelationshipNotSet(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePairForRelationship2_3("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") - if err == nil { - t.Errorf("expected error when calling parsePairFromRelationship2_3 without setting rln pointer") - } -} - -func TestParser2_3FailsIfRelationshipCommentWithoutRelationship(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - err := parser.parsePair2_3("RelationshipComment", "comment whatever") - if err == nil { - t.Errorf("expected error when calling parsePair2_3 for RelationshipComment without Relationship first") - } -} - -func TestParser2_3CanParseRelationshipTags(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // Relationship - err := parser.parsePair2_3("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { - t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) - } - if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { - t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) - } - if parser.rln.Relationship != "CONTAINS" { - t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) - } - - // Relationship Comment - cmt := "this is a comment" - err = parser.parsePair2_3("RelationshipComment", cmt) - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - if parser.rln.RelationshipComment != cmt { - t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) - } -} - -func TestParser2_3InvalidRelationshipTagsNoValueFail(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // no items - parser.rln = nil - err := parser.parsePair2_3("Relationship", "") - if err == nil { - t.Errorf("expected error for empty items in relationship, got nil") - } -} - -func TestParser2_3InvalidRelationshipTagsOneValueFail(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // one item - parser.rln = nil - err := parser.parsePair2_3("Relationship", "DESCRIBES") - if err == nil { - t.Errorf("expected error for only one item in relationship, got nil") - } -} - -func TestParser2_3InvalidRelationshipTagsTwoValuesFail(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // two items - parser.rln = nil - err := parser.parsePair2_3("Relationship", "SPDXRef-DOCUMENT DESCRIBES") - if err == nil { - t.Errorf("expected error for only two items in relationship, got nil") - } -} - -func TestParser2_3InvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // three items but with interspersed additional whitespace - parser.rln = nil - err := parser.parsePair2_3("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") - if err != nil { - t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) - } -} - -func TestParser2_3InvalidRelationshipTagsFourValuesFail(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // four items - parser.rln = nil - err := parser.parsePair2_3("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") - if err == nil { - t.Errorf("expected error for more than three items in relationship, got nil") - } -} - -func TestParser2_3InvalidRelationshipTagsInvalidRefIDs(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // four items - parser.rln = nil - err := parser.parsePair2_3("Relationship", "SPDXRef-a DESCRIBES b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } - - parser.rln = nil - err = parser.parsePair2_3("Relationship", "a DESCRIBES SPDXRef-b") - if err == nil { - t.Errorf("expected error for missing SPDXRef- prefix, got nil") - } -} - -func TestParser2_3SpecialValuesValidForRightSideOfRelationship(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // NONE in right side of relationship should pass - err := parser.parsePair2_3("Relationship", "SPDXRef-a CONTAINS NONE") - if err != nil { - t.Errorf("expected nil error for CONTAINS NONE, got %v", err) - } - - // NOASSERTION in right side of relationship should pass - err = parser.parsePair2_3("Relationship", "SPDXRef-a CONTAINS NOASSERTION") - if err != nil { - t.Errorf("expected nil error for CONTAINS NOASSERTION, got %v", err) - } - - // NONE in left side of relationship should fail - err = parser.parsePair2_3("Relationship", "NONE CONTAINS SPDXRef-a") - if err == nil { - t.Errorf("expected non-nil error for NONE CONTAINS, got nil") - } - - // NOASSERTION in left side of relationship should fail - err = parser.parsePair2_3("Relationship", "NOASSERTION CONTAINS SPDXRef-a") - if err == nil { - t.Errorf("expected non-nil error for NOASSERTION CONTAINS, got nil") - } -} - -func TestParser2_3FailsToParseUnknownTagInRelationshipSection(t *testing.T) { - parser := tvParser2_3{ - doc: &v2_3.Document{}, - st: psCreationInfo2_3, - } - - // Relationship - err := parser.parsePair2_3("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") - if err != nil { - t.Errorf("expected nil error, got %v", err) - } - // invalid tag - err = parser.parsePairForRelationship2_3("blah", "whoops") - if err == nil { - t.Errorf("expected non-nil error, got nil") - } -} diff --git a/tvloader/parser2v3/types.go b/tvloader/parser2v3/types.go deleted file mode 100644 index b6dc3ab4..00000000 --- a/tvloader/parser2v3/types.go +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package parser2v3 - -import ( - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -type tvParser2_3 struct { - // document into which data is being parsed - doc *v2_3.Document - - // current parser state - st tvParserState2_3 - - // current SPDX item being filled in, if any - pkg *v2_3.Package - pkgExtRef *v2_3.PackageExternalReference - file *v2_3.File - fileAOP *v2_3.ArtifactOfProject - snippet *v2_3.Snippet - otherLic *v2_3.OtherLicense - rln *v2_3.Relationship - ann *v2_3.Annotation - rev *v2_3.Review - // don't need creation info pointer b/c only one, - // and we can get to it via doc.CreationInfo -} - -// parser state (SPDX document version 2.3) -type tvParserState2_3 int - -const ( - // at beginning of document - psStart2_3 tvParserState2_3 = iota - - // in document creation info section - psCreationInfo2_3 - - // in package data section - psPackage2_3 - - // in file data section (including "unpackaged" files) - psFile2_3 - - // in snippet data section (including "unpackaged" files) - psSnippet2_3 - - // in other license section - psOtherLicense2_3 - - // in review section - psReview2_3 -) - -const nullSpdxElementId2_3 = common.ElementID("") diff --git a/tvloader/tvloader.go b/tvloader/tvloader.go deleted file mode 100644 index ece2c2b2..00000000 --- a/tvloader/tvloader.go +++ /dev/null @@ -1,64 +0,0 @@ -// Package tvloader is used to load and parse SPDX tag-value documents -// into tools-golang data structures. -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package tvloader - -import ( - "github.com/spdx/tools-golang/spdx/v2_3" - "github.com/spdx/tools-golang/tvloader/parser2v3" - "io" - - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/tvloader/parser2v1" - "github.com/spdx/tools-golang/tvloader/parser2v2" - "github.com/spdx/tools-golang/tvloader/reader" -) - -// Load2_1 takes an io.Reader and returns a fully-parsed SPDX Document -// (version 2.1) if parseable, or error if any error is encountered. -func Load2_1(content io.Reader) (*v2_1.Document, error) { - tvPairs, err := reader.ReadTagValues(content) - if err != nil { - return nil, err - } - - doc, err := parser2v1.ParseTagValues(tvPairs) - if err != nil { - return nil, err - } - - return doc, nil -} - -// Load2_2 takes an io.Reader and returns a fully-parsed SPDX Document -// (version 2.2) if parseable, or error if any error is encountered. -func Load2_2(content io.Reader) (*v2_2.Document, error) { - tvPairs, err := reader.ReadTagValues(content) - if err != nil { - return nil, err - } - - doc, err := parser2v2.ParseTagValues(tvPairs) - if err != nil { - return nil, err - } - - return doc, nil -} - -// Load2_3 takes an io.Reader and returns a fully-parsed SPDX Document -// (version 2.2) if parseable, or error if any error is encountered. -func Load2_3(content io.Reader) (*v2_3.Document, error) { - tvPairs, err := reader.ReadTagValues(content) - if err != nil { - return nil, err - } - - doc, err := parser2v3.ParseTagValues(tvPairs) - if err != nil { - return nil, err - } - - return doc, nil -} diff --git a/tvsaver/tvsaver.go b/tvsaver/tvsaver.go deleted file mode 100644 index a73b4b92..00000000 --- a/tvsaver/tvsaver.go +++ /dev/null @@ -1,36 +0,0 @@ -// Package tvsaver is used to save tools-golang data structures -// as SPDX tag-value documents. -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package tvsaver - -import ( - "io" - - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" - "github.com/spdx/tools-golang/tvsaver/saver2v1" - "github.com/spdx/tools-golang/tvsaver/saver2v2" - "github.com/spdx/tools-golang/tvsaver/saver2v3" -) - -// Save2_1 takes an io.Writer and an SPDX Document (version 2.1), -// and writes it to the writer in tag-value format. It returns error -// if any error is encountered. -func Save2_1(doc *v2_1.Document, w io.Writer) error { - return saver2v1.RenderDocument2_1(doc, w) -} - -// Save2_2 takes an io.Writer and an SPDX Document (version 2.2), -// and writes it to the writer in tag-value format. It returns error -// if any error is encountered. -func Save2_2(doc *v2_2.Document, w io.Writer) error { - return saver2v2.RenderDocument2_2(doc, w) -} - -// Save2_3 takes an io.Writer and an SPDX Document (version 2.3), -// and writes it to the writer in tag-value format. It returns error -// if any error is encountered. -func Save2_3(doc *v2_3.Document, w io.Writer) error { - return saver2v3.RenderDocument2_3(doc, w) -} diff --git a/utils/verification.go b/utils/verification.go index 72523b3b..b2d84cf5 100644 --- a/utils/verification.go +++ b/utils/verification.go @@ -9,94 +9,14 @@ import ( "sort" "strings" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// GetVerificationCode2_1 takes a slice of files and an optional filename -// for an "excludes" file, and returns a Package Verification Code calculated -// according to SPDX spec version 2.1, section 3.9.4. -func GetVerificationCode2_1(files []*v2_1.File, excludeFile string) (common.PackageVerificationCode, error) { - // create slice of strings - unsorted SHA1s for all files - shas := []string{} - for i, f := range files { - if f == nil { - return common.PackageVerificationCode{}, fmt.Errorf("got nil file for identifier %v", i) - } - if f.FileName != excludeFile { - // find the SHA1 hash, if present - for _, checksum := range f.Checksums { - if checksum.Algorithm == common.SHA1 { - shas = append(shas, checksum.Value) - } - } - } - } - - // sort the strings - sort.Strings(shas) - - // concatenate them into one string, with no trailing separators - shasConcat := strings.Join(shas, "") - - // and get its SHA1 value - hsha1 := sha1.New() - hsha1.Write([]byte(shasConcat)) - bs := hsha1.Sum(nil) - - code := common.PackageVerificationCode{ - Value: fmt.Sprintf("%x", bs), - ExcludedFiles: []string{excludeFile}, - } - - return code, nil -} - -// GetVerificationCode2_2 takes a slice of files and an optional filename -// for an "excludes" file, and returns a Package Verification Code calculated -// according to SPDX spec version 2.2, section 3.9.4. -func GetVerificationCode2_2(files []*v2_2.File, excludeFile string) (common.PackageVerificationCode, error) { - // create slice of strings - unsorted SHA1s for all files - shas := []string{} - for i, f := range files { - if f == nil { - return common.PackageVerificationCode{}, fmt.Errorf("got nil file for identifier %v", i) - } - if f.FileName != excludeFile { - // find the SHA1 hash, if present - for _, checksum := range f.Checksums { - if checksum.Algorithm == common.SHA1 { - shas = append(shas, checksum.Value) - } - } - } - } - - // sort the strings - sort.Strings(shas) - - // concatenate them into one string, with no trailing separators - shasConcat := strings.Join(shas, "") - - // and get its SHA1 value - hsha1 := sha1.New() - hsha1.Write([]byte(shasConcat)) - bs := hsha1.Sum(nil) - - code := common.PackageVerificationCode{ - Value: fmt.Sprintf("%x", bs), - ExcludedFiles: []string{excludeFile}, - } - - return code, nil -} - -// GetVerificationCode2_3 takes a slice of files and an optional filename +// GetVerificationCode takes a slice of files and an optional filename // for an "excludes" file, and returns a Package Verification Code calculated // according to SPDX spec version 2.3, section 3.9.4. -func GetVerificationCode2_3(files []*v2_3.File, excludeFile string) (common.PackageVerificationCode, error) { +func GetVerificationCode(files []*spdx.File, excludeFile string) (common.PackageVerificationCode, error) { // create slice of strings - unsorted SHA1s for all files shas := []string{} for i, f := range files { diff --git a/utils/verification_test.go b/utils/verification_test.go index beee8b6c..e389613e 100644 --- a/utils/verification_test.go +++ b/utils/verification_test.go @@ -5,284 +5,12 @@ package utils import ( "testing" - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_1" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/v2/common" ) -// ===== 2.1 Verification code functionality tests ===== - -func TestPackage2_1CanGetVerificationCode(t *testing.T) { - files := []*v2_1.File{ - { - FileName: "file2.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{{Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file1.txt", - FileSPDXIdentifier: "File1", - Checksums: []common.Checksum{{Value: "3333333333bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{{Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file5.txt", - FileSPDXIdentifier: "File3", - Checksums: []common.Checksum{{Value: "2222222222bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file4.txt", - FileSPDXIdentifier: "File4", - Checksums: []common.Checksum{{Value: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa", Algorithm: common.SHA1}}, - }, - } - - wantCode := common.PackageVerificationCode{Value: "ac924b375119c81c1f08c3e2722044bfbbdcd3dc"} - - gotCode, err := GetVerificationCode2_1(files, "") - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if wantCode.Value != gotCode.Value { - t.Errorf("expected %v, got %v", wantCode, gotCode) - } - -} - -func TestPackage2_1CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { - files := []*v2_1.File{ - { - FileName: "file1.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{{Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file2.txt", - FileSPDXIdentifier: "File1", - Checksums: []common.Checksum{{Value: "3333333333bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "thisfile.spdx", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{{Value: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa", Algorithm: common.SHA1}}, - }, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File3", - Checksums: []common.Checksum{{Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - { - FileName: "file4.txt", - FileSPDXIdentifier: "File4", - Checksums: []common.Checksum{{Value: "2222222222bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - } - - wantCode := common.PackageVerificationCode{Value: "17fab1bd18fe5c13b5d3983f1c17e5f88b8ff266"} - - gotCode, err := GetVerificationCode2_1(files, "thisfile.spdx") - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if wantCode.Value != gotCode.Value { - t.Errorf("expected %v, got %v", wantCode, gotCode) - } -} - -func TestPackage2_1GetVerificationCodeFailsIfNilFileInSlice(t *testing.T) { - files := []*v2_1.File{ - { - FileName: "file2.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{{Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - nil, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{{Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", Algorithm: common.SHA1}}, - }, - } - - _, err := GetVerificationCode2_1(files, "") - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.2 Verification code functionality tests ===== - -func TestPackage2_2CanGetVerificationCode(t *testing.T) { - files := []*v2_2.File{ - { - FileName: "file2.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file1.txt", - FileSPDXIdentifier: "File1", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "3333333333bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file5.txt", - FileSPDXIdentifier: "File3", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "2222222222bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file4.txt", - FileSPDXIdentifier: "File4", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa", - }, - }, - }, - } - - wantCode := common.PackageVerificationCode{Value: "ac924b375119c81c1f08c3e2722044bfbbdcd3dc"} - - gotCode, err := GetVerificationCode2_2(files, "") - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if wantCode.Value != gotCode.Value { - t.Errorf("expected %v, got %v", wantCode, gotCode) - } - -} - -func TestPackage2_2CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { - files := []*v2_2.File{ - { - FileName: "file1.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file2.txt", - FileSPDXIdentifier: "File1", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "3333333333bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "thisfile.spdx", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa", - }, - }, - }, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File3", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - { - FileName: "file4.txt", - FileSPDXIdentifier: "File4", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "2222222222bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - } - - wantCode := common.PackageVerificationCode{Value: "17fab1bd18fe5c13b5d3983f1c17e5f88b8ff266"} - - gotCode, err := GetVerificationCode2_2(files, "thisfile.spdx") - if err != nil { - t.Fatalf("expected nil error, got %v", err) - } - if wantCode.Value != gotCode.Value { - t.Errorf("expected %v, got %v", wantCode, gotCode) - } -} - -func TestPackage2_2GetVerificationCodeFailsIfNilFileInSlice(t *testing.T) { - files := []*v2_2.File{ - { - FileName: "file2.txt", - FileSPDXIdentifier: "File0", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - nil, - { - FileName: "file3.txt", - FileSPDXIdentifier: "File2", - Checksums: []common.Checksum{ - { - Algorithm: common.SHA1, - Value: "8888888888bbbbbbbbbbccccccccccdddddddddd", - }, - }, - }, - } - - _, err := GetVerificationCode2_2(files, "") - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } -} - -// ===== 2.3 Verification code functionality tests ===== - -func TestPackage2_3CanGetVerificationCode(t *testing.T) { - files := []*v2_3.File{ +func TestPackageCanGetVerificationCode(t *testing.T) { + files := []*spdx.File{ { FileName: "file2.txt", FileSPDXIdentifier: "File0", @@ -337,7 +65,7 @@ func TestPackage2_3CanGetVerificationCode(t *testing.T) { wantCode := common.PackageVerificationCode{Value: "ac924b375119c81c1f08c3e2722044bfbbdcd3dc"} - gotCode, err := GetVerificationCode2_3(files, "") + gotCode, err := GetVerificationCode(files, "") if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -347,8 +75,8 @@ func TestPackage2_3CanGetVerificationCode(t *testing.T) { } -func TestPackage2_3CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { - files := []*v2_3.File{ +func TestPackageCanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { + files := []*spdx.File{ { FileName: "file1.txt", FileSPDXIdentifier: "File0", @@ -403,7 +131,7 @@ func TestPackage2_3CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { wantCode := common.PackageVerificationCode{Value: "17fab1bd18fe5c13b5d3983f1c17e5f88b8ff266"} - gotCode, err := GetVerificationCode2_3(files, "thisfile.spdx") + gotCode, err := GetVerificationCode(files, "thisfile.spdx") if err != nil { t.Fatalf("expected nil error, got %v", err) } @@ -412,8 +140,8 @@ func TestPackage2_3CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) { } } -func TestPackage2_3GetVerificationCodeFailsIfNilFileInSlice(t *testing.T) { - files := []*v2_3.File{ +func TestPackageGetVerificationCodeFailsIfNilFileInSlice(t *testing.T) { + files := []*spdx.File{ { FileName: "file2.txt", FileSPDXIdentifier: "File0", @@ -437,7 +165,7 @@ func TestPackage2_3GetVerificationCodeFailsIfNilFileInSlice(t *testing.T) { }, } - _, err := GetVerificationCode2_3(files, "") + _, err := GetVerificationCode(files, "") if err == nil { t.Fatalf("expected non-nil error, got nil") } diff --git a/yaml/parser.go b/yaml/parser.go deleted file mode 100644 index 265a04d8..00000000 --- a/yaml/parser.go +++ /dev/null @@ -1,48 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_yaml - -import ( - "bytes" - "io" - - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" - "sigs.k8s.io/yaml" -) - -// Load2_2 takes in an io.Reader and returns an SPDX document. -func Load2_2(content io.Reader) (*v2_2.Document, error) { - // convert io.Reader to a slice of bytes and call the parser - buf := new(bytes.Buffer) - _, err := buf.ReadFrom(content) - if err != nil { - return nil, err - } - - var doc v2_2.Document - err = yaml.Unmarshal(buf.Bytes(), &doc) - if err != nil { - return nil, err - } - - return &doc, nil -} - -// Load2_3 takes in an io.Reader and returns an SPDX document. -func Load2_3(content io.Reader) (*v2_3.Document, error) { - // convert io.Reader to a slice of bytes and call the parser - buf := new(bytes.Buffer) - _, err := buf.ReadFrom(content) - if err != nil { - return nil, err - } - - var doc v2_3.Document - err = yaml.Unmarshal(buf.Bytes(), &doc) - if err != nil { - return nil, err - } - - return &doc, nil -} diff --git a/yaml/reader.go b/yaml/reader.go new file mode 100644 index 00000000..c4b39d4e --- /dev/null +++ b/yaml/reader.go @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +package yaml + +import ( + "bytes" + "fmt" + "io" + + "sigs.k8s.io/yaml" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx" + "github.com/spdx/tools-golang/spdx/common" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_3" +) + +// Read takes an io.Reader and returns a fully-parsed current model SPDX Document +// or an error if any error is encountered. +func Read(content io.Reader) (*spdx.Document, error) { + doc := spdx.Document{} + err := ReadInto(content, &doc) + return &doc, err +} + +// ReadInto takes an io.Reader, reads in the SPDX document at the version provided +// and converts to the doc version +func ReadInto(content io.Reader, doc common.AnyDocument) error { + if !convert.IsPtr(doc) { + return fmt.Errorf("doc to read into must be a pointer") + } + + buf := new(bytes.Buffer) + _, err := buf.ReadFrom(content) + if err != nil { + return err + } + + var data interface{} + err = yaml.Unmarshal(buf.Bytes(), &data) + if err != nil { + return err + } + + val, ok := data.(map[string]interface{}) + if !ok { + return fmt.Errorf("not a valid SPDX YAML document") + } + + version, ok := val["spdxVersion"] + if !ok { + return fmt.Errorf("YAML document does not contain spdxVersion field") + } + + switch version { + case v2_1.Version: + var doc v2_1.Document + err = yaml.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + case v2_2.Version: + var doc v2_2.Document + err = yaml.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + case v2_3.Version: + var doc v2_3.Document + err = yaml.Unmarshal(buf.Bytes(), &doc) + if err != nil { + return err + } + data = doc + default: + return fmt.Errorf("unsupported SDPX version: %s", version) + } + + return convert.Document(data, doc) +} diff --git a/yaml/writer.go b/yaml/writer.go index 06ef69a3..4ec078fa 100644 --- a/yaml/writer.go +++ b/yaml/writer.go @@ -1,32 +1,17 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -package spdx_yaml +package yaml import ( "io" - "github.com/spdx/tools-golang/spdx/v2_2" - "github.com/spdx/tools-golang/spdx/v2_3" "sigs.k8s.io/yaml" -) - -// Save2_2 takes an SPDX Document (version 2.2) and an io.Writer, and writes the document to the writer in YAML format. -func Save2_2(doc *v2_2.Document, w io.Writer) error { - buf, err := yaml.Marshal(doc) - if err != nil { - return err - } - - _, err = w.Write(buf) - if err != nil { - return err - } - return nil -} + "github.com/spdx/tools-golang/spdx/common" +) -// Save2_3 takes an SPDX Document (version 2.3) and an io.Writer, and writes the document to the writer in YAML format. -func Save2_3(doc *v2_3.Document, w io.Writer) error { +// Write takes an SPDX Document and an io.Writer, and writes the document to the writer in YAML format. +func Write(doc common.AnyDocument, w io.Writer) error { buf, err := yaml.Marshal(doc) if err != nil { return err diff --git a/yaml/yaml_v2_3_test.go b/yaml/yaml_v2_3_test.go deleted file mode 100644 index 02711500..00000000 --- a/yaml/yaml_v2_3_test.go +++ /dev/null @@ -1,492 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - -package spdx_yaml - -import ( - "bytes" - "flag" - "fmt" - "os" - "testing" - - "github.com/google/go-cmp/cmp" - - "github.com/spdx/tools-golang/spdx/common" - "github.com/spdx/tools-golang/spdx/v2_3" -) - -var update = *flag.Bool("update-snapshots", false, "update the example snapshot") - -func TestLoad2_3(t *testing.T) { - fileName := "../examples/sample-docs/yaml/SPDXYAMLExample-2.3.spdx.yaml" - - if update { - f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - t.Errorf("unable to write SPDX 2.3 example to YAML: %v", err) - } - err = Save2_3(&want2_3, f) - if err != nil { - t.Errorf("unable to serialize SPDX 2.3 example to YAML: %v", err) - } - } - - file, err := os.Open(fileName) - if err != nil { - panic(fmt.Errorf("error opening File: %s", err)) - } - - got, err := Load2_3(file) - if err != nil { - t.Errorf("yaml.parser.Load2_3() error = %v", err) - return - } - - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_3 - - if cmp.Equal(handwrittenExample, got) { - t.Errorf("got incorrect struct after parsing YAML example") - return - } -} - -func TestWrite2_3(t *testing.T) { - w := &bytes.Buffer{} - // get a copy of the handwritten struct so we don't mutate it on accident - handwrittenExample := want2_3 - if err := Save2_3(&handwrittenExample, w); err != nil { - t.Errorf("Save2_3() error = %v", err.Error()) - return - } - - // we should be able to parse what the writer wrote, and it should be identical to the original handwritten struct - parsedDoc, err := Load2_3(bytes.NewReader(w.Bytes())) - if err != nil { - t.Errorf("failed to parse written document: %v", err.Error()) - return - } - - if cmp.Equal(handwrittenExample, parsedDoc) { - t.Errorf("got incorrect struct after writing and re-parsing YAML example") - return - } -} - -// want is handwritten translation of the official example YAML SPDX v2.2 document into a Go struct. -// We expect that the result of parsing the official document should be this value. -// We expect that the result of writing this struct should match the official example document. -var want2_3 = v2_3.Document{ - DataLicense: "CC0-1.0", - SPDXVersion: "SPDX-2.2", - SPDXIdentifier: "SPDXRef-DOCUMENT", - DocumentName: "SPDX-Tools-v2.0", - DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", - CreationInfo: &v2_3.CreationInfo{ - LicenseListVersion: "3.9", - Creators: []common.Creator{ - {CreatorType: "Tool", Creator: "LicenseFind-1.0"}, - {CreatorType: "Organization", Creator: "ExampleCodeInspect ()"}, - {CreatorType: "Person", Creator: "Jane Doe ()"}, - }, - Created: "2010-01-29T18:30:22Z", - CreatorComment: "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.", - }, - DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.", - ExternalDocumentReferences: []v2_3.ExternalDocumentRef{ - { - DocumentRefID: "DocumentRef-spdx-tool-1.2", - URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301", - Checksum: common.Checksum{ - Algorithm: common.SHA1, - Value: "d6a770ba38583ed4bb4525bd96e50461655d2759", - }, - }, - }, - OtherLicenses: []*v2_3.OtherLicense{ - { - LicenseIdentifier: "LicenseRef-1", - ExtractedText: "/*\n * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-2", - ExtractedText: "This package includes the GRDDL parser developed by Hewlett Packard under the following license:\n� Copyright 2007 Hewlett-Packard Development Company, LP\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: \n\nRedistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. \nRedistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. \nThe name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. \nTHIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - }, - { - LicenseIdentifier: "LicenseRef-4", - ExtractedText: "/*\n * (c) Copyright 2009 University of Bristol\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * 3. The name of the author may not be used to endorse or promote products\n * derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/", - }, - { - LicenseIdentifier: "LicenseRef-Beerware-4.2", - ExtractedText: "\"THE BEER-WARE LICENSE\" (Revision 42):\nphk@FreeBSD.ORG wrote this file. As long as you retain this notice you\ncan do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp", - LicenseComment: "The beerware license has a couple of other standard variants.", - LicenseName: "Beer-Ware License (Version 42)", - LicenseCrossReferences: []string{"http://people.freebsd.org/~phk/"}, - }, - { - LicenseIdentifier: "LicenseRef-3", - ExtractedText: "The CyberNeko Software License, Version 1.0\n\n \n(C) Copyright 2002-2005, Andy Clark. All rights reserved.\n \nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions\nare met:\n\n1. Redistributions of source code must retain the above copyright\n notice, this list of conditions and the following disclaimer. \n\n2. Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in\n the documentation and/or other materials provided with the\n distribution.\n\n3. The end-user documentation included with the redistribution,\n if any, must include the following acknowledgment: \n \"This product includes software developed by Andy Clark.\"\n Alternately, this acknowledgment may appear in the software itself,\n if and wherever such third-party acknowledgments normally appear.\n\n4. The names \"CyberNeko\" and \"NekoHTML\" must not be used to endorse\n or promote products derived from this software without prior \n written permission. For written permission, please contact \n andyc@cyberneko.net.\n\n5. Products derived from this software may not be called \"CyberNeko\",\n nor may \"CyberNeko\" appear in their name, without prior written\n permission of the author.\n\nTHIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\nOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS\nBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, \nOR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT \nOF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR \nBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \nWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE \nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, \nEVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", - LicenseName: "CyberNeko License", - LicenseCrossReferences: []string{ - "http://people.apache.org/~andyc/neko/LICENSE", - "http://justasample.url.com", - }, - LicenseComment: "This is tye CyperNeko License", - }, - }, - Annotations: []*v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Jane Doe ()", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Document level annotation", - }, - { - Annotator: common.Annotator{ - Annotator: "Joe Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2010-02-10T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", - }, - { - Annotator: common.Annotator{ - Annotator: "Suzanne Reviewer", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-03-13T00:00:00Z", - AnnotationType: "REVIEW", - AnnotationComment: "Another example reviewer.", - }, - }, - Packages: []*v2_3.Package{ - { - PackageName: "glibc", - PackageSPDXIdentifier: "SPDXRef-Package", - PackageVersion: "2.11.1", - PackageFileName: "glibc-2.11.1.tar.gz", - PackageSupplier: &common.Supplier{ - Supplier: "Jane Doe (jane.doe@example.com)", - SupplierType: "Person", - }, - PackageOriginator: &common.Originator{ - Originator: "ExampleCodeInspect (contact@example.com)", - OriginatorType: "Organization", - }, - PackageDownloadLocation: "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz", - FilesAnalyzed: true, - PackageVerificationCode: &common.PackageVerificationCode{ - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - ExcludedFiles: []string{"./package.spdx"}, - }, - PackageChecksums: []common.Checksum{ - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - { - Algorithm: "SHA256", - Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd", - }, - }, - PackageHomePage: "http://ftp.gnu.org/gnu/glibc", - PackageSourceInfo: "uses glibc-2_11-branch from git://sourceware.org/git/glibc.git.", - PackageLicenseConcluded: "(LGPL-2.0-only OR LicenseRef-3)", - PackageLicenseInfoFromFiles: []string{ - "GPL-2.0-only", - "LicenseRef-2", - "LicenseRef-1", - }, - PackageLicenseDeclared: "(LGPL-2.0-only AND LicenseRef-3)", - PackageLicenseComments: "The license for this project changed with the release of version x.y. The version of the project included here post-dates the license change.", - PackageCopyrightText: "Copyright 2008-2010 John Smith", - PackageSummary: "GNU C library.", - PackageDescription: "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.", - PackageComment: "", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "SECURITY", - RefType: "cpe23Type", - Locator: "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*", - }, - { - Category: "OTHER", - RefType: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge", - Locator: "acmecorp/acmenator/4.1.3-alpha", - ExternalRefComment: "This is the external ref for Acme", - }, - }, - PackageAttributionTexts: []string{ - "The GNU C Library is free software. See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed. License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually.", - }, - Files: nil, - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "Package Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "Package level annotation", - }, - }, - }, - { - PackageSPDXIdentifier: "SPDXRef-fromDoap-1", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "http://commons.apache.org/proper/commons-lang/", - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseDeclared: "NOASSERTION", - PackageName: "Apache Commons Lang", - }, - { - PackageName: "Jena", - PackageSPDXIdentifier: "SPDXRef-fromDoap-0", - PackageCopyrightText: "NOASSERTION", - PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz", - PackageExternalReferences: []*v2_3.PackageExternalReference{ - { - Category: "PACKAGE_MANAGER", - RefType: "purl", - Locator: "pkg:maven/org.apache.jena/apache-jena@3.12.0", - }, - }, - FilesAnalyzed: false, - PackageHomePage: "http://www.openjena.org/", - PackageLicenseConcluded: "NOASSERTION", - PackageLicenseDeclared: "NOASSERTION", - PackageVersion: "3.12.0", - }, - { - PackageSPDXIdentifier: "SPDXRef-Saxon", - PackageChecksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "85ed0817af83a24ad8da68c2b5094de69833983c", - }, - }, - PackageCopyrightText: "Copyright Saxonica Ltd", - PackageDescription: "The Saxon package is a collection of tools for processing XML documents.", - PackageDownloadLocation: "https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download", - FilesAnalyzed: false, - PackageHomePage: "http://saxon.sourceforge.net/", - PackageLicenseComments: "Other versions available for a commercial license", - PackageLicenseConcluded: "MPL-1.0", - PackageLicenseDeclared: "MPL-1.0", - PackageName: "Saxon", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "8.8", - }, - { - PrimaryPackagePurpose: "CONTAINER", - PackageSPDXIdentifier: "SPDXRef-CentOS-7", - PackageCopyrightText: "NOASSERTION", - PackageDescription: "The CentOS container used to run the application.", - PackageDownloadLocation: "NOASSERTION", - FilesAnalyzed: false, - PackageHomePage: "https://www.centos.org/", - PackageName: "centos", - PackageFileName: "saxonB-8.8.zip", - PackageVersion: "centos7.9.2009", - BuiltDate: "2021-09-15T02:38:00Z", - ValidUntilDate: "2022-10-15T02:38:00Z", - ReleaseDate: "2021-10-15T02:38:00Z", - }, - }, - Files: []*v2_3.File{ - { - FileName: "./src/org/spdx/parser/DOAPProject.java", - FileSPDXIdentifier: "SPDXRef-DoapSource", - FileTypes: []string{ - "SOURCE", - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", - }, - }, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{ - "Apache-2.0", - }, - FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.", - FileContributors: []string{ - "Protecode Inc.", - "SPDX Technical Team Members", - "Open Logic Inc.", - "Source Auditor Inc.", - "Black Duck Software In.c", - }, - }, - { - FileSPDXIdentifier: "SPDXRef-CommonsLangSrc", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "c2b4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file is used by Jena", - FileCopyrightText: "Copyright 2001-2011 The Apache Software Foundation", - FileContributors: []string{"Apache Software Foundation"}, - FileName: "./lib-source/commons-lang3-3.1-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseConcluded: "Apache-2.0", - LicenseInfoInFiles: []string{"Apache-2.0"}, - FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())", - }, - { - FileSPDXIdentifier: "SPDXRef-JenaLib", - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "3ab4e1c67a2d28fced849ee1bb76e7391b93f125", - }, - }, - FileComment: "This file belongs to Jena", - FileCopyrightText: "(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP", - FileContributors: []string{"Apache Software Foundation", "Hewlett Packard Inc."}, - FileName: "./lib-source/jena-2.6.3-sources.jar", - FileTypes: []string{"ARCHIVE"}, - LicenseComments: "This license is used by Jena", - LicenseConcluded: "LicenseRef-1", - LicenseInfoInFiles: []string{"LicenseRef-1"}, - }, - { - FileSPDXIdentifier: "SPDXRef-File", - Annotations: []v2_3.Annotation{ - { - Annotator: common.Annotator{ - Annotator: "File Commenter", - AnnotatorType: "Person", - }, - AnnotationDate: "2011-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotationComment: "File level annotation", - }, - }, - Checksums: []common.Checksum{ - { - Algorithm: "SHA1", - Value: "d6a770ba38583ed4bb4525bd96e50461655d2758", - }, - { - Algorithm: "MD5", - Value: "624c1abb3664f4b35547e7c73864ad24", - }, - }, - FileComment: "The concluded license was taken from the package level that the file was included in.\nThis information was found in the COPYING.txt file in the xyz directory.", - FileCopyrightText: "Copyright 2008-2010 John Smith", - FileContributors: []string{"The Regents of the University of California", "Modified by Paul Mundt lethal@linux-sh.org", "IBM Corporation"}, - FileName: "./package/foo.c", - FileTypes: []string{"SOURCE"}, - LicenseComments: "The concluded license was taken from the package level that the file was included in.", - LicenseConcluded: "(LGPL-2.0-only OR LicenseRef-2)", - LicenseInfoInFiles: []string{"GPL-2.0-only", "LicenseRef-2"}, - FileNotice: "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", - }, - }, - Snippets: []v2_3.Snippet{ - { - SnippetSPDXIdentifier: "SPDXRef-Snippet", - SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource", - Ranges: []common.SnippetRange{ - { - StartPointer: common.SnippetRangePointer{ - Offset: 310, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - Offset: 420, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - { - StartPointer: common.SnippetRangePointer{ - LineNumber: 5, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - EndPointer: common.SnippetRangePointer{ - LineNumber: 23, - FileSPDXIdentifier: "SPDXRef-DoapSource", - }, - }, - }, - SnippetLicenseConcluded: "GPL-2.0-only", - LicenseInfoInSnippet: []string{"GPL-2.0-only"}, - SnippetLicenseComments: "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.", - SnippetCopyrightText: "Copyright 2008-2010 John Smith", - SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.", - SnippetName: "from linux kernel", - SnippetAttributionTexts: []string{"Snippet attribution"}, - }, - }, - Relationships: []*v2_3.Relationship{ - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - RelationshipComment: "A relationship comment", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("spdx-tool-1.2", "ToolsElement"), - Relationship: "COPY_OF", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "File"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "DOCUMENT"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "DESCRIBES", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "JenaLib"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "Package"), - RefB: common.MakeDocElementID("", "Saxon"), - Relationship: "DYNAMIC_LINK", - }, - { - RefA: common.MakeDocElementID("", "CommonsLangSrc"), - RefB: common.MakeDocElementSpecial("NOASSERTION"), - Relationship: "GENERATED_FROM", - }, - { - RefA: common.MakeDocElementID("", "JenaLib"), - RefB: common.MakeDocElementID("", "Package"), - Relationship: "CONTAINS", - }, - { - RefA: common.MakeDocElementID("", "File"), - RefB: common.MakeDocElementID("", "fromDoap-0"), - Relationship: "GENERATED_FROM", - }, - }, - Reviews: []*v2_3.Review{ - { - Reviewer: "joe@example.com", - ReviewerType: "Person", - ReviewDate: "2021-11-03T05:43:21Z", - ReviewComment: "This is a review comment", - }, - }, -}