aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Winslow <swinslow@gmail.com>2018-12-08 02:49:19 -0500
committerSteve Winslow <swinslow@gmail.com>2018-12-08 02:49:19 -0500
commitc426fb0530d2339dcd65a5f1273a77330281ccfe (patch)
tree091be83b07f58186508c2c5f21b939ebd8b71c7c
parentb7ba1c5e88bf4fecc99d5c1b7eaa8181b1b812ef (diff)
downloadspdx-tools-c426fb0530d2339dcd65a5f1273a77330281ccfe.tar.gz
Added tests and implementation to fix idsearcher with long prefix lines
Signed-off-by: Steve Winslow <swinslow@gmail.com>
-rw-r--r--testdata/project4/has-id-to-ignore.txt8
-rw-r--r--testdata/project4/has-id.txt3
-rw-r--r--testdata/project4/has-mix-of-ids.txt12
-rw-r--r--v0/idsearcher/idsearcher.go19
-rw-r--r--v0/idsearcher/idsearcher_test.go30
5 files changed, 64 insertions, 8 deletions
diff --git a/testdata/project4/has-id-to-ignore.txt b/testdata/project4/has-id-to-ignore.txt
new file mode 100644
index 0000000..bd63676
--- /dev/null
+++ b/testdata/project4/has-id-to-ignore.txt
@@ -0,0 +1,8 @@
+this file actually contains some code talking about IDs, and should be ignored
+
+if file.contains("SPDX-License-Identifier: GPL-2.0") {
+ // do something...
+
+}
+
+We don't want to pick up that line as the short-form ID for this file.
diff --git a/testdata/project4/has-id.txt b/testdata/project4/has-id.txt
new file mode 100644
index 0000000..5545f2c
--- /dev/null
+++ b/testdata/project4/has-id.txt
@@ -0,0 +1,3 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+somebody was kind enough to stick an spdx short-form ID in here
diff --git a/testdata/project4/has-mix-of-ids.txt b/testdata/project4/has-mix-of-ids.txt
new file mode 100644
index 0000000..b6aa5d1
--- /dev/null
+++ b/testdata/project4/has-mix-of-ids.txt
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+
+the short-form ID above is correct, and should be picked up.
+
+the following code is talking about IDs, and should be ignored
+
+if file.contains("SPDX-License-Identifier: GPL-2.0") {
+ // do something...
+
+}
+
+We don't want to pick up that line as the short-form ID for this file.
diff --git a/v0/idsearcher/idsearcher.go b/v0/idsearcher/idsearcher.go
index 2eab298..e025f3b 100644
--- a/v0/idsearcher/idsearcher.go
+++ b/v0/idsearcher/idsearcher.go
@@ -150,15 +150,18 @@ func searchFileIDs(filePath string) ([]string, error) {
scanner := bufio.NewScanner(f)
- // build the scan string this way, so that we can run idsearcher on itself
- // without picking up these lines as IDs...
- tag1 := "SPDX-License-"
- tag2 := "Identifier:"
- tag := tag1 + tag2
-
for scanner.Scan() {
- if strings.Contains(scanner.Text(), tag) {
- strs := strings.SplitAfterN(scanner.Text(), tag, 2)
+ if strings.Contains(scanner.Text(), "SPDX-License-Identifier:") {
+ strs := strings.SplitN(scanner.Text(), "SPDX-License-Identifier:", 2)
+
+ // if prefixed by more than n characters, it's probably not a
+ // short-form ID; it's probably code to detect short-form IDs.
+ // Like this function itself, for example =)
+ prefix := stripTrash(strs[0])
+ if len(prefix) > 5 {
+ continue
+ }
+
// stop before trailing */ if it is present
lidToExtract := strs[1]
lidToExtract = strings.Split(lidToExtract, "*/")[0]
diff --git a/v0/idsearcher/idsearcher_test.go b/v0/idsearcher/idsearcher_test.go
index 4399faa..10178f9 100644
--- a/v0/idsearcher/idsearcher_test.go
+++ b/v0/idsearcher/idsearcher_test.go
@@ -364,6 +364,36 @@ func TestCanStripTrailingStarSlash(t *testing.T) {
}
}
+func TestCanIgnoreShortFormIDWhenTooManyPrefixChars(t *testing.T) {
+ filePath := "../../testdata/project4/has-id-to-ignore.txt"
+
+ ids, err := searchFileIDs(filePath)
+ if err != nil {
+ t.Fatalf("expected nil error, got %v", err)
+ }
+
+ if len(ids) != 0 {
+ t.Fatalf("expected len 0, got %d", len(ids))
+ }
+}
+
+func TestCanPickJustTheRightID(t *testing.T) {
+ filePath := "../../testdata/project4/has-mix-of-ids.txt"
+
+ ids, err := searchFileIDs(filePath)
+ if err != nil {
+ t.Fatalf("expected nil error, got %v", err)
+ }
+
+ if len(ids) != 1 {
+ t.Fatalf("expected len 1, got %d", len(ids))
+ }
+
+ if ids[0] != "MIT" {
+ t.Errorf("expected %v, got %v", "MIT", ids[0])
+ }
+}
+
func TestCannotFindShortFormIDWhenAbsent(t *testing.T) {
filePath := "../../testdata/project2/no-id.txt"