anqiansong 3 years ago committed by GitHub
parent b939ce75ba
commit 08a8bd7ef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -75,12 +75,23 @@ func FileNameWithoutExt(file string) string {
// GetGoctlHome returns the path value of the goctl, the default path is ~/.goctl, if the path has // GetGoctlHome returns the path value of the goctl, the default path is ~/.goctl, if the path has
// been set by calling the RegisterGoctlHome method, the user-defined path refers to. // been set by calling the RegisterGoctlHome method, the user-defined path refers to.
func GetGoctlHome() (string, error) { func GetGoctlHome() (home string, err error) {
defer func() {
if err != nil {
return
}
info, err := os.Stat(home)
if err == nil && !info.IsDir() {
os.Rename(home, home+".old")
MkdirIfNotExist(home)
}
}()
if len(goctlHome) != 0 { if len(goctlHome) != 0 {
return goctlHome, nil home = goctlHome
return
} }
home, err = GetDefaultGoctlHome()
return GetDefaultGoctlHome() return
} }
// GetDefaultGoctlHome returns the path value of the goctl home where Join $HOME with .goctl. // GetDefaultGoctlHome returns the path value of the goctl home where Join $HOME with .goctl.

@ -74,3 +74,35 @@ func TestGetGitHome(t *testing.T) {
expected := filepath.Join(homeDir, goctlDir, gitDir) expected := filepath.Join(homeDir, goctlDir, gitDir)
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
func TestGetGoctlHome(t *testing.T) {
t.Run("goctl_is_file", func(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "a.tmp")
backupTempFile := tmpFile + ".old"
err := ioutil.WriteFile(tmpFile, nil, 0666)
if err != nil {
return
}
RegisterGoctlHome(tmpFile)
home, err := GetGoctlHome()
if err != nil {
return
}
info, err := os.Stat(home)
assert.Nil(t, err)
assert.True(t, info.IsDir())
_, err = os.Stat(backupTempFile)
assert.Nil(t, err)
})
t.Run("goctl_is_dir", func(t *testing.T) {
RegisterGoctlHome("")
dir := t.TempDir()
RegisterGoctlHome(dir)
home, err := GetGoctlHome()
assert.Nil(t, err)
assert.Equal(t, dir, home)
})
}

Loading…
Cancel
Save