From e5daeb13cbd446a4fe882fb722d8d3359dd9bef9 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 11 Jan 2026 20:36:38 +0700 Subject: all: ignore symlink, COPYING, LICENSE, and LICENSES For symlink, we ignore for now. COPYING, LICENSE, and LICENSES are common files part of SPDX/reuse specifications. While at it, fix checking if path is ignored by git by passing the relative path instead of base name. --- go.mod | 2 +- go.sum | 6 +++-- spdxconv.go | 24 ++++++++++++------- spdxconv_test.go | 42 +++++++++++++++++++--------------- testdata/loadReport/ok/spdxconv.report | 2 ++ testdata/scan/a/b/.gitignore | 5 ++++ testdata/scan/a/b/b1 | 2 ++ testdata/scan/a/b/c/c1 | 2 ++ testdata/scan/symdir | 1 + testdata/scan/test.go | 4 +++- 10 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 testdata/scan/a/b/.gitignore create mode 100644 testdata/scan/a/b/b1 create mode 100644 testdata/scan/a/b/c/c1 create mode 120000 testdata/scan/symdir diff --git a/go.mod b/go.mod index f42d1e6..43555f2 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ module git.sr.ht/~shulhan/spdxconv go 1.24.0 -require git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260108173808-34693c1b7ab1 +require git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260111142917-cdfebe3f55dc require ( golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 // indirect diff --git a/go.sum b/go.sum index bce51b2..7b10687 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,13 @@ -git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260108173808-34693c1b7ab1 h1:vxhrVP1emFV6B+ZkUEwFcjDuUCBeXIC49H8FTrMlg/4= -git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260108173808-34693c1b7ab1/go.mod h1:1MkKXbLZRHTcnheeSEbRpGztkym4Yxzh90ep+jCxbDc= +git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260111142917-cdfebe3f55dc h1:9oi3sLX/kyePdZyCVmx0qoIAmcPy63v8U1D3Br23gLc= +git.sr.ht/~shulhan/pakakeh.go v0.60.3-0.20260111142917-cdfebe3f55dc/go.mod h1:1MkKXbLZRHTcnheeSEbRpGztkym4Yxzh90ep+jCxbDc= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 h1:fQsdNF2N+/YewlRZiricy4P1iimyPKZ/xwniHj8Q2a0= golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93/go.mod h1:EPRbTFwzwjXj9NpYyyrvenVh9Y+GFeEvMNh7Xuz7xgU= golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= +golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= +golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= diff --git a/spdxconv.go b/spdxconv.go index be13a2c..d56b283 100644 --- a/spdxconv.go +++ b/spdxconv.go @@ -6,6 +6,7 @@ package spdxconv import ( "bytes" "fmt" + "io/fs" "log" "maps" "os" @@ -264,10 +265,14 @@ func (conv *SPDXConv) scanFile(dir, name string) (listFile []string, err error) // A common ignore file or directory name likes ".git", "node_modules", and // "vendor"; also will be excluded. func (conv *SPDXConv) scanDir(listDir []string) (listFile []string, err error) { + var logp = `scanDir` var commonIgnore = map[string]struct{}{ `.git`: struct{}{}, - `node_modules`: struct{}{}, ConfigFile: struct{}{}, + `COPYING`: struct{}{}, + `LICENSE`: struct{}{}, + `LICENSES`: struct{}{}, // Directory contains licenses. + `node_modules`: struct{}{}, ReportFile: struct{}{}, `vendor`: struct{}{}, } @@ -282,7 +287,7 @@ func (conv *SPDXConv) scanDir(listDir []string) (listFile []string, err error) { listde, err = os.ReadDir(dir) if err != nil { - return nil, err + return nil, fmt.Errorf(`%s: %w`, logp, err) } var listFileLicense = make(map[string]struct{}) @@ -293,19 +298,22 @@ func (conv *SPDXConv) scanDir(listDir []string) (listFile []string, err error) { listFileLicense[name] = struct{}{} continue } - if conv.scm.IsIgnored(name) { - continue - } _, ok = commonIgnore[name] if ok { continue } - var fullpath = filepath.Join(dir, name) + if de.Type()&fs.ModeSymlink != 0 { + continue + } + var relpath = filepath.Join(dir, name) + if conv.scm.IsIgnored(relpath) { + continue + } if de.IsDir() { - listDir = append(listDir, fullpath) + listDir = append(listDir, relpath) continue } - listNamePath[name] = fullpath + listNamePath[name] = relpath } // Remove the file that have the spdx .license file. for name := range listFileLicense { diff --git a/spdxconv_test.go b/spdxconv_test.go index 8a8e112..b58f7a0 100644 --- a/spdxconv_test.go +++ b/spdxconv_test.go @@ -227,21 +227,22 @@ func TestSPDXConv_scanDir(t *testing.T) { dir string exp []string } - t.Chdir(`testdata/`) var listCase = []testCase{{ - dir: `scan/`, + dir: `testdata/scan/`, exp: []string{ - `scan/.gitignore`, - `scan/test.go`, - `scan/test.html`, - `scan/test.sh`, + `.gitignore`, + `a/b/.gitignore`, + `test.go`, + `test.html`, + `test.sh`, + `with_spdx.go`, }, }, { - dir: `scan_no_scm/`, + dir: `testdata/scan_no_scm/`, exp: []string{ - `scan_no_scm/test.go`, - `scan_no_scm/test.html`, - `scan_no_scm/test.sh`, + `test.go`, + `test.html`, + `test.sh`, }, }} var tc testCase @@ -249,14 +250,17 @@ func TestSPDXConv_scanDir(t *testing.T) { var err error var got []string for _, tc = range listCase { - conv, err = New(tc.dir) - if err != nil { - t.Fatal(err) - } - got, err = conv.scanDir([]string{tc.dir}) - if err != nil { - t.Fatal(err) - } - test.Assert(t, tc.dir, tc.exp, got) + t.Run(tc.dir, func(tt *testing.T) { + tt.Chdir(tc.dir) + conv, err = New(`.`) + if err != nil { + tt.Fatal(err) + } + got, err = conv.scanDir([]string{`.`}) + if err != nil { + tt.Fatal(err) + } + test.Assert(tt, tc.dir, tc.exp, got) + }) } } diff --git a/testdata/loadReport/ok/spdxconv.report b/testdata/loadReport/ok/spdxconv.report index a83ade4..ef027b8 100644 --- a/testdata/loadReport/ok/spdxconv.report +++ b/testdata/loadReport/ok/spdxconv.report @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan // comment //spdxconv:regular diff --git a/testdata/scan/a/b/.gitignore b/testdata/scan/a/b/.gitignore new file mode 100644 index 0000000..405fc5e --- /dev/null +++ b/testdata/scan/a/b/.gitignore @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 M. Shulhan + +* +!/.gitignore diff --git a/testdata/scan/a/b/b1 b/testdata/scan/a/b/b1 new file mode 100644 index 0000000..be9e78a --- /dev/null +++ b/testdata/scan/a/b/b1 @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan diff --git a/testdata/scan/a/b/c/c1 b/testdata/scan/a/b/c/c1 new file mode 100644 index 0000000..be9e78a --- /dev/null +++ b/testdata/scan/a/b/c/c1 @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan diff --git a/testdata/scan/symdir b/testdata/scan/symdir new file mode 120000 index 0000000..5657f6e --- /dev/null +++ b/testdata/scan/symdir @@ -0,0 +1 @@ +vendor \ No newline at end of file diff --git a/testdata/scan/test.go b/testdata/scan/test.go index 46e2bd9..a50aac4 100644 --- a/testdata/scan/test.go +++ b/testdata/scan/test.go @@ -1,3 +1,5 @@ // Copyright 2022, Shulhan . All rights reserved. // Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. \ No newline at end of file +// license that can be found in the LICENSE file. + +package spdxconv_test -- cgit v1.3