diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-15 19:21:33 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-15 19:21:33 +0700 |
| commit | 73252709ac8f73d1be5f9752d5f1dda79a9c85a4 (patch) | |
| tree | fdc2a6ad8ddc15846d1388105e81b8c66895ac7d | |
| parent | 5247a96c9b0adce5e38460ad2d0ef5028e34b768 (diff) | |
| download | spdxconv-73252709ac8f73d1be5f9752d5f1dda79a9c85a4.tar.xz | |
report: do not return an error if report file does not exist
When scan running, it will try to load the previous report file to
minimize re-scanning of file that has been applied or detected (regular
or binary).
If the report file does not exist, do not return the error, keep going.
| -rw-r--r-- | file.go | 2 | ||||
| -rw-r--r-- | report.go | 6 | ||||
| -rw-r--r-- | spdxconv_test.go | 82 | ||||
| -rw-r--r-- | testdata/Scan_noConfigAndReport/.gitignore | 5 | ||||
| -rw-r--r-- | testdata/Scan_test.txt | 64 | ||||
| -rw-r--r-- | testdata/scan/.gitignore | 5 |
6 files changed, 111 insertions, 53 deletions
@@ -119,7 +119,7 @@ func (f *file) initLines(content []byte, maxLine int) (err error) { f.lines = f.lines[maxLine : nline-maxLine] } - if bytes.HasPrefix(f.topLines[0], []byte(`#!`)) { + if len(f.topLines) != 0 && bytes.HasPrefix(f.topLines[0], []byte(`#!`)) { f.hasSheBang = true } return nil @@ -52,9 +52,14 @@ func newReport() (rep *report) { // loadReport load the [ReportFile] from the current directory. func loadReport() (rep *report, err error) { var logp = `loadReport` + + rep = newReport() var content []byte content, err = os.ReadFile(ReportFile) if err != nil { + if os.IsNotExist(err) { + return rep, nil + } return nil, fmt.Errorf(`%s: %w`, logp, err) } @@ -64,7 +69,6 @@ func loadReport() (rep *report, err error) { csvr.ReuseRecord = true csvr.TrimLeadingSpace = true - rep = newReport() var group string var record []string var f *file diff --git a/spdxconv_test.go b/spdxconv_test.go index 84b3718..d2389e4 100644 --- a/spdxconv_test.go +++ b/spdxconv_test.go @@ -8,7 +8,6 @@ import ( "regexp" "testing" - "git.sr.ht/~shulhan/pakakeh.go/lib/git" "git.sr.ht/~shulhan/pakakeh.go/lib/test" ) @@ -121,54 +120,46 @@ func TestInit(t *testing.T) { } func TestScan(t *testing.T) { - const scanDir = `testdata/scan/` - t.Chdir(scanDir) + // Populate the test files into `testdata/`. - err := Scan(`.`) + var testData *test.Data + var err error + testData, err = test.LoadData(`testdata/Scan_test.txt`) if err != nil { t.Fatal(err) } + var tempDir = `testdata/` + testData.ExtractInput(tempDir) - got, err := os.ReadFile(ReportFile) - if err != nil { - t.Fatal(err) - } + // Run the test ... - exp := `// SPDX-License-Identifier: CC0-1.0 -// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + listCase := []struct { + dir string + expReport string + }{{ + dir: `testdata/scan/`, + expReport: string(testData.Output[`scan/spdxconv.report`]), + }, { + dir: `testdata/Scan_noConfigAndReport/`, + expReport: string(testData.Output[`Scan_noConfigAndReport/spdxconv.report`]), + }} + for _, tc := range listCase { + t.Run(tc.dir, func(tt *testing.T) { + tt.Chdir(tc.dir) -//spdxconv:version:v1 -//spdxconv:header:path,license_id,idx_license_id,year,copyright_id,idx_copyright_id -// -//spdxconv:regular -// -Makefile,default,0,2025,default,0,#, -go.mod,default,0,2025,default,0,//, -no_copyright_year.md,match,2,2026,default,0,<!--,--> -po/included.go,match,1,2026,match,0,//, -test.go,match,1,2022,match,0,//, -test.html,match,4,2022,match,1,<!--,--> -test.sh,default,0,2026,default,0,#, -test.sql,match,1,2022,match,0,--, -// -//spdxconv:binary -// -go.sum,default,0,2025,default,0,, -test.json,default,0,2026,default,0,, -// -//spdxconv:unknown -// -// -//spdxconv:done -// -.gitignore,exist,0,,exist,1,#, -a/b/.gitignore,exist,0,,exist,1,#, -po/test.po,,0,,,0,, -po/test.pot,,0,,,0,, -with_spdx.go,exist,0,,exist,1,//, -` + err := Scan(`.`) + if err != nil { + t.Fatal(err) + } + + got, err := os.ReadFile(ReportFile) + if err != nil { + t.Fatal(err) + } - test.Assert(t, `Scan: `+scanDir, exp, string(got)) + test.Assert(t, `Scan: `+tc.dir, tc.expReport, string(got)) + }) + } } func TestApply(t *testing.T) { @@ -259,19 +250,12 @@ func TestNew(t *testing.T) { testData.ExtractInput(tempDir) t.Chdir(tempDir) - var gitRoot *git.Git - gitRoot, err = git.New(`.`) - if err != nil { - t.Fatal(err) - } - var listCase = []testCase{{ dir: `.`, exp: &SPDXConv{ curDir: tempDir, dir: tempDir, - scmDir: tempDir, - scm: gitRoot, + scm: defaultNoSCM, }, }, { dir: `/tmp`, diff --git a/testdata/Scan_noConfigAndReport/.gitignore b/testdata/Scan_noConfigAndReport/.gitignore new file mode 100644 index 0000000..405fc5e --- /dev/null +++ b/testdata/Scan_noConfigAndReport/.gitignore @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +* +!/.gitignore diff --git a/testdata/Scan_test.txt b/testdata/Scan_test.txt new file mode 100644 index 0000000..dca80da --- /dev/null +++ b/testdata/Scan_test.txt @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +Test spdxconv.Scan function. + +>>> scan/spdxconv.report +// Reset the report. + +<<< scan/spdxconv.report +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +//spdxconv:version:v1 +//spdxconv:header:path,license_id,idx_license_id,year,copyright_id,idx_copyright_id +// +//spdxconv:regular +// +Makefile,default,0,2026,default,0,#, +go.mod,default,0,2026,default,0,//, +no_copyright_year.md,match,2,2026,default,0,<!--,--> +po/included.go,match,1,2026,match,0,//, +test.go,match,1,2022,match,0,//, +test.html,match,4,2022,match,1,<!--,--> +test.sh,default,0,2026,default,0,#, +test.sql,match,1,2022,match,0,--, +// +//spdxconv:binary +// +go.sum,default,0,2026,default,0,, +test.json,default,0,2026,default,0,, +// +//spdxconv:unknown +// +// +//spdxconv:done +// +.gitignore,exist,0,,exist,1,#, +a/b/.gitignore,exist,0,,exist,1,#, +po/test.po,,0,,,0,, +po/test.pot,,0,,,0,, +with_spdx.go,exist,0,,exist,1,//, + +>>> Scan_noConfigAndReport/spdxconv.report +// Reset the report. + +<<< Scan_noConfigAndReport/spdxconv.report +// SPDX-License-Identifier: CC0-1.0 +// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +//spdxconv:version:v1 +//spdxconv:header:path,license_id,idx_license_id,year,copyright_id,idx_copyright_id +// +//spdxconv:regular +// +// +//spdxconv:binary +// +// +//spdxconv:unknown +// +.gitignore,,0,,,0,, +// +//spdxconv:done +// diff --git a/testdata/scan/.gitignore b/testdata/scan/.gitignore index c13f6d6..d7514af 100644 --- a/testdata/scan/.gitignore +++ b/testdata/scan/.gitignore @@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-3.0-only # SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> -node_modules/ -*.html !test.html +*.html +node_modules/ +spdxconv.report vendor/ |
