From ff4136b1a2f0a66377c9e72573a5948612387aa2 Mon Sep 17 00:00:00 2001 From: Katrina Verey Date: Tue, 29 Mar 2022 18:50:40 -0400 Subject: [PATCH] cmd/config linter updates --- cmd/config/.golangci.yml | 38 +++++++++++++------ cmd/config/Makefile | 3 +- cmd/config/internal/commands/annotate.go | 1 - cmd/config/internal/commands/annotate_test.go | 10 ++--- cmd/config/internal/commands/cat_test.go | 10 ++--- .../internal/commands/cmdcreatesetter_test.go | 20 +++++----- .../commands/cmdcreatesubstitution_test.go | 20 +++++----- .../internal/commands/cmddeletesetter_test.go | 22 +++++------ .../commands/cmddeletesubstitution_test.go | 22 +++++------ .../internal/commands/cmdlistsetters_test.go | 2 +- cmd/config/internal/commands/cmdset_test.go | 14 +++---- cmd/config/internal/commands/cmdxargs.go | 2 +- cmd/config/internal/commands/count_test.go | 10 ++--- cmd/config/internal/commands/e2e/e2e_test.go | 16 ++++---- .../internal/commands/e2e/test_util_test.go | 10 +++-- cmd/config/internal/commands/fmt.go | 1 - cmd/config/internal/commands/fmt_test.go | 10 ++--- cmd/config/internal/commands/grep.go | 14 +++---- cmd/config/internal/commands/grep_test.go | 10 ++--- cmd/config/internal/commands/merge3_test.go | 24 ++++++------ cmd/config/internal/commands/run-fns.go | 3 +- cmd/config/internal/commands/run_test.go | 1 - cmd/config/internal/commands/source_test.go | 27 +++++++++++-- cmd/config/runner/runner_test.go | 26 ++++++------- 24 files changed, 178 insertions(+), 138 deletions(-) diff --git a/cmd/config/.golangci.yml b/cmd/config/.golangci.yml index d9f6c6a33..18de10587 100644 --- a/cmd/config/.golangci.yml +++ b/cmd/config/.golangci.yml @@ -12,35 +12,35 @@ linters: - bodyclose - deadcode - depguard -# - dogsled + - dogsled - dupl # - errcheck + - exportloopref # - funlen -# - gochecknoinits -# - goconst -# - gocritic -# - gocyclo + - gochecknoinits + - goconst + - gocritic + - gocyclo - gofmt - goimports -# - gosec + - gosec - gosimple - govet - ineffassign -# - lll + - lll - misspell - nakedret + - nolintlint - revive -# - scopelint - staticcheck - structcheck -# - stylecheck +# - stylecheck # seems redundant with revive, which replaced golint - typecheck - unconvert -# - unparam + - unparam - unused - varcheck -# - whitespace - + - whitespace linters-settings: dupl: @@ -49,4 +49,18 @@ linters-settings: line-length: 170 gocyclo: min-complexity: 30 + revive: + rules: + - name: var-naming + arguments: + - [ "ID", "API", "JSON" ] # AllowList + - [ ] # DenyList + gosec: + config: + G306: "0644" +issues: + exclude-rules: + - linters: + - revive + text: "don't use leading" diff --git a/cmd/config/Makefile b/cmd/config/Makefile index bfa4c6368..80b936436 100644 --- a/cmd/config/Makefile +++ b/cmd/config/Makefile @@ -36,7 +36,8 @@ clean: lint: $(GOBIN)/golangci-lint $(GOBIN)/golangci-lint \ --skip-dirs $(k8sGenDir) \ - run ./... + run ./... \ + --path-prefix=cmd/config license: $(GOBIN)/addlicense $(GOBIN)/addlicense \ diff --git a/cmd/config/internal/commands/annotate.go b/cmd/config/internal/commands/annotate.go index dd25a9323..43da1eb92 100644 --- a/cmd/config/internal/commands/annotate.go +++ b/cmd/config/internal/commands/annotate.go @@ -142,7 +142,6 @@ func (r *AnnotateRunner) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) { return nil, err } } - } return nodes, nil } diff --git a/cmd/config/internal/commands/annotate_test.go b/cmd/config/internal/commands/annotate_test.go index 85ba1e62b..33c86f43a 100644 --- a/cmd/config/internal/commands/annotate_test.go +++ b/cmd/config/internal/commands/annotate_test.go @@ -601,12 +601,12 @@ added annotations in the package } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Contains(t, actualNormalized, expectedNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/cat_test.go b/cmd/config/internal/commands/cat_test.go index c9b1ec024..074e993ff 100644 --- a/cmd/config/internal/commands/cat_test.go +++ b/cmd/config/internal/commands/cat_test.go @@ -654,12 +654,12 @@ spec: } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmdcreatesetter_test.go b/cmd/config/internal/commands/cmdcreatesetter_test.go index 6a0ffcd60..9777272c2 100644 --- a/cmd/config/internal/commands/cmdcreatesetter_test.go +++ b/cmd/config/internal/commands/cmdcreatesetter_test.go @@ -753,12 +753,12 @@ spec: t.FailNow() } - expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1) + expectedOut := strings.ReplaceAll(test.out, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expectedOut, "\\", "/") // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(out.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(out.String(), "\\", "/"), + "//", "/") if !assert.Contains(t, actualNormalized, expectedNormalized) { t.FailNow() @@ -863,12 +863,12 @@ setter with name "namespace" already exists, if you want to modify it, please de } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Contains(t, actualNormalized, expectedNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmdcreatesubstitution_test.go b/cmd/config/internal/commands/cmdcreatesubstitution_test.go index 4d9c1f791..36a4d46bc 100644 --- a/cmd/config/internal/commands/cmdcreatesubstitution_test.go +++ b/cmd/config/internal/commands/cmdcreatesubstitution_test.go @@ -393,12 +393,12 @@ spec: t.FailNow() } - expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1) + expectedOut := strings.ReplaceAll(test.out, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expectedOut, "\\", "/") // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(out.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(out.String(), "\\", "/"), + "//", "/") if !assert.Contains(t, actualNormalized, expectedNormalized) { t.FailNow() @@ -500,12 +500,12 @@ created substitution "image-tag"`, } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Contains(t, strings.TrimSpace(actualNormalized), strings.TrimSpace(expectedNormalized)) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmddeletesetter_test.go b/cmd/config/internal/commands/cmddeletesetter_test.go index 963a5c40c..3608b8514 100644 --- a/cmd/config/internal/commands/cmddeletesetter_test.go +++ b/cmd/config/internal/commands/cmddeletesetter_test.go @@ -340,12 +340,12 @@ kind: Deployment } // normalize path format for windows - actualNorm := strings.Replace( - strings.Replace(out.String(), "\\", "/", -1), - "//", "/", -1) + actualNorm := strings.ReplaceAll( + strings.ReplaceAll(out.String(), "\\", "/"), + "//", "/") - expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1) + expectedOut := strings.ReplaceAll(test.out, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expectedOut, "\\", "/") if !assert.Contains(t, strings.TrimSpace(actualNorm), expectedNormalized) { t.FailNow() @@ -427,7 +427,7 @@ deleted setter "namespace" t.FailNow() } copyutil.CopyDir(sourceDir, baseDir) - //defer os.RemoveAll(baseDir) + // defer os.RemoveAll(baseDir) runner := commands.NewDeleteSetterRunner("") actual := &bytes.Buffer{} runner.Command.SetOut(actual) @@ -438,12 +438,12 @@ deleted setter "namespace" } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmddeletesubstitution_test.go b/cmd/config/internal/commands/cmddeletesubstitution_test.go index 063d64a10..ca968a723 100644 --- a/cmd/config/internal/commands/cmddeletesubstitution_test.go +++ b/cmd/config/internal/commands/cmddeletesubstitution_test.go @@ -467,11 +467,11 @@ spec: } // normalize path format for windows - actualNorm := strings.Replace( - strings.Replace(out.String(), "\\", "/", -1), - "//", "/", -1) - expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1) - expectedNorm := strings.Replace(expectedOut, "\\", "/", -1) + actualNorm := strings.ReplaceAll( + strings.ReplaceAll(out.String(), "\\", "/"), + "//", "/") + expectedOut := strings.ReplaceAll(test.out, "${baseDir}", baseDir) + expectedNorm := strings.ReplaceAll(expectedOut, "\\", "/") if !assert.Contains(t, strings.TrimSpace(actualNorm), expectedNorm) { t.FailNow() @@ -544,7 +544,7 @@ deleted substitution "image-tag" t.FailNow() } copyutil.CopyDir(sourceDir, baseDir) - //defer os.RemoveAll(baseDir) + // defer os.RemoveAll(baseDir) runner := commands.NewDeleteSubstitutionRunner("") actual := &bytes.Buffer{} runner.Command.SetOut(actual) @@ -555,12 +555,12 @@ deleted substitution "image-tag" } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmdlistsetters_test.go b/cmd/config/internal/commands/cmdlistsetters_test.go index 6248399d0..f37716ab4 100644 --- a/cmd/config/internal/commands/cmdlistsetters_test.go +++ b/cmd/config/internal/commands/cmdlistsetters_test.go @@ -523,7 +523,7 @@ test/testdata/dataset-with-setters/mysql/ } // normalize path format for windows - actualNormalized := strings.Replace(actual.String(), "\\", "/", -1) + actualNormalized := strings.ReplaceAll(actual.String(), "\\", "/") if !assert.Contains(t, strings.TrimSpace(actualNormalized), strings.TrimSpace(test.expected)) { t.FailNow() diff --git a/cmd/config/internal/commands/cmdset_test.go b/cmd/config/internal/commands/cmdset_test.go index 9548f76d6..031dc486e 100644 --- a/cmd/config/internal/commands/cmdset_test.go +++ b/cmd/config/internal/commands/cmdset_test.go @@ -1129,14 +1129,14 @@ set 1 field(s) of setter "namespace" to value "otherspace" } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace( - strings.Replace(expected, "\\", "/", -1), - "//", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll( + strings.ReplaceAll(expected, "\\", "/"), + "//", "/") if !assert.Contains(t, strings.TrimSpace(actualNormalized), strings.TrimSpace(expectedNormalized)) { t.FailNow() } diff --git a/cmd/config/internal/commands/cmdxargs.go b/cmd/config/internal/commands/cmdxargs.go index 0c7daeec7..31d9a5f1d 100644 --- a/cmd/config/internal/commands/cmdxargs.go +++ b/cmd/config/internal/commands/cmdxargs.go @@ -106,7 +106,7 @@ func (r *XArgsRunner) runE(c *cobra.Command, _ []string) error { return fmt.Errorf("must specify -- before command") } r.Args = r.Args[cmdIndex:] - run := exec.Command(r.Args[0]) + run := exec.Command(r.Args[0]) // nolint: gosec if len(r.Args) > 1 { r.Args = r.Args[cmdIndex+1:] diff --git a/cmd/config/internal/commands/count_test.go b/cmd/config/internal/commands/count_test.go index c6d4a959b..4b598ea15 100644 --- a/cmd/config/internal/commands/count_test.go +++ b/cmd/config/internal/commands/count_test.go @@ -137,12 +137,12 @@ Deployment: 1 } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/e2e/e2e_test.go b/cmd/config/internal/commands/e2e/e2e_test.go index 8a1f39354..c4778f96c 100644 --- a/cmd/config/internal/commands/e2e/e2e_test.go +++ b/cmd/config/internal/commands/e2e/e2e_test.go @@ -35,7 +35,7 @@ func TestRunE2e(t *testing.T) { name: "exec_function_no_args", args: func(d string) []string { return []string{ - "--enable-exec", "--exec-path", filepath.Join(d, "e2econtainerconfig"), + "--enable-exec", "--exec-path", filepath.Join(d, e2eConfigDir), } }, files: func(d string) map[string]string { @@ -720,7 +720,7 @@ metadata: } args := append([]string{"fn", "run", "."}, tt.args(binDir)...) - cmd := exec.Command(filepath.Join(binDir, kyamlBin), args...) + cmd := exec.Command(filepath.Join(binDir, kyamlBin), args...) // nolint: gosec cmd.Dir = dir var stdErr, stdOut bytes.Buffer cmd.Stdout = &stdOut @@ -751,6 +751,8 @@ metadata: var buildOnce sync.Once var binDir string +const e2eConfigDir = "e2econtainerconfig" + func build() string { // only build the binaries once buildOnce.Do(func() { @@ -760,7 +762,7 @@ func build() string { panic(err) } - build := exec.Command("go", "build", "-o", + build := exec.Command("go", "build", "-o", // nolint: gosec filepath.Join(binDir, e2econtainerconfigBin)) build.Dir = "e2econtainerconfig" build.Stdout = os.Stdout @@ -772,7 +774,7 @@ func build() string { panic(err) } - build = exec.Command("go", "build", "-o", filepath.Join(binDir, kyamlBin)) + build = exec.Command("go", "build", "-o", filepath.Join(binDir, kyamlBin)) // nolint: gosec build.Dir = filepath.Join("..", "..", "..", "kubectl-krm") build.Stdout = os.Stdout build.Stderr = os.Stderr @@ -786,7 +788,7 @@ func build() string { } build = exec.Command( "docker", "build", ".", "-t", "gcr.io/kustomize-functions/e2econtainerconfig") - build.Dir = "e2econtainerconfig" + build.Dir = e2eConfigDir build.Stdout = os.Stdout build.Stderr = os.Stderr err = build.Run() @@ -803,9 +805,9 @@ var ( kyamlBin string ) -func init() { +func init() { //nolint: gochecknoinits kyamlBin = "kubectl-krm" - e2econtainerconfigBin = "e2econtainerconfig" + e2econtainerconfigBin = e2eConfigDir if runtime.GOOS == "windows" { kyamlBin = "kubectl-krm.exe" diff --git a/cmd/config/internal/commands/e2e/test_util_test.go b/cmd/config/internal/commands/e2e/test_util_test.go index 6835c4a0c..38c1cbbc6 100644 --- a/cmd/config/internal/commands/e2e/test_util_test.go +++ b/cmd/config/internal/commands/e2e/test_util_test.go @@ -17,9 +17,13 @@ import ( ) func TestMain(m *testing.M) { - d := build() - defer os.RemoveAll(d) - os.Exit(m.Run()) + var code int + func() { + d := build() + defer os.RemoveAll(d) + code = m.Run() + }() + os.Exit(code) } type test struct { diff --git a/cmd/config/internal/commands/fmt.go b/cmd/config/internal/commands/fmt.go index 2ab3d4eb2..e658cf2b2 100644 --- a/cmd/config/internal/commands/fmt.go +++ b/cmd/config/internal/commands/fmt.go @@ -69,7 +69,6 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error { } func (r *FmtRunner) runE(c *cobra.Command, args []string) error { - // format stdin if there are no args if len(args) == 0 { rw := &kio.ByteReadWriter{ diff --git a/cmd/config/internal/commands/fmt_test.go b/cmd/config/internal/commands/fmt_test.go index c0918ac4f..a3551a900 100644 --- a/cmd/config/internal/commands/fmt_test.go +++ b/cmd/config/internal/commands/fmt_test.go @@ -232,12 +232,12 @@ formatted resource files in the package } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Contains(t, strings.TrimSpace(actualNormalized), strings.TrimSpace(expectedNormalized)) { t.FailNow() } diff --git a/cmd/config/internal/commands/grep.go b/cmd/config/internal/commands/grep.go index 6333a280e..82846ac7b 100644 --- a/cmd/config/internal/commands/grep.go +++ b/cmd/config/internal/commands/grep.go @@ -73,19 +73,20 @@ func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error { } var last []string - if strings.Contains(parts[len(parts)-1], ">=") { + switch { + case strings.Contains(parts[len(parts)-1], ">="): last = strings.Split(parts[len(parts)-1], ">=") r.MatchType = filters.GreaterThanEq - } else if strings.Contains(parts[len(parts)-1], "<=") { + case strings.Contains(parts[len(parts)-1], "<="): last = strings.Split(parts[len(parts)-1], "<=") r.MatchType = filters.LessThanEq - } else if strings.Contains(parts[len(parts)-1], ">") { + case strings.Contains(parts[len(parts)-1], ">"): last = strings.Split(parts[len(parts)-1], ">") r.MatchType = filters.GreaterThan - } else if strings.Contains(parts[len(parts)-1], "<") { + case strings.Contains(parts[len(parts)-1], "<"): last = strings.Split(parts[len(parts)-1], "<") r.MatchType = filters.LessThan - } else { + default: last = strings.Split(parts[len(parts)-1], "=") r.MatchType = filters.Regexp } @@ -99,7 +100,7 @@ func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error { r.Value = last[1] } - r.Path = append(parts[:len(parts)-1], last[0]) + r.Path = append(parts[:len(parts)-1], last[0]) // nolint:gocritic return nil } @@ -136,7 +137,6 @@ func (r *GrepRunner) runE(c *cobra.Command, args []string) error { fmt.Fprintf(c.OutOrStdout(), "%s", res) return nil - } func (r *GrepRunner) ExecuteCmd(w io.Writer, pkgPath string) error { diff --git a/cmd/config/internal/commands/grep_test.go b/cmd/config/internal/commands/grep_test.go index 4667cc3e7..468fb0df4 100644 --- a/cmd/config/internal/commands/grep_test.go +++ b/cmd/config/internal/commands/grep_test.go @@ -416,12 +416,12 @@ spec: } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1) - expectedNormalized := strings.Replace(expected, "\\", "/", -1) + expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir) + expectedNormalized := strings.ReplaceAll(expected, "\\", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } diff --git a/cmd/config/internal/commands/merge3_test.go b/cmd/config/internal/commands/merge3_test.go index c23176f5b..bd8b9ea88 100644 --- a/cmd/config/internal/commands/merge3_test.go +++ b/cmd/config/internal/commands/merge3_test.go @@ -61,13 +61,13 @@ spec: return } - expected_dir, err := ioutil.TempDir("", "test-data-expected") - defer os.RemoveAll(expected_dir) + expectedDir, err := ioutil.TempDir("", "test-data-expected") + defer os.RemoveAll(expectedDir) if !assert.NoError(t, err) { return } - err = ioutil.WriteFile(filepath.Join(expected_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 + err = ioutil.WriteFile(filepath.Join(expectedDir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 kind: Deployment metadata: name: app @@ -111,13 +111,13 @@ spec: return } - updated_dir, err := ioutil.TempDir("", "test-data-updated") - defer os.RemoveAll(updated_dir) + updatedDir, err := ioutil.TempDir("", "test-data-updated") + defer os.RemoveAll(updatedDir) if !assert.NoError(t, err) { return } - err = ioutil.WriteFile(filepath.Join(updated_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 + err = ioutil.WriteFile(filepath.Join(updatedDir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 kind: Deployment metadata: name: app @@ -158,13 +158,13 @@ spec: return } - dest_dir, err := ioutil.TempDir("", "test-data-dest") - defer os.RemoveAll(dest_dir) + destDir, err := ioutil.TempDir("", "test-data-dest") + defer os.RemoveAll(destDir) if !assert.NoError(t, err) { return } - err = ioutil.WriteFile(filepath.Join(dest_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 + err = ioutil.WriteFile(filepath.Join(destDir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 kind: Deployment metadata: name: app @@ -212,15 +212,15 @@ spec: "--ancestor", datadir, "--from", - updated_dir, + updatedDir, "--to", - dest_dir, + destDir, }) if !assert.NoError(t, r.Command.Execute()) { return } - diffs, err := copyutil.Diff(dest_dir, expected_dir) + diffs, err := copyutil.Diff(destDir, expectedDir) if !assert.NoError(t, err) { t.FailNow() } diff --git a/cmd/config/internal/commands/run-fns.go b/cmd/config/internal/commands/run-fns.go index 481969fb7..815ba256c 100644 --- a/cmd/config/internal/commands/run-fns.go +++ b/cmd/config/internal/commands/run-fns.go @@ -47,7 +47,8 @@ func GetRunFnRunner(name string) *RunFnRunner { "run this image as a function instead of discovering them.") // NOTE: exec plugins execute arbitrary code -- never change the default value of this flag!!! r.Command.Flags().BoolVar( - &r.EnableExec, "enable-exec", false /*do not change!*/, "enable support for exec functions -- note: exec functions run arbitrary code -- do not use for untrusted configs!!! (Alpha)") + &r.EnableExec, "enable-exec", false, /*do not change!*/ + "enable support for exec functions -- note: exec functions run arbitrary code -- do not use for untrusted configs!!! (Alpha)") r.Command.Flags().StringVar( &r.ExecPath, "exec-path", "", "run an executable as a function. (Alpha)") r.Command.Flags().BoolVar( diff --git a/cmd/config/internal/commands/run_test.go b/cmd/config/internal/commands/run_test.go index 5a8ca8e26..72fbd6d08 100644 --- a/cmd/config/internal/commands/run_test.go +++ b/cmd/config/internal/commands/run_test.go @@ -412,7 +412,6 @@ apiVersion: v1 t.FailNow() } } - }) } } diff --git a/cmd/config/internal/commands/source_test.go b/cmd/config/internal/commands/source_test.go index c7ad70d06..a74eb6601 100644 --- a/cmd/config/internal/commands/source_test.go +++ b/cmd/config/internal/commands/source_test.go @@ -202,8 +202,22 @@ func TestSourceCommandJSON(t *testing.T) { if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1 kind: ResourceList items: -- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", "annotations": {"app": "nginx2", config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f1.json', internal.config.kubernetes.io/index: '0', internal.config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} -- {"apiVersion": "v1", "kind": "Abstraction", "metadata": {"name": "foo", "annotations": {"config.kubernetes.io/function": "container:\n image: gcr.io/example/reconciler:v1\n", "config.kubernetes.io/local-config": "true", config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f2.json', internal.config.kubernetes.io/index: '0', internal.config.kubernetes.io/path: 'f2.json'}}, "spec": {"replicas": 3}} +- {"kind": "Deployment", `+ + `"metadata": {`+ + `"labels": {"app": "nginx2"}, `+ + `"name": "foo", `+ + `"annotations": {`+ + `"app": "nginx2", `+ + `config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f1.json', internal.config.kubernetes.io/index: '0', internal.config.kubernetes.io/path: 'f1.json'}}, `+ + `"spec": {"replicas": 1}} +- {"apiVersion": "v1", "kind": "Abstraction", `+ + `"metadata": {`+ + `"name": "foo", `+ + `"annotations": {`+ + `"config.kubernetes.io/function": "container:\n image: gcr.io/example/reconciler:v1\n", `+ + `"config.kubernetes.io/local-config": "true", `+ + `config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f2.json', internal.config.kubernetes.io/index: '0', internal.config.kubernetes.io/path: 'f2.json'}}, `+ + `"spec": {"replicas": 3}} `, b.String()) { return } @@ -312,7 +326,14 @@ func TestSourceCommandJSON_Stdin(t *testing.T) { if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1 kind: ResourceList items: -- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", "annotations": {"app": "nginx2", config.kubernetes.io/index: '0', internal.config.kubernetes.io/index: '0'}}, "spec": {"replicas": 1}} +- {"kind": "Deployment", `+ + `"metadata": {`+ + `"labels": {"app": "nginx2"}, `+ + `"name": "foo", `+ + `"annotations": {`+ + `"app": "nginx2", `+ + `config.kubernetes.io/index: '0', internal.config.kubernetes.io/index: '0'}}, `+ + `"spec": {"replicas": 1}} `, out.String()) { return } diff --git a/cmd/config/runner/runner_test.go b/cmd/config/runner/runner_test.go index e1281bf65..6fd713b69 100644 --- a/cmd/config/runner/runner_test.go +++ b/cmd/config/runner/runner_test.go @@ -98,14 +98,14 @@ ${baseDir}/subpkg2/subpkg3/ } // normalize path format for windows - actualNormalized := strings.Replace( - strings.Replace(actual.String(), "\\", "/", -1), - "//", "/", -1) + actualNormalized := strings.ReplaceAll( + strings.ReplaceAll(actual.String(), "\\", "/"), + "//", "/") - expected := strings.Replace(test.expectedOut, "${baseDir}", dir+"/", -1) - expectedNormalized := strings.Replace( - strings.Replace(expected, "\\", "/", -1), - "//", "/", -1) + expected := strings.ReplaceAll(test.expectedOut, "${baseDir}", dir+"/") + expectedNormalized := strings.ReplaceAll( + strings.ReplaceAll(expected, "\\", "/"), + "//", "/") if !assert.Equal(t, expectedNormalized, actualNormalized) { t.FailNow() } @@ -141,27 +141,27 @@ func createTestDirStructure(dir string) error { if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "subpkg1", "Krmfile"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "subpkg1", "Krmfile"), []byte(""), 0644) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "subpkg2", "Krmfile"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "subpkg2", "Krmfile"), []byte(""), 0644) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "subpkg2/subpkg3", "Krmfile"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "subpkg2/subpkg3", "Krmfile"), []byte(""), 0644) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "error.txt"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "error.txt"), []byte(""), 0644) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "Krmfile"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "Krmfile"), []byte(""), 0644) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(""), 0777) + err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(""), 0644) if err != nil { return err }