kubernetes/cmd/gotemplate/gotemplate_test.go
Patrick Ohly ce9e668a93 golangci-lint: suppress one issue, demote others to "hints"
The voting in https://github.com/kubernetes/kubernetes/issues/117288 led to
one check that got rejected ("ifElseChain: rewrite if-else to switch
statement") and several that are "nice to know".

golangci-lint's support for issue "severity" is too limited to identify "nice
to know" issues in the output (filtering is only by linter without considering
the issue text; not part of text output). Therefore a third configuration gets
added which emits all issues (must fix and nits). The intention is to use
the "strict" configuration in pull-kubernetes-verify and the "hints"
configuration in a new non-blocking pull-kubernetes-linter-hints.

That way, "must fix" issues will block merging while issues that may be useful
will show up in a failed optional job. However, that job then also contains
"must fix" issues, partly because filtering out those would make the
configuration a lot larger and is likely to be unreliably (all "must fix"
issues would need to be identified and listed), partly because it may be useful
to have all issues in one place.

The previous approach of manually keeping two configs in sync with special
comments didn't scale to three configs. Now a single golangci.yaml.in with
text/template constructs contains the source for all three configs. A new
simple CLI frontend for text/template (cmd/gotemplate) is used by
hack/update-golangci-lint-config.sh to generate the three flavors.
2023-08-22 20:39:23 +02:00

109 lines
2.6 KiB
Go

/*
Copyright 2021 The Kubernetes 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
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.
*/
package main
import (
"bytes"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenerate(t *testing.T) {
for name, tt := range map[string]struct {
in string
data map[string]string
files map[string]string
expected string
expectedErr string
}{
"missing-file": {
in: `{{include "no-such-file.txt"}}`,
expectedErr: "open no-such-file.txt: no such file or directory",
},
"data": {
in: `{{.Hello}} {{.World}}`,
data: map[string]string{"Hello": "world", "World": "hello"},
expected: "world hello",
},
"include": {
in: `{{include "test.txt" | indent 2}}`,
files: map[string]string{"test.txt": "hello\nworld"},
expected: "hello\n world",
},
} {
cwd, err := os.Getwd()
require.NoError(t, err)
t.Run(name, func(t *testing.T) {
tmp := t.TempDir()
for fileName, fileContent := range tt.files {
err := os.WriteFile(path.Join(tmp, fileName), []byte(fileContent), 0666)
require.NoError(t, err, "create input file")
}
defer os.Chdir(cwd)
require.NoError(t, os.Chdir(tmp), "change into tmp directory")
in := strings.NewReader(tt.in)
var out bytes.Buffer
err := generate(in, &out, tt.data)
if tt.expectedErr == "" {
require.NoError(t, err, "expand template")
require.Equal(t, tt.expected, out.String())
} else {
require.Contains(t, err.Error(), tt.expectedErr)
}
})
}
}
func TestIndent(t *testing.T) {
for name, tt := range map[string]struct {
numSpaces int
content string
expected string
}{
"empty": {
numSpaces: 10,
content: "",
expected: "",
},
"trailing-newline": {
numSpaces: 2,
content: "hello\nworld\n",
expected: "hello\n world\n ",
},
"no-trailing-newline": {
numSpaces: 1,
content: "hello\nworld",
expected: "hello\n world",
},
"zero-indent": {
numSpaces: 0,
content: "hello\nworld",
expected: "hello\nworld",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tt.expected, indent(tt.numSpaces, tt.content))
})
}
}