1// Copyright (c) 2020 Denis Tingajkin
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at:
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package goheader_test
18
19import (
20	"go/ast"
21	"go/parser"
22	"go/token"
23	"io/ioutil"
24	"os"
25	"path"
26	"testing"
27
28	goheader "github.com/denis-tingajkin/go-header"
29	"github.com/stretchr/testify/require"
30)
31
32func header(header string) *goheader.Target {
33	return &goheader.Target{
34		File: &ast.File{
35			Comments: []*ast.CommentGroup{
36				{
37					List: []*ast.Comment{
38						{
39							Text: header,
40						},
41					},
42				},
43			},
44			Package: token.Pos(len(header)),
45		},
46		Path: os.TempDir(),
47	}
48}
49
50func TestAnalyzer_Analyze1(t *testing.T) {
51	a := goheader.New(
52		goheader.WithTemplate("A {{ YEAR }}\nB"),
53		goheader.WithValues(map[string]goheader.Value{
54			"YEAR": &goheader.ConstValue{
55				RawValue: "2020",
56			},
57		}))
58	issue := a.Analyze(header(`A 2020
59B`))
60	require.Nil(t, issue)
61}
62
63func TestAnalyzer_Analyze2(t *testing.T) {
64	a := goheader.New(
65		goheader.WithTemplate("{{COPYRIGHT HOLDER}}TEXT"),
66		goheader.WithValues(map[string]goheader.Value{
67			"COPYRIGHT HOLDER": &goheader.RegexpValue{
68				RawValue: "(A {{ YEAR }}\n(.*)\n)+",
69			},
70			"YEAR": &goheader.ConstValue{
71				RawValue: "2020",
72			},
73		}))
74	issue := a.Analyze(header(`A 2020
75B
76A 2020
77B
78TEXT
79`))
80	require.Nil(t, issue)
81}
82
83func TestAnalyzer_Analyze3(t *testing.T) {
84	a := goheader.New(
85		goheader.WithTemplate("{{COPYRIGHT HOLDER}}TEXT"),
86		goheader.WithValues(map[string]goheader.Value{
87			"COPYRIGHT HOLDER": &goheader.RegexpValue{
88				RawValue: "(A {{ YEAR }}\n(.*)\n)+",
89			},
90			"YEAR": &goheader.ConstValue{
91				RawValue: "2020",
92			},
93		}))
94	issue := a.Analyze(header(`A 2020
95B
96A 2021
97B
98TEXT
99`))
100	require.NotNil(t, issue)
101}
102
103func TestAnalyzer_Analyze4(t *testing.T) {
104	a := goheader.New(
105		goheader.WithTemplate("{{ A }}"),
106		goheader.WithValues(map[string]goheader.Value{
107			"A": &goheader.RegexpValue{
108				RawValue: "[{{ B }}{{ C }}]{{D}}",
109			},
110			"B": &goheader.ConstValue{
111				RawValue: "a-",
112			},
113			"C": &goheader.RegexpValue{
114				RawValue: "z",
115			},
116			"D": &goheader.ConstValue{
117				RawValue: "{{E}}",
118			},
119			"E": &goheader.ConstValue{
120				RawValue: "{7}",
121			},
122		}))
123	issue := a.Analyze(header(`abcdefg`))
124	require.Nil(t, issue)
125}
126
127func TestAnalyzer_Analyze5(t *testing.T) {
128	a := goheader.New(goheader.WithTemplate("abc"))
129	p := path.Join(os.TempDir(), t.Name()+".go")
130	defer func() {
131		_ = os.Remove(p)
132	}()
133	err := ioutil.WriteFile(p, []byte("/*abc*///comment\npackage abc"), os.ModePerm)
134	require.Nil(t, err)
135	s := token.NewFileSet()
136	f, err := parser.ParseFile(s, p, nil, parser.ParseComments)
137	require.Nil(t, err)
138	require.Nil(t, a.Analyze(&goheader.Target{File: f, Path: p}))
139}
140
141func TestREADME(t *testing.T) {
142	a := goheader.New(
143		goheader.WithTemplate(`{{ MY COMPANY }}
144SPDX-License-Identifier: Apache-2.0
145
146Licensed under the Apache License, Version 2.0 (the "License");
147you may not use this file except in compliance with the License.
148You may obtain a copy of the License at:
149
150    http://www.apache.org/licenses/LICENSE-2.0
151
152Unless required by applicable law or agreed to in writing, software
153distributed under the License is distributed on an "AS IS" BASIS,
154WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
155See the License for the specific language governing permissions and
156limitations under the License.`),
157		goheader.WithValues(map[string]goheader.Value{
158			"MY COMPANY": &goheader.ConstValue{
159				RawValue: "mycompany.com",
160			},
161		}))
162	issue := a.Analyze(header(`mycompany.com
163SPDX-License-Identifier: Apache-2.0
164
165Licensed under the Apache License, Version 2.0 (the "License");
166you may not use this file except in compliance with the License.
167You may obtain a copy of the License at:
168
169    http://www.apache.org/licenses/LICENSE-2.0
170
171Unless required by applicable law or agreed to in writing, software
172distributed under the License is distributed on an "AS IS" BASIS,
173WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
174See the License for the specific language governing permissions and
175limitations under the License.`))
176	require.Nil(t, issue)
177}
178