| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "path" |
| 23 | "testing" |
| 24 | |
| 25 | "golang.org/x/tools/go/analysis/analysistest" |
| 26 | "golang.org/x/tools/go/analysis/passes/printf" |
| 27 | ) |
| 28 | |
| 29 | // TestGoVet checks that "go vet" detects incorrect klog calls like |
| 30 | // mismatched format specifiers and arguments. |
| 31 | func TestGoVet(t *testing.T) { |
| 32 | testdata := analysistest.TestData() |
| 33 | src := path.Join(testdata, "src") |
| 34 | t.Cleanup(func() { |
| 35 | os.RemoveAll(src) |
| 36 | }) |
| 37 | |
| 38 | // analysistest doesn't support using existing code |
| 39 | // via modules (https://github.com/golang/go/issues/37054). |
| 40 | // Populating the "testdata/src" directory with the |
| 41 | // result of "go mod vendor" is a workaround. |
| 42 | cmd := exec.Command("go", "mod", "vendor", "-o", src) |
| 43 | out, err := cmd.CombinedOutput() |
| 44 | if err != nil { |
| 45 | t.Fatalf("%s failed: %v\nOutput: %s", cmd, err, string(out)) |
| 46 | } |
| 47 | |
| 48 | analyzer := printf.Analyzer |
| 49 | analysistest.Run(t, testdata, analyzer, "") |
| 50 | } |