Skip to content

Commit 04ed9c9

Browse files
authored
Add YAML files to license header generation (#4518)
1 parent ddb8521 commit 04ed9c9

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

private/pkg/licenseheader/licenseheader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ All rights reserved.`,
8989
".h": "//",
9090
".py": "#",
9191
".pyi": "#",
92+
".yaml": "#",
93+
".yml": "#",
9294
}
9395
)
9496

private/pkg/licenseheader/licenseheader_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ const (
3939
testProprietaryGoHeader = `// Copyright 2020-2021 Foo Bar, Inc.
4040
//
4141
// All rights reserved.`
42+
testApacheYAMLHeader = `# Copyright 2020-2021 Foo Bar, Inc.
43+
#
44+
# Licensed under the Apache License, Version 2.0 (the "License");
45+
# you may not use this file except in compliance with the License.
46+
# You may obtain a copy of the License at
47+
#
48+
# http://www.apache.org/licenses/LICENSE-2.0
49+
#
50+
# Unless required by applicable law or agreed to in writing, software
51+
# distributed under the License is distributed on an "AS IS" BASIS,
52+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53+
# See the License for the specific language governing permissions and
54+
# limitations under the License.`
4255
)
4356

4457
func TestBasic(t *testing.T) {
@@ -116,3 +129,80 @@ package foo`,
116129
string(modifiedData),
117130
)
118131
}
132+
133+
func TestYAML(t *testing.T) {
134+
t.Parallel()
135+
136+
const yamlBody = `- name: Kind
137+
type: byte`
138+
139+
modifiedData, err := Modify(
140+
LicenseTypeApache,
141+
testCopyrightHolder,
142+
testYearRange,
143+
"foo/bar.yaml",
144+
[]byte(yamlBody),
145+
)
146+
require.NoError(t, err)
147+
require.Equal(
148+
t,
149+
testApacheYAMLHeader+"\n\n"+yamlBody,
150+
string(modifiedData),
151+
)
152+
require.Contains(
153+
t,
154+
string(modifiedData),
155+
"http://www.apache.org/licenses/LICENSE-2.0",
156+
"URL must not be clobbered when stamping a YAML license header",
157+
)
158+
159+
// Repair an existing YAML header where // in the URL was clobbered to #.
160+
clobberedData := `# Copyright 2020-2024 Foo Bar, Inc.
161+
#
162+
# Licensed under the Apache License, Version 2.0 (the "License");
163+
# you may not use this file except in compliance with the License.
164+
# You may obtain a copy of the License at
165+
#
166+
# http:#www.apache.org/licenses/LICENSE-2.0
167+
#
168+
# Unless required by applicable law or agreed to in writing, software
169+
# distributed under the License is distributed on an "AS IS" BASIS,
170+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
171+
# See the License for the specific language governing permissions and
172+
# limitations under the License.
173+
174+
` + yamlBody
175+
modifiedData, err = Modify(
176+
LicenseTypeApache,
177+
testCopyrightHolder,
178+
testYearRange,
179+
"foo/bar.yaml",
180+
[]byte(clobberedData),
181+
)
182+
require.NoError(t, err)
183+
require.Equal(
184+
t,
185+
testApacheYAMLHeader+"\n\n"+yamlBody,
186+
string(modifiedData),
187+
)
188+
require.NotContains(
189+
t,
190+
string(modifiedData),
191+
"http:#www.apache.org",
192+
"clobbered URL must be repaired",
193+
)
194+
195+
modifiedData, err = Modify(
196+
LicenseTypeApache,
197+
testCopyrightHolder,
198+
testYearRange,
199+
"foo/bar.yml",
200+
[]byte(yamlBody),
201+
)
202+
require.NoError(t, err)
203+
require.Equal(
204+
t,
205+
testApacheYAMLHeader+"\n\n"+yamlBody,
206+
string(modifiedData),
207+
)
208+
}

0 commit comments

Comments
 (0)