Skip to content

Commit 8df03b7

Browse files
authored
Fix .hbs file copyright parsing by supporting indented content (#188)
- Add support for handlebars (.hbs) files with {{! ... }} multi-line comments - Copyright lines in .hbs files are indented with spaces but lack direct comment prefixes - Added ' ' (two spaces) to commentPrefixes array to handle indented content within multi-line comments - Added comprehensive test coverage for handlebars copyright parsing - All existing tests continue to pass Revert copyright year change in update.go - Change back from 2022, 2026 to 2023, 2026
1 parent be31874 commit 8df03b7

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

licensecheck/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ var commentPrefixes = []string{
251251
"// ", "//",
252252
"# ", "#",
253253
"% ", "%",
254+
" ",
254255
"* ", "*",
255256
}
256257

licensecheck/update_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package licensecheck
55

66
import (
7+
"fmt"
78
"os"
89
"path/filepath"
910
"strconv"
@@ -98,6 +99,20 @@ func TestParseCopyrightLine(t *testing.T) {
9899
TrailingText: "",
99100
},
100101
},
102+
{
103+
name: "Handlebars indented copyright",
104+
line: " Copyright IBM Corp. 2021, 2025",
105+
lineNum: 2,
106+
expectedInfo: &CopyrightInfo{
107+
LineNumber: 2,
108+
OriginalLine: " Copyright IBM Corp. 2021, 2025",
109+
Holder: "IBM Corp.",
110+
StartYear: 2021,
111+
EndYear: 2025,
112+
Prefix: " ",
113+
TrailingText: "",
114+
},
115+
},
101116
}
102117

103118
for _, tt := range tests {
@@ -728,6 +743,7 @@ func TestExtractCommentPrefix_AllFormats(t *testing.T) {
728743
{"Erlang style", "% Copyright", "% "},
729744
{"Haskell/SQL style", "-- Copyright", "-- "},
730745
{"Handlebars style", "{{! Copyright", "{{! "},
746+
{"Handlebars indented content", " Copyright", " "},
731747
{"OCaml style", "(** Copyright", "(** "},
732748
{"EJS template style", "<%/* Copyright", "<%/* "},
733749
{"JSDoc style", "/** Copyright", "/** "},
@@ -837,3 +853,56 @@ func TestCalculateYearUpdates(t *testing.T) {
837853
assert.Equal(t, currentYear, newEnd)
838854
})
839855
}
856+
857+
func TestUpdateCopyrightHeader_HandlebarsFiles(t *testing.T) {
858+
currentYear := time.Now().Year()
859+
tempDir := t.TempDir()
860+
testFile := filepath.Join(tempDir, "test.hbs")
861+
862+
// Test .hbs file with multi-line comment format
863+
initialContent := `{{!
864+
Copyright IBM Corp. 2021, 2025
865+
SPDX-License-Identifier: MPL-2.0
866+
}}
867+
868+
<html>
869+
<body>
870+
<h1>{{title}}</h1>
871+
</body>
872+
</html>`
873+
874+
err := os.WriteFile(testFile, []byte(initialContent), 0644)
875+
require.NoError(t, err)
876+
877+
// Test that it needs an update
878+
needsUpdate, err := NeedsUpdate(testFile, "IBM Corp.", 2021, true)
879+
require.NoError(t, err)
880+
assert.True(t, needsUpdate, "Should detect that .hbs file needs copyright update")
881+
882+
// Test updating the copyright header
883+
modified, err := UpdateCopyrightHeader(testFile, "IBM Corp.", 2021, true)
884+
require.NoError(t, err)
885+
assert.True(t, modified, "Should successfully update .hbs file copyright")
886+
887+
// Verify the content was updated correctly
888+
content, err := os.ReadFile(testFile)
889+
require.NoError(t, err)
890+
891+
expectedContent := fmt.Sprintf(`{{!
892+
Copyright IBM Corp. 2021, %d
893+
SPDX-License-Identifier: MPL-2.0
894+
}}
895+
896+
<html>
897+
<body>
898+
<h1>{{title}}</h1>
899+
</body>
900+
</html>`, currentYear)
901+
902+
assert.Equal(t, expectedContent, string(content))
903+
904+
// Test that it doesn't need another update
905+
needsUpdate2, err := NeedsUpdate(testFile, "IBM Corp.", 2021, true)
906+
require.NoError(t, err)
907+
assert.False(t, needsUpdate2, "Should not need another update after being updated to current year")
908+
}

0 commit comments

Comments
 (0)