Skip to content

Commit

Permalink
move inner method into internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 16, 2024
1 parent f0e98e4 commit e6ece0c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
18 changes: 18 additions & 0 deletions credentials/internal/utils/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"os"
"runtime"
)

var getOS = func() string {
return runtime.GOOS
}

func GetHomePath() string {
if getOS() == "windows" {
return os.Getenv("USERPROFILE")
}

return os.Getenv("HOME")
}
38 changes: 38 additions & 0 deletions credentials/internal/utils/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import (
"os"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_getOS(t *testing.T) {
assert.Equal(t, runtime.GOOS, getOS())
}

func TestGetHomePath(t *testing.T) {
originGetOS := getOS
originUserProfile := os.Getenv("USERPROFILE")
originHome := os.Getenv("HOME")
defer func() {
getOS = originGetOS
os.Setenv("USERPROFILE", originUserProfile)
os.Setenv("HOME", originHome)
}()

getOS = func() string {
return "windows"
}
os.Setenv("USERPROFILE", "/path/to/custom_home")

assert.Equal(t, "/path/to/custom_home", GetHomePath())

getOS = func() string {
return "darwin"
}

os.Setenv("HOME", "/Users/jacksontian")
assert.Equal(t, "/Users/jacksontian", GetHomePath())
}
23 changes: 2 additions & 21 deletions credentials/profile_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"errors"
"fmt"
"os"
"runtime"
"strings"

"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials/internal/utils"
ini "gopkg.in/ini.v1"
)

Expand All @@ -17,10 +17,6 @@ type profileProvider struct {

var providerProfile = newProfileProvider()

var hookOS = func(goos string) string {
return goos
}

var hookState = func(info os.FileInfo, err error) (os.FileInfo, error) {
return info, err
}
Expand Down Expand Up @@ -294,23 +290,8 @@ func getType(path, profile string) (*ini.Key, *ini.Section, error) {
return value, section, nil
}

func getHomePath() string {
if hookOS(runtime.GOOS) == "windows" {
path, ok := os.LookupEnv("USERPROFILE")
if !ok {
return ""
}
return path
}
path, ok := os.LookupEnv("HOME")
if !ok {
return ""
}
return path
}

func checkDefaultPath() (path string, err error) {
path = getHomePath()
path = utils.GetHomePath()
if path == "" {
return "", errors.New("the default credential file path is invalid")
}
Expand Down
23 changes: 0 additions & 23 deletions credentials/profile_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,29 +503,6 @@ func TestProfileProvider(t *testing.T) {
assert.Equal(t, "invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair", err.Error())
}

func TestHookOS(t *testing.T) {
goos := "windows"
goos = hookOS(goos)
assert.Equal(t, "windows", goos)

originHookOs := hookOS
originUserProfile := os.Getenv("USERPROFILE")
hookOS = func(goos string) string {
return "windows"
}
defer func() {
hookOS = originHookOs
os.Setenv("USERPROFILE", originUserProfile)
}()
os.Unsetenv("USERPROFILE")
path := getHomePath()
assert.Equal(t, "", path)

os.Setenv("USERPROFILE", "ok")
path = getHomePath()
assert.Equal(t, "ok", path)
}

func TestHookState(t *testing.T) {
info, err := hookState(nil, nil)
assert.Nil(t, info)
Expand Down

0 comments on commit e6ece0c

Please sign in to comment.