Skip to content

Commit

Permalink
test: add unit test (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzl authored Dec 10, 2023
1 parent 3f62d64 commit 35d9d72
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/config/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package file

import (
"os"
"path/filepath"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 35d9d72

Please sign in to comment.