From 0ee0d225f27a8893174b7b6c2145a99624ecb1a6 Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Wed, 15 Jun 2022 15:17:24 +0300 Subject: [PATCH] Replaces path.Operation with filepath.Operation (staging) The path module has a few different functions: Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not take into account the OS-specific path separator, meaning that they won't behave as intended on Windows. For example, Dir is supposed to return all but the last element of the path. For the path "C:\some\dir\somewhere", it is supposed to return "C:\some\dir\", however, it returns ".". Instead of these functions, the ones in filepath should be used instead. Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7 --- pkg/cmd/clusterinfo/clusterinfo_dump.go | 5 +++-- pkg/cmd/clusterinfo/clusterinfo_dump_test.go | 4 ++-- pkg/cmd/config/config_test.go | 6 +++--- pkg/cmd/create/create_configmap.go | 4 ++-- pkg/cmd/create/create_secret.go | 4 ++-- pkg/cmd/create/create_secret_tls_test.go | 6 +++--- pkg/cmd/diff/diff_test.go | 11 +++++------ 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/clusterinfo/clusterinfo_dump.go b/pkg/cmd/clusterinfo/clusterinfo_dump.go index 34fb617ac..e67f849fd 100644 --- a/pkg/cmd/clusterinfo/clusterinfo_dump.go +++ b/pkg/cmd/clusterinfo/clusterinfo_dump.go @@ -22,6 +22,7 @@ import ( "io" "os" "path" + "path/filepath" "time" "github.com/spf13/cobra" @@ -118,8 +119,8 @@ func setupOutputWriter(dir string, defaultWriter io.Writer, filename string, fil if len(dir) == 0 || dir == "-" { return defaultWriter } - fullFile := path.Join(dir, filename) + fileExtension - parent := path.Dir(fullFile) + fullFile := filepath.Join(dir, filename) + fileExtension + parent := filepath.Dir(fullFile) cmdutil.CheckErr(os.MkdirAll(parent, 0755)) file, err := os.Create(fullFile) diff --git a/pkg/cmd/clusterinfo/clusterinfo_dump_test.go b/pkg/cmd/clusterinfo/clusterinfo_dump_test.go index ed7406d9c..8f178a4f3 100644 --- a/pkg/cmd/clusterinfo/clusterinfo_dump_test.go +++ b/pkg/cmd/clusterinfo/clusterinfo_dump_test.go @@ -18,7 +18,7 @@ package clusterinfo import ( "os" - "path" + "path/filepath" "testing" "k8s.io/cli-runtime/pkg/genericiooptions" @@ -60,7 +60,7 @@ func TestSetupOutputWriterFile(t *testing.T) { if err != nil { t.Errorf("unexpected error: %v", err) } - fullPath := path.Join(dir, file) + extension + fullPath := filepath.Join(dir, file) + extension defer os.RemoveAll(dir) _, _, buf, _ := genericiooptions.NewTestIOStreams() diff --git a/pkg/cmd/config/config_test.go b/pkg/cmd/config/config_test.go index 28603f261..a4bac0fa2 100644 --- a/pkg/cmd/config/config_test.go +++ b/pkg/cmd/config/config_test.go @@ -19,7 +19,7 @@ package config import ( "fmt" "os" - "path" + "path/filepath" "reflect" "strings" "testing" @@ -415,7 +415,7 @@ func TestEmptyTokenAndCertAllowed(t *testing.T) { defer utiltesting.CloseAndRemove(t, fakeCertFile) expectedConfig := newRedFederalCowHammerConfig() authInfo := clientcmdapi.NewAuthInfo() - authInfo.ClientCertificate = path.Base(fakeCertFile.Name()) + authInfo.ClientCertificate = filepath.Base(fakeCertFile.Name()) expectedConfig.AuthInfos["another-user"] = authInfo test := configCommandTest{ @@ -660,7 +660,7 @@ func TestCAClearsInsecure(t *testing.T) { clusterInfoWithInsecure.InsecureSkipTLSVerify = true clusterInfoWithCA := clientcmdapi.NewCluster() - clusterInfoWithCA.CertificateAuthority = path.Base(fakeCAFile.Name()) + clusterInfoWithCA.CertificateAuthority = filepath.Base(fakeCAFile.Name()) startingConfig := newRedFederalCowHammerConfig() startingConfig.Clusters["another-cluster"] = clusterInfoWithInsecure diff --git a/pkg/cmd/create/create_configmap.go b/pkg/cmd/create/create_configmap.go index 988cbb1ef..f43f91b01 100644 --- a/pkg/cmd/create/create_configmap.go +++ b/pkg/cmd/create/create_configmap.go @@ -20,7 +20,7 @@ import ( "context" "fmt" "os" - "path" + "path/filepath" "strings" "unicode/utf8" @@ -320,7 +320,7 @@ func handleConfigMapFromFileSources(configMap *corev1.ConfigMap, fileSources []s return fmt.Errorf("error listing files in %s: %v", filePath, err) } for _, item := range fileList { - itemPath := path.Join(filePath, item.Name()) + itemPath := filepath.Join(filePath, item.Name()) if item.Type().IsRegular() { keyName = item.Name() err = addKeyFromFileToConfigMap(configMap, keyName, itemPath) diff --git a/pkg/cmd/create/create_secret.go b/pkg/cmd/create/create_secret.go index b9bb191f9..4b4f394a9 100644 --- a/pkg/cmd/create/create_secret.go +++ b/pkg/cmd/create/create_secret.go @@ -20,7 +20,7 @@ import ( "context" "fmt" "os" - "path" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -349,7 +349,7 @@ func handleSecretFromFileSources(secret *corev1.Secret, fileSources []string) er return fmt.Errorf("error listing files in %s: %v", filePath, err) } for _, item := range fileList { - itemPath := path.Join(filePath, item.Name()) + itemPath := filepath.Join(filePath, item.Name()) if item.Type().IsRegular() { keyName = item.Name() if err := addKeyFromFileToSecret(secret, keyName, itemPath); err != nil { diff --git a/pkg/cmd/create/create_secret_tls_test.go b/pkg/cmd/create/create_secret_tls_test.go index 51f3a7730..900d05797 100644 --- a/pkg/cmd/create/create_secret_tls_test.go +++ b/pkg/cmd/create/create_secret_tls_test.go @@ -18,7 +18,7 @@ package create import ( "os" - "path" + "path/filepath" "testing" corev1 "k8s.io/api/core/v1" @@ -197,8 +197,8 @@ func write(path, contents string, t *testing.T) { } func writeKeyPair(tmpDirPath, key, cert string, t *testing.T) (keyPath, certPath string) { - keyPath = path.Join(tmpDirPath, "tls.key") - certPath = path.Join(tmpDirPath, "tls.cert") + keyPath = filepath.Join(tmpDirPath, "tls.key") + certPath = filepath.Join(tmpDirPath, "tls.cert") write(keyPath, key, t) write(certPath, cert, t) return diff --git a/pkg/cmd/diff/diff_test.go b/pkg/cmd/diff/diff_test.go index 037858c5c..391900418 100644 --- a/pkg/cmd/diff/diff_test.go +++ b/pkg/cmd/diff/diff_test.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "os" - "path" "path/filepath" "strings" "testing" @@ -130,7 +129,7 @@ func TestDiffVersion(t *testing.T) { if err != nil { t.Fatal(err) } - fcontent, err := os.ReadFile(path.Join(diff.Dir.Name, obj.Name())) + fcontent, err := os.ReadFile(filepath.Join(diff.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -201,7 +200,7 @@ func TestDiffer(t *testing.T) { if err != nil { t.Fatal(err) } - fcontent, err := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) + fcontent, err := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -210,7 +209,7 @@ func TestDiffer(t *testing.T) { t.Fatalf("File has %q, expected %q", string(fcontent), econtent) } - fcontent, err = os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) + fcontent, err = os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -286,12 +285,12 @@ metadata: t.Fatal(err) } - actualFromContent, _ := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) + actualFromContent, _ := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name())) if string(actualFromContent) != tc.expectedFromContent { t.Fatalf("File has %q, expected %q", string(actualFromContent), tc.expectedFromContent) } - actualToContent, _ := os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) + actualToContent, _ := os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name())) if string(actualToContent) != tc.expectedToContent { t.Fatalf("File has %q, expected %q", string(actualToContent), tc.expectedToContent) }