Skip to content

Commit

Permalink
context: abs - fix ~
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 15, 2024
1 parent affaa65 commit ae6b7e6
Show file tree
Hide file tree
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
Expand Up @@ -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
}
Expand Down
52 changes: 29 additions & 23 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit ae6b7e6

Please sign in to comment.