Skip to content

Commit 93ac344

Browse files
authored
Merge pull request #19 from github/elr/licenseRef
add support for licenseRefs processing in Satisfies
2 parents 96f5a7c + e9a7844 commit 93ac344

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

spdxexp/node.go

+13
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ func (nodes *nodePair) licensesAreCompatible() bool {
209209
return nodes.licensesExactlyEqual()
210210
}
211211

212+
func (nodes *nodePair) licenseRefsAreCompatible() bool {
213+
if !nodes.firstNode.isLicenseRef() || !nodes.secondNode.isLicenseRef() {
214+
return false
215+
}
216+
217+
compatible := *nodes.firstNode.licenseRef() == *nodes.secondNode.licenseRef()
218+
compatible = compatible && (nodes.firstNode.hasDocumentRef() == nodes.secondNode.hasDocumentRef())
219+
if compatible && nodes.firstNode.hasDocumentRef() {
220+
compatible = compatible && (*nodes.firstNode.documentRef() == *nodes.secondNode.documentRef())
221+
}
222+
return compatible
223+
}
224+
212225
// Return true if two licenses are compatible in the context of their ranges; otherwise, false.
213226
func (nodes *nodePair) rangesAreCompatible() bool {
214227
if nodes.licensesExactlyEqual() {

spdxexp/satisfies.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func isCompatible(expressionPart, allowed []*node) bool {
8585
compatible := false
8686
for _, allowedLicense := range allowed {
8787
nodes := &nodePair{firstNode: expLicense, secondNode: allowedLicense}
88-
if nodes.licensesAreCompatible() {
88+
if nodes.licensesAreCompatible() || nodes.licenseRefsAreCompatible() {
8989
compatible = true
9090
break
9191
}
@@ -192,7 +192,7 @@ func (n *node) expandAnd() [][]*node {
192192
// expandAndTerm expands the terms of an AND expression.
193193
func expandAndTerm(term *node) [][]*node {
194194
var result [][]*node
195-
if term.isLicense() {
195+
if term.isLicense() || term.isLicenseRef() {
196196
result = append(result, []*node{term})
197197
} else if term.isExpression() {
198198
if term.isAndExpression() {

spdxexp/satisfies_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ func TestSatisfies(t *testing.T) {
104104
"MIT AND (GPL-2.0 OR ISC)", []string{"MIT"}, false, nil},
105105
{"! (MIT OR Apache-2.0) AND (ISC OR GPL-2.0) satisfies [MIT]",
106106
"(MIT OR Apache-2.0) AND (ISC OR GPL-2.0)", []string{"MIT"}, false, nil},
107+
{"licenseRef is expression",
108+
"LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
109+
{"licenseRef in expression",
110+
"MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
111+
{"licenseRef not in expression",
112+
"MIT AND Apache-2.0", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
113+
{"licenseRef not allowed",
114+
"MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0"}, false, nil},
115+
{"licenseRef with documentRef is expression",
116+
"DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
117+
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
118+
{"licenseRef with documentRef in expression",
119+
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
120+
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
121+
{"licenseRef with documentRef not in expression",
122+
"MIT AND Apache-2.0",
123+
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
124+
{"licenseRef with documentRef not allowed",
125+
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
126+
[]string{"MIT", "Apache-2.0"}, false, nil},
127+
{"licenseRef allowed, but documentRef not allowed",
128+
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
129+
[]string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
130+
{"licenseRef alone not allowed, but with documentRef allowed",
131+
"MIT AND LicenseRef-X-BSD-3-Clause-Golang",
132+
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
107133
}
108134

109135
for _, test := range tests {

0 commit comments

Comments
 (0)