Skip to content
This repository has been archived by the owner on Dec 11, 2021. It is now read-only.

Commit

Permalink
Fix multiline boilerplates (operator-framework#1544)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cblecker authored and estroz committed Jul 17, 2019
1 parent 031d71e commit d61f46f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
47 changes: 45 additions & 2 deletions internal/pkg/scaffold/boilerplate_go_txt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,56 @@ func TestBoilerplate(t *testing.T) {
}
}

const boilerplate = `// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac
func TestBoilerplateMultiline(t *testing.T) {
s, buf := setupScaffoldAndWriter()
s.Fs = afero.NewMemMapFs()
f, err := afero.TempFile(s.Fs, "", "")
if err != nil {
t.Fatal(err)
}
if _, err = f.Write([]byte(boilerplateMulti)); err != nil {
t.Fatal(err)
}
if err = f.Close(); err != nil {
t.Fatal(err)
}
s.BoilerplatePath = f.Name()
err = s.Execute(appConfig, &Apis{})
if err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}

if boilerplateMultiExp != buf.String() {
diffs := diffutil.Diff(boilerplateMultiExp, buf.String())
t.Fatalf("Expected vs actual differs.\n%v", diffs)
}
}

const (
boilerplate = `// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac
// velit a lacus tempor accumsan sit amet eu velit. Mauris orci lectus,
// rutrum vitae porttitor in, interdum nec mauris. Praesent porttitor
// lectus a sem volutpat, ac fringilla magna fermentum. Donec a nibh
// a urna fringilla eleifend. Curabitur vitae lorem nulla. Ut at risus
// varius, blandit risus quis, porta tellus. Vivamus scelerisque turpis
// quis viverra rhoncus. Aenean non arcu velit.
`
boilerplateExp = boilerplate + "\n" + apisExp
boilerplateMulti = `/*
Copyright The Project Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
const boilerplateExp = boilerplate + "\n" + apisExp
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
`
boilerplateMultiExp = boilerplateMulti + "\n" + apisExp
)
8 changes: 1 addition & 7 deletions internal/pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,8 @@ func validateBoilerplateBytes(b []byte) error {
cb = append(cb, []byte(strings.TrimSpace(c.Text)+"\n")...)
}
}
// Remove empty lines before comparison.
var tb []byte
for _, l := range bytes.Split(b, []byte("\n")) {
if len(l) > 0 {
tb = append(tb, append(bytes.TrimSpace(l), []byte("\n")...)...)
}
}
tb, cb = bytes.TrimSpace(tb), bytes.TrimSpace(cb)
tb, cb = bytes.TrimSpace(b), bytes.TrimSpace(cb)
if bytes.Compare(tb, cb) != 0 {
return fmt.Errorf(`boilerplate contains text other than comments:\n"%s"\n`, tb)
}
Expand Down

0 comments on commit d61f46f

Please sign in to comment.