From 4c244e67814fe38e2cc3b45d3508a1fe16e34376 Mon Sep 17 00:00:00 2001 From: creatorHead Date: Tue, 3 Feb 2026 12:46:45 +0530 Subject: [PATCH] Fix .hbs file copyright parsing by supporting indented content - 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 --- licensecheck/update.go | 1 + licensecheck/update_test.go | 69 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/licensecheck/update.go b/licensecheck/update.go index 02811b8..3d6715a 100644 --- a/licensecheck/update.go +++ b/licensecheck/update.go @@ -251,6 +251,7 @@ var commentPrefixes = []string{ "// ", "//", "# ", "#", "% ", "%", + " ", "* ", "*", } diff --git a/licensecheck/update_test.go b/licensecheck/update_test.go index 70f7a5a..d534de5 100644 --- a/licensecheck/update_test.go +++ b/licensecheck/update_test.go @@ -4,6 +4,7 @@ package licensecheck import ( + "fmt" "os" "path/filepath" "strconv" @@ -98,6 +99,20 @@ func TestParseCopyrightLine(t *testing.T) { TrailingText: "", }, }, + { + name: "Handlebars indented copyright", + line: " Copyright IBM Corp. 2021, 2025", + lineNum: 2, + expectedInfo: &CopyrightInfo{ + LineNumber: 2, + OriginalLine: " Copyright IBM Corp. 2021, 2025", + Holder: "IBM Corp.", + StartYear: 2021, + EndYear: 2025, + Prefix: " ", + TrailingText: "", + }, + }, } for _, tt := range tests { @@ -728,6 +743,7 @@ func TestExtractCommentPrefix_AllFormats(t *testing.T) { {"Erlang style", "% Copyright", "% "}, {"Haskell/SQL style", "-- Copyright", "-- "}, {"Handlebars style", "{{! Copyright", "{{! "}, + {"Handlebars indented content", " Copyright", " "}, {"OCaml style", "(** Copyright", "(** "}, {"EJS template style", "<%/* Copyright", "<%/* "}, {"JSDoc style", "/** Copyright", "/** "}, @@ -837,3 +853,56 @@ func TestCalculateYearUpdates(t *testing.T) { assert.Equal(t, currentYear, newEnd) }) } + +func TestUpdateCopyrightHeader_HandlebarsFiles(t *testing.T) { + currentYear := time.Now().Year() + tempDir := t.TempDir() + testFile := filepath.Join(tempDir, "test.hbs") + + // Test .hbs file with multi-line comment format + initialContent := `{{! + Copyright IBM Corp. 2021, 2025 + SPDX-License-Identifier: MPL-2.0 +}} + + + +

{{title}}

+ +` + + err := os.WriteFile(testFile, []byte(initialContent), 0644) + require.NoError(t, err) + + // Test that it needs an update + needsUpdate, err := NeedsUpdate(testFile, "IBM Corp.", 2021, true) + require.NoError(t, err) + assert.True(t, needsUpdate, "Should detect that .hbs file needs copyright update") + + // Test updating the copyright header + modified, err := UpdateCopyrightHeader(testFile, "IBM Corp.", 2021, true) + require.NoError(t, err) + assert.True(t, modified, "Should successfully update .hbs file copyright") + + // Verify the content was updated correctly + content, err := os.ReadFile(testFile) + require.NoError(t, err) + + expectedContent := fmt.Sprintf(`{{! + Copyright IBM Corp. 2021, %d + SPDX-License-Identifier: MPL-2.0 +}} + + + +

{{title}}

+ +`, currentYear) + + assert.Equal(t, expectedContent, string(content)) + + // Test that it doesn't need another update + needsUpdate2, err := NeedsUpdate(testFile, "IBM Corp.", 2021, true) + require.NoError(t, err) + assert.False(t, needsUpdate2, "Should not need another update after being updated to current year") +}