Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment variable expansion in absPathify #495

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions util.go
Expand Up @@ -91,13 +91,23 @@ func insensitiviseMap(m map[string]interface{}) {
func absPathify(inPath string) string {
jww.INFO.Println("Trying to resolve absolute path to", inPath)

if strings.HasPrefix(inPath, "$HOME") {
if strings.HasPrefix(inPath, "$HOME") &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be rewritten as something like inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+os.PathSeparator) or equivalent (or even using some sort of splitting)? The current condition is not really expressive.

Side note: looking at the path/filepath package there isn't much that can help here, but ideally it would be nice.

(len(inPath) == 5 || inPath[5] == os.PathSeparator) {
inPath = userHomeDir() + inPath[5:]
}

if strings.HasPrefix(inPath, "$") {
end := strings.Index(inPath, string(os.PathSeparator))
inPath = os.Getenv(inPath[1:end]) + inPath[end:]

var value, suffix string
if end == -1 {
value = os.Getenv(inPath[1:])
} else {
value = os.Getenv(inPath[1:end])
suffix = inPath[end:]
}

inPath = value + suffix
}

if filepath.IsAbs(inPath) {
Expand Down
37 changes: 37 additions & 0 deletions util_test.go
Expand Up @@ -11,6 +11,8 @@
package viper

import (
"os"
"path/filepath"
"reflect"
"testing"
)
Expand Down Expand Up @@ -52,3 +54,38 @@ func TestCopyAndInsensitiviseMap(t *testing.T) {
t.Fatal("Input map changed")
}
}

func TestAbsPathify(t *testing.T) {
home := userHomeDir()
homer := filepath.Join(home, "homer")
wd, _ := os.Getwd()

os.Setenv("HOMER_ABSOLUTE_PATH", homer)
os.Setenv("VAR_WITH_RELATIVE_PATH", "relative")

tests := []struct {
input string
output string
}{
{"", wd},
{"sub", filepath.Join(wd, "sub")},
{"./", wd},
{"./sub", filepath.Join(wd, "sub")},
{"$HOME", home},
{"$HOME/", home},
{"$HOME/sub", filepath.Join(home, "sub")},
{"$HOMER_ABSOLUTE_PATH", homer},
{"$HOMER_ABSOLUTE_PATH/", homer},
{"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")},
{"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")},
{"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")},
{"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")},
}

for _, test := range tests {
got := absPathify(test.input)
if got != test.output {
t.Errorf("Got %v\nexpected\n%q", got, test.output)
}
}
}