Skip to content

Commit

Permalink
bugfix: make unittests pass on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
memetb committed Oct 23, 2024
1 parent a5ef98d commit bf8e95a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libvirt/util/expandenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ var (
// this is a drop-in replacement for os.ExpandEnv but is additionally '~' aware.
func ExpandEnvExt(path string) string {
path = filepath.Clean(expandEnv(path))
if strings.HasPrefix(path, "~/") {
tilde := filepath.FromSlash("~/")

// note to maintainers: tilde without a following slash character is simply
// interpreted as part of the filename (e.g. ~foo/bar != ~/foo/bar). However,
// when running on windows, the filepath will be represented by backslashes ('\'),
// therefore we need to convert "~/" to the platform specific format to test for
// it, otherwise on windows systems the prefix test will always fail.
if strings.HasPrefix(path, tilde) {
home, err := userHomeDir()
if err != nil {
return path // return path as-is if unable to resolve home directory
}
// Replace ~ with home directory
path = filepath.Join(home, strings.TrimPrefix(path, "~/"))
path = filepath.Join(home, strings.TrimPrefix(path, tilde))
}
return path
}

0 comments on commit bf8e95a

Please sign in to comment.