no panic if HOME does not exist (#2148)

* no home dir exist

* dolt root can be empty

* move test to bottom

* dir -> directory

* Windows complaining about smth
This commit is contained in:
Maximilian Hoffman
2021-09-17 14:12:35 -07:00
committed by GitHub
parent 2752b0d13e
commit 47a4640e4c
3 changed files with 22 additions and 4 deletions

View File

@@ -236,6 +236,7 @@ func runMain() int {
root, err := env.GetCurrentUserHomeDir()
if err != nil {
cli.PrintErrln(color.RedString("Failed to load the HOME directory: %v", err))
return 1
}

View File

@@ -44,14 +44,21 @@ func GetCurrentUserHomeDir() (string, error) {
if doltRootPath, ok := os.LookupEnv(doltRootPathEnvVar); ok && doltRootPath != "" {
return doltRootPath, nil
}
var home string
if homeEnvPath, ok := os.LookupEnv(homeEnvVar); ok && homeEnvPath != "" {
return homeEnvPath, nil
}
if usr, err := user.Current(); err != nil {
home = homeEnvPath
} else if usr, err := user.Current(); err != nil {
return "", err
} else {
return usr.HomeDir, nil
home = usr.HomeDir
}
_, err := os.Stat(home)
if err != nil {
return "", err
}
return home, nil
}
func getCredsDir(hdp HomeDirProvider) (string, error) {

View File

@@ -272,3 +272,13 @@ NOT_VALID_REPO_ERROR="The current directory is not a valid dolt repository."
run dolt checkout help
[ "$status" -ne 0 ]
}
@test "no-repo: don't panic if invalid HOME" {
DOLT_ROOT_PATH=
HOME=/this/is/garbage
run dolt
[ "$status" -eq 1 ]
[[ ! "$output" =~ "panic" ]]
[[ "$output" =~ "Failed to load the HOME directory" ]]
}