From b37a4cd18827d6a8dafe2729ba72a8e13e8fbb8c Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 14 Jan 2026 04:40:19 +0700 Subject: all: fix error when scanning and apply empty file --- file.go | 3 ++ spdxconv_test.go | 32 +++++++++++++++ testdata/Apply_fromReport/.gitignore | 5 +++ testdata/Apply_fromReport_test.txt | 76 ++++++++++++++++++++++++++++++++++++ testdata/Apply_test.txt | 6 +++ 5 files changed, 122 insertions(+) create mode 100644 testdata/Apply_fromReport/.gitignore create mode 100644 testdata/Apply_fromReport_test.txt diff --git a/file.go b/file.go index 11d2154..c98beb1 100644 --- a/file.go +++ b/file.go @@ -95,6 +95,9 @@ func (f *file) initLines(content []byte, maxLine int) (err error) { if err != nil { return err } + if len(content) == 0 { + return nil + } } f.lines = bytes.Split(content, []byte{'\n'}) diff --git a/spdxconv_test.go b/spdxconv_test.go index aa37cba..f473a2e 100644 --- a/spdxconv_test.go +++ b/spdxconv_test.go @@ -189,6 +189,38 @@ func TestApply(t *testing.T) { } } +func TestApply_fromReport(t *testing.T) { + // Populate the test files. + + var testData *test.Data + var err error + testData, err = test.LoadData(`testdata/Apply_fromReport_test.txt`) + if err != nil { + t.Fatal(err) + } + var tempDir = `testdata/Apply_fromReport/` + testData.ExtractInput(tempDir) + t.Chdir(tempDir) + + err = Apply() + if err != nil { + t.Fatal(err) + } + + for input, _ := range testData.Input { + if input == ConfigFile || input == ReportFile { + continue + } + + got, err := os.ReadFile(input) + if err != nil { + t.Fatal(err) + } + test.Assert(t, input+`: after`, + string(testData.Output[input]), string(got)) + } +} + func TestNew(t *testing.T) { type testCase struct { exp *SPDXConv diff --git a/testdata/Apply_fromReport/.gitignore b/testdata/Apply_fromReport/.gitignore new file mode 100644 index 0000000..234c892 --- /dev/null +++ b/testdata/Apply_fromReport/.gitignore @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 M. Shulhan + +/* +!/.gitignore diff --git a/testdata/Apply_fromReport_test.txt b/testdata/Apply_fromReport_test.txt new file mode 100644 index 0000000..33d4e85 --- /dev/null +++ b/testdata/Apply_fromReport_test.txt @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 M. Shulhan + +// REUSE-IgnoreStart + +>>> spdxconv.cfg +# SPDX-License-Identifier: CC0-1.0 +# SPDX-FileCopyrightText: 2026 M. Shulhan + +[default] +license_identifier = GPL-3.0-only +copyright_year = 2026 +file_copyright_text = M. Shulhan + +[match-file-comment] +pattern = "^.*\\.(adoc|asciidoc|c|cc|cpp|cs|dart|go|h|hh|hpp|java|js|jsx|jsonc|kt|kts|php|rs|sass|scss|swift|ts|tsx)$" +prefix = "//" + +[match-file-comment] +pattern = "^.*\\.(aff|bash|csh|dockerfile|env|gitignore|hcl|ipynb|make|pl|pm|py|ps1|rb|sh|tf|yaml|yml|zsh)$" +prefix = "#" + +[match-file-comment] +pattern = "^.*\\.(css)$" +prefix = "/*" +suffix = "*/" + +[match-file-comment] +pattern = "^.*\\.(fxml|htm|html|html5|kml|markdown|md|xml)$" +prefix = "" + +[match-file-comment] +pattern = "^.*\\.(lua|sql)$" +prefix = "--" + +[match-file-comment] +pattern = "^.*\\.(rst)$" +prefix = ".." + +[match-file-comment] +pattern = "^.*\\.(tex)$" +prefix = "%" + +# File name that match with this pattern will have the ".license" file +# created. +[match-file-comment] +pattern = "^.*\\.(apk|app|bz2|csv|doc|docx|exe|gif|gz|jpeg|jpg|json|pdf|png|ppt|pptx|svg|svgz|tar|tgz|xls|xlsx|zip)$" + +[match-license] +pattern = "^(//+|#+|/\\*+|" +//spdxconv:binary +//spdxconv:unknown + +>>> empty.html + +<<< empty.html + + + +<<< END +// REUSE-IgnoreEnd diff --git a/testdata/Apply_test.txt b/testdata/Apply_test.txt index a8c3e6d..13ee6be 100644 --- a/testdata/Apply_test.txt +++ b/testdata/Apply_test.txt @@ -59,6 +59,12 @@ pattern = "^(//+|#+|/\\*+| + + >>> without_spdx_license_id.go 1 2 -- cgit v1.3