Skip to content

Commit 99b7b09

Browse files
author
Eric Stroczynski
authored
v0.8.x: fix boilerplate check (#1544) (#1690)
* Fix multiline boilerplates (#1544) Go permits multiline, block-style comments. It's common for boilerplates to utilize these for licences (e.g. Apache-2.0's standard boilerplate). This PR removes the line-by-line stripping of whitespace, and adds a test for it. * CHANGELOG.md: add #1544
1 parent 22af0b1 commit 99b7b09

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Bug Fixes
1212

13+
- Fixes header file content validation when the content contains empty lines or centered text. ([#1544](https://github.com/operator-framework/operator-sdk/pull/1544))
14+
1315
## v0.8.1
1416

1517
### Bug Fixes

internal/pkg/scaffold/boilerplate_go_txt_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,56 @@ func TestBoilerplate(t *testing.T) {
4747
}
4848
}
4949

50-
const boilerplate = `// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac
50+
func TestBoilerplateMultiline(t *testing.T) {
51+
s, buf := setupScaffoldAndWriter()
52+
s.Fs = afero.NewMemMapFs()
53+
f, err := afero.TempFile(s.Fs, "", "")
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
if _, err = f.Write([]byte(boilerplateMulti)); err != nil {
58+
t.Fatal(err)
59+
}
60+
if err = f.Close(); err != nil {
61+
t.Fatal(err)
62+
}
63+
s.BoilerplatePath = f.Name()
64+
err = s.Execute(appConfig, &Apis{})
65+
if err != nil {
66+
t.Fatalf("Failed to execute the scaffold: (%v)", err)
67+
}
68+
69+
if boilerplateMultiExp != buf.String() {
70+
diffs := diffutil.Diff(boilerplateMultiExp, buf.String())
71+
t.Fatalf("Expected vs actual differs.\n%v", diffs)
72+
}
73+
}
74+
75+
const (
76+
boilerplate = `// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac
5177
// velit a lacus tempor accumsan sit amet eu velit. Mauris orci lectus,
5278
// rutrum vitae porttitor in, interdum nec mauris. Praesent porttitor
5379
// lectus a sem volutpat, ac fringilla magna fermentum. Donec a nibh
5480
// a urna fringilla eleifend. Curabitur vitae lorem nulla. Ut at risus
5581
// varius, blandit risus quis, porta tellus. Vivamus scelerisque turpis
5682
// quis viverra rhoncus. Aenean non arcu velit.
5783
`
84+
boilerplateExp = boilerplate + "\n" + apisExp
85+
boilerplateMulti = `/*
86+
Copyright The Project Authors.
87+
88+
Licensed under the Apache License, Version 2.0 (the "License");
89+
you may not use this file except in compliance with the License.
90+
You may obtain a copy of the License at
5891
59-
const boilerplateExp = boilerplate + "\n" + apisExp
92+
http://www.apache.org/licenses/LICENSE-2.0
93+
94+
Unless required by applicable law or agreed to in writing, software
95+
distributed under the License is distributed on an "AS IS" BASIS,
96+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97+
See the License for the specific language governing permissions and
98+
limitations under the License.
99+
*/
100+
`
101+
boilerplateMultiExp = boilerplateMulti + "\n" + apisExp
102+
)

internal/pkg/scaffold/scaffold.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,8 @@ func validateBoilerplateBytes(b []byte) error {
9797
cb = append(cb, []byte(strings.TrimSpace(c.Text)+"\n")...)
9898
}
9999
}
100-
// Remove empty lines before comparison.
101100
var tb []byte
102-
for _, l := range bytes.Split(b, []byte("\n")) {
103-
if len(l) > 0 {
104-
tb = append(tb, append(bytes.TrimSpace(l), []byte("\n")...)...)
105-
}
106-
}
107-
tb, cb = bytes.TrimSpace(tb), bytes.TrimSpace(cb)
101+
tb, cb = bytes.TrimSpace(b), bytes.TrimSpace(cb)
108102
if bytes.Compare(tb, cb) != 0 {
109103
return fmt.Errorf(`boilerplate contains text other than comments:\n"%s"\n`, tb)
110104
}

0 commit comments

Comments
 (0)