Skip to content

Commit

Permalink
Merge pull request #1038 from carapace-sh/context-abs-fix-home
Browse files Browse the repository at this point in the history
context: abs - fix `~`
rsteube authored Aug 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents affaa65 + ae6b7e6 commit 07d8bbe
Showing 2 changed files with 35 additions and 24 deletions.
7 changes: 6 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
@@ -114,7 +114,12 @@ func expandHome(s string) (string, error) {
return "", err
}
home = filepath.ToSlash(home)
s = strings.Replace(s, "~/", home+"/", 1)
switch s {
case "~":
s = home
default:
s = strings.Replace(s, "~/", home+"/", 1)
}
}
return s, nil
}
52 changes: 29 additions & 23 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -31,33 +31,39 @@ func parent(s string) string {
}

func TestContextAbs(t *testing.T) {
tests := append([]string{},
"/", "file", "/file",
"", "file", wd("file"),
"", "../", parent(""),
"", "../file", parent("file"),
"", "~/file", home("file"),
"/", "~/file", home("file"),
"/", "file", "/file",
"/dir", "file", "/dir/file",
"/dir", "./.file", "/dir/.file",
"", "/dir/", "/dir/",
"/dir/", "", "/dir/",
"~/", "file", home("file"),
"", "/", "/",
"", ".hidden", wd(".hidden"),
"", "./", wd("")+"/",
"", "", wd("")+"/",
"", ".", wd("")+"/"+".",
)
testCases := []struct {
Dir string
Path string
Expected string
}{
{"/", "file", "/file"},
{"", "file", wd("file")},
{"", "../", parent("")},
{"", "../file", parent("file")},
{"", "~/file", home("file")},
{"/", "~/file", home("file")},
{"/", "file", "/file"},
{"/dir", "file", "/dir/file"},
{"/dir", "./.file", "/dir/.file"},
{"", "/dir/", "/dir/"},
{"/dir/", "", "/dir/"},
{"~/", "file", home("file")},
{"", "/", "/"},
{"", ".hidden", wd(".hidden")},
{"", "./", wd("") + "/"},
{"", "", wd("") + "/"},
{"", ".", wd("") + "/" + "."},
{"", "~", home("")},
{"", "~/file", home("file")},
}

for index := 0; index < len(tests); index += 3 {
actual, err := Context{Dir: tests[index]}.Abs(tests[index+1])
for _, tc := range testCases {
actual, err := Context{Dir: tc.Dir}.Abs(tc.Path)
if err != nil {
t.Error(err.Error())
}
if expected := tests[index+2]; expected != actual {
t.Errorf("context: '%v' arg: '%v' expected: '%v' was: '%v'", tests[index], tests[index+1], expected, actual)
if tc.Expected != actual {
t.Errorf("context: '%v' arg: '%v' expected: '%v' was: '%v'", tc.Dir, tc.Path, tc.Expected, actual)
}
}
}

0 comments on commit 07d8bbe

Please sign in to comment.