From 35d9d728a7c3a822be4b0da4d4dde4ce828d2e2f Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sun, 10 Dec 2023 19:13:31 +0800 Subject: [PATCH] test: add unit test (#808) --- pkg/config/file/file_test.go | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pkg/config/file/file_test.go b/pkg/config/file/file_test.go index 6c86bff0..59e20a76 100644 --- a/pkg/config/file/file_test.go +++ b/pkg/config/file/file_test.go @@ -18,6 +18,8 @@ package file import ( + "os" + "path/filepath" "reflect" "sync" "testing" @@ -470,3 +472,64 @@ func Test_storeOperate_searchDefaultConfigFile(t *testing.T) { }) } } + +func Test_formatPath(t *testing.T) { + // Get current user home dir + home, _ := os.UserHomeDir() + tests := []struct { + name string + input string + want string + expectErr bool + }{ + { + name: "PATH1", + input: "~/folder", + want: filepath.Join(home, "folder"), + expectErr: false, + }, + { + name: "PATH2", + input: "/tmp/folder", + want: "/tmp/folder", + expectErr: false, + }, + { + name: "PATH3", + input: "./folder", + want: filepath.Clean("./folder"), + expectErr: false, + }, + { + name: "PATH4", + input: "///tmp///folder", + want: "/tmp/folder", + expectErr: false, + }, + { + name: "PATH5", + input: "", + want: ".", + expectErr: false, + }, + { + name: "PATH6", + input: "~", + want: home, + expectErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := formatPath(tt.input) + if (err != nil) != tt.expectErr { + t.Errorf("formatPath() error = %v, expectErr %v", err, tt.expectErr) + return + } + if got != tt.want { + t.Errorf("formatPath() = %v, want %v", got, tt.want) + } + }) + } +}