Skip to content

Commit 68ab096

Browse files
authored
header: fix default variable regexp (#7)
1 parent fd23627 commit 68ab096

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Project
22
_bench/
3+
.cover
34
bin/
45
dist/
56

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ run: build
4949

5050
.PHONY: test
5151
test:
52-
go test ./...
52+
go test -v -coverprofile=.cover ./...
53+
54+
.PHONY: cover
55+
cover:
56+
go tool cover -html .cover
5357

5458
.PHONY: test-race
5559
test-race:

cmd/golicenser/golicenser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ var analyzer = &analysis.Analyzer{
114114
}
115115

116116
// Parse variables
117-
vars := make(map[string]golicenser.Var)
117+
vars := make(map[string]*golicenser.Var)
118118
if variables != "" {
119119
for _, v := range strings.Split(variables, ",") {
120120
parts := strings.SplitN(v, "=", 2)
121121
if len(parts) != 2 {
122122
log.Fatal("invalid variable: ", v)
123123
}
124-
vars[parts[0]] = golicenser.Var{Value: parts[1]}
124+
vars[parts[0]] = &golicenser.Var{Value: parts[1]}
125125
}
126126
}
127127
if variableRegexps != "" {

header.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ type Header struct {
220220
matcher *regexp.Regexp
221221

222222
author string
223-
variables map[string]Var
223+
variables map[string]*Var
224224
yearMode YearMode
225225
commentStyle CommentStyle
226226
}
@@ -246,7 +246,7 @@ type HeaderOpts struct {
246246
MatcherEscape bool
247247
Author string
248248
AuthorRegexp string
249-
Variables map[string]Var
249+
Variables map[string]*Var
250250
YearMode YearMode
251251
CommentStyle CommentStyle
252252
}
@@ -429,7 +429,7 @@ func (h *Header) render(filename, year string) (string, error) {
429429
return b.String(), nil
430430
}
431431

432-
func headerMatcher(tmpl *template.Template, escapeTmpl bool, authorRegexp *regexp.Regexp, variables map[string]Var) (*regexp.Regexp, error) {
432+
func headerMatcher(tmpl *template.Template, escapeTmpl bool, authorRegexp *regexp.Regexp, variables map[string]*Var) (*regexp.Regexp, error) {
433433
m := map[string]string{
434434
"author": "__VAR_author__",
435435
"filename": "__VAR_filename__",
@@ -468,7 +468,7 @@ func headerMatcher(tmpl *template.Template, escapeTmpl bool, authorRegexp *regex
468468
return regexp.Compile(headerExpr)
469469
}
470470

471-
func addVariables(m map[string]any, vars map[string]Var) {
471+
func addVariables(m map[string]any, vars map[string]*Var) {
472472
for k, v := range vars {
473473
m[k] = v.Value
474474
}

header_test.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestNewHeader(t *testing.T) {
219219
header: HeaderOpts{
220220
Template: "{{.project}} by {{.person}}",
221221
Author: "test",
222-
Variables: map[string]Var{
222+
Variables: map[string]*Var{
223223
"project": {Value: "project"},
224224
"person": {Value: "person"},
225225
},
@@ -230,7 +230,7 @@ func TestNewHeader(t *testing.T) {
230230
header: HeaderOpts{
231231
Template: "{{.project}} by {{.person}}",
232232
Author: "test",
233-
Variables: map[string]Var{
233+
Variables: map[string]*Var{
234234
"project": {Value: "project", Regexp: "(golicenser|project)"},
235235
"person": {Value: "human", Regexp: "(human|person)"},
236236
},
@@ -241,7 +241,7 @@ func TestNewHeader(t *testing.T) {
241241
header: HeaderOpts{
242242
Template: "{{.project}} by {{.person}}",
243243
Author: "test",
244-
Variables: map[string]Var{
244+
Variables: map[string]*Var{
245245
"project": {Value: "project", Regexp: "(project"},
246246
"person": {Value: "person", Regexp: "person)"},
247247
},
@@ -341,7 +341,7 @@ func TestHeaderCreate(t *testing.T) {
341341
header: HeaderOpts{
342342
Template: "{{.project}} by {{.person}}",
343343
Author: "test",
344-
Variables: map[string]Var{
344+
Variables: map[string]*Var{
345345
"project": {Value: "project"},
346346
"person": {Value: "person"},
347347
},
@@ -353,7 +353,7 @@ func TestHeaderCreate(t *testing.T) {
353353
header: HeaderOpts{
354354
Template: "{{.project}} by {{.person}}",
355355
Author: "test",
356-
Variables: map[string]Var{
356+
Variables: map[string]*Var{
357357
"project": {Value: "project", Regexp: "(golicenser|project)"},
358358
"person": {Value: "human", Regexp: "(human|person)"},
359359
},
@@ -460,6 +460,35 @@ func TestHeaderUpdate(t *testing.T) {
460460
want: "// Copyright (c) 2025 Joshua Sing\n",
461461
wantModified: true,
462462
},
463+
{
464+
name: "custom variables",
465+
header: HeaderOpts{
466+
Template: "Copyright (c) {{.year}} {{.author}}\nProject: {{.project}}, Greet: {{.greet}}",
467+
Author: "Joshua Sing",
468+
YearMode: YearModeThisYear,
469+
Variables: map[string]*Var{
470+
"project": {Value: "project"},
471+
"greet": {Value: "Hello world"},
472+
},
473+
},
474+
existing: "// Copyright (c) 2024 Joshua Sing\n// Project: project, Greet: Hello world",
475+
want: "// Copyright (c) 2025 Joshua Sing\n// Project: project, Greet: Hello world\n",
476+
wantModified: true,
477+
},
478+
{
479+
name: "custom variables with regexp",
480+
header: HeaderOpts{
481+
Template: "Copyright (c) {{.year}} {{.author}}\nProject: {{.project}}, Greet: {{.greet}}",
482+
Author: "Joshua Sing",
483+
Variables: map[string]*Var{
484+
"project": {Value: "project"},
485+
"greet": {Value: "Hello world", Regexp: "Hello (.+)"},
486+
},
487+
},
488+
existing: "// Copyright (c) 2025 Joshua Sing\n// Project: project, Greet: Hello there!",
489+
want: "// Copyright (c) 2025 Joshua Sing\n// Project: project, Greet: Hello world\n",
490+
wantModified: true,
491+
},
463492
{
464493
name: "change MIT to OpenBSD",
465494
header: HeaderOpts{
@@ -537,7 +566,7 @@ func TestHeaderMatcher(t *testing.T) {
537566
name string
538567
matcher string
539568
escape bool
540-
variables map[string]Var
569+
variables map[string]*Var
541570
authorRegexp *regexp.Regexp
542571
wantErr bool
543572
matchTests []matchTest
@@ -574,7 +603,7 @@ func TestHeaderMatcher(t *testing.T) {
574603
name: "custom variables",
575604
matcher: "{{.project}} by {{.name}} - Copyright (c) {{.year}} {{.author}}",
576605
escape: true,
577-
variables: map[string]Var{
606+
variables: map[string]*Var{
578607
"project": {Value: "golicenser", Regexp: "golicenser"},
579608
"name": {Value: "joshuasing", Regexp: "joshuasing"},
580609
},
@@ -596,7 +625,7 @@ func TestHeaderMatcher(t *testing.T) {
596625
name: "custom variables with regexp",
597626
matcher: "{{.project}} by {{.name}} - Copyright (c) {{.year}} {{.author}}",
598627
escape: true,
599-
variables: map[string]Var{
628+
variables: map[string]*Var{
600629
"project": {Value: "golicenser", Regexp: "go-?licenser"},
601630
"name": {Value: "joshuasing", Regexp: "(joshuasing|someone)"},
602631
},

0 commit comments

Comments
 (0)