From 5321f51af46535f31c79d22c70350035b6e0d173 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 14 Jan 2026 16:07:16 +0700 Subject: match_file_comment: do not add space on prefix and suffix The space should be added only when generating the SPDX lines. In the spdxconv.report, it should print the prefix and suffix as in match-file-comment. --- file.go | 35 +++++++++++++++++++++++++---------- file_test.go | 12 ++++++------ match_file_comment.go | 6 ------ spdxconv_test.go | 30 +++++++++++++++--------------- testdata/Apply_fromReport_test.txt | 2 +- 5 files changed, 47 insertions(+), 38 deletions(-) diff --git a/file.go b/file.go index c98beb1..42bc245 100644 --- a/file.go +++ b/file.go @@ -269,29 +269,44 @@ func (f *file) apply(conv *SPDXConv) (err error) { f.applyDelete(&conv.cfg) + // Write the license identifier ... + + var line bytes.Buffer + if f.commentPrefix != `` { + line.WriteString(f.commentPrefix + ` `) + } // REUSE-IgnoreStart - line := fmt.Sprintf("%sSPDX-License-Identifier: %s%s", - f.commentPrefix, f.licenseID, f.commentSuffix) + line.WriteString(`SPDX-License-Identifier: ` + f.licenseID) // REUSE-IgnoreEnd + if f.commentSuffix != `` { + line.WriteString(` ` + f.commentSuffix) + } - rawline := []byte(line) if f.hasSheBang { f.idxLicenseID = 1 } else { f.idxLicenseID = 0 } - f.topLines = slices.Insert(f.topLines, f.idxLicenseID, rawline) + f.topLines = slices.Insert(f.topLines, f.idxLicenseID, bytes.Clone(line.Bytes())) - if f.copyrightYear != `` { - f.copyrightYear += ` ` + // Write the copyright text ... + + line.Reset() + if f.commentPrefix != `` { + line.WriteString(f.commentPrefix + ` `) } // REUSE-IgnoreStart - line = fmt.Sprintf("%sSPDX-FileCopyrightText: %s%s%s", - f.commentPrefix, f.copyrightYear, f.copyrightText, f.commentSuffix) + line.WriteString(`SPDX-FileCopyrightText: `) // REUSE-IgnoreEnd + if f.copyrightYear != `` { + line.WriteString(f.copyrightYear + ` `) + } + line.WriteString(f.copyrightText) + if f.commentSuffix != `` { + line.WriteString(` ` + f.commentSuffix) + } - rawline = []byte(line) - f.topLines = slices.Insert(f.topLines, f.idxLicenseID+1, rawline) + f.topLines = slices.Insert(f.topLines, f.idxLicenseID+1, bytes.Clone(line.Bytes())) f.insertEmptyLine(f.idxLicenseID + 2) diff --git a/file_test.go b/file_test.go index 20110ca..878789c 100644 --- a/file_test.go +++ b/file_test.go @@ -26,7 +26,7 @@ func TestFile_detectComment(t *testing.T) { topLines: [][]byte{ []byte(`#!/bin/sh`), }, - commentPrefix: `# `, + commentPrefix: `#`, hasSheBang: true, }, }, { @@ -35,7 +35,7 @@ func TestFile_detectComment(t *testing.T) { }, expFile: file{ path: `test.rb`, - commentPrefix: `# `, + commentPrefix: `#`, }, }, { f: file{ @@ -43,7 +43,7 @@ func TestFile_detectComment(t *testing.T) { }, expFile: file{ path: `test.c`, - commentPrefix: `// `, + commentPrefix: `//`, }, }, { f: file{ @@ -51,7 +51,7 @@ func TestFile_detectComment(t *testing.T) { }, expFile: file{ path: `test.go`, - commentPrefix: `// `, + commentPrefix: `//`, }, }, { f: file{ @@ -59,8 +59,8 @@ func TestFile_detectComment(t *testing.T) { }, expFile: file{ path: `test.html`, - commentPrefix: ``, + commentPrefix: ``, }, }} cfg := &config{} diff --git a/match_file_comment.go b/match_file_comment.go index ac2db29..e5b4233 100644 --- a/match_file_comment.go +++ b/match_file_comment.go @@ -28,12 +28,6 @@ func (mfc *matchFileComment) init() (err error) { if err != nil { return fmt.Errorf(`%s: pattern %q: %w`, logp, mfc.Pattern, err) } - if mfc.Prefix != `` && mfc.Prefix[len(mfc.Prefix)-1] != ' ' { - mfc.Prefix += ` ` - } - if mfc.Suffix != `` && mfc.Suffix[0] != ' ' { - mfc.Suffix = ` ` + mfc.Suffix - } return nil } diff --git a/spdxconv_test.go b/spdxconv_test.go index fd318b8..d73c69f 100644 --- a/spdxconv_test.go +++ b/spdxconv_test.go @@ -39,27 +39,27 @@ func TestInit(t *testing.T) { MaxLineMatch: 10, MatchFileComment: []*matchFileComment{{ 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: `// `, + Prefix: `//`, }, { Pattern: `^.*\.(aff|bash|csh|dockerfile|env|gitignore|hcl|ipynb|make|pl|pm|py|ps1|rb|sh|tf|yaml|yml|zsh)$`, - Prefix: `# `, + Prefix: `#`, }, { Pattern: `^.*\.(css)$`, - Prefix: `/* `, - Suffix: ` */`, + Prefix: `/*`, + Suffix: `*/`, }, { Pattern: `^.*\.(fxml|htm|html|html5|kml|markdown|md|xml)$`, - Prefix: ``, + Prefix: ``, }, { Pattern: `^.*\.(lua|sql)$`, - Prefix: `-- `, + Prefix: `--`, }, { Pattern: `^.*\.(rst)$`, - Prefix: `.. `, + Prefix: `..`, }, { Pattern: `^.*\.(tex)$`, - Prefix: `% `, + Prefix: `%`, }, { Pattern: `^.*\.(apk|app|bz2|csv|doc|docx|exe|gif|gz|jpeg|jpg|json|pdf|png|ppt|pptx|svg|svgz|tar|tgz|xls|xlsx|zip)$`, }}, @@ -134,12 +134,12 @@ func TestScan(t *testing.T) { // //spdxconv:regular // -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,-- , +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 // diff --git a/testdata/Apply_fromReport_test.txt b/testdata/Apply_fromReport_test.txt index 33d4e85..cecf3ee 100644 --- a/testdata/Apply_fromReport_test.txt +++ b/testdata/Apply_fromReport_test.txt @@ -62,7 +62,7 @@ delete_line_after = "^(//+|#+|\\*+/|--+>|--+)$" >>> spdxconv.report //spdxconv:regular -empty.html,default,0,2026,default,0," +empty.html,default,0,2026,default,0, //spdxconv:binary //spdxconv:unknown -- cgit v1.3