|
1 | 1 | 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 |
| -} |
0 commit comments