Skip to content

Commit c8c6a5e

Browse files
committed
feat: add path_resolver_test.go
1 parent 7217c55 commit c8c6a5e

File tree

7 files changed

+133
-138
lines changed

7 files changed

+133
-138
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ require github.com/urfave/cli/v3 v3.0.0-beta1
66

77
require (
88
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
9+
github.com/google/go-cmp v0.7.0 // indirect
910
github.com/manifoldco/promptui v0.9.0 // indirect
11+
github.com/samber/lo v1.50.0 // indirect
1012
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
13+
golang.org/x/text v0.22.0 // indirect
1114
)

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
44
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
8+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
79
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
810
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
911
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1012
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY=
14+
github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc=
1115
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1216
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1318
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
1419
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
1520
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
1621
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
22+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
23+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
1724
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1825
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

handler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (sh *SessionHandler) NewSession(ctx context.Context) error {
3636
/*build fzf input and build hashmap to retrieve filepath from entry's name.*/
3737
var input bytes.Buffer
3838
for _, project := range sh.config.Projects {
39-
replaced, err := sh.pathResolver.ReplaceHomeDir(project.filepath)
39+
replaced, err := sh.pathResolver.ExpandPath(project.filepath)
4040
if err != nil {
4141
return errors.New("failed to replace home directory to ~")
4242
}

handler/handler_test.go

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1 @@
11
package handler
2-
3-
import (
4-
"errors"
5-
"os"
6-
"path/filepath"
7-
"testing"
8-
)
9-
10-
func TestParseConfig(t *testing.T) {
11-
t.Parallel()
12-
tmpRoot := t.TempDir()
13-
project1Name, project2Name := "project1", "project2"
14-
projectDir1 := filepath.Join(tmpRoot, project1Name)
15-
projectDir2 := filepath.Join(tmpRoot, project2Name)
16-
projects := []string{projectDir1, projectDir2}
17-
if err := os.Mkdir(projectDir1, 0755); err != nil {
18-
t.Errorf("failed to make project1 directory")
19-
}
20-
if err := os.Mkdir(projectDir2, 0755); err != nil {
21-
t.Errorf("failed to make project2 directory")
22-
}
23-
24-
configContent := "default=" + tmpRoot
25-
26-
tmpConfigFile := filepath.Join(tmpRoot, ".tmux-sessionizer")
27-
if err := os.WriteFile(tmpConfigFile, []byte(configContent), 0600); err != nil {
28-
t.Errorf("failed to write config file: %v", err)
29-
}
30-
31-
sh := &SessionHandler{}
32-
cfg, err := sh.parseConfig(tmpConfigFile)
33-
if err != nil {
34-
t.Errorf("failed to parse config file: %v", err)
35-
}
36-
37-
if len(cfg.projects) != len(projects) {
38-
t.Errorf("expected %v projects, but got %v", len(projects), len(cfg.projects))
39-
}
40-
wantNames := map[string]bool{project1Name: true, project2Name: true}
41-
for _, p := range cfg.projects {
42-
if !wantNames[p.name] {
43-
t.Errorf("unexpected project: %+v", p)
44-
}
45-
}
46-
}
47-
48-
func TestExpandPath(t *testing.T) {
49-
t.Parallel()
50-
type args struct {
51-
relative string
52-
}
53-
54-
tests := []struct {
55-
name string
56-
args args
57-
want string
58-
wantErr error
59-
}{
60-
{
61-
name: "home directory case",
62-
args: args{
63-
relative: "~/hoge/fuga",
64-
},
65-
/* I don't care about windows, sorry.*/
66-
want: os.Getenv("HOME") + "/hoge/fuga",
67-
wantErr: nil,
68-
},
69-
{
70-
name: "complex relative path case",
71-
args: args{
72-
relative: "~/puga/hoge/../../fuga",
73-
},
74-
want: os.Getenv("HOME") + "/fuga",
75-
wantErr: nil,
76-
},
77-
}
78-
for _, tt := range tests {
79-
t.Run(tt.name, func(t *testing.T) {
80-
t.Parallel()
81-
sh := SessionHandler{}
82-
got, err := sh.expandPath(tt.args.relative)
83-
if err != nil && !errors.Is(err, tt.wantErr) {
84-
t.Errorf("failed to expand path: %v", err)
85-
return
86-
}
87-
if got != tt.want {
88-
t.Errorf("failed to expandPath. expected: %v, but got %v", tt.want, got)
89-
}
90-
})
91-
}
92-
}
93-
94-
func TestReplaceHomeDir(t *testing.T) {
95-
t.Parallel()
96-
homeDir, _ := os.UserHomeDir()
97-
type args struct {
98-
fullpath string
99-
}
100-
tests := []struct {
101-
name string
102-
args args
103-
want string
104-
wantErr error
105-
}{
106-
{
107-
name: "Normal test case1",
108-
args: args{
109-
fullpath: filepath.Join(homeDir, "a/b/c"),
110-
},
111-
want: "~/a/b/c",
112-
wantErr: nil,
113-
},
114-
}
115-
for _, tt := range tests {
116-
t.Run(tt.name, func(t *testing.T) {
117-
t.Parallel()
118-
sh := SessionHandler{}
119-
got, err := sh.replaceHomeDir(tt.args.fullpath)
120-
if err != nil && !errors.Is(err, tt.wantErr) {
121-
t.Errorf("failed to replace home directory: %v", err)
122-
return
123-
}
124-
if got != tt.want {
125-
t.Errorf("failed to replace home directory, expected: %v, but got %v", tt.want, got)
126-
}
127-
})
128-
}
129-
}

handler/path_resolver.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,11 @@ func (p *PathResolver) ExpandPath(path string) (string, error) {
2525
return path, nil
2626
}
2727

28-
func (p *PathResolver) ReplaceHomeDir(fullpath string) (string, error) {
29-
home, err := os.UserHomeDir()
30-
if err != nil {
31-
return "", err
32-
}
33-
return strings.Replace(fullpath, home, "~", 1), nil
34-
}
35-
3628
func (p *PathResolver) BuildProjectInfo(config *Config) (map[string]string, map[string]string, error) {
3729
var input bytes.Buffer
3830
projectNameFullPathMap, projectExpressionNameMap := make(map[string]string, 0), make(map[string]string, 0)
3931
for _, project := range config.Projects {
40-
replaced, err := p.ReplaceHomeDir(project.filepath)
32+
replaced, err := p.ExpandPath(project.filepath)
4133
if err != nil {
4234
return nil, nil, errors.New("failed to replace home directory to ~")
4335
}

handler/path_resolver_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
testutils "github.com/TlexCypher/my-tmux-sessionizer/test_utils"
8+
"github.com/google/go-cmp/cmp"
9+
)
10+
11+
func TestExpandPath(t *testing.T) {
12+
t.Parallel()
13+
tests := []struct {
14+
description string
15+
path string
16+
want string
17+
wantErr error
18+
}{
19+
20+
{
21+
description: "Home directory expansion (Normal case)",
22+
path: "~/hoge/fuga",
23+
want: fmt.Sprintf("%v/hoge/fuga", testutils.GetUserHomeDir()),
24+
wantErr: nil,
25+
},
26+
{
27+
description: "No home directory",
28+
path: "/home/hoge",
29+
want: "/home/hoge",
30+
wantErr: nil,
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.description, func(t *testing.T) {
36+
p := NewPathResolver()
37+
got, err := p.ExpandPath(tt.path)
38+
if err != tt.wantErr {
39+
t.Fatal(err)
40+
}
41+
if diff := cmp.Diff(tt.want, got); diff != "" {
42+
t.Errorf("PathResolver.ExpandPath() result diff (-expect, +got)\n%s", diff)
43+
}
44+
})
45+
}
46+
}
47+
48+
type want struct {
49+
projectNameFullPathMap map[string]string
50+
projectExpressionNameMap map[string]string
51+
}
52+
53+
type args struct {
54+
config *Config
55+
}
56+
57+
func TestBuildProjectInfo(t *testing.T) {
58+
t.Parallel()
59+
60+
tests := []struct {
61+
description string
62+
args args
63+
want want
64+
wantErr error
65+
}{
66+
{
67+
description: "Normal case",
68+
args: args{
69+
config: &Config{
70+
Projects: []project{
71+
{
72+
name: "tmux-sessionizer",
73+
filepath: "~/tmux-sessionizer",
74+
},
75+
{
76+
name: "nvim",
77+
filepath: "~/.config/nvim",
78+
},
79+
},
80+
},
81+
},
82+
want: want{
83+
projectNameFullPathMap: map[string]string{
84+
"tmux-sessionizer": "~/tmux-sessionizer",
85+
"nvim": "~/.config/nvim",
86+
},
87+
projectExpressionNameMap: map[string]string{
88+
fmt.Sprintf("%v/tmux-sessionizer", testutils.GetUserHomeDir()): "tmux-sessionizer",
89+
fmt.Sprintf("%v/.config/nvim", testutils.GetUserHomeDir()): "nvim",
90+
},
91+
},
92+
},
93+
}
94+
95+
for _, tt := range tests {
96+
t.Run(tt.description, func(t *testing.T) {
97+
p := NewPathResolver()
98+
got1, got2, err := p.BuildProjectInfo(tt.args.config)
99+
if tt.wantErr != err {
100+
t.Fatal(err)
101+
}
102+
if diff := cmp.Diff(tt.want.projectNameFullPathMap, got1); diff != "" {
103+
t.Errorf("PathResolver.BuildProjectInfo() result diff (-expect, +got)\n%s", diff)
104+
}
105+
if diff := cmp.Diff(tt.want.projectExpressionNameMap, got2); diff != "" {
106+
t.Errorf("PathResolver.BuildProjectInfo() result diff (-expect, +got)\n%s", diff)
107+
}
108+
})
109+
}
110+
}

test_utils/test_utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testutils
2+
3+
import (
4+
"os"
5+
6+
"github.com/samber/lo"
7+
)
8+
9+
func GetUserHomeDir() string {
10+
return lo.Must(os.UserHomeDir())
11+
}

0 commit comments

Comments
 (0)