Skip to content

Commit

Permalink
test: cover path expansion
Browse files Browse the repository at this point in the history
lint: fix separators
  • Loading branch information
umputun committed Dec 25, 2023
1 parent b8156e5 commit 17183b1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -320,3 +321,46 @@ func Test_checkVolumeMount(t *testing.T) {
})
}
}

func Test_expandPath(t *testing.T) {
home, err := os.UserHomeDir()
require.NoError(t, err)
currentDir, err := os.Getwd()
require.NoError(t, err)

tests := []struct {
name string
path string
want string
}{
{"Empty Path", "", ""},
{"Home Directory", "~", home},
{"Relative Path", ".", ""},
{"Relative Path with directory", "data", filepath.Join(currentDir, "data")},
{"Absolute Path", "/tmp", "/tmp"},
{"Path with Tilde and Subdirectory", "~/Documents", filepath.Join(home, "Documents")},
{"Path with Multiple Relative Directories", "../parent/child", ""},
{"Path with Special Characters", "data/special @#$/file", ""},
{"Invalid Path", "/some/nonexistent/path", "/some/nonexistent/path"},
{"Home Directory with Trailing Slash", "~/", home},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := expandPath(tt.path)

switch {
case strings.Contains(tt.path, "~"):
assert.Equal(t, filepath.Join(home, tt.path[1:]), got)
case tt.path == ".", strings.HasPrefix(tt.path, ".."), strings.Contains(tt.path, "/"):
// For relative paths, paths starting with "..", and paths with special characters
expected, err := filepath.Abs(tt.path)
require.NoError(t, err)
assert.Equal(t, expected, got)
default:
// For absolute paths and invalid paths
assert.Equal(t, tt.want, got)
}
})
}
}

0 comments on commit 17183b1

Please sign in to comment.